-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrangement.cpp
113 lines (102 loc) · 3.37 KB
/
arrangement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "arrangement.h"
Arrangement::Arrangement()
{
arrangementItems_a[0].status = ARRITEM_ACTIVE;
arrangementTick = 0;
for (uint8_t itemId = 0; itemId < NR_ARRITEMS; itemId++) memset(arrangementItems_a[itemId].muteArray, 0, NR_TRACKS);
}
uint8_t Arrangement::tick()
{
arrangementTick++;
return getCurrentArrItem();
}
uint8_t Arrangement::getCurrentArrItem()
{
for (uint8_t itemId = NR_ARRITEMS; itemId > 0; itemId--)
{
if ( (arrangementItems_a[itemId].status == ARRITEM_ACTIVE) && (arrangementItems_a[itemId].startTick <= arrangementTick) ) return itemId;
}
return 0;
}
uint8_t Arrangement::newArrangementItem()
{
for (uint8_t itemId = 0; itemId < NR_ARRITEMS; itemId++)
{
if (arrangementItems_a[itemId].status == ARRITEM_INACTIVE)
{
arrangementItems_a[itemId].status = ARRITEM_ACTIVE;
updateArrangementStartPositions();
return itemId;
}
}
return 0;
}
uint8_t Arrangement::deleteArrangementItem(uint8_t itemId)
{
arrangementItems_a[itemId].status = ARRITEM_INACTIVE;
_sortItems();
updateArrangementStartPositions();
return 0;
}
void Arrangement::clear()
{
for (uint8_t itemId = 1; itemId < NR_ARRITEMS; itemId++)
{
arrangementItems_a[itemId].status = ARRITEM_INACTIVE;
}
arrangementItems_a[0].startTick = 0;
arrangementItems_a[0].lengthTicks = 4 * 4 * RESOLUTION;
arrangementItems_a[0].patternIndex = 0;
arrangementItems_a[0].status = ARRITEM_ACTIVE;
memset(arrangementItems_a[0].muteArray, 0, NR_TRACKS);
}
void Arrangement::updateArrangementStartPositions()
{
for (uint8_t itemId = 0; itemId < NR_ARRITEMS; itemId++)
{
if (itemId == 0) arrangementItems_a[itemId].startTick = 0;
if (arrangementItems_a[itemId].status == ARRITEM_ACTIVE && itemId > 0)
{
uint32_t previousArrItemStart = arrangementItems_a[itemId - 1].startTick;
uint32_t previousArrItemLength = arrangementItems_a[itemId - 1].lengthTicks;
arrangementItems_a[itemId].startTick = previousArrItemStart + previousArrItemLength;
}
}
}
void Arrangement::moveItemUp(uint8_t itemId)
{
Serial.printf("move up, id %d\n", itemId);
if (itemId > 0)
{
ArrangementItem tempItem;
tempItem = arrangementItems_a[itemId - 1];
arrangementItems_a[itemId - 1] = arrangementItems_a[itemId];
arrangementItems_a[itemId] = tempItem;
updateArrangementStartPositions();
}
}
void Arrangement::moveItemDown(uint8_t itemId)
{
Serial.printf("move down, id %d\n", itemId);
if ( (itemId < (NR_ARRITEMS - 1)) && (arrangementItems_a[itemId + 1].status != ARRITEM_INACTIVE) )
{
ArrangementItem tempItem;
tempItem = arrangementItems_a[itemId + 1];
arrangementItems_a[itemId + 1] = arrangementItems_a[itemId];
arrangementItems_a[itemId] = tempItem;
updateArrangementStartPositions();
}
}
void Arrangement::_sortItems() { qsort(arrangementItems_a, NR_ARRITEMS, sizeof(ArrangementItem), compareArrItems); }
int compareArrItems(const void *s1, const void *s2)
{
struct ArrangementItem *e1 = (struct ArrangementItem *)s1;
struct ArrangementItem *e2 = (struct ArrangementItem *)s2;
// Active items first in array
if(e1->status == ARRITEM_INACTIVE && e2->status != ARRITEM_INACTIVE) return 1;
if(e1->status != ARRITEM_INACTIVE && e2->status == ARRITEM_INACTIVE) return -1;
// Then sort by tick
//if(e1->timestamp < e2->timestamp) return -1;
//if(e1->timestamp > e2->timestamp) return 1;
return 0;
}