2016-04-28 08:33:55 -04:00
|
|
|
# 1-Wire driver for MicroPython on ESP8266
|
|
|
|
# MIT license; Copyright (c) 2016 Damien P. George
|
|
|
|
|
2015-12-28 19:22:51 -05:00
|
|
|
import _onewire as _ow
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
class OneWireError(Exception):
|
|
|
|
pass
|
|
|
|
|
2015-12-28 19:22:51 -05:00
|
|
|
class OneWire:
|
2016-04-28 08:33:55 -04:00
|
|
|
SEARCH_ROM = const(0xf0)
|
|
|
|
MATCH_ROM = const(0x55)
|
|
|
|
SKIP_ROM = const(0xcc)
|
2015-12-28 19:22:51 -05:00
|
|
|
|
|
|
|
def __init__(self, pin):
|
|
|
|
self.pin = pin
|
2016-05-05 16:47:37 -04:00
|
|
|
self.pin.init(pin.OPEN_DRAIN)
|
2015-12-28 19:22:51 -05:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
return _ow.reset(self.pin)
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def readbit(self):
|
2015-12-28 19:22:51 -05:00
|
|
|
return _ow.readbit(self.pin)
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def readbyte(self):
|
2015-12-28 19:22:51 -05:00
|
|
|
return _ow.readbyte(self.pin)
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def read(self, count):
|
2015-12-28 19:22:51 -05:00
|
|
|
buf = bytearray(count)
|
|
|
|
for i in range(count):
|
|
|
|
buf[i] = _ow.readbyte(self.pin)
|
|
|
|
return buf
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def writebit(self, value):
|
2015-12-28 19:22:51 -05:00
|
|
|
return _ow.writebit(self.pin, value)
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def writebyte(self, value):
|
2015-12-28 19:22:51 -05:00
|
|
|
return _ow.writebyte(self.pin, value)
|
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def write(self, buf):
|
2015-12-28 19:22:51 -05:00
|
|
|
for b in buf:
|
|
|
|
_ow.writebyte(self.pin, b)
|
|
|
|
|
|
|
|
def select_rom(self, rom):
|
|
|
|
self.reset()
|
2016-04-28 08:33:55 -04:00
|
|
|
self.writebyte(MATCH_ROM)
|
|
|
|
self.write(rom)
|
2015-12-28 19:22:51 -05:00
|
|
|
|
|
|
|
def scan(self):
|
|
|
|
devices = []
|
|
|
|
diff = 65
|
|
|
|
rom = False
|
|
|
|
for i in range(0xff):
|
|
|
|
rom, diff = self._search_rom(rom, diff)
|
|
|
|
if rom:
|
|
|
|
devices += [rom]
|
|
|
|
if diff == 0:
|
|
|
|
break
|
|
|
|
return devices
|
|
|
|
|
|
|
|
def _search_rom(self, l_rom, diff):
|
|
|
|
if not self.reset():
|
|
|
|
return None, 0
|
2016-04-28 08:33:55 -04:00
|
|
|
self.writebyte(SEARCH_ROM)
|
2015-12-28 19:22:51 -05:00
|
|
|
if not l_rom:
|
|
|
|
l_rom = bytearray(8)
|
|
|
|
rom = bytearray(8)
|
|
|
|
next_diff = 0
|
|
|
|
i = 64
|
|
|
|
for byte in range(8):
|
|
|
|
r_b = 0
|
|
|
|
for bit in range(8):
|
2016-04-28 08:33:55 -04:00
|
|
|
b = self.readbit()
|
|
|
|
if self.readbit():
|
2015-12-28 19:22:51 -05:00
|
|
|
if b: # there are no devices or there is an error on the bus
|
|
|
|
return None, 0
|
|
|
|
else:
|
|
|
|
if not b: # collision, two devices with different bit meaning
|
|
|
|
if diff > i or ((l_rom[byte] & (1 << bit)) and diff != i):
|
|
|
|
b = 1
|
|
|
|
next_diff = i
|
2016-04-28 08:33:55 -04:00
|
|
|
self.writebit(b)
|
2015-12-28 19:22:51 -05:00
|
|
|
if b:
|
|
|
|
r_b |= 1 << bit
|
|
|
|
i -= 1
|
|
|
|
rom[byte] = r_b
|
|
|
|
return rom, next_diff
|
|
|
|
|
|
|
|
def crc8(self, data):
|
|
|
|
return _ow.crc8(data)
|
|
|
|
|
|
|
|
class DS18B20:
|
2016-04-28 08:33:55 -04:00
|
|
|
CONVERT = const(0x44)
|
|
|
|
RD_SCRATCH = const(0xbe)
|
|
|
|
WR_SCRATCH = const(0x4e)
|
2015-12-28 19:22:51 -05:00
|
|
|
|
|
|
|
def __init__(self, onewire):
|
|
|
|
self.ow = onewire
|
|
|
|
|
|
|
|
def scan(self):
|
2016-04-28 08:33:55 -04:00
|
|
|
return [rom for rom in self.ow.scan() if rom[0] == 0x28]
|
2015-12-28 19:22:51 -05:00
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def convert_temp(self):
|
2015-12-28 19:22:51 -05:00
|
|
|
if not self.ow.reset():
|
2016-04-28 08:33:55 -04:00
|
|
|
raise OneWireError
|
|
|
|
self.ow.writebyte(SKIP_ROM)
|
|
|
|
self.ow.writebyte(CONVERT)
|
2015-12-28 19:22:51 -05:00
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def read_scratch(self, rom):
|
2015-12-28 19:22:51 -05:00
|
|
|
if not self.ow.reset():
|
2016-04-28 08:33:55 -04:00
|
|
|
raise OneWireError
|
|
|
|
self.ow.select_rom(rom)
|
|
|
|
self.ow.writebyte(RD_SCRATCH)
|
|
|
|
buf = self.ow.read(9)
|
|
|
|
if self.ow.crc8(buf):
|
|
|
|
raise OneWireError
|
|
|
|
return buf
|
2015-12-28 19:22:51 -05:00
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def write_scratch(self, rom, buf):
|
|
|
|
if not self.ow.reset():
|
|
|
|
raise OneWireError
|
2015-12-28 19:22:51 -05:00
|
|
|
self.ow.select_rom(rom)
|
2016-04-28 08:33:55 -04:00
|
|
|
self.ow.writebyte(WR_SCRATCH)
|
|
|
|
self.ow.write(buf)
|
2015-12-28 19:22:51 -05:00
|
|
|
|
2016-04-28 08:33:55 -04:00
|
|
|
def read_temp(self, rom):
|
|
|
|
buf = self.read_scratch(rom)
|
|
|
|
return (buf[1] << 8 | buf[0]) / 16
|