-
Notifications
You must be signed in to change notification settings - Fork 4
/
MidiHub.c
808 lines (731 loc) · 19.5 KB
/
MidiHub.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//////////////////////////////////////////////////////////////////////////
//
// // //
// // // // // // // //
// // // // ////// //////
// //////// // ////// // ////// // // ////// // //
// // // // // // // // // // // // // // // //
// // // // // // // // // // // // // //
// // // // // // // // // // // // //
// // // // ////// // // // ////// //////
//
// MIDI HUB WITH BEAT CLOCK METRONOME
// By Jason Hotchkiss
// FOR PIC16F1825 SOURCEBOOST C
//
// This work is licensed under the Creative Commons
// Attribution-NonCommercial 3.0 Unported License.
// To view a copy of this license, please visit:
// http://creativecommons.org/licenses/by-nc/3.0/
//
// Please contact me directly if you'd like a CC
// license allowing use for commercial purposes:
// jason_hotchkiss<at>hotmail.com
//
// Full repository with hardware information:
// https://github.com/hotchk155/MIDI-Hub
// Rev H0: Feb 2013 - port of original PIC16F688 code
// Rev H1: 29 Mar 2013 - fix issue with input buffer position
// Rev H2: 17 May 2013 - increase debounce period
// Rev H3: 12 Jul 2013 - use 16MHz clock, increase BPM accuracy, LED PWM
// Rev H4: 1 Sep 2013 - tap tempo mode, options menu
// Rev H5: 17 Nov 2013 - new options for explicit midi START/STOP/CONTINUE
// Rev H6: 29 Dec 2013 - tighter baudrate definition, added version display
//
#define FIRMWARE_VERSION 6
//////////////////////////////////////////////////////////////////////////
//
// HEADER FILES
//
#include <system.h>
#include <rand.h>
#include <eeprom.h>
// PIC CONFIG BITS
// - RESET INPUT DISABLED
// - WATCHDOG TIMER OFF
// - INTERNAL OSC
#pragma DATA _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF &_CLKOUTEN_OFF
#pragma DATA _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_19 & _LVP_OFF
#pragma CLOCK_FREQ 16000000
//
// TYPE DEFS
//
typedef unsigned char byte;
//
// MACRO DEFS
//
// inputs
#define P_RUN portc.3
#define P_DEC porta.4
#define P_INC porta.5
// outputs
#define P_LED0 portc.0
#define P_LED1 portc.1
#define P_LED2 portc.2
#define P_LED3 porta.1
#define P_LED4 porta.0
#define P_LED5 porta.2
// bit masks for button tracking
#define M_BUTTON_RUN 0x01
#define M_BUTTON_INC 0x02
#define M_BUTTON_DEC 0x04
#define M_LONG_PRESS 0x40
#define M_AUTO_REPEAT 0x80
// MIDI beat clock messages
#define MIDI_SYNCH_TICK 0xf8
#define MIDI_SYNCH_START 0xfa
#define MIDI_SYNCH_CONTINUE 0xfb
#define MIDI_SYNCH_STOP 0xfc
// Auto repeat delays
#define AUTO_REPEAT_INTERVAL 80
#define AUTO_REPEAT_DELAY 500
// Key debounce period
#define DEBOUNCE_PERIOD 100
// PWM fade stuff
#define FADE_PERIOD 30
#define PWM_DIM 5
#define PWM_MAX 50
#define INITIAL_DUTY 10
#define TIMER_0_INIT_SCALAR 5 // Timer 0 is an 8 bit timer counting at 250kHz
// using this init scalar means that rollover
// interrupt fires once per ms
// Tempo defs
#define BPM_MIN 30
#define BPM_MAX 300
#define BPM_DEFAULT 120
// EEPROM usage
#define EEPROM_ADDR_MAGIC_COOKIE 9
#define EEPROM_ADDR_OPTIONS 10
#define EEPROM_MAGIC_COOKIE 0xA5
// Menu size
#define MENU_SIZE 6
//
// GLOBAL DATA
//
// timer stuff
volatile byte tick_flag = 0;
volatile unsigned int timer_init_scalar = 0;
volatile unsigned long systemTicks = 0; // each system tick is 1ms
// define the buffer used to receive MIDI input
#define SZ_RXBUFFER 20
volatile byte rxBuffer[SZ_RXBUFFER];
volatile byte rxHead = 0;
volatile byte rxTail = 0;
// Configuration options
enum {
OPTION_PASSREALTIMEMSG = 0x01,
OPTION_PASSOTHERMSG = 0x02,
OPTION_STARTSTOP = 0x04,
OPTION_THRUANIMATE = 0x08,
OPTION_DISCREET = 0x10,
OPTIONS_DEFAULT = OPTION_PASSOTHERMSG|OPTION_STARTSTOP|OPTION_THRUANIMATE
};
byte _options = OPTIONS_DEFAULT;
// Operating modes
enum {
MODE_STEP, // BEAT CLOCK ON, INC/DEC SET
MODE_TAP, // BEAT CLOCK ON, TAP SET
MODE_NOCLOCK, // BEAT CLOCK OFF
MODE_MENU // BEAT CLOCK OFF, OPTIONS MENU
};
byte _mode = MODE_STEP;
// Brightness settings
#define NUM_BRIGHTNESS_LEVELS 6
byte brightnessLevels[NUM_BRIGHTNESS_LEVELS];
byte _brightness = 0;
// LED duty buffer
byte duty[6];
// BPM setting
int _bpm = 0;
////////////////////////////////////////////////////////////
// INTERRUPT HANDLER CALLED WHEN CHARACTER RECEIVED AT
// SERIAL PORT OR WHEN TIMER 1 OVERLOWS
void interrupt( void )
{
// timer 0 rollover ISR. Maintains the count of
// "system ticks" that we use for key debounce etc
if(intcon.2)
{
tmr0 = TIMER_0_INIT_SCALAR;
systemTicks++;
intcon.2 = 0;
}
// timer 1 rollover ISR. Responsible for timing
// the tempo of the MIDI clock
if(pir1.0)
{
tmr1l=(timer_init_scalar & 0xff);
tmr1h=(timer_init_scalar>>8);
tick_flag = 1;
pir1.0 = 0;
}
// serial rx ISR
if(pir1.5)
{
// get the byte
byte b = rcreg;
// calculate next buffer head
byte nextHead = (rxHead + 1);
if(nextHead >= SZ_RXBUFFER)
{
nextHead -= SZ_RXBUFFER;
}
// if buffer is not full
if(nextHead != rxTail)
{
// store the byte
rxBuffer[rxHead] = b;
rxHead = nextHead;
}
}
}
////////////////////////////////////////////////////////////
// SAVE OPTIONS TO EEPROM
void saveOptions()
{
eeprom_write(EEPROM_ADDR_OPTIONS, _options);
eeprom_write(EEPROM_ADDR_MAGIC_COOKIE, EEPROM_MAGIC_COOKIE);
}
////////////////////////////////////////////////////////////
// LOAD OPTIONS FROM EEPROM
void loadOptions()
{
// "magic cookie" is a known value written to the EEPROM
// with each valid save. Makes sure we can avoid reading
// garbage from EEPROM when there is no previous save
_options = eeprom_read(EEPROM_ADDR_OPTIONS);
if(eeprom_read(EEPROM_ADDR_MAGIC_COOKIE) != EEPROM_MAGIC_COOKIE)
_options = OPTIONS_DEFAULT;
}
////////////////////////////////////////////////////////////
// INITIALISE SERIAL PORT FOR MIDI
void initUSART()
{
pir1.1 = 1; //TXIF
pir1.5 = 0; //RCIF
pie1.1 = 0; //TXIE no interrupts
pie1.5 = 1; //RCIE enable
baudcon.4 = 0; // SCKP synchronous bit polarity
baudcon.3 = 1; // BRG16 enable 16 bit brg
baudcon.1 = 0; // WUE wake up enable off
baudcon.0 = 0; // ABDEN auto baud detect
txsta.6 = 0; // TX9 8 bit transmission
txsta.5 = 1; // TXEN transmit enable
txsta.4 = 0; // SYNC async mode
txsta.3 = 0; // SEDNB break character
txsta.2 = 0; // BRGH high baudrate
txsta.0 = 0; // TX9D bit 9
rcsta.7 = 1; // SPEN serial port enable
rcsta.6 = 0; // RX9 8 bit operation
rcsta.5 = 1; // SREN enable receiver
rcsta.4 = 1; // CREN continuous receive enable
spbrgh = 0; // brg high byte
spbrg = 31; // brg low byte (31250)
}
////////////////////////////////////////////////////////////
// SEND A BYTE ON SERIAL PORT
void send(byte c)
{
txreg = c;
while(!txsta.1);
}
////////////////////////////////////////////////////////////
// RUN MIDI THRU
void midiThru()
{
// loop until there is no more data or
// we receive a full message
for(;;)
{
// buffer overrun error?
if(rcsta.1)
{
rcsta.4 = 0;
rcsta.4 = 1;
}
// any data in the buffer?
if(rxHead == rxTail)
{
// no data ready
return;
}
// read the character out of buffer
byte q = rxBuffer[rxTail];
if(++rxTail >= SZ_RXBUFFER)
rxTail -= SZ_RXBUFFER;
// Check for MIDI realtime message (e.g. clock)
if((q & 0xF8) == 0xF8)
{
// if we are not passing realtime messages then skip it
if(!(_options & OPTION_PASSREALTIMEMSG))
continue;
}
else
{
// if we are not passing non-realtime messages then skip it
if(!(_options & OPTION_PASSOTHERMSG))
continue;
}
// should we animate the LEDs based on thru traffic?
if(MODE_NOCLOCK == _mode && (_options & OPTION_THRUANIMATE))
{
// animate and send
duty[q%6] = q%INITIAL_DUTY;
send(q);
}
// should we indicate thru traffic with flickering LEDs?
else
{
// flicker and send
P_LED2 = 1;
P_LED3 = 1;
send(q);
P_LED2 = 0;
P_LED3 = 0;
}
}
}
////////////////////////////////////////////////////////////
// SETUP THE TIMER FOR A SPECIFIC BPM
void setBPM(int b)
{
if(b < BPM_MIN)
b = BPM_MIN;
else if(b > BPM_MAX)
b = BPM_MAX;
_bpm = b;
/*
beats per second = bpm / 60
midi ticks per second = 24 * (bpm / 60)
timer counts per MIDI tick = (timer counts per second)/(midi ticks per second)
= (timer counts per second)/(24 * (bpm / 60))
= (timer counts per second/24)/(bpm / 60)
= 60 * (timer counts per second/24)/bpm
timer init scalar = 65536 - timer counts per MIDI tick
*/
#define TIMER_COUNTS_PER_SECOND (unsigned long)500000
unsigned long x = (60 * TIMER_COUNTS_PER_SECOND)/24;
x = x / _bpm;
timer_init_scalar = 65535 - x;
}
////////////////////////////////////////////////////////////
// SHOW VERSION
void showVersion()
{
P_LED0 = !!(FIRMWARE_VERSION&0x20);
P_LED1 = !!(FIRMWARE_VERSION&0x10);
P_LED2 = !!(FIRMWARE_VERSION&0x08);
P_LED3 = !!(FIRMWARE_VERSION&0x04);
P_LED4 = !!(FIRMWARE_VERSION&0x02);
P_LED5 = !!(FIRMWARE_VERSION&0x01);
delay_s(5);
}
////////////////////////////////////////////////////////////
// MAIN
void main()
{
// initialise app variables
byte running = 0;
byte tickCount = 0;
byte lastButtonStatus = 0;
unsigned long autoRepeatBegin = 0;
unsigned long nextAutoRepeat = 0;
unsigned long nextFade = 0;
unsigned long debouncePeriodEnd = 0;
unsigned long lastTapSystemTicks = 0;
unsigned long firstTapSystemTicks = 0;
unsigned long tapPeriodAccumulator = 0;
unsigned long menuLoopCount = 0;
byte tapCount = 0;
byte menuOption = 0;
byte runLock = 0;
byte midiRestart = 0;
// initialise brightness levels
brightnessLevels[0] = 50;
brightnessLevels[1] = 20;
brightnessLevels[2] = 10;
brightnessLevels[3] = 5;
brightnessLevels[4] = 2;
brightnessLevels[5] = 1;
byte maxDuty = brightnessLevels[0];
byte pwm=0;
duty[0] = duty[1] = duty[2] = duty[3] = duty[4] = duty[5] = 0;
// osc control / 16MHz / internal
osccon = 0b01111010;
// configure io
trisa = 0b00110000;
trisc = 0b00111000;
ansela = 0b00000000;
anselc = 0b00000000;
porta=0;
portc=0;
// Version display
if(!P_RUN)
showVersion();
// initialise MIDI comms
initUSART();
// setup default BPM
setBPM(BPM_DEFAULT);
// Configure timer 1 (controls tempo)
// Input 4MHz
// Prescaled to 500KHz
tmr1l = 0; // reset timer count register
tmr1h = 0;
t1con.7 = 0; // } Fosc/4 rate
t1con.6 = 0; // }
t1con.5 = 1; // } 1:8 prescale
t1con.4 = 1; // }
t1con.0 = 1; // timer 1 on
pie1.0 = 1; // timer 1 interrupt enable
// Configure timer 0 (controls systemticks)
// timer 0 runs at 4MHz
// prescaled 1/16 = 250kHz
// rollover at 250 = 1kHz
// 1ms per rollover
option_reg.5 = 0; // timer 0 driven from instruction cycle clock
option_reg.3 = 0; // timer 0 is prescaled
option_reg.2 = 0; // }
option_reg.1 = 1; // } 1/16 prescaler
option_reg.0 = 1; // }
intcon.5 = 1; // enabled timer 0 interrrupt
intcon.2 = 0; // clear interrupt fired flag
// enable interrupts
intcon.7 = 1; //GIE
intcon.6 = 1; //PEIE
// default operating mode is clock/step control
_mode = MODE_STEP;
// load options
loadOptions();
// App loop
for(;;)
{
// run midi thru
midiThru();
// RUNNING MENU
if(MODE_MENU == _mode)
{
menuLoopCount++;
byte flash = ((menuLoopCount & 0xF00) == 0x100);
duty[0] = (flash && (menuOption == 0)) ? PWM_MAX : ((_options & (1<<0)) ? PWM_DIM : 0);
duty[1] = (flash && (menuOption == 1)) ? PWM_MAX : ((_options & (1<<1)) ? PWM_DIM : 0);
duty[2] = (flash && (menuOption == 2)) ? PWM_MAX : ((_options & (1<<2)) ? PWM_DIM : 0);
duty[3] = (flash && (menuOption == 3)) ? PWM_MAX : ((_options & (1<<3)) ? PWM_DIM : 0);
duty[4] = (flash && (menuOption == 4)) ? PWM_MAX : ((_options & (1<<4)) ? PWM_DIM : 0);
duty[5] = maxDuty;
}
else
// SPLIT-ONLY MODE
if(MODE_NOCLOCK == _mode)
{
// animation is driven from MIDI thru function,
// but we'll fade the LEDs in this main loop
if(systemTicks > nextFade)
{
for(byte i=0;i<6;++i)
if(duty[i])
--duty[i];
nextFade = systemTicks + FADE_PERIOD;
}
}
else
// STEP/TAP MODE
if(tick_flag)
{
tick_flag = 0;
if(++tickCount > 23)
{
if(midiRestart)
{
send(MIDI_SYNCH_START);
midiRestart = 0;
}
tickCount = 0;
}
if(running)
send(MIDI_SYNCH_TICK);
// mid-tap entry?
if(tapCount)
{
// illuminate LEDs for tap tempo entry
duty[0] = PWM_MAX;
duty[1] = (tapCount > 1) ? maxDuty : 0;
duty[2] = (tapCount > 2) ? maxDuty : 0;
duty[3] = (tapCount > 3) ? maxDuty : 0;
duty[4] = (tapCount > 4) ? maxDuty : 0;
duty[5] = (tapCount > 5) ? maxDuty : 0;
// exit tap temp entry if it has been more than
// 1 second since the last valid tap
if(systemTicks - lastTapSystemTicks > 1000)
{
lastTapSystemTicks = 0;
firstTapSystemTicks = 0;
tapCount = 0;
}
}
else if(running)
{
if(_options & OPTION_DISCREET)
{
duty[0] = maxDuty;
duty[1] = 0;
duty[2] = 0;
duty[3] = 0;
duty[4] = 0;
duty[5] = (tickCount == 1)? maxDuty : 0;
}
else
{
// Cycling "running" animation
byte whichLED = tickCount/4;
duty[0] = (whichLED == 0)? maxDuty : 0;
duty[1] = (whichLED == 1)? maxDuty : 0;
duty[2] = (whichLED == 2)? maxDuty : 0;
duty[3] = (whichLED == 3)? maxDuty : 0;
duty[4] = (whichLED == 4)? maxDuty : 0;
duty[5] = (whichLED == 5)? maxDuty : 0;
}
}
else
{
if(_options & OPTION_DISCREET)
{
duty[0] = 0;
duty[1] = 0;
duty[2] = 0;
duty[3] = 0;
duty[4] = 0;
duty[5] = tickCount? 0 : maxDuty;
}
else
{
// Flashing "paused" indicator (leds 0,1,4,5)
duty[0] = tickCount? 0 : maxDuty;
duty[1] = tickCount? 0 : maxDuty;
duty[2] = 0;
duty[3] = 0;
duty[4] = tickCount? 0 : maxDuty;
duty[5] = tickCount? 0 : maxDuty;
}
}
}
// PWM the LEDs
P_LED0 = (duty[0]>pwm);
P_LED1 = (duty[1]>pwm);
P_LED2 = (duty[2]>pwm);
P_LED3 = (duty[3]>pwm);
P_LED4 = (duty[4]>pwm);
P_LED5 = (duty[5]>pwm);
if(++pwm > PWM_MAX)
pwm=0;
// HANDLE USER INPUT
if(systemTicks >= debouncePeriodEnd) // check not debouncing
{
// gather up the button statuses into a single byte
byte thisButtonStatus =
(!P_RUN ? M_BUTTON_RUN : 0) |
(!P_DEC ? M_BUTTON_DEC : 0) |
(!P_INC ? M_BUTTON_INC : 0);
// see if anything has changed since last poll
byte buttonActivity = thisButtonStatus ^ lastButtonStatus;
lastButtonStatus = thisButtonStatus;
byte buttonsPressed = 0;
if(!buttonActivity)
{
// do we need to autorepeat?
if(thisButtonStatus && systemTicks > autoRepeatBegin)
{
if(!nextAutoRepeat)
{
thisButtonStatus = thisButtonStatus|M_LONG_PRESS;
buttonsPressed = thisButtonStatus;
nextAutoRepeat = systemTicks + AUTO_REPEAT_INTERVAL;
}
else if(systemTicks > nextAutoRepeat)
{
thisButtonStatus = thisButtonStatus|M_AUTO_REPEAT;
buttonsPressed = thisButtonStatus;
nextAutoRepeat = systemTicks + AUTO_REPEAT_INTERVAL;
}
}
}
else
{
// mask out just buttons that have been pressed
buttonsPressed = buttonActivity & thisButtonStatus;
if(buttonsPressed)
{
// prepare debounce and auto repeat
autoRepeatBegin = systemTicks + AUTO_REPEAT_DELAY;
nextAutoRepeat = 0;
}
debouncePeriodEnd = systemTicks + DEBOUNCE_PERIOD;
}
// any new button presses or auto repeats?
if(buttonsPressed)
{
// execute the command
switch(thisButtonStatus)
{
////////////////////////////////////////////////////////////
// ALL BUTTONS PRESSED TOGETHER (MENU MODE)
case M_BUTTON_RUN|M_BUTTON_DEC|M_BUTTON_INC:
menuOption = 0;
_mode = MODE_MENU;
break;
////////////////////////////////////////////////////////////
// DEC AND RUN PRESSED TOGETHER (TAP TEMPO MODE)
case M_BUTTON_RUN|M_BUTTON_DEC:
_mode = MODE_TAP;
break;
////////////////////////////////////////////////////////////
// INC AND RUN PRESSED TOGETHER (SPLIT ONLY MODE)
case M_BUTTON_RUN|M_BUTTON_INC:
_mode = MODE_NOCLOCK;
break;
////////////////////////////////////////////////////////////
// INC AND DEC PRESSED TOGETHER (DEFAULT BPM)
case M_BUTTON_INC|M_BUTTON_DEC:
if(MODE_STEP == _mode)
setBPM(BPM_DEFAULT);
break;
////////////////////////////////////////////////////////////
// RUN PRESSED
case M_BUTTON_RUN:
if(MODE_TAP == _mode || MODE_STEP == _mode)
{
if(runLock)
{
midiRestart = 1;
}
else
{
// When clock is enabled we toggle run/paused
running = !running;
if(_options & OPTION_STARTSTOP)
{
if(running)
{
tickCount = 0;
send(MIDI_SYNCH_START);
}
else
{
send(MIDI_SYNCH_STOP);
}
}
}
}
else
if(MODE_NOCLOCK == _mode) // SPLIT MODE
{
send(MIDI_SYNCH_START);
running = 1;
}
else
if(MODE_MENU == _mode)
{
// Exits from menu mode
_mode = MODE_STEP;
running = 0;
}
break;
case M_BUTTON_RUN|M_LONG_PRESS:
if(!runLock)
{
runLock = 1;
running = 1;
}
else
{
runLock = 0;
}
break;
////////////////////////////////////////////////////////////
// DEC PRESSED
case M_BUTTON_DEC:
if(MODE_MENU == _mode)
{
if(5 == menuOption)
{
_brightness = (_brightness + 1) % NUM_BRIGHTNESS_LEVELS;
maxDuty = brightnessLevels[_brightness];
}
else
{
// In menu mode, toggles options on/off
_options ^= (1<<menuOption);
}
saveOptions();
break;
}
else
if(MODE_NOCLOCK == _mode)
{
// Exits split-only mode
_mode = MODE_STEP;
break;
}
else
if(MODE_TAP == _mode)
{
// In tap mode, counts a "tap"
if(!tapCount)
{
tapCount = 1;
firstTapSystemTicks = systemTicks;
}
else
if(tapCount < 6 && systemTicks > firstTapSystemTicks)//rollover check
{
unsigned long period = systemTicks - firstTapSystemTicks;
period = period / tapCount;
setBPM(60000UL / period);
tapCount++;
}
lastTapSystemTicks = systemTicks;
break;
}//fallthru
case M_LONG_PRESS|M_BUTTON_DEC:
case M_AUTO_REPEAT|M_BUTTON_DEC:
if(MODE_STEP == _mode)
setBPM(_bpm-1);
break;
////////////////////////////////////////////////////////////
// INC PRESSED
case M_BUTTON_INC:
if(MODE_MENU == _mode)
{
// menu mode - select menu option
menuOption = (menuOption+1) % MENU_SIZE;
break;
}
else
if(MODE_NOCLOCK == _mode) // SPLIT MODE
{
if(running)
{
send(MIDI_SYNCH_STOP);
running = 0;
}
else
{
send(MIDI_SYNCH_CONTINUE);
running = 1;
}
}
if(MODE_TAP == _mode)
{
// tap mode - exits tap mode
_mode = MODE_STEP;
break;
}//fallthru
case M_LONG_PRESS|M_BUTTON_INC:
case M_AUTO_REPEAT|M_BUTTON_INC:
if(MODE_STEP == _mode)
setBPM(_bpm+1);
break;
}
}
}
}
}