Skip to content

Commit 711af84

Browse files
authored
Merge pull request #12 from LBertrandDC/asynchronous-functions
Add asynchhronous functions
2 parents a035aa4 + 972f828 commit 711af84

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

adafruit_ds18x20.py

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
_WR_SCRATCH = b'\x4E'
4141
_CONVERSION_TIMEOUT = const(1)
4242
RESOLUTION = (9, 10, 11, 12)
43+
# Maximum conversion delay in seconds, from DS18B20 datasheet.
44+
_CONVERSION_DELAY = {9:0.09375, 10:0.1875, 11:0.375, 12:0.750}
4345

4446
class DS18X20(object):
4547
"""Class which provides interface to DS18X20 temperature sensor."""
@@ -49,6 +51,7 @@ def __init__(self, bus, address):
4951
self._address = address
5052
self._device = OneWireDevice(bus, address)
5153
self._buf = bytearray(9)
54+
self._conv_delay = _CONVERSION_DELAY[12] # pessimistic default
5255
else:
5356
raise ValueError('Incorrect family code in device address.')
5457

@@ -111,3 +114,15 @@ def _write_scratch(self, buf):
111114
with self._device as dev:
112115
dev.write(_WR_SCRATCH)
113116
dev.write(buf, end=3)
117+
118+
def start_temperature_read(self):
119+
"""Start asynchronous conversion, returns immediately.
120+
Returns maximum conversion delay [seconds] based on resolution."""
121+
with self._device as dev:
122+
dev.write(_CONVERT)
123+
return _CONVERSION_DELAY[self.resolution]
124+
125+
def read_temperature(self):
126+
"""Read the temperature. No polling of the conversion busy bit
127+
(assumes that the conversion has completed)."""
128+
return self._read_temp()

docs/examples.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
Simple test
22
------------
33

4-
Ensure your device works with this simple test.
4+
Ensure your device works with these simple tests.
55

66
.. literalinclude:: ../examples/ds18x20_simpletest.py
77
:caption: examples/ds18x20_simpletest.py
88
:linenos:
9+
10+
.. literalinclude:: ../examples/ds18x20_asynctest.py
11+
:caption: examples/ds18x20_asynctest.py
12+
:linenos:

examples/ds18x20_asynctest.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Simple demo of printing the temperature from the first found DS18x20 sensor every second.
2+
# Using the asynchronous functions start_temperature_read() and
3+
# read_temperature() to allow the main loop to keep processing while
4+
# the conversion is in progress.
5+
# Author: Louis Bertrand, based on original by Tony DiCola
6+
7+
import time
8+
9+
import board
10+
11+
from adafruit_onewire.bus import OneWireBus
12+
from adafruit_ds18x20 import DS18X20
13+
14+
15+
# Initialize one-wire bus on board pin D1.
16+
ow_bus = OneWireBus(board.D1)
17+
18+
# Scan for sensors and grab the first one found.
19+
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
20+
ds18.resolution = 12
21+
22+
# Main loop to print the temperature every second.
23+
while True:
24+
conversion_delay = ds18.start_temperature_read()
25+
conversion_ready_at = time.monotonic() + conversion_delay
26+
print("waiting", end="")
27+
while time.monotonic() < conversion_ready_at:
28+
print(".", end="")
29+
time.sleep(0.1)
30+
print('\nTemperature: {0:0.3f}C\n'.format(ds18.read_temperature()))
31+
time.sleep(1.0)

0 commit comments

Comments
 (0)