Skip to content

Commit 24e4602

Browse files
authored
Merge pull request #1 from deshipu/master
Initial commit
2 parents c892fd8 + 8b86a23 commit 24e4602

File tree

5 files changed

+173
-23
lines changed

5 files changed

+173
-23
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries
3+
Copyright (c) 2017 Radomir Dopieralski, written for Adafruit Industries
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.rst

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,61 @@
1-
21
Introduction
32
============
43

54
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-si7021/badge/?version=latest
65
:target: https://circuitpython.readthedocs.io/projects/si7021/en/latest/
76
:alt: Documentation Status
87

9-
.. image :: https://badges.gitter.im/adafruit/circuitpython.svg
8+
.. image:: https://badges.gitter.im/adafruit/circuitpython.svg
109
:target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
1110
:alt: Gitter
1211

13-
TODO
14-
1512
Dependencies
1613
=============
17-
This driver depends on:
1814

19-
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
20-
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
15+
This driver depends on the `Bus Device
16+
<https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_ library.
17+
Please ensure is is also available on the CircuitPython filesystem. This is
18+
easily achieved by downloading `a library and driver bundle
19+
<https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
2120

22-
Please ensure all dependencies are available on the CircuitPython filesystem.
23-
This is easily achieved by downloading
24-
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
2521

26-
Usage Example
27-
=============
22+
Usage Notes
23+
===========
24+
25+
Of course, you must import the library to use it:
26+
27+
.. code:: python
28+
29+
import adafruit_si7021
30+
31+
This driver takes an instantiated and active I2C object (from the `nativeio` or
32+
the `bitbangio` library) as an argument to its constructor. The way to create
33+
an I2C object depends on the board you are using. For boards with labeled SCL
34+
and SDA pins, you can:
35+
36+
.. code:: python
37+
38+
from bitbangio import I2C
39+
#from nativeio import I2C
40+
from board import SCL, SDA
41+
42+
i2c = I2C(SCL, SDA)
43+
44+
Once you have created the I2C interface object, you can use it to instantiate
45+
the sensor object:
46+
47+
.. code:: python
48+
49+
sensor = adafruit_si7021.SI7021(i2c)
50+
51+
52+
And then you can start measuring the temperature and humidity:
53+
54+
.. code:: python
55+
56+
print(sensor.temperature)
57+
print(sensor.relative_humidity)
2858
29-
TODO
3059
3160
Contributing
3261
============
@@ -35,6 +64,7 @@ Contributions are welcome! Please read our `Code of Conduct
3564
<https://github.com/adafruit/Adafruit_CircuitPython_si7021/blob/master/CODE_OF_CONDUCT.md>`_
3665
before contributing to help this project stay welcoming.
3766

67+
3868
API Reference
3969
=============
4070

