What you learn today
- Read PrefabHash and NameHash
- Recognise devices
- Compare hashes
So far your code always knew which device hangs where – you connected it yourself. A chip that other players take from the library does not know that. But it can ask: every logic-capable device reports what it is and what it is called.
Three identifiers
| LogicType | returns |
|---|---|
PrefabHash | the hash of the prefab name, i.e. the device type |
NameHash | the hash of the name you gave it with the labeller |
ReferenceId | the unique number of this one device |
PrefabHash is the same number that HASH("StructureGasSensor") returns. NameHash the same as HASH("Raum"). So you simply compare with a define:
l kind d0 PrefabHash
seq tmp kind SENSOR # 1 = a gas sensor is on d0
Recognising devices on pins
With the indirect pin dr0 (day 102) a loop walks through all six pins. bdns skips empty pins before l raises an error:
move pin 0
loop:
bdns dr0 next # empty pin: skip
l kind dr0 PrefabHash
next:
add pin pin 1
blt pin PINS loop
That way the chip counts how many sensors and batteries it has, and finds the device with a certain name – no matter which pin it sits on.
What it is good for
- Self-check: a chip checks at start whether there really is a gas sensor on d0, and reports an error otherwise instead of controlling nonsense.
- Universal display: depending on the device type the chip reads
TemperatureorRatio. - Names instead of pins: the chip looks for “Haupt”, whether somebody put the battery on d1 or d3.
An empty pin is not a device. l kind dr0 PrefabHash on an empty pin stops the chip with an error. First bdns, then read.
On the network you need this less often, there lbn already searches by type and name. But NameHash helps there too: a chip can check that way whether a device carries the name it expects.
Diagram
Exercise
Task
Gas sensors, batteries or nothing hang on pins d0–d5. Write a loop over all pins:
- display “Sensoren”: number of gas sensors
- display “Akkus”: number of station batteries
- display “Haupt”: pin number of the device named “Haupt”, −1 if it is missing
The pin number is in pin (r0) so that dr0 works.
Hints
Hint 1
bdns dr0 next skips empty pins.
Hint 2
l kind dr0 PrefabHash, then seq with SENSOR and BATT and add up.
Hint 3
Name: l tmp dr0 NameHash, seq tmp tmp MAIN, select main tmp pin main.
Quiz
3 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