2017-06-22 07:28:07 +01:00
|
|
|
# 1-Wire driver for MicroPython
|
|
|
|
# MIT license; Copyright (c) 2016 Damien P. George
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
import _onewire as _ow
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
class OneWireError(Exception):
|
|
|
|
pass
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2015-03-03 01:29:52 +00:00
|
|
|
class OneWire:
|
2020-03-23 03:07:13 +00:00
|
|
|
SEARCH_ROM = 0xF0
|
|
|
|
MATCH_ROM = 0x55
|
|
|
|
SKIP_ROM = 0xCC
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def __init__(self, pin):
|
|
|
|
self.pin = pin
|
2017-06-26 08:48:05 +01:00
|
|
|
self.pin.init(pin.OPEN_DRAIN, pin.PULL_UP)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def reset(self, required=False):
|
|
|
|
reset = _ow.reset(self.pin)
|
|
|
|
if required and not reset:
|
|
|
|
raise OneWireError
|
|
|
|
return reset
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def readbit(self):
|
|
|
|
return _ow.readbit(self.pin)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def readbyte(self):
|
|
|
|
return _ow.readbyte(self.pin)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def readinto(self, buf):
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = _ow.readbyte(self.pin)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def writebit(self, value):
|
|
|
|
return _ow.writebit(self.pin, value)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def writebyte(self, value):
|
|
|
|
return _ow.writebyte(self.pin, value)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
2017-06-22 07:28:07 +01:00
|
|
|
def write(self, buf):
|
|
|
|
for b in buf:
|
|
|
|
_ow.writebyte(self.pin, b)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
|
|
|
def select_rom(self, rom):
|
|
|
|
self.reset()
|
2020-03-23 03:07:13 +00:00
|
|
|
self.writebyte(self.MATCH_ROM)
|
2017-06-22 07:28:07 +01:00
|
|
|
self.write(rom)
|
2015-03-03 01:29:52 +00:00
|
|
|
|
|
|
|
def scan(self):
|
|
|
|
devices = []
|
2017-06-22 07:28:07 +01:00
|
|
|
diff = 65
|
|
|
|
rom = False
|
2020-02-27 04:36:53 +00:00
|
|
|
for i in range(0xFF):
|
2017-06-22 07:28:07 +01:00
|
|
|
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
|
2020-03-23 03:07:13 +00:00
|
|
|
self.writebyte(self.SEARCH_ROM)
|
2017-06-22 07:28:07 +01: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):
|
|
|
|
b = self.readbit()
|
|
|
|
if self.readbit():
|
2020-02-27 04:36:53 +00:00
|
|
|
if b: # there are no devices or there is an error on the bus
|
2017-06-22 07:28:07 +01:00
|
|
|
return None, 0
|
2015-03-03 01:29:52 +00:00
|
|
|
else:
|
2020-02-27 04:36:53 +00:00
|
|
|
if not b: # collision, two devices with different bit meaning
|
2017-06-22 07:28:07 +01:00
|
|
|
if diff > i or ((l_rom[byte] & (1 << bit)) and diff != i):
|
|
|
|
b = 1
|
|
|
|
next_diff = i
|
|
|
|
self.writebit(b)
|
|
|
|
if b:
|
|
|
|
r_b |= 1 << bit
|
|
|
|
i -= 1
|
|
|
|
rom[byte] = r_b
|
|
|
|
return rom, next_diff
|
|
|
|
|
|
|
|
def crc8(self, data):
|
|
|
|
return _ow.crc8(data)
|