Skip to content

Commit a95b06f

Browse files
committed
drivers/onewire: Fix ds18x20.read_temp so it works when no rom given.
1 parent 92d4b51 commit a95b06f

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

drivers/onewire/ds18x20.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
The following example assumes the ground of your DS18x20 is connected to
88
Y11, vcc is connected to Y9 and the data pin is connected to Y10.
99
10-
>>> gnd = Pin('Y11')
11-
>>> gnd.init(Pin.OUT_PP)
10+
>>> from pyb import Pin
11+
>>> gnd = Pin('Y11', Pin.OUT_PP)
1212
>>> gnd.low()
13-
14-
>>> vcc = Pin('Y9')
15-
>>> vcc.init(Pin.OUT_PP)
13+
>>> vcc = Pin('Y9', Pin.OUT_PP)
1614
>>> vcc.high()
1715
16+
>>> from ds18x20 import DS18X20
1817
>>> d = DS18X20(Pin('Y10'))
1918
2019
Call read_temps to read all sensors:
@@ -47,27 +46,22 @@ def __init__(self, pin):
4746
# correct # first byte in their rom for a DS18x20 device.
4847
self.roms = [rom for rom in self.ow.scan() if rom[0] == 0x10 or rom[0] == 0x28]
4948

50-
def _select_rom(self, rom):
51-
if rom:
52-
self.ow.select_rom(rom)
53-
else:
54-
self.ow.skip_rom()
55-
5649
def read_temp(self, rom=None):
5750
"""
5851
Read and return the temperature of one DS18x20 device.
5952
Pass the 8-byte bytes object with the ROM of the specific device you want to read.
6053
If only one DS18x20 device is attached to the bus you may omit the rom parameter.
6154
"""
55+
rom = rom or self.roms[0]
6256
ow = self.ow
6357
ow.reset()
64-
self._select_rom(rom)
58+
ow.select_rom(rom)
6559
ow.write_byte(0x44) # Convert Temp
6660
while True:
6761
if ow.read_bit():
6862
break
6963
ow.reset()
70-
self._select_rom(rom)
64+
ow.select_rom(rom)
7165
ow.write_byte(0xbe) # Read scratch
7266
data = ow.read_bytes(9)
7367
return self.convert_temp(rom[0], data)

0 commit comments

Comments
 (0)