/
main.c
1584 lines (1442 loc) · 43.5 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
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: main.c 626 2009-01-19 21:36:33Z tk $
/*
* Pedal Switcher
*
* ==========================================================================
*
* Copyright (C) <2012> <R.Mouneyres> (<gorest@free.fr>)
* Licensed for personal non-commercial use only.
* All other rights reserved.
*
* ==========================================================================
*/
/////////////////////////////////////////////////////////////////////////////
// Include files
/////////////////////////////////////////////////////////////////////////////
#include <cmios.h>
#include <pic18fregs.h>
#include <debug_msg.h>
/////////////////////////////////////////////////////////////////////////////
// Defines
/////////////////////////////////////////////////////////////////////////////
#define DEBUG 1 // 1 : activate debug traces
#define MAX_RELAYS_NB 8 //number of relays connected to DOUT module
#define MAX_RELAY_LED_NB 8 //number of relay leds connected to DOUT module
#define MAX_RELAY_BUTTONS_NB 8 //number of relay buttons connected to DIN module
#define MAX_MENU_BUTTONS_NB 4 //number of menu buttons connected to DIN module
#define MAX_FOOTSWITCHES_NB 4 //number of footswitches connected to DIN module
#define DIN_DEBOUNCE_VALUE 10 // 0..255
#define NUMBER_OF_SRIO 16 //1...16, 1 DIN and 1 DOUT module
#define MENU_BUTTON_1 1 //Prog, Save, next
#define MENU_BUTTON_2 2 //Opts, Back, Q
#define MENU_BUTTON_3 3 //UP, +
#define MENU_BUTTON_4 4 //DOWN, -
#define MOMENTARY_PUSH_LENGTH 200 // time in ms for momentary push relays
#define Free 0
#define Radio 1
#define Toggle 0
#define Momentary 1
#define Inactive 0
#define Active 1
/////////////////////////////////////////////////////////////////////////////
// Hardware DIN/DOUT Pin Assignation
/////////////////////////////////////////////////////////////////////////////
// DOUT
// 1>8 relay leds (220R mounted on DOUT module)
const unsigned char RelayLedPin[MAX_RELAY_LED_NB] = {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07 };
// 9>16 relays (220R NOT mounted on DOUT module)
const unsigned char RelayPin[MAX_RELAYS_NB] = {
0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F };
// 17>20 free DOUT pins
// 21>24 Footswitches leds (220R mounted on DOUT module)
const unsigned char FootLedPin[MAX_FOOTSWITCHES_NB] = {
0x17, 0x16, 0x15, 0x14 };
// DIN
// 1>8 Buttons to command relays
const unsigned char ButtonPin[MAX_RELAY_BUTTONS_NB] = {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07 };
// 9>12 Buttons for menu
const unsigned char MenuButtonPin[MAX_MENU_BUTTONS_NB] = {
0x0B, 0x0A, 0x08, 0x09 };
// 13>16 Footswitches
const unsigned char FootButtonPin[MAX_FOOTSWITCHES_NB] = {
0x0E, 0x0F, 0x0D, 0x0C };
/////////////////////////////////////////////////////////////////////////////
// Local variables
/////////////////////////////////////////////////////////////////////////////
// eeprom global options
unsigned char MidiChannel; // AllChannels=0, 1-16=MidiChannel
unsigned char CurrentPreset; // Current and last used preset onload
unsigned char SendMidiOnButtonChange; // Inactive=0, Active=1
unsigned char ReloadActivePreset; // Don't=0, Do=1
unsigned char ForceStartupPreset; // Don't=0, Do=1
unsigned char ThisStartupPreset; // Preset number for specific startup
unsigned char RelayType[MAX_RELAYS_NB]; // Toggle=0, Momentary=1
unsigned char RelayMode[MAX_RELAYS_NB]; // Free=0, radiogroup=1
unsigned char FootPreset[MAX_FOOTSWITCHES_NB]; // Preset number for each footswitch
// preset values
unsigned char RelayState[MAX_RELAYS_NB]; // Open/Inactive=0, Closed/Active=1
// menu handling
unsigned char LastButton;
unsigned char MenuPos; // 0 : Default Screen
// 1 : PROG Menu
// 2 : OPTS Menu
// ...
// running variables
unsigned char TargetPreset;
unsigned char MidiIn;
/////////////////////////////////////////////////////////////////////////////
// Local user functions
/////////////////////////////////////////////////////////////////////////////
// change the output state according to the options
//---------------------------------------------------
void toggle_dout(unsigned char relay_nb)
{
if ( RelayType[relay_nb] == Toggle )
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Relay");
DEBUG_MSG_SendBCD3(relay_nb);
DEBUG_MSG_SendCString(" original state");
DEBUG_MSG_SendBCD3(RelayState[relay_nb]);
DEBUG_MSG_SendFooter();
#endif
//toggle Relay
MIOS_DOUT_PinSet(RelayPin[relay_nb], RelayState[relay_nb] ? 0 : 1);
//toggle Led
MIOS_DOUT_PinSet(RelayLedPin[relay_nb], RelayState[relay_nb] ? 0 : 1);
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Toggled relay");
DEBUG_MSG_SendBCD3(relay_nb);
DEBUG_MSG_SendFooter();
#endif
}
else if ( RelayType[relay_nb] == Momentary )
{
//push relay_nb
MIOS_DOUT_PinSet1(RelayPin[relay_nb]);
MIOS_Delay(MOMENTARY_PUSH_LENGTH);
MIOS_DOUT_PinSet0(RelayPin[relay_nb]);
//manage led state
if ( RelayState[relay_nb] == Inactive )
MIOS_DOUT_PinSet1(RelayLedPin[relay_nb]);
else
MIOS_DOUT_PinSet0(RelayLedPin[relay_nb]);
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Pushed relay");
DEBUG_MSG_SendBCD3(relay_nb);
DEBUG_MSG_SendFooter();
#endif
}
//Update RelayState
if ( RelayState[relay_nb] )
{ RelayState[relay_nb] = 0; }
else
{ RelayState[relay_nb] = 1; }
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString(" to state");
DEBUG_MSG_SendBCD3(RelayState[relay_nb]);
DEBUG_MSG_SendFooter();
#endif
}
// act on the relay output after a change request (panel button or midi message)
//---------------------------------------------------
void DoRelay(unsigned char relay_nb)
{
unsigned char Found = 0;
unsigned int i;
if ( RelayMode[relay_nb] == Free )
{
// free relay, toggle it
toggle_dout(relay_nb);
}
else if ( RelayMode[relay_nb] == Radio )
{
if ( RelayState[relay_nb] == Active )
{
// Deactivate an active radio button
toggle_dout(relay_nb);
}
else
{
for ( i=0; i<MAX_RELAYS_NB; i++ )
{
if ( (RelayMode[i] == Radio) && (RelayState[i] == Active) )
{
// Activate a new radio button
//désactiver led relay_i
MIOS_DOUT_PinSet0(RelayLedPin[i]);
//push relay_nb
MIOS_DOUT_PinSet1(RelayPin[relay_nb]);
MIOS_Delay(200);
MIOS_DOUT_PinSet0(RelayPin[relay_nb]);
//activer led_relay_nb
MIOS_DOUT_PinSet1(RelayLedPin[relay_nb]);
Found = 1;
//Update RelayState
RelayState[i] = Inactive;
RelayState[relay_nb] = Active;
}
}
if ( Found == 0 )
{
// Activate an inactive radio button
toggle_dout(relay_nb);
}
}
}
}
// return the switch number if the button is a footswitch
// else return 0
//---------------------------------------------------
unsigned char FootButton ( unsigned char pin_number )
{
unsigned int i;
for ( i=0; i<MAX_FOOTSWITCHES_NB; i++ )
{
if ( FootButtonPin[i] == pin_number )
{
return i+1;
}
}
return 0;
}
// return the relay number if the button commands a relay
// else return 0
//---------------------------------------------------
unsigned char RelayButton ( unsigned char pin_number )
{
unsigned int i;
for ( i=0; i<MAX_RELAY_BUTTONS_NB; i++ )
{
if ( ButtonPin[i] == pin_number )
{
return i+1;
}
}
return 0;
}
// return the button number if the button is defined as a MENU button
// else return 0
//---------------------------------------------------
unsigned char MenuButton ( unsigned char pin_number )
{
unsigned int i;
for ( i=0; i<MAX_MENU_BUTTONS_NB; i++ )
{
if ( MenuButtonPin[i] == pin_number )
{
return i+1;
}
}
return 0;
}
// Load a test preset (use for debugging)
//---------------------------------------------------
void LoadTestPreset()
{
unsigned int i;
// TEST PRESET
//------------------
MidiChannel = 0;
CurrentPreset = 1;
SendMidiOnButtonChange = 0;
for ( i=0; i<5; i++)
{
RelayType[i] = 0; //Toggle=0, Momentary=1
RelayMode[i] = 0; //Free=0, radiogroup=1
RelayState[i] = 0; //Open/Inactive=0, Closed/Active=1
}
for ( i=5; i<8; i++)
{
RelayType[i] = 1; //Toggle=0, Momentary=1
RelayMode[i] = 1; //Free=0, radiogroup=1
RelayState[i] = 0; //Open/Inactive=0, Closed/Active=1
}
}
// Load Global Options from EEPROM
//---------------------------------------------------
void LoadGlobalOptions ()
{
unsigned char eeprom_byte;
// EEPROM BYTE 0x00 : global options
// MidiChannel // bit0>3
// SendMidiOnButtonChange // bit4
// ReloadActivePreset // bit5
// ForceStartupPreset // bit6
// // bits7>8 free
eeprom_byte = MIOS_EEPROM_Read(0x00);
MidiChannel = eeprom_byte & 0x0F;
SendMidiOnButtonChange = (eeprom_byte & 0x10)>>4;
ReloadActivePreset = (eeprom_byte & 0x20)>>5;
ForceStartupPreset = (eeprom_byte & 0x40)>>6;
// EPROM BYTE 0x01 : Current Preset
//CurrentPreset // bits0>7
CurrentPreset = MIOS_EEPROM_Read(0x01);
// EPROM BYTE 0x02 : RelayType[1..8]
eeprom_byte = MIOS_EEPROM_Read(0x02);
RelayType[0] = eeprom_byte & 0x01;
RelayType[1] = (eeprom_byte & 0x02)>>1;
RelayType[2] = (eeprom_byte & 0x04)>>2;
RelayType[3] = (eeprom_byte & 0x08)>>3;
RelayType[4] = (eeprom_byte & 0x10)>>4;
RelayType[5] = (eeprom_byte & 0x20)>>5;
RelayType[6] = (eeprom_byte & 0x40)>>6;
RelayType[7] = (eeprom_byte & 0x80)>>7;
// EPROM BYTE 0x03 : RelayMode[1..8]
eeprom_byte = MIOS_EEPROM_Read(0x03);
RelayMode[0] = eeprom_byte & 0x01;
RelayMode[1] = (eeprom_byte & 0x02)>>1;
RelayMode[2] = (eeprom_byte & 0x04)>>2;
RelayMode[3] = (eeprom_byte & 0x08)>>3;
RelayMode[4] = (eeprom_byte & 0x10)>>4;
RelayMode[5] = (eeprom_byte & 0x20)>>5;
RelayMode[6] = (eeprom_byte & 0x40)>>6;
RelayMode[7] = (eeprom_byte & 0x80)>>7;
// EPROM BYTE 0x04 : Footswitch 1 preset
FootPreset[0] = MIOS_EEPROM_Read(0x04);
// EPROM BYTE 0x05 : Footswitch 2 preset
FootPreset[1] = MIOS_EEPROM_Read(0x05);
// EPROM BYTE 0x06 : Footswitch 3 preset
FootPreset[2] = MIOS_EEPROM_Read(0x06);
// EPROM BYTE 0x07 : Footswitch 4 preset
FootPreset[3] = MIOS_EEPROM_Read(0x07);
// EPROM BYTE 0x08 : Preset for startup when selected
ThisStartupPreset = MIOS_EEPROM_Read(0x08);
// EPROM BYTE 0x09 : *reserved*
// EPROM BYTE 0x0A : *reserved*
// EPROM BYTE 0x0B : *reserved*
// EPROM BYTE 0x0C : *reserved*
// EPROM BYTE 0x0D : *reserved*
// EPROM BYTE 0x0E : *reserved*
// EPROM BYTE 0x0F : *reserved*
}
// Save Global Options to EEPROM
//---------------------------------------------------
void SaveGlobalOptions ()
{
unsigned char eeprom_byte;
// EEPROM BYTE 0x00 : global options
// MidiChannel // bit0>3
// SendMidiOnButtonChange // bit4
// ReloadActivePreset // bit5
// ForceStartupPreset // bit6
// //bits7>8 free
eeprom_byte = MidiChannel & 0x0F;
eeprom_byte = eeprom_byte | SendMidiOnButtonChange<<4;
eeprom_byte = eeprom_byte | ReloadActivePreset<<5;
eeprom_byte = eeprom_byte | ForceStartupPreset<<6;
MIOS_EEPROM_Write(0x00,eeprom_byte);
// EPROM BYTE 0x01 : Current Preset
//CurrentPreset = 1; // bits0>7
MIOS_EEPROM_Write(0x01,CurrentPreset);
// EPROM BYTE 0x02 : RelayType[1..8]
eeprom_byte = RelayType[0];
eeprom_byte = eeprom_byte | RelayType[1]<<1;
eeprom_byte = eeprom_byte | RelayType[2]<<2;
eeprom_byte = eeprom_byte | RelayType[3]<<3;
eeprom_byte = eeprom_byte | RelayType[4]<<4;
eeprom_byte = eeprom_byte | RelayType[5]<<5;
eeprom_byte = eeprom_byte | RelayType[6]<<6;
eeprom_byte = eeprom_byte | RelayType[7]<<7;
MIOS_EEPROM_Write(0x02,eeprom_byte);
// EPROM BYTE 0x03 : RelayMode[1..8]
eeprom_byte = RelayMode[0];
eeprom_byte = eeprom_byte | RelayMode[1]<<1;
eeprom_byte = eeprom_byte | RelayMode[2]<<2;
eeprom_byte = eeprom_byte | RelayMode[3]<<3;
eeprom_byte = eeprom_byte | RelayMode[4]<<4;
eeprom_byte = eeprom_byte | RelayMode[5]<<5;
eeprom_byte = eeprom_byte | RelayMode[6]<<6;
eeprom_byte = eeprom_byte | RelayMode[7]<<7;
MIOS_EEPROM_Write(0x03,eeprom_byte);
// EPROM BYTE 0x04 : Footswitch 1 preset
eeprom_byte = FootPreset[0];
MIOS_EEPROM_Write(0x04,eeprom_byte);
// EPROM BYTE 0x05 : Footswitch 2 preset
eeprom_byte = FootPreset[1];
MIOS_EEPROM_Write(0x05,eeprom_byte);
// EPROM BYTE 0x06 : Footswitch 3 preset
eeprom_byte = FootPreset[2];
MIOS_EEPROM_Write(0x06,eeprom_byte);
// EPROM BYTE 0x07 : Footswitch 4 preset
eeprom_byte = FootPreset[3];
MIOS_EEPROM_Write(0x07,eeprom_byte);
// EPROM BYTE 0x08 : Preset for startup when selected
MIOS_EEPROM_Write(0x08,ThisStartupPreset);
// EPROM BYTE 0x09 : *reserved*
// EPROM BYTE 0x0A : *reserved*
// EPROM BYTE 0x0B : *reserved*
// EPROM BYTE 0x0C : *reserved*
// EPROM BYTE 0x0D : *reserved*
// EPROM BYTE 0x0E : *reserved*
// EPROM BYTE 0x0F : *reserved*
}
// Set the parameter as active preset and update eeprom
// if send midi is active, a midi out message is sent
//---------------------------------------------------
void SetActivePreset ( unsigned char preset_nb )
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Set Active preset");
DEBUG_MSG_SendBCD3(preset_nb);
DEBUG_MSG_SendFooter();
#endif
// set global variable
CurrentPreset = preset_nb;
// Save to eeprom
MIOS_EEPROM_Write(0x01,CurrentPreset);
// Update display
// print temporary message on LCD
MIOS_LCD_Clear();
MIOS_LCD_CursorSet(0x00 + 0);
MIOS_LCD_PrintCString("** Change to **");
MIOS_LCD_CursorSet(0x40 + 0);
MIOS_LCD_PrintCString("** Preset **");
MIOS_LCD_CursorSet(0x40 + 10);
MIOS_LCD_PrintBCD3(CurrentPreset);
MIOS_LCD_MessageStart(100);
}
// turn off all footswitch leds
//---------------------------------------------------
void FootLedsOff ()
{
unsigned int i;
// clear all leds
for ( i=0; i<MAX_FOOTSWITCHES_NB; i++ )
{
MIOS_DOUT_PinSet(FootLedPin[i], 0);
}
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Footswitch Leds Off");
DEBUG_MSG_SendFooter();
#endif
}
// activate the led corresponding to a footswitch
//---------------------------------------------------
void SetFootLed ( unsigned char led_nb )
{
unsigned int i;
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Footswitch Led: ");
DEBUG_MSG_SendBCD3(FootLedPin[led_nb]);
DEBUG_MSG_SendChar(' ');
DEBUG_MSG_SendCString(" On");
DEBUG_MSG_SendFooter();
#endif
// activate the good led
MIOS_DOUT_PinSet1(FootLedPin[led_nb]);
}
// Load a preset from EEPROM and set preset active
//---------------------------------------------------
void LoadPreset ( unsigned char preset_nb )
{
unsigned int i;
unsigned int ActiveRadioFound = 0;
unsigned char eeprom_byte;
unsigned char TargetRelayState[MAX_RELAYS_NB]; // Open/Inactive=0, Closed/Active=1
// load preset only if necessary/allowed
if ( CurrentPreset == preset_nb )
{
if(ReloadActivePreset)
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("ReLoading active preset");
DEBUG_MSG_SendFooter();
#endif
//reload active preset (implicit below)
}
else
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("NOT ReLoading active preset");
DEBUG_MSG_SendFooter();
#endif
//ignore the change request
return;
}
}
else
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Loading preset ");
DEBUG_MSG_SendBCD3(preset_nb);
DEBUG_MSG_SendFooter();
#endif
}
// read preset byte from eeprom
// A preset is using 1 bytes for RelayState[1..8]
// 100 presets, BYTE 0x10 ... 0x74
if ( preset_nb > 0x69 )
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Discarded loading preset ");
DEBUG_MSG_SendBCD3(preset_nb);
DEBUG_MSG_SendFooter();
#endif
return;
}
eeprom_byte = MIOS_EEPROM_Read(preset_nb+0x10);
TargetRelayState[0] = eeprom_byte & 0x01;
TargetRelayState[1] = (eeprom_byte & 0x02)>>1;
TargetRelayState[2] = (eeprom_byte & 0x04)>>2;
TargetRelayState[3] = (eeprom_byte & 0x08)>>3;
TargetRelayState[4] = (eeprom_byte & 0x10)>>4;
TargetRelayState[5] = (eeprom_byte & 0x20)>>5;
TargetRelayState[6] = (eeprom_byte & 0x40)>>6;
TargetRelayState[7] = (eeprom_byte & 0x80)>>7;
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Relays state byte : ");
DEBUG_MSG_SendBCD3(eeprom_byte);
DEBUG_MSG_SendFooter();
#endif
// send midi message ?
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Send midi ?");
DEBUG_MSG_SendBCD1(SendMidiOnButtonChange);
DEBUG_MSG_SendFooter();
#endif
if (SendMidiOnButtonChange)
{
MIOS_MIDI_BeginStream();
// Program change = 0xC?
MIOS_MIDI_TxBufferPut((0xC0)+MidiChannel-1);
MIOS_MIDI_TxBufferPut(preset_nb-1); //PC
MIOS_MIDI_EndStream();
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString(" midi message :");
DEBUG_MSG_SendBCD3((0xC0)+MidiChannel-1);
DEBUG_MSG_SendBCD3(preset_nb-1);
DEBUG_MSG_SendFooter();
#endif
}
// activate footswitch led if corresponding preset
FootLedsOff();
for ( i=0; i<MAX_FOOTSWITCHES_NB; i++ )
{
if ( FootPreset[i] == preset_nb )
{
SetFootLed(i);
}
}
// Update relay state
for ( i=0; i<MAX_RELAYS_NB; i++)
{
// Change free relays state
if ( RelayMode[i] == Free )
{
MIOS_DOUT_PinSet(RelayPin[i],TargetRelayState[i]);
MIOS_DOUT_PinSet(RelayLedPin[i],TargetRelayState[i]);
// update global variable
RelayState[i] = TargetRelayState[i];
}
// Deal with radio grouped relays
if (( RelayMode[i] == Radio ) && (TargetRelayState[i] == Active))
{
ActiveRadioFound = 1;
if (RelayState[i] == Active)
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Don't push an already active radio relay");
DEBUG_MSG_SendBCD3(i);
DEBUG_MSG_SendFooter();
#endif
// force led lit, but don't push relay
MIOS_DOUT_PinSet(RelayLedPin[i],Active);
}
else
{ //push relay
DoRelay(i);
}
}
}
if ( ActiveRadioFound == 0 ) // No radio relay should be active
{
for ( i=0; i<MAX_RELAYS_NB; i++) // verify if one is active, then desactivate
{
if (( RelayMode[i] == Radio ) && (RelayState[i] == Active))
{
DoRelay(i);
}
}
}
SetActivePreset(preset_nb);
}
// Save a Preset to EEPROM
// starting at adress 0x10
//---------------------------------------------------
void SavePreset ( unsigned char preset_nb )
{
unsigned char eeprom_byte;
if ( preset_nb == 0 )
{
// ignore save to preset 0, factory
// print message on LCD for 2 seconds
MIOS_LCD_Clear();
MIOS_LCD_CursorSet(0x00);
MIOS_LCD_PrintCString("Cannot Save to");
MIOS_LCD_CursorSet(0x40);
MIOS_LCD_PrintCString("Preset 0");
MIOS_LCD_MessageStart(255);
return;
}
if ( preset_nb > 0x69 )
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Discarded saving preset ");
DEBUG_MSG_SendBCD3(preset_nb);
DEBUG_MSG_SendFooter();
#endif
return;
}
// EPROM BYTE 0x01 : Current Preset
//CurrentPreset // bits0>7
MIOS_EEPROM_Write(0x01,CurrentPreset);
// write preset byte to eeprom
// A preset is using 1 bytes for RelayState[1..8]
// 100 presets, BYTE 0x10 ... 0x74
eeprom_byte = RelayState[0];
eeprom_byte = eeprom_byte | RelayState[1]<<1;
eeprom_byte = eeprom_byte | RelayState[2]<<2;
eeprom_byte = eeprom_byte | RelayState[3]<<3;
eeprom_byte = eeprom_byte | RelayState[4]<<4;
eeprom_byte = eeprom_byte | RelayState[5]<<5;
eeprom_byte = eeprom_byte | RelayState[6]<<6;
eeprom_byte = eeprom_byte | RelayState[7]<<7;
MIOS_EEPROM_Write(preset_nb+0x10,eeprom_byte);
// set global variable
CurrentPreset = preset_nb;
// print message
MIOS_LCD_Clear();
MIOS_LCD_CursorSet(0x00 + 0);
MIOS_LCD_PrintCString("** SAVED to **");
MIOS_LCD_CursorSet(0x40 + 0);
MIOS_LCD_PrintCString("** Preset **");
MIOS_LCD_CursorSet(0x40 + 10);
MIOS_LCD_PrintBCD3(CurrentPreset);
MIOS_LCD_MessageStart(100);
}
// Go up one preset
//---------------------------------------------------
unsigned char Preset_Up()
{
if ( CurrentPreset < 100 )
{
LoadPreset(CurrentPreset+1);
}
else
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Ignored, already at highest preset");
DEBUG_MSG_SendFooter();
#endif
//ignore message, already at highest preset
return 0;
}
return 1;
}
// Go down one preset
//---------------------------------------------------
unsigned char Preset_Down()
{
if ( CurrentPreset > 0 )
{
LoadPreset(CurrentPreset-1);
}
else
{
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Ignored, already at lowest preset");
DEBUG_MSG_SendFooter();
#endif
//ignore message, already at lowest preset
return 0;
}
return 1;
}
// Reset EEPROM Memory with default values
//---------------------------------------------------
unsigned char FactoryReset ()
{
unsigned int i;
unsigned char eeprom_byte;
unsigned char error = 0;
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Factory Reset");
DEBUG_MSG_SendFooter();
#endif
// EEPROM BYTE 0x00 : global options
// MidiChannel = 0; // bit0>3
// SendMidiOnButtonChange = 0; // bit4
// ReloadActivePreset = 0; // bit5
// ForceStartupPreset = 0; // bit6
// //bits7>8 free = 0
eeprom_byte = 0x00;
MIOS_EEPROM_Write(0x00,eeprom_byte);
// EPROM BYTE 0x01 : Current Preset
SetActivePreset(0);
// EPROM BYTE 0x02 : RelayType[1..8]
eeprom_byte = 0x00; //all free
MIOS_EEPROM_Write(0x02,eeprom_byte);
// EPROM BYTE 0x03 : RelayMode[1..8]
eeprom_byte = 0x00; //all toggle
MIOS_EEPROM_Write(0x03,eeprom_byte);
// EPROM BYTE 0x04 : Footswitch 1 preset
MIOS_EEPROM_Write(0x04,1);
// EPROM BYTE 0x05 : Footswitch 2 preset
MIOS_EEPROM_Write(0x05,2);
// EPROM BYTE 0x06 : Footswitch 3 preset
MIOS_EEPROM_Write(0x06,3);
// EPROM BYTE 0x07 : Footswitch 4 preset
MIOS_EEPROM_Write(0x07,4);
// EPROM BYTE 0x08 : Preset for startup when selected
MIOS_EEPROM_Write(0x07,0);
// EPROM BYTE 0x09 : *reserved*
// EPROM BYTE 0x0A : *reserved*
// EPROM BYTE 0x0B : *reserved*
// EPROM BYTE 0x0C : *reserved*
// EPROM BYTE 0x0D : *reserved*
// EPROM BYTE 0x0E : *reserved*
// EPROM BYTE 0x0F : *reserved*
for ( i = 0x08; i<0x10; i++)
{
MIOS_EEPROM_Write(i,0x00);
}
// A preset is using 1 bytes for RelayState[1..8]
// 100 presets, BYTE 0x10 ... 0x74
for ( i = 0; i<100; i++)
{
error |= MIOS_EEPROM_Write(i+16,0x00); //all off
}
if( error ) {
return 0;
}
return 1;
}
// Get last preset from eeprom and load it
//---------------------------------------------------
void StartupPreset ()
{
CurrentPreset = 0; // force preset 0 to allow loading at startup even
// with the "Don't reload active preset" option on
if ( ForceStartupPreset )
{
ThisStartupPreset = MIOS_EEPROM_Read(0x08);
LoadPreset(ThisStartupPreset);
return;
}
// EPROM BYTE 0x01 : Current Preset
//CurrentPreset // bits0>7
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("STARTUP PRESET");
DEBUG_MSG_SendBCD3(CurrentPreset);
DEBUG_MSG_SendFooter();
#endif
LoadPreset(MIOS_EEPROM_Read(0x01));
}
/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS after startup to initialize the
// application
/////////////////////////////////////////////////////////////////////////////
void Init(void) __wparam
{
// set shift register update frequency
MIOS_SRIO_UpdateFrqSet(1); // ms
// maximum number of DIN/DOUT shift registers (1..16)
MIOS_SRIO_NumberSet(NUMBER_OF_SRIO);
// debouncing value for DINs
MIOS_SRIO_DebounceSet(DIN_DEBOUNCE_VALUE);
//Init menu buttons
LastButton = 0;
MenuPos = 0;
MidiIn = 0;
// factory reset startup button sequence?
// Load global options
LoadGlobalOptions();
// SPECIFIC
// Using the Sansamp programmable BDDI, the first relay is active on power on
RelayState[5]=1;
// Led on
MIOS_DOUT_PinSet(RelayLedPin[5], 1);
// Load Last preset used or single one (see options)
StartupPreset();
// send SysEx message to MIOS Terminal
#if DEBUG
DEBUG_MSG_SendHeader();
DEBUG_MSG_SendCString("Application ready.");
DEBUG_MSG_SendFooter();
#endif
}
/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS in the mainloop when nothing else is to do
//
// >>>>> MENU HANDLING IS HERE <<<<<
//
/////////////////////////////////////////////////////////////////////////////
void Tick(void) __wparam
{
// MENU handling
if ( MenuPos == 0 ) // default screen
{
switch (LastButton)
{
case 0 : // nothing to do
break;
case MENU_BUTTON_1 : // enter PROG menu
// change menu level
MenuPos = 1;
// print new menu
TargetPreset = CurrentPreset;
MIOS_LCD_Clear();
MIOS_LCD_CursorSet(0x00 + 0);
MIOS_LCD_PrintCString("SAVE");
MIOS_LCD_CursorSet(0x40 + 0);
MIOS_LCD_PrintCString("BACK");
MIOS_LCD_CursorSet(0x00 + 6);
MIOS_LCD_PrintCString("to preset ");
MIOS_LCD_CursorSet(0x40 + 8);
MIOS_LCD_PrintBCD3(TargetPreset);
break;
case MENU_BUTTON_2 : // enter OPTS menu
// change menu level
MenuPos = 6;
// print new menu
TargetPreset = CurrentPreset;
MIOS_LCD_Clear();
MIOS_LCD_CursorSet(0x00 + 0);
MIOS_LCD_PrintCString(">");
MIOS_LCD_CursorSet(0x40 + 0);
MIOS_LCD_PrintCString("Q");
MIOS_LCD_CursorSet(0x00 + 1);
MIOS_LCD_PrintCString(" Footswitch 1 ");
MIOS_LCD_CursorSet(0x40 + 1);
MIOS_LCD_PrintCString(" Preset ");
MIOS_LCD_CursorSet(0x40 + 11);
MIOS_LCD_PrintBCD3(FootPreset[0]);
break;
case MENU_BUTTON_3 : // Preset UP
Preset_Up();
break;
case MENU_BUTTON_4 : // Preset DOWN
Preset_Down();
break;
}
}
else if ( MenuPos == 1 ) // PROG menu
{
switch (LastButton)
{
case 0 : // nothing to do
break;
case MENU_BUTTON_1 : // save
// save it
SavePreset(TargetPreset);
SetActivePreset(TargetPreset);
// exit menu
MenuPos = 0;
break;
case MENU_BUTTON_2 : // back (without save)
// exit menu
MenuPos = 0;
// show message
MIOS_LCD_Clear();
MIOS_LCD_CursorSet(0x00 + 0);
MIOS_LCD_PrintCString("** Nothing **");
MIOS_LCD_CursorSet(0x40 + 0);
MIOS_LCD_PrintCString("** Saved **");
MIOS_LCD_MessageStart(100);
break;
case MENU_BUTTON_3 : // target value up
case MENU_BUTTON_4 : // target value down
if ( (LastButton-4) == 0 ) // Down button
{
if ( TargetPreset > 1)
{ TargetPreset--; }
else
{ TargetPreset = 100; }
}
else if ( (LastButton-3) == 0 ) // Up button
{
if ( TargetPreset < 100)
{ TargetPreset++; }
else
{ TargetPreset = 1; }
}
// update display
MIOS_LCD_CursorSet(0x40 + 8);
MIOS_LCD_PrintBCD3(TargetPreset);
break;
}
}
else if ( MenuPos >= 6 ) // OPTS menu
{
switch (LastButton)