-
Notifications
You must be signed in to change notification settings - Fork 9
/
GateSequencer.h
174 lines (165 loc) · 4.03 KB
/
GateSequencer.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef _GATE_SEQUENCER_H_
#define _GATE_SEQUENCER_H_
#include "Sequence.h"
#include "DeadbandController.h"
class GateSequencer : public Sequence<SEQUENCER_BITS_TYPE> {
public:
class SequenceController : public DeadbandController<SEQUENCER_DEADBAND_THRESHOLD> {
public:
GateSequencer* seq;
SequenceController(GateSequencer* s) : seq(s) {}
void hasChanged(uint16_t value){
seq->recalculate = true;
}
};
class RotateController : public DeadbandController<SEQUENCER_DEADBAND_THRESHOLD> {
public:
GateSequencer* seq;
RotateController(GateSequencer* s) : seq(s) {}
void hasChanged(uint16_t val){
val >>= 8; // scale 0-4095 down to 0-15
seq->rotate(val);
}
};
enum GateSequencerMode {
DISABLED = 0,
TRIGGERING = 1,
ALTERNATING = 2
};
public:
SequenceController step;
SequenceController fill;
RotateController rotation;
bool recalculate;
GateSequencer(uint8_t p1, uint8_t p2, uint8_t p3, uint8_t p4):
step(this), fill(this), rotation(this), mode(DISABLED), recalculate(true),
output(p1), trigger(p2), alternate(p3), led(p4){
#ifdef SEQUENCER_TRIGGER_SWITCH_PINS
SEQUENCER_TRIGGER_SWITCH_DDR &= ~_BV(trigger);
SEQUENCER_TRIGGER_SWITCH_PORT |= _BV(trigger);
#endif /* SEQUENCER_TRIGGER_SWITCH_PINS */
SEQUENCER_ALTERNATE_SWITCH_DDR &= ~_BV(alternate);
SEQUENCER_ALTERNATE_SWITCH_PORT |= _BV(alternate);
SEQUENCER_OUTPUT_DDR |= _BV(output);
SEQUENCER_LEDS_DDR |= _BV(led);
SEQUENCER_LEDS_PORT |= _BV(led);
off();
}
void update(){
if(recalculate){
uint8_t s = SEQUENCER_STEPS_RANGE - (step.value >> SEQUENCER_STEP_SCALING_FACTOR);
uint8_t f = s - ((fill.value >> 2) * s) / (ADC_VALUE_RANGE >> 2);
calculate(s, f);
recalculate = false;
#ifdef SERIAL_DEBUG
printInteger(s);
printByte('.');
printInteger(f);
printNewline();
#endif
}
if(isTriggering())
mode = TRIGGERING;
else if(isAlternating())
mode = ALTERNATING;
else
mode = DISABLED;
}
void push(GateSequencer& seq){
if(isOn())
seq.on();
else
seq.off();
}
void rise(){
switch(mode){
case TRIGGERING:
if(next())
on();
break;
case ALTERNATING:
if(next())
toggle();
break;
case DISABLED:
next();
off();
break;
}
}
void fall(){
switch(mode){
case DISABLED:
case TRIGGERING:
off();
break;
case ALTERNATING:
break;
}
}
void reset(){
Sequence<SEQUENCER_BITS_TYPE>::reset();
off();
}
inline void on(){
SEQUENCER_OUTPUT_PORT &= ~_BV(output);
SEQUENCER_LEDS_PORT |= _BV(led);
}
inline void off(){
SEQUENCER_OUTPUT_PORT |= _BV(output);
SEQUENCER_LEDS_PORT &= ~_BV(led);
}
inline void toggle(){
SEQUENCER_OUTPUT_PORT ^= _BV(output);
SEQUENCER_LEDS_PORT ^= _BV(led);
}
inline bool isOn(){
return !(SEQUENCER_OUTPUT_PINS & _BV(output));
}
inline bool isTriggering(){
#ifdef SEQUENCER_TRIGGER_SWITCH_PINS
return !(SEQUENCER_TRIGGER_SWITCH_PINS & _BV(trigger));
#else
return !isAlternating();
#endif /* SEQUENCER_TRIGGER_SWITCH_PINS */
}
inline bool isAlternating(){
return !(SEQUENCER_ALTERNATE_SWITCH_PINS & _BV(alternate));
}
inline bool isEnabled(){
return isAlternating() || isTriggering();
}
#ifdef SERIAL_DEBUG
void dump(){
printInteger(pos);
printString(", ");
printInteger(length);
printString(", ");
printInteger(offset);
if(isOn())
printString(", on");
if(isTriggering())
printString(", triggering");
if(isAlternating())
printString(", alternating");
switch(mode){
case DISABLED:
printString(" DISABLED");
break;
case TRIGGERING:
printString(" TRIGGERING");
break;
case ALTERNATING:
printString(" ALTERNATING");
break;
}
}
#endif
private:
uint8_t output;
uint8_t trigger;
uint8_t alternate;
uint8_t led;
volatile GateSequencerMode mode;
};
#endif /* _GATE_SEQUENCER_H_ */