Read a DS18B20 1-Wire temperature sensor in degrees Celsius: reset, ROM commands, conversion, scratchpad with CRC and resolution.
import hw.ds18b20
hw.ds18b20 talks to a Maxim DS18B20 temperature sensor over its single data wire. The bus is bit-banged through the pin and timing functions of hw.board, so it needs no special hardware: wire the data pin to a pull-up resistor of about 4.7 kilohms and open the sensor on that pin. On a computer the simulated board can hold one DS18B20 (board.SimOneWire) whose temperature you set with board.SimTemperature, and the driver talks to it with the real timings.
Reading the temperature
ReadCelsius does the whole exchange: it checks the resolution, starts a conversion, waits for the sensor to finish (up to 750 milliseconds at 12 bits) and reads the scratchpad. Every scratchpad read is checked with the CRC the sensor sends, and a missing sensor or a damaged read gives ERROR_C.
With one sensor on the wire, Open skips addressing. When several share the wire, read each unique 64-bit ROM code with ReadRom while it is alone and open it later with OpenRom, which selects it by that code.
Reads the ROM code of the only sensor on the wire and returns it as a 64-bit number with the family code in the low byte. Returns 0 when no sensor answers or the CRC does not match.
Reads the nine scratchpad bytes (temperature, alarm limits, configuration, CRC). Returns an empty vector when no sensor answers or the CRC does not match.
Sets the resolution to 9, 10, 11 or 12 bits, keeping the alarm limits, and returns true when the sensor reports the new value. Anything else returns false.
Calls Ready once per millisecond for up to the given number of milliseconds and returns true as soon as the conversion is done. On a computer the waiting only advances the simulated clock.
The temperature stored in the scratchpad, without starting a conversion. A sensor that has not converted since power-up reports 85.0. Returns ERROR_C when the read fails.
Starts a conversion, waits for it, and returns the temperature in degrees Celsius, or ERROR_C when the sensor is missing or a read fails its CRC check.