You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
64 lines
1.7 KiB
7 years ago
|
import gex
|
||
|
|
||
|
class OneWire(gex.Unit):
|
||
|
"""
|
||
|
Dallas 1-Wire master
|
||
|
"""
|
||
|
|
||
|
def _type(self):
|
||
|
return '1WIRE'
|
||
|
|
||
7 years ago
|
def test_presence(self):
|
||
|
""" Test presence fo any 1wire devices on the bus """
|
||
|
resp = self._query(0)
|
||
|
pp = gex.PayloadParser(resp)
|
||
|
return pp.bool()
|
||
|
|
||
|
def read_address(self, as_array=False):
|
||
|
""" Read the address of a lone device on the bus """
|
||
|
resp = self._query(4)
|
||
|
pp = gex.PayloadParser(resp)
|
||
|
if as_array:
|
||
|
return list(pp.tail())
|
||
|
else:
|
||
|
return pp.u64()
|
||
|
|
||
|
def search(self, alarm=False):
|
||
|
""" Find all devices, or devices with alarm """
|
||
|
devices = []
|
||
|
|
||
|
resp = self._query(2 if alarm else 1)
|
||
|
hasmore = True
|
||
|
while hasmore:
|
||
|
pp = gex.PayloadParser(resp)
|
||
|
hasmore = pp.bool()
|
||
|
while pp.length() > 0:
|
||
|
devices.append(pp.u64())
|
||
|
|
||
|
if hasmore:
|
||
|
resp = self._query(3)
|
||
|
|
||
|
return devices
|
||
|
|
||
7 years ago
|
def query(self, request, rcount, addr=0, verify=True, as_array=False):
|
||
7 years ago
|
""" Query a device """
|
||
|
pb = gex.PayloadBuilder()
|
||
7 years ago
|
pb.u64(addr)
|
||
7 years ago
|
pb.u16(rcount)
|
||
7 years ago
|
pb.bool(verify)
|
||
7 years ago
|
pb.blob(request)
|
||
|
|
||
7 years ago
|
resp = self._query(11, pb.close())
|
||
7 years ago
|
return resp.data if not as_array else list(resp.data)
|
||
|
|
||
7 years ago
|
def write(self, payload, addr=0, confirm=True):
|
||
7 years ago
|
""" Write to a device """
|
||
|
pb = gex.PayloadBuilder()
|
||
7 years ago
|
pb.u64(addr)
|
||
7 years ago
|
pb.blob(payload)
|
||
|
|
||
7 years ago
|
self._send(10, pb.close(), confirm=confirm)
|
||
7 years ago
|
|
||
|
def wait_ready(self):
|
||
|
""" Wait for DS18x20 to complete measurement (or other chip using the same polling mechanism) """
|
||
|
self._query(20)
|