-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metronome.ino
526 lines (488 loc) · 11.5 KB
/
Metronome.ino
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
// PIF_TOOL_CHAIN_OPTION: UPLOAD_OPTIONS := -c "raw,cr"
// PIF_TOOL_CHAIN_OPTION: EXTRA_LIBS := ArduinoTools
#include <Arduino.h>
/* tests to handle a 7 segments 3 digits display with one 74LS164
*/
#ifdef PIF_TOOL_CHAIN
#include "setInterval/setInterval.h"
#include "pwm/pwm.h"
#else
#include "setInterval.h"
#include "pwm.h"
#endif
#ifndef DEFAULT_BAUDRATE
#ifdef __AVR_ATmega644P__
#define DEFAULT_BAUDRATE 19200
#else
#define DEFAULT_BAUDRATE 115200
#endif
#endif
#define DEMUX_2_TO_3
#if defined __AVR_ATtinyX5__
#define DATA 2
#define CLOCK 3
#define BEEP PWM_1_B // = 4
#define BEEP_PWM 1
#define BEEP_PWM_MODE_ON WGM_1_FAST_OCR1C | SET_PWM_1_B
#define BEEP_PWM_MODE_OFF SET_PWM_1_NONE
// doesn't work with COMPARE_OUTPUT_MODE_1_SET_NONE : why ???
#define BEEP_PWM_OUTPUT_ON COMPARE_OUTPUT_MODE_1_CLEAR_NONE
#define BEEP_PWM_OUTPUT_OFF COMPARE_OUTPUT_MODE_1_NONE_NONE
#ifdef DEMUX_2_TO_3
#define DIGIT_L 0
#define DIGIT_H 1
#else
#define DIGIT_1 ?
#define DIGIT_2 ?
#define DIGIT_3 ?
#endif
#else
#define DATA 2
#define CLOCK 3
#define BEEP_PWM 1
#define BEEP PWM_1_B // = 10
#define BEEP_PWM_MODE_ON WGM_1_FAST_ICR
#define BEEP_PWM_MODE_OFF WGM_1_FAST_ICR
#define BEEP_PWM_OUTPUT_ON COMPARE_OUTPUT_MODE_TOGGLE
#define BEEP_PWM_OUTPUT_OFF COMPARE_OUTPUT_MODE_NONE
#ifdef DEMUX_2_TO_3
#define DIGIT_L 8
#define DIGIT_H 9
#else
#define DIGIT_1 8
#define DIGIT_2 9
#define DIGIT_3 10
#endif
// #define DEBUG
#endif
// single char mappings
byte mapSegments[] = {
//tRrblLmp
0b11111100, // 0
0b01100000, // 1
0b11011010, // 2
0b11110010, // 3
0b01100110, // 4
0b10110110, // 5
0b10111110, // 6
0b11100000, // 7
0b11111110, // 8
0b11110110, // 9
0b00000001, // .
0b00000000, // ' '
};
// 3 digits mapping
byte mapDisplays[] = {
// tempo : "tmp"
0b00001110 , 0b00101010 , 0b11001110 ,
// times : "tim"
0b00001110 , 0b00001000 , 0b00101010 ,
// subdivisions : "sub"
0b10110110 , 0b00111000 , 0b00111110 ,
// time down : "___"
0b00010000 , 0b00010000 , 0b00010000 ,
// time down : " _ "
0b00000000 , 0b00010000 , 0b00000000 ,
// time left :"| "
0b00001100 , 0b00000000 , 0b00000000 ,
// time left :"' "
0b00000100 , 0b00000000 , 0b00000000 ,
// time right: " |"
0b00000000 , 0b00000000 , 0b01100000 ,
// time right: " '"
0b00000000 , 0b00000000 , 0b01000000 ,
// time up : "^^^"
0b10000000 , 0b10000000 , 0b10000000 ,
// time up : " ^ "
0b00000000 , 0b10000000 , 0b00000000 ,
// half : " - "
0b00000000 , 0b00000010 , 0b00000000 ,
// empty: " "
0b00000000 , 0b00000000 , 0b00000000 ,
};
#define MSG_TEMPO (mapDisplays + 0)
#define MSG_TIMES (mapDisplays + 3)
#define MSG_SUBS (mapDisplays + 6)
#define MSG_DOWN (mapDisplays + 9)
#define MSG_DOWN2 (mapDisplays + 12)
#define MSG_LEFT (mapDisplays + 15)
#define MSG_LEFT2 (mapDisplays + 18)
#define MSG_RIGHT (mapDisplays + 21)
#define MSG_RIGHT2 (mapDisplays + 24)
#define MSG_TOP (mapDisplays + 27)
#define MSG_TOP2 (mapDisplays + 30)
#define MSG_HALF (mapDisplays + 33)
#define MSG_NONE (mapDisplays + 36)
byte currentDisplay[3];
void displayChars(byte *map) {
currentDisplay[0] = map[0];
currentDisplay[1] = map[1];
currentDisplay[2] = map[2];
}
void addChars(byte *map) {
currentDisplay[0] |= map[0];
currentDisplay[1] |= map[1];
currentDisplay[2] |= map[2];
}
void subChars(byte *map) {
currentDisplay[0] &= ~(map[0]);
currentDisplay[1] &= ~(map[1]);
currentDisplay[2] &= ~(map[2]);
}
void displayNumber(int value) {
currentDisplay[0] = mapSegments[(value / 100) % 10];
currentDisplay[1] = mapSegments[(value / 10) % 10];
currentDisplay[2] = mapSegments[value % 10];
}
void outputChar(byte map) {
for(byte mask = 0x01; mask; mask <<= 1) {
digitalWrite(DATA, mask & map ? LOW : HIGH);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
}
}
// times are marked during 1/FLASH time duration
#define FLASH 5
// tempo times per minute
byte tempo = 60;
#define TEMPO_MIN 30
#define TEMPO_MAX 180
// times per measure : 1 - 4
byte times = 4;
// current time
byte timeNb;
// subdivisions per time : 1 - 4
byte subs = 2;
// number of ticks needed to handle everything (FLASH * times * subs)
byte nbTicks;
// current tick
byte tick;
byte digit = 0;
byte buttons = 0;
byte oldButtons;
#define DEBOUNCE_DELAY 10
byte debounce = DEBOUNCE_DELAY;
#define BUTTON_MINUS 0x04
#define BUTTON_PLUS 0x02
#define BUTTON_MODE 0x01
enum {
enteringTempo, inTempo,
enteringTimes, inTimes,
enteringSubs, inSubs,
running
} menuState = running;
word top, prescale; // initialized once in setup
setIntervalTimer tacTimer = -1;
void stopTac(void *, long, int) {
setPWM(BEEP_PWM, 0,
BEEP_PWM_OUTPUT_OFF, 0,
BEEP_PWM_OUTPUT_OFF ,0,
BEEP_PWM_MODE_OFF, prescale);
// GTCCR = 0;
// TCCR1 = 0;
changeInterval(tacTimer, SET_INTERVAL_PAUSED);
}
void tac(unsigned long delay) {
// TCNT1 = 0;
// OCR1A = top / 2;
// OCR1C = top;
// TCCR1 = 0b10000111;
// GTCCR = 0b01110000;
setPWM(BEEP_PWM, top,
BEEP_PWM_OUTPUT_OFF, 0,
BEEP_PWM_OUTPUT_ON, top / 2,
BEEP_PWM_MODE_ON, prescale);
changeInterval(tacTimer, delay);
}
setIntervalTimer tickTimer;
void computeTicks() {
nbTicks = times * subs * FLASH;
long interval = 60000L / (tempo * subs * FLASH);
tick = 0;
changeInterval(tickTimer, interval);
}
void nextTick(void *, long, int) {
tick++;
if (tick == nbTicks) {
tick = 0;
}
if (tick % (FLASH * subs) == 0) {
// new time : find which one, depending on time number and nb times
// 2/4 => down,top
// 3/4 => down,right,top
// 4/4 => down,left,right,top
// thus : t0 = down, last = top, previous = right, else = left.
if(menuState == running) {
timeNb = tick / (FLASH * subs);
byte *msg;
if (timeNb == 0) {
msg = MSG_DOWN;
} else if (timeNb == times - 1) {
msg = MSG_TOP;
} else if (timeNb == times - 2) {
msg = MSG_RIGHT;
} else {
msg = MSG_LEFT;
}
displayChars(msg);
}
tac(10);
} else if (tick % (FLASH * subs) == 1) {
if(menuState == running) {
// reduce current time
byte *msg;
if (timeNb == 0) {
msg = MSG_DOWN2;
} else if (timeNb == times - 1) {
msg = MSG_TOP2;
} else if (timeNb == times - 2) {
msg = MSG_RIGHT2;
} else {
msg = MSG_LEFT2;
}
displayChars(msg);
}
} else if (tick % FLASH == 0) {
if(menuState == running) {
// add half
addChars(MSG_HALF);
}
tac(5);
} else if (tick % FLASH == 1) {
if(menuState == running) {
// remove half
subChars(MSG_HALF);
}
}
}
void doPlus() {
#ifdef DEBUG
Serial.print('+');
#endif
switch(menuState) {
case inTempo:
tempo += 2;
if (tempo > TEMPO_MAX) {
tempo = TEMPO_MIN;
}
displayNumber(tempo);
break;
case inTimes:
if (times < 4) {
times++;
displayNumber(times);
}
break;
case inSubs:
if (subs < 4) {
subs++;
displayNumber(subs);
}
break;
}
computeTicks();
}
void doMinus() {
#ifdef DEBUG
Serial.print('-');
#endif
switch(menuState) {
case inTempo:
tempo -= 2;
if (tempo < TEMPO_MIN) {
tempo = TEMPO_MAX;
}
displayNumber(tempo);
break;
case inTimes:
if (times > 1) {
times--;
displayNumber(times);
}
break;
case inSubs:
if (subs > 1) {
subs--;
displayNumber(subs);
}
break;
}
computeTicks();
}
#define AUTOREPEAT_DELAY 1000
#define AUTOREPEAT_SPEED 100
setIntervalTimer autorepeatTimer;
void autorepeat(void *, long, int) {
changeInterval(autorepeatTimer, AUTOREPEAT_SPEED);
if (buttons & BUTTON_PLUS) {
doPlus();
} else {
doMinus();
}
}
#define PRESS(which) (!(oldButtons & which) && (buttons & which))
#define RELEASE(which) ((oldButtons & which) && !(buttons & which))
void handleButtons(byte buttons, byte oldButtons) {
#ifdef DEBUG
Serial.print('b');
#endif
// press "mode" button -> enter next menu item
if (PRESS(BUTTON_MODE)) {
switch(menuState) {
case running:
menuState = enteringTempo;
displayChars(MSG_TEMPO);
return;
case inTempo:
menuState = enteringTimes;
displayChars(MSG_TIMES);
return;
case inTimes:
menuState = enteringSubs;
displayChars(MSG_SUBS);
return;
case inSubs:
menuState = running;
displayChars(MSG_NONE);
return;
default:
return;
}
}
// release "mode" button -> display current value
if (RELEASE(BUTTON_MODE)) {
switch(menuState) {
case enteringTempo:
menuState = inTempo;
displayNumber(tempo);
return;
case enteringTimes:
menuState = inTimes;
displayNumber(times);
return;
case enteringSubs:
menuState = inSubs;
displayNumber(subs);
return;
default:
return;
}
}
if (PRESS(BUTTON_PLUS)) {
changeInterval(autorepeatTimer, AUTOREPEAT_DELAY);
doPlus();
} else if (RELEASE(BUTTON_PLUS)) {
changeInterval(autorepeatTimer, SET_INTERVAL_PAUSED);
} else if (PRESS(BUTTON_MINUS)) {
changeInterval(autorepeatTimer, AUTOREPEAT_DELAY);
doMinus();
} else if (RELEASE(BUTTON_MINUS)) {
changeInterval(autorepeatTimer, SET_INTERVAL_PAUSED);
}
}
void updateDisplay(void *, long, int) {
// read buttons state
pinMode(DATA, INPUT_PULLUP);
// _NOP();
if (digitalRead(DATA)) {
buttons &= ~(1 << digit);
} else {
buttons |= 1 << digit;
}
pinMode(DATA, OUTPUT);
// set 'dot' segment like button states
if (buttons & BUTTON_MODE) {
currentDisplay[0] |= 0x01;
} else {
currentDisplay[0] &= 0xFE;
}
if (buttons & BUTTON_MINUS) {
currentDisplay[1] |= 0x01;
} else {
currentDisplay[1] &= 0xFE;
}
if (buttons & BUTTON_PLUS) {
currentDisplay[2] |= 0x01;
} else {
currentDisplay[2] &= 0xFE;
}
// handle button change
if (oldButtons != buttons) {
if (debounce == 0) {
handleButtons(buttons, oldButtons);
oldButtons = buttons;
debounce = DEBOUNCE_DELAY;
} else {
debounce--;
}
} else {
debounce = DEBOUNCE_DELAY;
}
#ifdef DEMUX_2_TO_3
// display one digit
PORTB |= 0x03; // set bits 0, 1 and 2
outputChar(currentDisplay[digit]); // prepare output
PORTB &= 0xFC | digit; // set it to current digit
#else
// display one digit
PORTB |= 0x07; // set bits 0, 1 and 2
outputChar(currentDisplay[digit]); // prepare output
PORTB &= ~(1 << digit); // set it to current digit
#endif
if (digit == 2) {
digit = 0;
} else {
digit++;
}
}
void setup() {
#ifdef __AVR_ATtinyX5__
// set clock frequency prescaler to 1 => 8MHz
CLKPR = 0x80; // set high bit to enabled prescale write
CLKPR = 0x00; // set prescale to 0 = /1
#endif
pinMode(DATA, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(BEEP, OUTPUT);
#ifdef DEMUX_2_TO_3
pinMode(DIGIT_L, OUTPUT);
pinMode(DIGIT_H, OUTPUT);
#else
pinMode(DIGIT_1, OUTPUT);
pinMode(DIGIT_2, OUTPUT);
pinMode(DIGIT_3, OUTPUT);
#endif
digitalWrite(DATA, LOW);
digitalWrite(CLOCK, LOW);
#ifdef DEMUX_2_TO_3
digitalWrite(DIGIT_L, HIGH);
digitalWrite(DIGIT_H, HIGH);
#else
digitalWrite(DIGIT_1, HIGH);
digitalWrite(DIGIT_2, HIGH);
digitalWrite(DIGIT_3, HIGH);
#endif
unsigned long frequency = 294 * 2; // D, octave 1
computePWM(BEEP_PWM, frequency, prescale, top);
// usefull to debug on ATtiny
// tempo=frequency;
// times=prescale;
// subs=top;
// reserve timers, but stop them
autorepeatTimer = setInterval(SET_INTERVAL_PAUSED, autorepeat, NULL);
tacTimer = setInterval(SET_INTERVAL_PAUSED, stopTac, NULL);
tickTimer = setInterval(SET_INTERVAL_PAUSED, nextTick, NULL);
computeTicks();
setIntervalTimer updateDisplayTimer = setInterval(3, updateDisplay, NULL);
#ifdef DEBUG
Serial.begin(DEFAULT_BAUDRATE);
Serial.print("tickTimer = ");Serial.println(tickTimer);
Serial.print("updateDisplayTimer = ");Serial.println(updateDisplayTimer);
Serial.print("autorepeatTimer = ");Serial.println(autorepeatTimer);
Serial.print("tacTimer = ");Serial.println(tacTimer);
Serial.println("Ready ..");
#endif
}
void loop() {
setIntervalStep();
}