-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClockDelay.cpp
466 lines (431 loc) · 10.2 KB
/
ClockDelay.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#define SERIAL_DEBUG
#ifdef SERIAL_DEBUG
#include "serial.h"
#endif // SERIAL_DEBUG
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "device.h"
#include "adc_freerunner.h"
// #include "DiscreteController.h"
#include "DeadbandController.h"
inline bool clockIsHigh(){
return !(CLOCKDELAY_CLOCK_PINS & _BV(CLOCKDELAY_CLOCK_PIN));
}
inline bool resetIsHigh(){
return !(CLOCKDELAY_RESET_PINS & _BV(CLOCKDELAY_RESET_PIN));
}
inline bool isCountMode(){
return !(MODE_SWITCH_PINS & _BV(MODE_SWITCH_PIN_A));
}
inline bool isDelayMode(){
return !(MODE_SWITCH_PINS & _BV(MODE_SWITCH_PIN_B));
}
enum OperatingMode {
DISABLED_MODE = 0,
DIVIDE_MODE = 1,
DELAY_MODE = 2
};
class ClockCounter {
public:
inline void reset(){
pos = 0;
off();
}
bool next(){
if(++pos > value){
pos = 0;
return true;
}
return false;
}
public:
uint8_t pos;
uint8_t value;
void rise(){
if(next())
on();
else
off();
}
inline void fall(){
off();
}
virtual bool isOff(){
return DELAY_OUTPUT_PINS & _BV(DELAY_OUTPUT_PIN);
}
virtual void on(){
DELAY_OUTPUT_PORT &= ~_BV(DELAY_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_3_PIN);
}
virtual void off(){
DELAY_OUTPUT_PORT |= _BV(DELAY_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_3_PIN);
}
#ifdef SERIAL_DEBUG
virtual void dump(){
printString("pos ");
printInteger(pos);
printString(", value ");
printInteger(value);
if(isOff())
printString(" off");
else
printString(" on");
}
#endif
};
class ClockDivider {
public:
inline void reset(){
pos = 0;
toggled = false;
off();
}
bool next(){
if(++pos > value){
pos = 0;
return true;
}
return false;
}
public:
uint8_t pos;
int8_t value;
bool toggled;
inline bool isOff(){
return DIVIDE_OUTPUT_PINS & _BV(DIVIDE_OUTPUT_PIN);
}
void rise(){
if(next()){
toggle();
toggled = true;
}
}
void fall(){
if(value == -1)
off();
}
void toggle(){
DIVIDE_OUTPUT_PORT ^= _BV(DIVIDE_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT ^= _BV(CLOCKDELAY_LED_2_PIN);
}
void on(){
DIVIDE_OUTPUT_PORT &= ~_BV(DIVIDE_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_2_PIN);
}
void off(){
DIVIDE_OUTPUT_PORT |= _BV(DIVIDE_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_2_PIN);
}
#ifdef SERIAL_DEBUG
virtual void dump(){
printString("pos ");
printInteger(pos);
printString(", value ");
printInteger(value);
if(isOff())
printString(" off");
else
printString(" on");
}
#endif
};
class ClockDelay {
public:
uint16_t riseMark;
uint16_t fallMark;
uint16_t value;
volatile uint16_t pos;
volatile bool running;
inline void start(){
pos = 0;
fallMark = 0;
running = true;
}
inline void stop(){
running = false;
}
inline void reset(){
stop();
off();
}
inline void rise(){
riseMark = value;
start();
}
inline void fall(){
fallMark = riseMark+pos;
}
inline void clock(){
if(running){
if(++pos == riseMark){
on();
}else if(pos == fallMark){
off();
stop(); // one-shot
}
}
}
virtual void on(){
DELAY_OUTPUT_PORT &= ~_BV(DELAY_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_3_PIN);
}
virtual void off(){
DELAY_OUTPUT_PORT |= _BV(DELAY_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_3_PIN);
}
virtual bool isOff(){
return DELAY_OUTPUT_PINS & _BV(DELAY_OUTPUT_PIN);
}
#ifdef SERIAL_DEBUG
virtual void dump(){
printString("rise ");
printInteger(riseMark);
printString(", fall ");
printInteger(fallMark);
printString(", pos ");
printInteger(pos);
printString(", value ");
printInteger(value);
if(running)
printString(" running");
else
printString(" stopped");
if(isOff())
printString(" off");
else
printString(" on");
}
#endif
};
class ClockSwing : public ClockDelay {
public:
void on(){
COMBINED_OUTPUT_PORT &= ~_BV(COMBINED_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_1_PIN);
}
void off(){
COMBINED_OUTPUT_PORT |= _BV(COMBINED_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_1_PIN);
}
bool isOff(){
return COMBINED_OUTPUT_PINS & _BV(COMBINED_OUTPUT_PIN);
}
};
class DividingCounter : public ClockCounter {
public:
void on(){
COMBINED_OUTPUT_PORT &= ~_BV(COMBINED_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_1_PIN);
}
void off(){
COMBINED_OUTPUT_PORT |= _BV(COMBINED_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_1_PIN);
}
bool isOff(){
return COMBINED_OUTPUT_PINS & _BV(COMBINED_OUTPUT_PIN);
}
};
ClockCounter counter;
ClockDivider divider;
ClockDelay delay; // manually triggered from Timer0 interrupt
ClockSwing swinger;
DividingCounter divcounter;
class DelayController {
public:
void update(uint16_t value){
// value = (value>>1)+1; // divide by 2, add 1
value = value+1;
delay.value = value;
swinger.value = value;
}
};
class CounterController : public DeadbandController<CLOCKDELAY_DEADBAND_THRESHOLD> {
public:
void hasChanged(uint16_t v){
v >>= 7; // scale 0-4095 down to 0-31
counter.value = v;
divcounter.value = v;
}
};
class DividerController : public DeadbandController<CLOCKDELAY_DEADBAND_THRESHOLD> {
public:
void hasChanged(uint16_t v){
if(v < (ADC_VALUE_RANGE/32/2))
v = -1;
else
v >>= 7; // scale 0-4095 down to 0-31
divider.value = v;
}
};
DelayController delayControl;
DividerController dividerControl;
CounterController counterControl;
void reset(){
divider.reset();
counter.reset();
divcounter.reset();
delay.reset();
swinger.reset();
}
volatile OperatingMode mode;
inline void updateMode(){
if(isCountMode()){
mode = DIVIDE_MODE;
}else if(isDelayMode()){
cli();
reset();
while(isDelayMode());
sei();
}else{
mode = DELAY_MODE;
}
}
void setup(){
cli();
// define hardware interrupts 0 and 1
// EICRA = (1<<ISC10) | (1<<ISC01) | (1<<ISC00); // trigger int0 on rising edge
EICRA = (1<<ISC10) | (1<<ISC01);
// trigger int0 on the falling edge, since input is inverted
// trigger int1 on any logical change.
// pulses that last longer than one clock period will generate an interrupt.
EIMSK = (1<<INT1) | (1<<INT0); // enables INT0 and INT1
CLOCKDELAY_CLOCK_DDR &= ~_BV(CLOCKDELAY_CLOCK_PIN);
CLOCKDELAY_CLOCK_PORT |= _BV(CLOCKDELAY_CLOCK_PIN); // enable pull-up resistor
CLOCKDELAY_RESET_DDR &= ~_BV(CLOCKDELAY_RESET_PIN);
CLOCKDELAY_RESET_PORT |= _BV(CLOCKDELAY_RESET_PIN); // enable pull-up resistor
MODE_SWITCH_DDR &= ~_BV(MODE_SWITCH_PIN_A);
MODE_SWITCH_PORT |= _BV(MODE_SWITCH_PIN_A);
MODE_SWITCH_DDR &= ~_BV(MODE_SWITCH_PIN_B);
MODE_SWITCH_PORT |= _BV(MODE_SWITCH_PIN_B);
DIVIDE_OUTPUT_DDR |= _BV(DIVIDE_OUTPUT_PIN);
DELAY_OUTPUT_DDR |= _BV(DELAY_OUTPUT_PIN);
COMBINED_OUTPUT_DDR |= _BV(COMBINED_OUTPUT_PIN);
CLOCKDELAY_LEDS_DDR |= _BV(CLOCKDELAY_LED_1_PIN);
CLOCKDELAY_LEDS_DDR |= _BV(CLOCKDELAY_LED_2_PIN);
CLOCKDELAY_LEDS_DDR |= _BV(CLOCKDELAY_LED_3_PIN);
DIVIDE_OUTPUT_PORT |= _BV(DIVIDE_OUTPUT_PIN);
DELAY_OUTPUT_PORT |= _BV(DELAY_OUTPUT_PIN);
COMBINED_OUTPUT_PORT |= _BV(COMBINED_OUTPUT_PIN);
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_1_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_2_PIN);
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_3_PIN);
// At 16MHz CPU clock and prescaler 64, Timer 0 should run at 1024Hz.
// configure Timer 0 to Fast PWM, 0xff top.
TCCR0A |= _BV(WGM01) | _BV(WGM00);
// TCCR0B |= _BV(CS01) | _BV(CS00); // prescaler: 64.
TCCR0B |= _BV(CS01); // prescaler: 8
// enable timer 0 overflow interrupt
TIMSK0 |= _BV(TOIE0);
// dividerControl.range = 33;
dividerControl.value = -1;
// counterControl.range = 33;
counterControl.value = -1;
setup_adc();
reset();
updateMode();
sei();
#ifdef SERIAL_DEBUG
beginSerial(9600);
printString("hello\n");
#endif
}
/* Timer 0 overflow interrupt */
ISR(TIMER0_OVF_vect){
delay.clock();
swinger.clock();
}
/* Reset interrupt */
ISR(INT0_vect){
reset();
// hold everything until reset is released
while(resetIsHigh());
}
/* Clock interrupt */
ISR(INT1_vect){
if(clockIsHigh()){
divider.rise();
switch(mode){
case DELAY_MODE:
delay.rise();
if(divider.toggled){
swinger.rise();
}else{
COMBINED_OUTPUT_PORT &= ~_BV(COMBINED_OUTPUT_PIN); // pass through clock
CLOCKDELAY_LEDS_PORT |= _BV(CLOCKDELAY_LED_1_PIN);
}
break;
case DIVIDE_MODE:
counter.rise();
if(divider.toggled){
divcounter.rise();
if(!divcounter.isOff())
divider.toggled = false;
}
break;
}
}else{
switch(mode){
case DELAY_MODE:
delay.fall();
if(divider.toggled){
swinger.fall();
divider.toggled = false;
}else{
COMBINED_OUTPUT_PORT |= _BV(COMBINED_OUTPUT_PIN); // pass through clock
CLOCKDELAY_LEDS_PORT &= ~_BV(CLOCKDELAY_LED_1_PIN);
}
break;
case DIVIDE_MODE:
counter.fall();
divcounter.fall();
break;
}
divider.fall();
}
}
void loop(){
updateMode();
dividerControl.update(getAnalogValue(DIVIDE_ADC_CHANNEL));
counterControl.update(getAnalogValue(DELAY_ADC_CHANNEL));
delayControl.update(getAnalogValue(DELAY_ADC_CHANNEL));
#ifdef SERIAL_DEBUG
if(serialAvailable() > 0){
switch(serialRead()){
case '.':
CLOCKDELAY_CLOCK_PORT ^= _BV(CLOCKDELAY_CLOCK_PIN);
INT1_vect();
break;
case ':':
CLOCKDELAY_RESET_PORT &= ~_BV(CLOCKDELAY_RESET_PIN);
TIMER0_OVF_vect();
CLOCKDELAY_RESET_PORT |= _BV(CLOCKDELAY_RESET_PIN);
TIMER0_OVF_vect();
break;
}
printString("div[");
divider.dump();
printString("] ");
printString("cnt[");
counter.dump();
printString("] ");
printString("del[");
delay.dump();
printString("] ");
printString("swing[");
swinger.dump();
printString("] ");
printBinary(DELAY_OUTPUT_PINS);
switch(mode){
case DIVIDE_MODE:
printString(" count ");
break;
case DELAY_MODE:
printString(" delay ");
break;
}
printNewline();
}
#endif
}