-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
542 lines (424 loc) · 12 KB
/
main.c
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <eepromVariables.h>
// F_CPU = 1000000 Hz
// Global variables
volatile uint16_t millis = 0; // this counter will roll over around 65 seconds
// saves memory and is fine for this application
#define RUN 0
#define PROGRAM 1
uint8_t machineState = RUN;
uint16_t statusBlinkCounter = 0; // counter for status LED
uint16_t statusBlinkDelay = 0; // delay setting for status LED
uint16_t playbackDelay = 128; // delay setting for playback speed
uint8_t playbackIndex = 0; // current frame within pattern memory
uint8_t playbackConfig = 0;
uint8_t playbackPattern = 0; // current pattern to play back (0 to 5)
uint8_t playbackMarkerPos;
uint8_t playbackDir = 0;
uint8_t outByte = 0; // current output channel map
#define BTN_UPDATE_INTERVAL 5
#define BTN_HOLD_DELAY 2000
#define EEPROMSAVEINTERVAL 1000
// - Outpin pin mapping -
// Channel 1: PA1
// Channel 2: PA0
// Channel 3: PD2
// Channel 4: PD3
// Channel 5: PD4
// Channel 6: PD5
// Status LED: PD6
#define STATUSLED PD6
// - Input pin mapping -
// Channel 1: PB5
// Channe1 2: PB4
// Channel 3: PB3
// Channel 4: PB2
// Channel 5: PB1
// Channel 6: PB0
// Control 1: PB6
// Control 2: PB7
// Configure timer 0 interrupt
void setupTimer(void) {
TIMSK = _BV(OCIE0A); // enable Timer 0
TCNT0 = 0x00; // clear Timer 0
TCCR0A = _BV(WGM01); // set Timer 0 to CTC mode
TCCR0B = _BV(CS01); // set Timer 0 prescaler to clk/8
OCR0A = 124; // 124 = 125 counts. 1 MHz / 8 = 125kHz, 125kHz / 125 = 1kHz interrupt
sei(); // enable interrupts
}
// Setup input and output ports
void initIO(void) {
DDRD = 0b01111100;
DDRA = 0b00000011;
DDRB = 0b00000000;
PORTB = 0b11111111; // set pullups active
}
// Define structure for button processing
typedef struct {
uint8_t edgeDetect;
uint16_t pressTimer;
uint8_t statusFlags;
} button;
button Buttons[8];
#define EDGE_RISE 0b01111111
#define EDGE_FALL 0b11111110
#define STABLE_POS 0b11111111
#define STABLE_NEG 0b00000000
#define STATUS_PRESSED 0b00000001
#define STATUS_RELEASED 0b00000010
#define STATUS_ACTIVE 0b00000100
#define STATUS_INACTIVE 0b00001000
#define STATUS_HELD 0b00010000
// Update button inputs
void updateButtons(void) {
uint8_t i;
uint8_t inputByte;
inputByte = ~PINB; // invert active-low inputs
for (i = 0; i < 8; i++) {
// load the current button level into the debounce array
Buttons[i].edgeDetect <<= 1; // shift debounce array left
if (inputByte & (1 << i)) Buttons[i].edgeDetect |= 1; // if button is active, write 1 to LSB
// Determine button status and update status flags
switch(Buttons[i].edgeDetect) {
case EDGE_RISE:
Buttons[i].statusFlags |= STATUS_PRESSED;
Buttons[i].statusFlags &= ~(STATUS_RELEASED | STATUS_HELD);
Buttons[i].pressTimer = 7*BTN_UPDATE_INTERVAL; // preset to 35ms
break;
case EDGE_FALL:
Buttons[i].statusFlags |= STATUS_RELEASED;
Buttons[i].pressTimer = 0; // clear timer
break;
case STABLE_POS:
if (Buttons[i].statusFlags & STATUS_PRESSED) {
Buttons[i].statusFlags |= STATUS_ACTIVE;
Buttons[i].statusFlags &= ~STATUS_INACTIVE;
Buttons[i].pressTimer += BTN_UPDATE_INTERVAL; // increment 5ms
if (Buttons[i].pressTimer > BTN_HOLD_DELAY) Buttons[i].statusFlags |= STATUS_HELD;
if (Buttons[i].pressTimer > 10000) Buttons[i].pressTimer = 10000; // max 10 seconds
}
break;
case STABLE_NEG:
Buttons[i].statusFlags |= STATUS_INACTIVE;
Buttons[i].statusFlags &= ~STATUS_ACTIVE;
Buttons[i].pressTimer = 0;
break;
}
}
}
uint8_t checkHeld(uint8_t buttonID) {
if (Buttons[buttonID].statusFlags & STATUS_HELD) {
Buttons[buttonID].statusFlags &= ~(STATUS_HELD | STATUS_PRESSED);
return 1;
} else {
return 0;
}
}
uint8_t checkClicked(uint8_t buttonID) {
if ((Buttons[buttonID].statusFlags & (STATUS_RELEASED | STATUS_PRESSED)) == (STATUS_RELEASED | STATUS_PRESSED)) {
Buttons[buttonID].statusFlags &= ~(STATUS_RELEASED | STATUS_PRESSED | STATUS_HELD);
return 1;
} else {
return 0;
}
}
uint8_t checkActive(uint8_t buttonID) {
if ((Buttons[buttonID].statusFlags & STATUS_ACTIVE)) {
return 1;
} else {
return 0;
}
}
// Map input byte to output channels
// Channel 1 is bit 0, Channel 6 is bit 5
// Input byte is active-high, output ports are active-high
void writeChannels(uint8_t inByte) {
inByte |= 0b11000000;
PORTD = (PORTD & 0b11000011) | (inByte & 0b00111100);
PORTA = (PORTA & 0b11111100) | ((inByte & 0b0000001) << 1) | ((inByte & 0b00000010) >> 1);
}
// Display information on the status LED
#define OFF 0
#define ON 1
#define SLOWBLINK 400
#define FASTBLINK 50
#define STUTTERBLINK 300
void setStatusLED(uint16_t status) {
if (status == ON) {
PORTD |= _BV(STATUSLED);
statusBlinkDelay = 0;
} else if (status == OFF) {
PORTD &= ~(_BV(STATUSLED));
statusBlinkDelay = 0;
} else {
statusBlinkDelay = status;
}
}
// Timer 0 ISR (system tick)
// Increment millis value for system timing
ISR(TIMER0_COMPA_vect) {
millis++;
}
uint8_t *mapEEPROM(uint8_t pattern, uint8_t index) {
switch(pattern) {
case 0:
return &patternData1[index % 8];
break;
case 1:
return &patternData2[index % 8];
break;
case 2:
return &patternData3[index % 16];
break;
case 3:
return &patternData4[index % 16];
break;
case 4:
return &patternData5[index % 32];
break;
case 5:
return &patternData6[index % 32];
break;
default:
return 0;
break;
}
}
uint8_t getFrame(uint8_t pattern, uint8_t index) {
return eeprom_read_byte(mapEEPROM(pattern, index));
}
void writeFrame(uint8_t pattern, uint8_t index, uint8_t data) {
cli(); // disable interrupts while writing EEPROM
// we may lose a millisecond or two, but not a big deal in this application!
eeprom_update_byte(mapEEPROM(pattern, index), data); // save data to EEPROM
eeprom_busy_wait(); // wait for EEPROM write to finish
sei(); // enable interrupts after writing EEPROM
}
uint8_t getPatternMax(void) {
switch(playbackPattern) {
case 0:
case 1:
return 7;
break;
case 2:
case 3:
return 15;
break;
case 4:
case 5:
return 31;
break;
default:
return 0;
break;
}
}
void loadPatternConfigs(uint8_t pattern) {
playbackDelay = eeprom_read_word(&patternConfig[pattern]) & 0x0FFF;
playbackConfig = eeprom_read_word(&patternConfig[pattern]) >> 12;
playbackMarkerPos = getPatternMax();
for (uint8_t i = 0; i < playbackMarkerPos; i++) {
uint8_t tempByte = getFrame(playbackPattern, i);
if (tempByte & 0b10000000) {
playbackMarkerPos = i;
break;
}
}
}
void fwdPattern(void) {
if (playbackIndex < getPatternMax()) playbackIndex++;
}
void revPattern(void) {
if (playbackIndex > 0) playbackIndex--;
}
void clearButtonFlags(uint8_t flags) {
for (uint8_t i = 0; i < 8; i++) {
Buttons[i].statusFlags &= ~(flags);
}
}
void nextFrame(void) {
outByte = getFrame(playbackPattern, playbackIndex);
writeChannels(outByte);
if (outByte & 0b10000000) {
setStatusLED(FASTBLINK);
} else if ((playbackIndex == getPatternMax()) || (playbackIndex == 0)) {
setStatusLED(ON);
setStatusLED(STUTTERBLINK);
} else {
setStatusLED(SLOWBLINK);
}
}
void clearMarker(void) {
uint8_t tempByte = getFrame(playbackPattern, playbackMarkerPos);
writeFrame(playbackPattern, playbackMarkerPos, tempByte & 0b01111111);
}
#define PLAY_FORWARD 0
#define PLAY_REVERSE 1
#define PLAY_BOUNCE 2
void cyclePlaybackMode(void) {
playbackConfig++;
if (playbackConfig > 2) playbackConfig = 0;
}
#define BTN1 0
#define BTN2 1
#define BTN3 2
#define BTN4 3
#define BTN5 4
#define BTN6 5
#define BTNUP 6
#define BTNDN 7
void processButtons(void) {
static uint16_t debounceEvent = 0;
uint8_t i;
if (millis - debounceEvent > (BTN_UPDATE_INTERVAL - 1)) {
debounceEvent = millis;
updateButtons();
// Running state
if (machineState == RUN) {
for (i = 0; i < 6; i++) {
if (checkHeld(5-i)) {
setStatusLED(SLOWBLINK);
machineState = PROGRAM;
playbackIndex = 0;
playbackPattern = i;
clearButtonFlags(STATUS_HELD | STATUS_PRESSED | STATUS_RELEASED);
nextFrame();
loadPatternConfigs(i);
break;
}
if (checkClicked(5-i)) {
if (playbackPattern == i) {
cyclePlaybackMode();
} else {
playbackPattern = i;
playbackIndex = 0;
loadPatternConfigs(i);
}
}
}
if (checkClicked(BTNUP)) {
playbackDelay += (playbackDelay >> 3);
if (playbackDelay > 2000) playbackDelay = 2000;
}
if (checkClicked(BTNDN)) {
playbackDelay -= (playbackDelay >> 3);
if (playbackDelay < 8) playbackDelay = 8;
}
// Programming state
} else if (machineState == PROGRAM) {
for (i = 0; i < 6; i++) {
if (checkClicked(5-i)) {
outByte ^= (1 << i);
writeChannels(outByte);
} else if (checkHeld(i)) { // exit programming mode if any channel button is held down
setStatusLED(ON);
clearButtonFlags(STATUS_HELD | STATUS_PRESSED | STATUS_RELEASED);
machineState = RUN;
break;
}
}
if (checkClicked(BTNUP)) {
writeFrame(playbackPattern, playbackIndex, outByte); // save last frame to EEPROM
revPattern();
nextFrame();
}
if (checkClicked(BTNDN)) {
writeFrame(playbackPattern, playbackIndex, outByte); // save last frame to EEPROM
fwdPattern();
nextFrame();
}
if (checkHeld(BTNUP) || checkHeld(BTNDN)) {
clearMarker(); // delete any existing end-sequence marker
outByte |= 0b10000000; // set end of pattern marker
writeFrame(playbackPattern, playbackIndex, outByte); // save to EEPROM
loadPatternConfigs(playbackPattern);
nextFrame();
}
}
}
}
// Blink status LED if needed
void processStatusLED(void) {
static uint16_t statusBlinkCounter = 0;
if (statusBlinkDelay) {
if (millis - statusBlinkCounter > statusBlinkDelay) {
statusBlinkCounter = millis;
if (statusBlinkDelay == 100) {
statusBlinkDelay = 300;
} else if (statusBlinkDelay == 300) {
statusBlinkDelay = 100;
}
PORTD ^= _BV(STATUSLED);
}
}
}
// Control output channels
void processOutputs(void) {
static uint16_t playbackCounter = 0;
if (machineState == RUN) {
if (playbackConfig == PLAY_FORWARD) playbackDir = 0;
else if (playbackConfig == PLAY_REVERSE) playbackDir = 1;
if (millis - playbackCounter > playbackDelay) {
playbackCounter = millis;
if (playbackDir == 0) {
if (playbackIndex < playbackMarkerPos) {
playbackIndex++;
} else {
if (playbackConfig == PLAY_FORWARD) {
playbackIndex = 0;
} else if (playbackConfig == PLAY_BOUNCE){
playbackDir = 1;
playbackIndex--;
}
}
} else if (playbackDir == 1) {
if (playbackIndex > 0) {
playbackIndex--;
} else {
if (playbackConfig == PLAY_REVERSE) {
playbackIndex = playbackMarkerPos;
} else if (playbackConfig == PLAY_BOUNCE) {
playbackDir = 0;
playbackIndex++;
}
}
}
outByte = getFrame(playbackPattern,playbackIndex);
writeChannels(outByte);
}
}
}
// Save EEPROM values if needed
void processEEPROMsave(void) {
static uint16_t EEPROMSaveCounter = 0;
if (machineState == RUN) {
if (millis - EEPROMSaveCounter > EEPROMSAVEINTERVAL) {
EEPROMSaveCounter = millis;
cli(); // disable interrupts
eeprom_update_word(&patternConfig[playbackPattern], (playbackDelay & 0x0FFF) | (playbackConfig << 12));
eeprom_update_byte(&lastPlaybackPattern, playbackPattern);
eeprom_busy_wait();
sei(); // enable interrupts
}
}
}
void main(void) __attribute__ ((noreturn));
void main(void) {
// startup tasks
initIO();
setupTimer();
playbackPattern = eeprom_read_byte(&lastPlaybackPattern);
loadPatternConfigs(playbackPattern);
machineState = RUN; // start up in RUN mode
setStatusLED(ON); // steady on means RUN mode
writeChannels(0); // turn off all channels on startup
// run this loop forever
for (;;) {
processButtons(); // read and react to button presses
processStatusLED(); // control status LED
processOutputs(); // play back patterns
processEEPROMsave(); // save the EEPROM periodically
}
}