esp8266/modules/onewire: Change onewire.read() to onewire.readinto().
This allows 1-wire drivers (eg DS18X20) to perform in-place operations and hence do less memory allocations.
This commit is contained in:
parent
9fba618356
commit
8e9b98e974
@ -8,6 +8,7 @@ _WR_SCRATCH = const(0x4e)
|
|||||||
class DS18X20:
|
class DS18X20:
|
||||||
def __init__(self, onewire):
|
def __init__(self, onewire):
|
||||||
self.ow = onewire
|
self.ow = onewire
|
||||||
|
self.buf = bytearray(9)
|
||||||
|
|
||||||
def scan(self):
|
def scan(self):
|
||||||
return [rom for rom in self.ow.scan() if rom[0] == 0x28]
|
return [rom for rom in self.ow.scan() if rom[0] == 0x28]
|
||||||
@ -21,10 +22,10 @@ class DS18X20:
|
|||||||
self.ow.reset(True)
|
self.ow.reset(True)
|
||||||
self.ow.select_rom(rom)
|
self.ow.select_rom(rom)
|
||||||
self.ow.writebyte(_RD_SCRATCH)
|
self.ow.writebyte(_RD_SCRATCH)
|
||||||
buf = self.ow.read(9)
|
self.ow.readinto(self.buf)
|
||||||
if self.ow.crc8(buf):
|
if self.ow.crc8(self.buf):
|
||||||
raise Exception('CRC error')
|
raise Exception('CRC error')
|
||||||
return buf
|
return self.buf
|
||||||
|
|
||||||
def write_scratch(self, rom, buf):
|
def write_scratch(self, rom, buf):
|
||||||
self.ow.reset(True)
|
self.ow.reset(True)
|
||||||
|
@ -27,11 +27,9 @@ class OneWire:
|
|||||||
def readbyte(self):
|
def readbyte(self):
|
||||||
return _ow.readbyte(self.pin)
|
return _ow.readbyte(self.pin)
|
||||||
|
|
||||||
def read(self, count):
|
def readinto(self, buf):
|
||||||
buf = bytearray(count)
|
for i in range(len(buf)):
|
||||||
for i in range(count):
|
|
||||||
buf[i] = _ow.readbyte(self.pin)
|
buf[i] = _ow.readbyte(self.pin)
|
||||||
return buf
|
|
||||||
|
|
||||||
def writebit(self, value):
|
def writebit(self, value):
|
||||||
return _ow.writebit(self.pin, value)
|
return _ow.writebit(self.pin, value)
|
||||||
|
Loading…
Reference in New Issue
Block a user