OrganProject

Check-in [4a81302b24]
Login

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

Overview
Comment:Cleaned up the menu construction code a little bit.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:4a81302b24225545e2eb385b2521d2642f8951e7
User & Date: 200002852 2009-08-19 22:01:53
Context
2009-08-19
23:44
Completed stop list in the menus check-in: d386cb24e6 user: 200002852 tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to 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
..
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
...
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
...
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 = \{"
................................................................................
	} 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} {
................................................................................
		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
................................................................................
    {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
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>
>
>
>
>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



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

>
>
>
>
>
>
>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







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





|





>
>












<







 







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

>
>
|
|





|


>
>
|
|
|

|


>
>
|
|
|
>
>
>
>









>

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
...
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
...
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
...
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
# 
# makeMenus.tcl --
#
#	Tcl script to make the menus for the Allen organ card reader
#	enhancement.
#
# Usage:
#	makeMenus.tcl
#
# Output:
#	Writes two files:
#	    stoplist.h - Contains the declaration of an enumeration that
#		         describes all the stops that appear in the menus
#	    menus.c - Contains the C definitions of all the menus.
#
#------------------------------------------------------------------------------
 
#------------------------------------------------------------------------------
#
# init --
#
#	Initializes global variables
#
# The following variables are used throughout:
#	menuNames - Dictionary whose keys give the names of all menus defined
#		    so far, and whose values are immaterial.
#	programText - List containing the lines of the program that have
#		      been constructed so far
#	stopCNames - Dictionary whose keys are the names of the stops as they
#		     will appear in C source code and whose values are 
#		     immaterial.
#	stringPool - List of literal strings appearing in the menus
#	stringIndex - Dictionary whose keys are the literal strings in the
#		      menus and whose values are their positions in
#		      $stringPool
#
#------------------------------------------------------------------------------

proc init {} {
    variable menuNames {}
    variable programText {}
    variable stopCNames {}
    variable stringPool {}
    variable stringIndex {}
    rename init {}
}
init
 
#------------------------------------------------------------------------------
#
# makeString --
#
#	Adds a string to the string pool and returns a C reference to it.
#
# Parameters:
#	text - Text to add
#
# Results:
#	Returns the name of a C variable containing the given text.
#
#------------------------------------------------------------------------------

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 */"
}
 
#------------------------------------------------------------------------------
#
# makeStop --
#
#	Converts the name of a stop to a C enumerator, adds the 
#	enumerator to stopCNames, and returns the enumerator.
#
# Parameters:
#	stopName - Name of the stop
#
# Results:
#	Returns the enumerator.
#
#------------------------------------------------------------------------------

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
}
 
#------------------------------------------------------------------------------
#
# makeMenu --
#
#	Constructs a menu
#
# Parameters:
#	name - Name of the menu
#	type - Type of the menu (i.e., name of the procedure that handles
#	       its keypresses)
#	flags - Flags (MENU_UP, MENU_DOWN, MENU_MAIN) to put on the menu
#	back - Parent menu; where the 'Back' button should go.
#	line1-line3 - Lines of text to display on the panel
#	args - Alternating values. The even-numbered arguments are the
#	       keys on the display, and the odd-numbered arguments are
#	       the actions (depending on menu type) corresponding to the
#	       keys.
#
#------------------------------------------------------------------------------

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 = \{"
................................................................................
	} else {
	    lappend programText "(void*)$v$sep /* $k */"
	}
    }
    lappend programText \}
    lappend programText \}\;
}
 
#------------------------------------------------------------------------------
#
# selectionMenu --
#
#	Makes a menu that simply routes to submenus
#
# Parameters:
#	name - Name of the menu
#	back - Parent menu; where the 'Back' button should go.
#	line1-line3 - Lines of text to display on the panel
#	args - Alternating keys (0-9) and names of menus where those keys go.
#
#------------------------------------------------------------------------------

proc selectionMenu {name back line1 line2 line3 args} {
    makeMenu $name menuSelHandler 0 $back $line1 $line2 $line3 {*}$args
}
 
#------------------------------------------------------------------------------
#
# brightnessMenu --
#
#	Makes a menu to control display brightness
#
# Parameters:
#	name - Name of the menu
#	back - Parent menu; where the 'Back' button should go.
#	line1-line3 - Lines of text to display on the panel
#
#------------------------------------------------------------------------------

proc brightnessMenu {name back line1 line2 line3} {
    makeMenu $name brightMenuHandler 0 $back $line1 $line2 $line3
}
 
#------------------------------------------------------------------------------
#
# alphabeticalMenu --
#
#	Make a menu with the alphabet as it is laid out on a telephone keyboard
#
# Parameters:
#	name - Name of the menu
#	back - Parent menu; where the 'Back' button should go.
#	args - Alternating keys (0-9) and names of menus where those keys go.
#
#------------------------------------------------------------------------------

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
}
 
#------------------------------------------------------------------------------
#
# stopMenu --
#
#	Make a menu for selecting individual stops
#
# Parameters:
#	name - Name of the menu
#	back - Parent menu; where the 'Back' button should go.
#	args - Names of the available stops from this menu
#
# The menu will be a scrolling menu with the 6 key meaning 'page up'
# and the 9 key meaning 'page down'.
#
#------------------------------------------------------------------------------

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} {
................................................................................
		set line$j ""
	    }
	}
	makeMenu $menuName stopMenuHandler $flags $back \
	    $line1 $line2 $line3 {*}$keys
    }
}
 
#------------------------------------------------------------------------------
#
# indent --
#
#	Emit the C code.
#
# Parameters:
#	CFile - Channel where the C code should be written
#	lines - Lines of text to write
#
# Writes the C code to the given file, handling rudimentary {} indentation.
#
#------------------------------------------------------------------------------

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

# THE MENUS

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
................................................................................
    {Crystal Bell}
    {Dolce Bell}
    Multi-Bell
    Octa-Bell
    {Tierce Bell}
    {Tubular Chimes}
}
 

# Write the file header

set CFile [open menus.c w]

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

# Write the literal pool

puts $CFile "/* String literals */"
puts $CFile ""
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 $CFile "static const char S$slot\[\] PROGMEM = \"$val\";"
}

# Write the menu declarations

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

# Write the menus themselves

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

close $CFile

# Write the stop list

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