adafruit_si7021.py

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22
#
3-
# Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries
3+
# Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries.
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -19,11 +19,133 @@
1919
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
22+
2223
"""
23-
`adafruit_si7021`
24-
====================================================
24+
``adafruit_si7021``
25+
===================
2526
26-
TODO(description)
27+
This is a CircuitPython driver for the SI7021 temperature and humidity sensor.
2728
28-
* Author(s): Radomir Dopieralski
2929
"""
30+
31+
32+
from micropython import const
33+
import ustruct
34+
import sys
35+
36+
from adafruit_bus_device.i2c_device import I2CDevice
37+
38+
39+
HUMIDITY = const(0xf5)
40+
TEMPERATURE = const(0xf3)
41+
_RESET = const(0xfe)
42+
_READ_USER1 = const(0xe7)
43+
_USER1_VAL = const(0x3a)
44+
45+
46+
def _crc(data):
47+
crc = 0
48+
for byte in data:
49+
crc ^= byte
50+
for i in range(8):
51+
if crc & 0x80:
52+
crc <<= 1
53+
crc ^= 0x131
54+
else:
55+
crc <<= 1
56+
return crc
57+
58+
59+
class SI7021:
60+
"""
61+
A driver for the SI7021 temperature and humidity sensor.
62+
"""
63+
64+
def __init__(self, i2c, address=0x40):
65+
self.i2c_device = I2CDevice(i2c, address)
66+
self.init()
67+
self._measurement = 0
68+
69+
def init(self):
70+
self.reset()
71+
# Make sure the USER1 settings are correct.
72+
while True:
73+
# While restarting, the sensor doesn't respond to reads or writes.
74+
try:
75+
data = bytearray([_READ_USER1])
76+
with self.i2c_device as i2c:
77+
i2c.writeto(data, stop=False)
78+
i2c.readfrom_into(data)
79+
value = data[0]
80+
except OSError as e:
81+
if e.args[0] not in ('I2C bus error', 19): # errno 19 ENODEV
82+
raise
83+
else:
84+
break
85+
if value != _USER1_VAL:
86+
raise RuntimeError("bad USER1 register (%x!=%x)" % (
87+
value, _USER1_VAL))
88+
89+
def _command(self, command):
90+
with self.i2c_device as i2c:
91+
i2c.writeto(ustruct.pack('B', command))
92+
93+
def _data(self):
94+
data = bytearray(3)
95+
data[0] = 0xff
96+
while True:
97+
# While busy, the sensor doesn't respond to reads.
98+
try:
99+
with self.i2c_device as i2c:
100+
i2c.readfrom_into(data)
101+
except OSError as e:
102+
if e.args[0] not in ('I2C bus error', 19): # errno 19 ENODEV
103+
raise
104+
else:
105+
if data[0] != 0xff: # Check if read succeeded.
106+
break
107+
value, checksum = ustruct.unpack('>HB', data)
108+
if checksum != _crc(data[:2]):
109+
raise ValueError("CRC mismatch")
110+
return value
111+
112+
def reset(self):
113+
self._command(_RESET)
114+
115+
@property
116+
def relative_humidity(self):
117+
"""The measured relative humidity in percents."""
118+
self.start_measurement(HUMIDITY)
119+
value = self._data()
120+
self._measurement = 0
121+
return value * 125 / 65536 - 6
122+
123+
@property
124+
def temperature(self):
125+
"""The measured temperature in degrees Celcius."""
126+
self.start_measurement(TEMPERATURE)
127+
value = self._data()
128+
self._measurement = 0
129+
return value * 175.72 / 65536 - 46.85
130+
131+
def start_measurement(self, what):
132+
"""
133+
Starts a measurement.
134+
135+
Starts a measurement of either ``HUMIDITY`` or ``TEMPERATURE``
136+
depending on the ``what`` argument. Returns immediately, and the
137+
result of the measurement can be retrieved with the
138+
``temperature`` and ``relative_humidity`` properties. This way it
139+
will take much less time.
140+
141+
This can be useful if you want to start the measurement, but don't
142+
want the call to block until the measurement is ready -- for instance,
143+
when you are doing other things at the same time.
144+
"""
145+
if what not in (HUMIDITY, TEMPERATURE):
146+
raise ValueError()
147+
if not self._measurement:
148+
self._command(what)
149+
elif self._measurement != what:
150+
raise RuntimeError("other measurement in progress")
151+
self._measurement = what

api.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11

2-
.. If you created a package, create one automodule per module in the package.
3-
42
.. automodule:: adafruit_si7021
53
:members:

conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# General information about the project.
2929
project = u'Adafruit SI7021 Library'
3030
copyright = u'2017 Radomir Dopieralski'
31-
author = u'Scott Shawcroft'
31+
author = u'Radomir Dopieralski'
3232

3333
# The version info for the project you're documenting, acts as replacement for
3434
# |version| and |release|, also used in various other places throughout the
@@ -118,7 +118,7 @@
118118
# author, documentclass [howto, manual, or own class]).
119119
latex_documents = [
120120
(master_doc, 'AdafruitSI7021Library.tex', u'Adafruit SI7021 Library Documentation',
121-
u'Phiilip Moyer', 'manual'),
121+
u'Radomir Dopieralski', 'manual'),
122122
]
123123

124124
# -- Options for manual page output ---------------------------------------

0 commit comments

Comments
 (0)