OrganProject

Check-in [78153b1d43]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added Tcl script to build the C code for menus, and made the menu system more compact.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:78153b1d43a2326d5a7e951a4138475079662c81
User & Date: 200002852 2009-08-19 21:36:09
Context
2009-08-19
22:01
Cleaned up the menu construction code a little bit. check-in: 4a81302b24 user: 200002852 tags: trunk
21:36
Added Tcl script to build the C code for menus, and made the menu system more compact. check-in: 78153b1d43 user: 200002852 tags: trunk
2009-08-12
01:24
Changed firmware to place display ENABLE on pin 14. Schematic changed in last commit check-in: c2ed20c6a4 user: 200002852 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added terminal/makeMenus.tcl.



































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
proc init {} {
    variable menuNames {}
    variable programText {}
    variable stopCNames {}
    variable stringPool {}
    variable stringIndex {}
    rename init {}
}
init

proc makeString {text} {
    variable stringPool
    variable stringIndex
    if {![dict exists $stringIndex $text]} {
	dict set stringIndex $text [llength $stringPool]
	lappend stringPool $text
    }
    return "S[dict get $stringIndex $text] /* $text */"
}

proc makeStop {stopName} {
    variable stopCNames
    regsub -all {(\d)'} $stopName {\1FOOT} cname
    set cname [string map {
	{ 2/3} TWOTHIRDS { 3/5} THREEFIFTHS { 1/3} ONETHIRD
	{ 1/7} ONESEVENTH
    } $cname]
    regsub -all {[^[:alnum:]]} $cname _ cname
    set cname STOP_[string toupper $cname]
    dict set stopCNames $cname {}
    return $cname
}

proc makeMenu {name type flags back line1 line2 line3 args} {
    variable programText
    variable menuNames
    dict set menuNames $name {}
    lappend programText ""
    lappend programText "Menu $name PROGMEM = \{"
    lappend programText \{
    lappend programText [makeString $line1],
    lappend programText [makeString $line2],
    lappend programText [makeString $line3]
    lappend programText \},
    lappend programText "$type, $flags,"
    set actions [dict create]
    for {set i 0} {$i < 10} {incr i} {
	dict set actions $i NULL
    }
    dict set actions back $back
    set actions [dict merge $actions $args]
    lappend programText \{
    set sep {}
    dict for {k v} $actions {
	if {$k eq {back}} {
	    set sep { }
	} else {
	    set sep ,
	}
	if {[regexp {[[:lower:]]} $v]} {
	    lappend programText "&$v$sep /* $k */"
	} else {
	    lappend programText "(void*)$v$sep /* $k */"
	}
    }
    lappend programText \}
    lappend programText \}\;
}
proc selectionMenu {name back line1 line2 line3 args} {
    makeMenu $name menuSelHandler 0 $back $line1 $line2 $line3 {*}$args
}
proc brightnessMenu {name back line1 line2 line3 args} {
    makeMenu $name brightMenuHandler 0 $back $line1 $line2 $line3 {*}$args
}

proc alphabeticalMenu {name back args} {
    selectionMenu $name $back \
	{       2:ABC  3:DEF} \
	{4:GHI  5:JKL  6:MNO} \
	{7:PQRS 8:TUV  9:WXYZ} \
	{*}$args
}

proc stopMenu {name back args} {
    for {set k 0} {$k < [llength $args]} {incr k 3} {
	set flags 0
	set keys {}
	set menuName $name
	if {$k > 0} {
	    if {$k > 3} {
		dict set keys 6 $name[expr {($k-3)/3}]
	    } else {
		dict set keys 6 $name
	    }
	    append menuName [expr {$k/3}]
	    append flags |MENU_UP
	}
	if {$k+3 < [llength $args]} {
	    dict set keys 9 $name[expr {($k+3)/3}]
	    append flags |MENU_DOWN
	}
	for {set i 0} {$i < 3} {set i $j} {
	    set j [expr {$i + 1}]
	    if {[llength $args] > $i+$k} {
		set stopName [lindex $args [expr {$i+$k}]]
		set line$j "$j:$stopName"
		dict set keys $j [makeStop $stopName]
	    } else {
		set line$j ""
	    }
	}
	makeMenu $menuName stopMenuHandler $flags $back \
	    $line1 $line2 $line3 {*}$keys
    }
}

