Skip to content

Commit 852c4dd

Browse files
authored
Merge pull request #42 from anlorn/add_functions_to_set_get_baseline
Add functions to get/set baseline
2 parents 0dc9ef4 + 7279d2c commit 852c4dd

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,15 @@
4444
__version__ = "0.0.0-auto.0"
4545
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CCS811.git"
4646

47+
4748
_ALG_RESULT_DATA = const(0x02)
4849
_RAW_DATA = const(0x03)
4950
_ENV_DATA = const(0x05)
5051
_NTC = const(0x06)
5152
_THRESHOLDS = const(0x10)
5253

53-
# _BASELINE = 0x11
54+
_BASELINE = const(0x11)
55+
5456
# _HW_ID = 0x20
5557
# _HW_VERSION = 0x21
5658
# _FW_BOOT_VERSION = 0x23
@@ -106,7 +108,6 @@ def __init__(self, i2c_bus, address=0x5A):
106108
raise RuntimeError(
107109
"Device ID returned is not correct! Please check your wiring."
108110
)
109-
110111
# try to start the app
111112
buf = bytearray(1)
112113
buf[0] = 0xF4
@@ -156,6 +157,32 @@ def _update_data(self):
156157
if self.error:
157158
raise RuntimeError("Error:" + str(self.error_code))
158159

160+
@property
161+
def baseline(self):
162+
"""
163+
The propery reads and returns the current baseline value.
164+
The returned value is packed into an integer.
165+
Later the same integer can be used in order
166+
to set a new baseline.
167+
"""
168+
buf = bytearray(3)
169+
buf[0] = _BASELINE
170+
with self.i2c_device as i2c:
171+
i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
172+
return struct.unpack("<H", buf[1:])[0]
173+
174+
@baseline.setter
175+
def baseline(self, baseline_int):
176+
"""
177+
The property lets you set a new baseline. As a value accepts
178+
integer which represents packed baseline 2 bytes value.
179+
"""
180+
buf = bytearray(3)
181+
buf[0] = _BASELINE
182+
struct.pack_into("<H", 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)