What you learn today
- Check whether a pin is set
- Avoid the DeviceNotFound error
- Safe branch without a device
A device on a pin is gone quickly: deconstructed, moved, hit by a meteor. Then l r0 d0 Temperature reports an error, and the chip stops on exactly that line. It retries it every tick – everything after it no longer happens. The heater stays in its last state, the warning light never turns on.
Better: the chip asks first.
Anybody there? sdse and sdns
sdse r0 d0 # 1 if a device is attached to d0
sdns r1 d0 # 1 if d0 is empty
sdse stands for set if device set, sdns for set if device not set. Both return 0 or 1 and can be used like any comparison – for example straight for a warning light:
sdns missing sensor
s warning On missing
Jump away right away: bdse and bdns
Often you just want to skip a part when the device is missing:
bdns sensor start # no sensor: back to the start
l temp sensor Temperature
bdns jumps when the device is missing, bdse when it is there. Here the jump lands on start, and because yield sits there, the chip waits politely for the next tick.
| Instruction | does |
|---|---|
sdse r? d? | r = 1 if device set |
sdns r? d? | r = 1 if device missing |
bdse d? target | jumps if device set |
bdns d? target | jumps if device missing |
A register holding a ReferenceId works as the device too – more on that yesterday with the ReferenceId.
And on the data network?
For devices you address via lbn you do not need this check: if the device is missing there, Average returns NaN, and you catch that with snan. The pin check is for everything attached to d0 to d5.
The order matters: check first, then read. A check after the l comes too late – the error has already happened.
bdseal and bdnsal additionally jump into a subroutine with a return address. They come in week 19.
Diagram
Exercise
Task
d0 holds a gas sensor, d1 a warning light, d2 an LED display.
- Sensor present: warning light off, the display shows the temperature in °C
- Sensor missing: warning light on, the display keeps its last value
- The chip must never stop with an error
Use sdse or sdns and bdse or bdns.
Hints
Hint 1
First sdse present sensor, then flip it with seqz and write it to the warning light.
Hint 2
Before the l: bdns sensor start – if the sensor is missing, go back to the start.
Hint 3
The display is only written when the sensor is there – that way it keeps the last value.
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