Skip to content

Commit 3d65d3b

Browse files
committed
Add functions to get/set baseline
1 parent 0dc9ef4 commit 3d65d3b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

adafruit_ccs811.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@
4444
__version__ = "0.0.0-auto.0"
4545
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CCS811.git"
4646

47+
_BASELINE_PACKING_FORMAT = "<H"
48+
4749
_ALG_RESULT_DATA = const(0x02)
4850
_RAW_DATA = const(0x03)
4951
_ENV_DATA = const(0x05)
5052
_NTC = const(0x06)
5153
_THRESHOLDS = const(0x10)
5254

53-
# _BASELINE = 0x11
55+
_BASELINE = const(0x11)
56+
5457
# _HW_ID = 0x20
5558
# _HW_VERSION = 0x21
5659
# _FW_BOOT_VERSION = 0x23
@@ -106,7 +109,6 @@ def __init__(self, i2c_bus, address=0x5A):
106109
raise RuntimeError(
107110
"Device ID returned is not correct! Please check your wiring."
108111
)
109-
110112
# try to start the app
111113
buf = bytearray(1)
112114
buf[0] = 0xF4
@@ -156,6 +158,31 @@ def _update_data(self):
156158
if self.error:
157159
raise RuntimeError("Error:" + str(self.error_code))
158160

161+
@property
162+
def baseline(self):
163+
"""
164+
The function read and return the current baseline value.
165+
The returned value is packed into an integer.
166+
Later the same integer can be passed `set_baseline` function
167+
in order to set a new baseline.
168+
"""
169+
buf = bytearray(3)
170+
buf[0] = _BASELINE
171+
with self.i2c_device as i2c:
172+
i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
173+
return struct.unpack(_BASELINE_PACKING_FORMAT, buf[1:])[0]
174+
175+
def set_baseline(self, baseline_int):
176+
"""
177+
The function lets you set a new baseline. As an argument accepts
178+
integer which represents packed baseline 2 bytes value.
179+
"""
180+
buf = bytearray(3)
181+
buf[0] = _BASELINE
182+
struct.pack_into(_BASELINE_PACKING_FORMAT, buf, 1, baseline_int)
183+
with self.i2c_device as i2c:
184+
i2c.write(buf)
185+
159186
@property
160187
def tvoc(self): # pylint: disable=invalid-name
161188
"""Total Volatile Organic Compound in parts per billion."""

0 commit comments

Comments
 (0)