Skip to content

Commit

Permalink
esp8266/modules/onewire: Change onewire.read() to onewire.readinto().
Browse files Browse the repository at this point in the history
This allows 1-wire drivers (eg DS18X20) to perform in-place operations and
hence do less memory allocations.
  • Loading branch information
dpgeorge committed Aug 29, 2016
1 parent 9fba618 commit 8e9b98e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 4 additions & 3 deletions esp8266/modules/ds18x20.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class DS18X20:
def __init__(self, onewire):
self.ow = onewire
self.buf = bytearray(9)

def scan(self):
return [rom for rom in self.ow.scan() if rom[0] == 0x28]
Expand All @@ -21,10 +22,10 @@ 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):
self.ow.readinto(self.buf)
if self.ow.crc8(self.buf):
raise Exception('CRC error')
return buf
return self.buf

def write_scratch(self, rom, buf):
self.ow.reset(True)
Expand Down
6 changes: 2 additions & 4 deletions esp8266/modules/onewire.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ def readbit(self):
def readbyte(self):
return _ow.readbyte(self.pin)

def read(self, count):
buf = bytearray(count)
for i in range(count):
def readinto(self, buf):
for i in range(len(buf)):
buf[i] = _ow.readbyte(self.pin)
return buf

def writebit(self, value):
return _ow.writebit(self.pin, value)
Expand Down

0 comments on commit 8e9b98e

Please sign in to comment.