summaryrefslogtreecommitdiff
path: root/esp8266/modules/ds18x20.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-08-29 12:12:49 +1000
committerDamien George <damien.p.george@gmail.com>2016-08-29 12:12:49 +1000
commit9fba618356679b0eede00b7cd746d1a53e6b3772 (patch)
treed85db417528ed3338cc5e75ded4d888fcd4df4ea /esp8266/modules/ds18x20.py
parented0a06a93fc275e4f3980b81e4e5314ed63279c6 (diff)
esp8266/modules: Split onewire.py into OneWire and DS18X20 driver.
The OneWire class is now in its own onewire.py module, and the temperature sensor class is in its own ds18x20.py module. The latter is renamed to DS18X20 to reflect the fact that it will support both the "S" and "B" variants of the device. These files are moved to the modules/ subdirectory to take advantage of frozen bytecode.
Diffstat (limited to 'esp8266/modules/ds18x20.py')
-rw-r--r--esp8266/modules/ds18x20.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/esp8266/modules/ds18x20.py b/esp8266/modules/ds18x20.py
new file mode 100644
index 000000000..66983f5bc
--- /dev/null
+++ b/esp8266/modules/ds18x20.py
@@ -0,0 +1,37 @@
+# DS18x20 temperature sensor driver for MicroPython.
+# MIT license; Copyright (c) 2016 Damien P. George
+
+_CONVERT = const(0x44)
+_RD_SCRATCH = const(0xbe)
+_WR_SCRATCH = const(0x4e)
+
+class DS18X20:
+ def __init__(self, onewire):
+ self.ow = onewire
+
+ def scan(self):
+ return [rom for rom in self.ow.scan() if rom[0] == 0x28]
+
+ def convert_temp(self):
+ self.ow.reset(True)
+ self.ow.writebyte(self.ow.SKIP_ROM)
+ self.ow.writebyte(_CONVERT)
+
+ def read_scratch(self, rom):
+ self.ow.reset(True)
+ self.ow.select_rom(rom)
+ self.ow.writebyte(_RD_SCRATCH)
+ buf = self.ow.read(9)
+ if self.ow.crc8(buf):
+ raise Exception('CRC error')
+ return buf
+
+ def write_scratch(self, rom, buf):
+ self.ow.reset(True)
+ self.ow.select_rom(rom)
+ self.ow.writebyte(_WR_SCRATCH)
+ self.ow.write(buf)
+
+ def read_temp(self, rom):
+ buf = self.read_scratch(rom)
+ return (buf[1] << 8 | buf[0]) / 16