proc indent {lines} {
    set ind 0
    foreach l $lines {
	foreach match [regexp -all -inline "\\\}" $l] {
	    incr ind -4
	}
	puts [format %*s%s $ind {} $l]
	foreach match [regexp -all -inline "\\\{" $l] {
	    incr ind 4
	}
    }
}

selectionMenu programMenu mainMenu {*}{
    {1:Flue  3:Mixture}
    {2:Reed  4:Percussion}
    {5:Hamm  6:Brightness}
    1 flueMenu 2 reedMenu 3 mixtMenu 4 percMenu 6 brightMenu
}

brightnessMenu brightMenu programMenu {*}{
    { Enter brightness:}
    {              (0-9):}
    {}

}

selectionMenu flueMenu programMenu \
    "1:8'   2:4'   3:2 2/3'" \
    "4:2'   5:1 3/5' 6:1 1/3'" \
    "7:1 1/7' 8:1'   9: <1'" \
    1 flue8Menu

selectionMenu flue8Menu flueMenu {*}{
    {1:String}
    {2:Principal}
    {3:Flute}
    1 string8Menu
}

stopMenu string8Menu flue8Menu {*}{
    {Aeoline 8' A}
    {Aeoline 8' B}
    {Echo String 8'}
    {Gamba 8'}
    {Geigen Diapason 8'}
    {Gemshorn Harmonic}
    {Viole d'Orch. 8' *}
    {Viole d'Orchestre}
    {Violin 8'}
}

alphabeticalMenu reedMenu programMenu {*}{
}

selectionMenu mixtMenu programMenu {*}{
    {1:Carillon  2:Cornet}
    {3:Mixture   4:Others}
    {}
    1 carillonMenu 2 cornetMenu 3 mixtureMenu 4 mixtOtherMenu
}

stopMenu carillonMenu mixtMenu {*}{
    {Carillon No. 1}
    {Carillon No. 2}
}

stopMenu cornetMenu mixtMenu {*}{
    {Cornet III}
    {Cornet IV}
    {Cornet V}
    {Dolce Cornet A}
    {Dolce Cornet B}
}

stopMenu mixtureMenu mixtMenu {*}{
    {Mixture III A}
    {Mixture III B}
    {Mixture III C}
    {Mixture II D}
    {Mixture III E}
    {Mixture III F}
    {Mixture III G}
    {Mixture III H}
    {Mixture III J}
}

stopMenu mixtOtherMenu mixtMenu {*}{
    {Cymbal V}
    {Jeu de Clochette}
    {Sesquialtera}
}

stopMenu percMenu programMenu {*}{
    {Bell 8' 2 2/3' 1'}
    Chimes
    {Crystal Bell}
    {Dolce Bell}
    Multi-Bell
    Octa-Bell
    {Tierce Bell}
    {Tubular Chimes}
}

puts "/*"
puts " * Menu Definitions"
puts " *"
puts " * This file is generated by [info script]."
puts " * You probably shouldn't be editing it directly."
puts " */"

puts "/* String literals */"
puts ""
dict for {val slot} $stringIndex {
    set val [string map {
	{ 2/3} \\2\\4 { 3/5} \\3\\5 { 1/3} \\1\\4 { 1/7} \\1\\7
    } $val]
    # TODO - Look up 
    puts "static const char S$slot\[\] PROGMEM = \"$val\";"
}

puts ""
puts "/* Declarations of menus (for forward references) */"
puts ""
foreach menu [lsort -dictionary [dict keys $menuNames]] {
    puts "extern Menu $menu;"
}

puts ""
puts "/* Menus */"
indent $programText

set f [open stoplist.h w]
puts $f "enum Stop \{"
set i 0
foreach name [lsort -dictionary [dict keys $stopCNames]] {
    puts $f "    $name,"
}
puts $f "\};"
close $f
exit

Changes to terminal/terminal.pde.



1
2
3
4
5
6
7
...
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
...
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
...
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


#include <avr/pgmspace.h>
#include <avr/eeprom.h>

extern "C" void __cxa_pure_virtual() {
  while (1) {}
}
 
................................................................................

/* Pin assignments for the data nybble, LSB first */

static const prog_uchar dispDataPinsPGM[4] PROGMEM = { 10, 9, 8, 7 };

/* LED duty cycles corresponding to the 12 display brightness settings */

static const prog_uchar dispBrightness[12] PROGMEM = {
  0,    1,   6,   15,  28,  45,
  67,  94,  127, 164, 207, 255
};

/* CGRAM content. */

static const prog_uchar dispCGRAM[] PROGMEM = {
  0x01, 0x06, 0x0c, 0x06, 0x01, 0x0c, 0x06, 0x01,
				/* 0x08 - less than or equal */
  0x08, 0x08, 0x08, 0x08, 0x09, 0x02, 0x04, 0x00,
				/* 0x01 - vulgar fraction numerator 1 */
  0x18, 0x04, 0x18, 0x10, 0x1D, 0x02, 0x04, 0x00,
				/* 0x02 - vulgar fraction numerator 2 */
  0x18, 0x04, 0x18, 0x04, 0x19, 0x02, 0x04, 0x00,
				/* 0x03 - vulgar fraction numerator 3 */
  0x04, 0x08, 0x16, 0x01, 0x06, 0x01, 0x06, 0x00,
				/* 0x04 - vulgar fraction denominator 3 */
  0x04, 0x08, 0x17, 0x04, 0x06, 0x01, 0x06, 0x00,
				/* 0x05 - vulgar fraction denominator 5 */
  0x00, 0x00, 0x00, 0x11, 0x0a, 0x04, 0x00, 0x00,
				/* 0x06 - inverted caret */
  0x04, 0x08, 0x17, 0x01, 0x02, 0x02, 0x04, 0x00
				/* 0x07 - vulgar fraction denominator 7 */
};

/* DDRAM addresses of the starts of the lines on the display */

static const prog_uchar dispLineAddr[4] PROGMEM = {
  0x00, 0x40, 0x14, 0x54
................................................................................
   * it to single-line mode and a 5x8 font.  If the display is in 8-bit
   * bus mode, the same settings will simply be executed twice.
   */

  digitalWrite(DISP_REG_SELECT_PIN, LOW); /* Command mode */

  /*
   * Next command (set 8-bit mode and delay 5 milliseconds)
   * is a bit of 'cargo cult': nobody is quite sure why it's needed,
   * but the display doesn't quite initialize without it.


   */
   
  dispWriteNybble(0x3); delay(5);
  
















  dispWriteNybble(0x3);		  /* 8-bit bus, 1 line, 5x8 */










  dispWriteNybble(0x3);		  /* Either an ignored nybble,
				   * or a repeat of the above. */

  /*
   * Now that the display is in 8-bit bus mode, it can be commanded into
   * 4-bit bus mode predictably.  The '1 line' and '5x8 font' attributes
   * are wired in the hardware in the least significant bits, so this command
   * also sets them up.
   */

  dispWriteNybble(0x02);	/* 4-bit bus, 1 line, 5x8 */

  /*


   * Finish the initialization, a byte at a time.
   */

  dispWriteByte(0x06);		/* Autoincrement cursor, no display shift */
  dispWriteByte(0x0C);		/* Display enable, invisible cursor, no blink */
  dispWriteByte(0x01);		/* Clear the display and home the cursor */
  delayMicroseconds(DISP_CLEAR_DELAY_MICROS);
................................................................................
void
dispClear() {
  digitalWrite(DISP_REG_SELECT_PIN, LOW);
  dispWriteByte(0x01);		/* Clear display */
  digitalWrite(DISP_REG_SELECT_PIN, HIGH);
  delayMicroseconds(DISP_CLEAR_DELAY_MICROS);
};
 










































































void 














































































































































































setup() {

  kpInit();			/* Initialize the keypad */

  dispInit();			/* Initialize the display */



#if 0
  Serial.begin(9600);		/* Initialize serial comms with the 
				 * programmer host */
  Serial.println("starting");
#endif
  dispMoveTo(0, 0); dispWriteStringPGM(PSTR("1\x01\x07\x08" "1\x03\x05\x08" "2\x02\x04 up^ dn\x06"));
  dispMoveTo(1, 7); dispWriteStringPGM(PSTR("Hello,"));
  dispMoveTo(2, 7); dispWriteStringPGM(PSTR("World!"));
  dispMoveTo(3, 0);
  

}

void
loop() {
  


  byte state;
  

  state = kpPoll();
  if (state != KP_NOKEY) {
#if 0
    Serial.print("Button: <");
    Serial.print((int)state, HEX);
    Serial.print(">");
    Serial.print((int) pgm_read_byte(brightness+state));
    Serial.println("!");
#endif
    dispMoveTo(3,0);
    dispWriteStringPGM(PSTR("Brightness = "));
    dispWriteByte(state/10 ? '1' : ' ');
    dispWriteByte(state%10 + '0');
    dispSetBrightness(state);
  }


}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 2
 * End:
 */
>
>







 







|
|
|







|

|









|







 







|
|
|
>
>


|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|
<



|




|


>
>







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






>
>




<




|
>





>
>

|
>


<
<




<






>
>








1
2
3
4
5
6
7
8
9
...
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
...
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
...
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
#include "stoplist.h"

#include <avr/pgmspace.h>
#include <avr/eeprom.h>

extern "C" void __cxa_pure_virtual() {
  while (1) {}
}
 
................................................................................

/* Pin assignments for the data nybble, LSB first */

static const prog_uchar dispDataPinsPGM[4] PROGMEM = { 10, 9, 8, 7 };

/* LED duty cycles corresponding to the 12 display brightness settings */

static const prog_uchar dispBrightness[10] PROGMEM = {
  0,   15,  28,  45,  67,
  94,  127, 164, 207, 255
};

/* CGRAM content. */

static const prog_uchar dispCGRAM[] PROGMEM = {
  0x01, 0x06, 0x0c, 0x06, 0x01, 0x0c, 0x06, 0x01,
				/* 0x08 - less than or equal */
  0x08, 0x18, 0x08, 0x08, 0x1D, 0x02, 0x04, 0x00,
				/* 0x01 - vulgar fraction numerator 1 */
  0x18, 0x04, 0x08, 0x10, 0x1D, 0x02, 0x04, 0x00,
				/* 0x02 - vulgar fraction numerator 2 */
  0x18, 0x04, 0x18, 0x04, 0x19, 0x02, 0x04, 0x00,
				/* 0x03 - vulgar fraction numerator 3 */
  0x04, 0x08, 0x16, 0x01, 0x06, 0x01, 0x06, 0x00,
				/* 0x04 - vulgar fraction denominator 3 */
  0x04, 0x08, 0x17, 0x04, 0x06, 0x01, 0x06, 0x00,
				/* 0x05 - vulgar fraction denominator 5 */
  0x00, 0x00, 0x00, 0x11, 0x0a, 0x04, 0x00, 0x00,
				/* 0x06 - inverted caret */
  0x04, 0x08, 0x17, 0x01, 0x02, 0x04, 0x04, 0x00
				/* 0x07 - vulgar fraction denominator 7 */
};

/* DDRAM addresses of the starts of the lines on the display */

static const prog_uchar dispLineAddr[4] PROGMEM = {
  0x00, 0x40, 0x14, 0x54
................................................................................
   * it to single-line mode and a 5x8 font.  If the display is in 8-bit
   * bus mode, the same settings will simply be executed twice.
   */

  digitalWrite(DISP_REG_SELECT_PIN, LOW); /* Command mode */

  /*
   * If the display started out with a half-finished data transfer
   * in 4-bit mode, write a nybble to finish it. If it started out
   * idle in 4 bit mode, write 0x3 to the high-order command nybble.
   * If it started out in 8-bit mode, write 0x38 (the 8 is hard-wired
   * into the less significant nybble) which keeps it in 8-bit mode.
   */
   
  dispWriteNybble(0x3); 

  /*
   * If it started out in 4-bit mode with a half-finished command, that
   * command may need time to finish. Wait for it.
   */
  delay(5);
  
  /*
   * If the display started out in 4-bit mode with a half-finished command,
   * it's now finished. Writing 0x3 starts another command (which will command
   * 8-bit mode).  If the display started in 4-bit mode and idle, it's
   * got a half-finished command. Sending an 0x3 nybble executes 0x33, which
   * will set 8-bit mode, 1 line, 5x8 characters. If the display started
   * out in 8-bit mode, it's still in 8-bit mode and the next write will be
   * ignored.
   */

  dispWriteNybble(0x3);

  /* 
   * If the display started out in 4-bit mode with a half-finished command,
   * the next write commands it into 8-bit mode (0x33 - 8-bits, 1-line,
   * 5x8 chars). If it started out in 4-bit mode and idle, it's now in 8-bit
   * mode (also 1-line, 5x8 chars), and the next write will be ignored.
   * If it started out in 8-bit mode, this write is ignored like the
   * three previous ones.
   */

  dispWriteNybble(0x3);


  /*
   * Now that the display is in 8-bit bus mode, it can be commanded into
   * 4-bit bus mode predictably.  The '2 line' and '5x8 font' attributes
   * are wired in the hardware in the least significant bits, so this command
   * also sets them up.
   */

  dispWriteNybble(0x02);	/* 4-bit bus, 2 lines, 5x8 */

  /*
   * Finally, every possible state has come to the same mode; 4 bit bus,
   * 2 display lines, 5x8 characters.
   * Finish the initialization, a byte at a time.
   */

  dispWriteByte(0x06);		/* Autoincrement cursor, no display shift */
  dispWriteByte(0x0C);		/* Display enable, invisible cursor, no blink */
  dispWriteByte(0x01);		/* Clear the display and home the cursor */
  delayMicroseconds(DISP_CLEAR_DELAY_MICROS);
................................................................................
void
dispClear() {
  digitalWrite(DISP_REG_SELECT_PIN, LOW);
  dispWriteByte(0x01);		/* Clear display */
  digitalWrite(DISP_REG_SELECT_PIN, HIGH);
  delayMicroseconds(DISP_CLEAR_DELAY_MICROS);
};
 
/******************************************************************************
 *                                                                            *
 *				MENU SYSTEM				      *
 *                                                                            *
 ******************************************************************************/

/* Data structure corresponding to one screen menu */

typedef struct Menu {
  const char* screenContent[3];	/* First three lines of data to paint when
				 * this menu appears. */
  Menu* (*keyfn)(byte key);	/* Key handler */
  byte flags;			/* Flags */
  void* clientData[11];		/* One pointer-sized word of data per key
				 * on the keypad */
} Menu;

enum menuFlags {
  MENU_UP	= 0x01,
  MENU_DOWN	= 0x02,
  MENU_MAIN	= 0x04,
};

#define MENU_TIMEOUT_MILLIS 30000 
				/* Menus time out after 30 s of inactivity */

/* RAM for the menu system */

Menu* menuCurrent;		/* Current menu on display */
int menuLastKeyMillis;		/* Time the last keypress happened */

/* Forward function declarations */

Menu* menuSelHandler(byte);	/* Handler for menus that are just
				 * selections that dispatch to other
				 * menus */

Menu* mainMenuHandler(byte);	/* Handler for the main menu */

Menu* brightMenuHandler(byte);	/* Handler for the 'brightness' menu  */\

Menu* stopMenuHandler(byte);	/* Handler for menus that present lists
				 * of 3-5 stops with navigation on 69*# */

/* Bring in the menu data proper */

extern Menu mainMenu;
#include "menus.c"

const char mainMenuLine1[] PROGMEM = "| Organum Antiquum |";
const char mainMenuLine2[] PROGMEM = "+------------------+";
const char mainMenuLine3[] PROGMEM = "";

Menu mainMenu PROGMEM = {
  { mainMenuLine1, mainMenuLine2, mainMenuLine3 },
  mainMenuHandler, MENU_MAIN,
  {
    NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL,
    &programMenu
  }
};

 
/*
 *-----------------------------------------------------------------------------
 *
 * menuShow --
 * 
 *	Paints a menu on the screen and sets it as the current menu.
 *
 *-----------------------------------------------------------------------------
 */

void
menuShow(
    Menu* menu)			/* Menu to display */
{
  byte i;

  /* Wipe the display */

  dispClear();

  /* Unscramble the lines of the screen */

  for (i = 0; i < 3; ++i) {
    dispMoveTo(i,0);
    /* menu is a pointer to the menu data structure in progmem */
    /* &(menu->screenContent) should be a pointer to the first char*
     * of the menu text */
    dispWriteStringPGM((const char*)pgm_read_word(&(menu->screenContent[i])));
  }
  i = pgm_read_byte(&(menu->flags));
  dispMoveTo(3,0); 
  if (i & MENU_MAIN) {
    dispWriteStringPGM(PSTR("*:Program     #:Card"));
  } else {
    dispWriteStringPGM(PSTR("*:Back"));
    if (i & MENU_UP) {
      dispMoveTo(3,7); dispWriteStringPGM(PSTR("6:^"));
    }
    if (i & MENU_DOWN) {
      dispMoveTo(3,11); dispWriteStringPGM(PSTR("9:\6"));
    }
    if (i & (MENU_UP | MENU_DOWN)) {
      dispMoveTo(3,15); dispWriteStringPGM(PSTR("#:Can"));
    } else {
      dispMoveTo(3,12); dispWriteStringPGM(PSTR("#:Cancel"));
    }
  }

  /* Set the current menu */

  menuCurrent = menu;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * menuPoll --
 *
 *	Function called from the scan loop to manage the menu system
 *
 * This function polls the keypad, and if a key has been pressed, calls the
 * current menu's key handler to decide what to do with it.  If there is a
 * transition to another menu, it paints the next menu. It then records
 * the time at which the current action completed, for timeout processing.
 *
 *-----------------------------------------------------------------------------
 */

void
menuPoll() {
  int state;
  Menu* nextMenu;

  /* TODO - Handle timeout processing by resetting to mainMenu */

  /* Read the keypad */
  state = kpPoll();
  if (state != KP_NOKEY) {

    if (state == KP_OCTOTHORP) {
      menuShow(&mainMenu);
    } else {
    
      /* Call the menu's key handler */
      Menu* (*keyfn)(byte) = (Menu* (*)(byte))
	pgm_read_word(&(menuCurrent->keyfn));
      nextMenu = keyfn(state);

      /* Transition to the next menu */
      if (nextMenu != menuCurrent && nextMenu != NULL) {
	menuShow(nextMenu);
      }
    }

    /* Wind the clock */
    menuLastKeyMillis = millis();
  }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * menuSelHandler --
 *
 *	Generic selection handler for menus that just relay to other menus
 *
 * The procedure just reads the dispatch table from the current menu,
 * and returns the next menu.
 *
 *-----------------------------------------------------------------------------
 */

Menu*
menuSelHandler(byte key)
{
  return (Menu*) pgm_read_word(menuCurrent->clientData + key);
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * stopMenuHandler --
 *
 *	Handle keypresses on a menu to select stops
 *
 *-----------------------------------------------------------------------------
 */

Menu*
stopMenuHandler(byte key) {
  if (key >= 1 && key <= 5) {
    /* TODO: Select a stop and ask what to do with it. */
    return &mainMenu;
  } else {
    return menuSelHandler(key);
  }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * mainMenuHandler --
 *
 *	Handle keypresses at the main menu
 *
 * This procedure manages coming out of the 'standby' state if we were
 * in standby. It does quick stop programming for numeric keys, or starts
 * programming if * is pressed. (# requests a manual card read, which can
 * be done at any time that we're neither in standby nor sending a card image.)
 *
 *-----------------------------------------------------------------------------
 */
Menu*
mainMenuHandler(byte key)
{
  /* TODO - come out of standby */
  /* TODO - quick programming of stops */
  if (key == KP_ASTERISK) {
    return &programMenu;
  }
  return &mainMenu;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * brightMenuHandler --
 *
 *	Handle keypresses at the 'brightness' menu
 *
 * Digits set display brightness. * and # just return to the main menu.
 *
 *-----------------------------------------------------------------------------
 */

Menu*
brightMenuHandler(byte key)
{
  if (key <= 9) {
    dispSetBrightness(key);
  }
  return &mainMenu;
}
 
void 
setup() {

  kpInit();			/* Initialize the keypad */

  dispInit();			/* Initialize the display */

  menuLastKeyMillis = millis();

#if 0
  Serial.begin(9600);		/* Initialize serial comms with the 
				 * programmer host */
  Serial.println("starting");

  dispMoveTo(0, 0); dispWriteStringPGM(PSTR("1\x01\x07\x08" "1\x03\x05\x08" "2\x02\x04 up^ dn\x06"));
  dispMoveTo(1, 7); dispWriteStringPGM(PSTR("Hello,"));
  dispMoveTo(2, 7); dispWriteStringPGM(PSTR("World!"));
  dispMoveTo(3, 0);
#endif
  menuShow(&mainMenu);
}

void
loop() {
  
  
#if 0
  byte state;

  Serial.print("Button: <");
  state = kpPoll();
  if (state != KP_NOKEY) {


    Serial.print((int)state, HEX);
    Serial.print(">");
    Serial.print((int) pgm_read_byte(brightness+state));
    Serial.println("!");

    dispMoveTo(3,0);
    dispWriteStringPGM(PSTR("Brightness = "));
    dispWriteByte(state/10 ? '1' : ' ');
    dispWriteByte(state%10 + '0');
    dispSetBrightness(state);
  }
#endif
  menuPoll();
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 2
 * End:
 */