Also looking into why '5' and '6' don't work (still not sure why!) I would also suggest a refactor to the displayDigit handler. This currently actually involves 3 handlers: DisplayDigit, TurnOn and TurnOff, and you are keeping a list of both segments to show and segments to hide.
Your current code is 69 lines long with 3 handlers:
Code: Select all
command DisplayDigit pDigit
local tSegmentsOn, tSegmentsOff, tempOn, tempOff
switch pDigit
case "0"
put "A,B,C,D,E,F" into tSegmentsOn
put "G" into tSegmentsOff
break
case "1"
put "B,C" into tSegmentsOn
put "A,D,E,F,G" into tSegmentsOff
break
case "2"
put "A,B,D,E,G" into tSegmentsOn
put "C,F" into tSegmentsOff
break
case "3"
put "A,B,C,D,G" into tSegmentsOn
put "E,F" into tSegmentsOff
break
case "4"
put "B,C,F,G" into tSegmentsOn
put "A,D,E" into tSegmentsOff
break
case "5"
put "A,C,D,F,G" into tSegmentsOn
put "B,E" into tSegmentsOff
case "6"
put "A,C,D,E,F,G" into tSegmentsOn
put "B" into tSegmentsOff
case "7"
put "A,B,C" into tSegmentsOn
put "D,E,F,G" into tSegmentsOff
break
case "8"
put "A,B,C,D,E,F,G" into tSegmentsOn
-- No segments to turn off
break
case "9"
put "A,B,C,F,G" into tSegmentsOn
put "D,E" into tSegmentsOff
break
default
put "Invalid digit: " & pDigit into field "ErrorMessage"
exit DisplayDigit
end switch
-- Correctly formatted call statements
if tSegmentsOn is not empty then
call ("TurnOn" && quote & tSegmentsOn & quote)
end if
if tSegmentsOff is not empty then
call ("TurnOff" && quote & tSegmentsOff & quote)
end if
end DisplayDigit
command TurnOn pSegments
-- Turn on specified segments
repeat for each item tSegment in pSegments
set the visible of graphic ("segment" & tSegment) to true
end repeat
end TurnOn
command TurnOff pSegments
-- Turn off specified segments
repeat for each item tSegment in pSegments
set the visible of graphic ("segment" & tSegment) to false
end repeat
end TurnOff
This can be very efficiently refactored by setting the visibility of each graphic to a
condition that resolves to True or False, rather than acting on lists of graphics to show and graphics to hide (so you don't need the turnOn and turnOff handlers), and declaring the visible graphics for each digit as constants.
The refactored code is 10 lines of constant declarations and
a single handler with only 7 lines of code as the displayDigit hander!
Code: Select all
constant k0 = "A,B,C,D,E,F"
constant k1 = "B,C"
constant k2 = "A,B,D,E,G"
constant k3 = "A,B,C,D,G"
constant k4 = "B,C,F,G"
constant k5 = "A,C,D,F,G"
constant k6 = "A,C,D,E,F,G"
constant k7 = "A,B,C"
constant k8 = "A,B,C,D,E,F,G"
constant k9 = "A,B,C,F,G"
Code: Select all
command displayDigit pDigit
local tSegmentsOn
put value("k" & pDigit) into tSegmentsOn
repeat for each item tSegment in "A,B,C,D,E,F,G"
set the visible of graphic ("segment" & tSegment) to tSegmentsOn contains tSegment
end repeat
end displayDigit
This would be my preferred approach as it makes it much easier to read/understand/debug...
Stam