NCL 306: Changing a Setting
In NCL 305, we changed part of an existing screen without redrawing everything around it.
Now we'll let the user make that change.
We'll build a small room temperature control:
ROOM CONTROL
TEMPERATURE: 21 °C
(69 °F)
↑ / ↓ ADJUST
ESC TO EXIT
The Up and Down arrow keys will change the temperature.
When the setting changes, we'll update only the temperature fields.
The temperature setting
The program needs to remember the current temperature.
We'll store it in:
#temperature $000
MOVE #temperature 21
This is the value the user will actually control.
We'll also need somewhere to calculate the Fahrenheit temperature:
#fahrenheit $001
Unlike #temperature, we don't need to give #fahrenheit an initial value.
We'll calculate it whenever we update the Display.
Drawing the screen
Most of our interface never changes.
We can draw all of that once:
D.FIL " "
D.CUR 8 1
D.TXT "ROOM CONTROL"
D.CUR 5 4
D.TXT "TEMPERATURE:"
D.CUR 8 8
D.TXT "↑ / ↓ ADJUST"
D.CUR 9 9
D.TXT "ESC TO EXIT"
D.BLT
The temperature itself will be drawn separately.
That gives us two kinds of Display contents:
- text that stays the same;
- fields that change while the program is running.
We don't need to redraw the first kind every time the second kind changes.
Calculating Fahrenheit
Our program stores the temperature in Celsius, but we'd also like to show the Fahrenheit equivalent.
Converting Celsius to Fahrenheit
Fahrenheit can be calculated from Celsius with:
°F = (°C × 9 ÷ 5) + 32
Our program will perform the same calculation one operation at a time.
Because we're using integer arithmetic, any fractional part is discarded.
We can calculate it in #fahrenheit:
MUL #fahrenheit #temperature 9
DIV #fahrenheit #fahrenheit 5
ADD #fahrenheit #fahrenheit 32
#temperature is the setting.
#fahrenheit is calculated from it.
If #temperature contains 21, the calculation leaves 69 in #fahrenheit.
Store the setting, calculate what follows from it
There is only one temperature setting.
#temperature stores that setting. #fahrenheit is derived from it whenever the Display is updated.
We don't need two independent values that could disagree about the current temperature.
Updating the temperature fields
In NCL 305, we built a restore string that clears a field and returns the cursor to its beginning.
We'll use the same technique here.
Our temperature fields will be eight characters wide:
#restore $002
S.MOVE #restore ""
S.PADR #restore #restore " " 8
S.PADR #restore #restore "\b" 16
#restore contains eight spaces followed by eight backspaces.
Now we can put the calculation and both field updates together:
:draw
MUL #fahrenheit #temperature 9
DIV #fahrenheit #fahrenheit 5
ADD #fahrenheit #fahrenheit 32
D.CUR 18 4
D.CHR #restore
D.CHR #temperature
D.CHR " °C"
D.CUR 18 5
D.CHR #restore
D.CHR "("
D.CHR #fahrenheit
D.CHR " °F)"
JUMP :input
Whenever the temperature changes, we can jump to :draw.
It calculates the Fahrenheit value from the current setting, updates both fields, then returns to :input.
The rest of the screen stays untouched.
Waiting for input
We'll need somewhere for the Keyboard to store its result:
#key $003
Then we can wait for a key:
:input
SYS.AKEY #key
The keys we're interested in produce these values:
| Key | Value |
|---|---|
| ↑ | "UP" |
| ↓ | "DOWN" |
| Escape | "ESC" |
We can branch according to the value we received:
S.BEQ :up #key "UP"
S.BEQ :down #key "DOWN"
S.BEQ :exit #key "ESC"
JUMP :input
If none of the branches succeed, execution reaches JUMP :input.
The key is ignored and the program waits for another one.
Changing the setting
Let's allow temperatures from 15 °C through 30 °C.
Up increases the setting unless it's already at the maximum:
:up
BGE :input #temperature 30
INC #temperature
JUMP :draw
Down does the same thing in the other direction:
:down
BLE :input #temperature 15
DEC #temperature
JUMP :draw
If a limit has already been reached, execution returns directly to :input.
Nothing changed, so there's nothing to redraw.
Otherwise, the setting changes and execution goes to :draw.
Because we're changing the temperature by one degree at a time, checking the old value before the change is enough to keep #temperature between 15 and 30.
The complete program
We've now built every part of the room control:
- a stored setting;
- a value calculated from it;
- static parts of the screen;
- fields that update when the setting changes;
- Keyboard controls;
- limits on the allowed setting.
Put those pieces together and we have a complete interactive program.
Complete program
#temperature $000
#fahrenheit $001
#restore $002
#key $003
MOVE #temperature 21
-- Build the field restoration string.
S.MOVE #restore ""
S.PADR #restore #restore " " 8
S.PADR #restore #restore "\b" 16
-- Draw the parts of the screen that don't change.
D.FIL " "
D.CUR 8 1
D.TXT "ROOM CONTROL"
D.CUR 5 4
D.TXT "TEMPERATURE:"
D.CUR 8 8
D.TXT "↑ / ↓ ADJUST"
D.CUR 9 9
D.TXT "ESC TO EXIT"
D.BLT
-- Draw the initial temperature.
JUMP :draw
:input
SYS.AKEY #key
S.BEQ :up #key "UP"
S.BEQ :down #key "DOWN"
S.BEQ :exit #key "ESC"
-- Ignore any other key.
JUMP :input
:up
BGE :input #temperature 30
INC #temperature
JUMP :draw
:down
BLE :input #temperature 15
DEC #temperature
JUMP :draw
:draw
MUL #fahrenheit #temperature 9
DIV #fahrenheit #fahrenheit 5
ADD #fahrenheit #fahrenheit 32
D.CUR 18 4
D.CHR #restore
D.CHR #temperature
D.CHR " °C"
D.CUR 18 5
D.CHR #restore
D.CHR "("
D.CHR #fahrenheit
D.CHR " °F)"
JUMP :input
:exit
Run it and press Up and Down.
The Celsius setting changes by one degree at a time, and the Fahrenheit value underneath is recalculated to match.
Press Up repeatedly until the temperature reaches 30 °C. Further Up presses do nothing.
Do the same with Down at 15 °C.
Press Escape when you're finished.
The interaction loop
Look at the larger shape of the program.
Once the screen has been prepared, it settles into a repeating pattern:
- wait for input;
- decide what the input means;
- change the program's state;
- update the Display;
- wait again.
This is our first complete interaction loop.
At:
SYS.AKEY #key
execution waits until the user does something.
Once a key arrives, the program decides what it means.
If the key changes the temperature, the program changes #temperature, updates the fields, and returns to :input.
Then it waits again.
Interactive programs spend a lot of time waiting
An interactive program often has nothing useful to do until something happens.
That might be a key press, mouse movement, incoming data, a timer, or some other event.
Our program has only one source of input to worry about, so SYS.AKEY gives us a simple version of the same idea: wait for something to happen, handle it, then wait again.
Try it
Change the starting temperature:
MOVE #temperature 21
Try 15, 25, or 30.
The Fahrenheit value should be correct as soon as the program starts.
Then change the allowed range.
For example, make the controls stop at 18 °C and 25 °C.
Finally, change the amount adjusted by each key press.
Instead of:
INC #temperature
and:
DEC #temperature
try:
ADD #temperature #temperature 2
and:
SUB #temperature #temperature 2
Watch what happens near the limits.
Suppose the upper limit is 25 and the current temperature is 24.
The existing check allows the change because 24 is below 25.
But then:
ADD #temperature #temperature 2
produces 26.
Does our existing check still guarantee that the temperature stays inside the allowed range?
How could you change the program so that it does?
Our program now has a complete interaction loop:
wait → decide → change → update → wait
So far, each key has directly performed an action: Up increases the temperature, and Down decreases it.
In the next lesson, NCL 307: Making a Selection, we'll give the user a selection of choices, let them move between those choices, and decide what happens when they make one.
We'll also use everything we've learned so far to build our first game.