What you learn today
- Namespaces as numbers
- Translate values back
- Build tables
The battery reports Mode 2. What does that mean? The display should not show a number but a colour: red, orange, yellow, green. For that you need to know which numbers hide behind the names – and how to translate the other way round.
Namespaces are numbers
Every name with a prefix is a constant. The preprocessor does not replace it, but the chip reads it as a number:
| Name | Number |
|---|---|
LogicType.Temperature | 6 |
Color.Red | 4 |
Color.Green | 2 |
GasType.Oxygen | 1 |
GasType.Nitrogen | 2 |
GasType.Air | 3 |
SortingClass.Ores | 9 |
Notice something about GasType? The gases are bit values: Oxygen 1, Nitrogen 2, CarbonDioxide 4, Methane 8. GasType.Air = 3 is oxygen and nitrogen – two bits set. That is bit week, just hidden in the game.
Bare names are only valid at their typed position: l r0 d0 Temperature is right, move r0 Temperature is an error. At number positions you need the prefix: move r0 LogicType.Temperature.
Translating values back
Devices often report a state as a number. The meaning is in the list of modes, for the battery for example:
Mode | Meaning |
|---|---|
| 0 | Empty |
| 1 | Critical |
| 2 | VeryLow |
| 3 | Low |
| 4 | Medium |
| 5 | High |
| 6 | Full |
For a few values a select chain is enough. For many a table is better: the value read is the index, the translation sits at that address in the stack.
Building tables
Before start: you set up the table once with poke. In the loop get … db index reads the matching entry:
poke 0 Color.Red # Empty
poke 1 Color.Red # Critical
poke 2 Color.Orange # VeryLow
start:
yield
lbn mode BATT HASH("Haupt") Mode Average
get colour db mode # mode is the index
A NaN as an index is dangerous: converted into an integer it gives no valid address, and get halts the chip. Check the value with snan first and set a safe index before you use get.
The table costs seven lines. A select chain for seven values would be twice as long – and you would have to hunt for every colour change in the chain instead of in a list.
Diagram
Exercise
Task
The table mode → colour is already in the stack. Read the Mode of the battery “Haupt” and output on the display “Akku”:
Setting= mode, −1 without a batteryColor= colour from the table,Color.Graywithout a battery
With NaN, get must not run with NaN as the address.
Hints
Hint 1
snanz ok mode, then select mode ok mode 0 – that way the index is always valid.
Hint 2
get colour db mode reads the colour.
Hint 3
Finally replace both values for the failure case with select.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in