|
44 | 44 | __version__ = "0.0.0-auto.0"
|
45 | 45 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CCS811.git"
|
46 | 46 |
|
| 47 | +_BASELINE_PACKING_FORMAT = "<H" |
| 48 | + |
47 | 49 | _ALG_RESULT_DATA = const(0x02)
|
48 | 50 | _RAW_DATA = const(0x03)
|
49 | 51 | _ENV_DATA = const(0x05)
|
50 | 52 | _NTC = const(0x06)
|
51 | 53 | _THRESHOLDS = const(0x10)
|
52 | 54 |
|
53 |
| -# _BASELINE = 0x11 |
| 55 | +_BASELINE = const(0x11) |
| 56 | + |
54 | 57 | # _HW_ID = 0x20
|
55 | 58 | # _HW_VERSION = 0x21
|
56 | 59 | # _FW_BOOT_VERSION = 0x23
|
@@ -106,7 +109,6 @@ def __init__(self, i2c_bus, address=0x5A):
|
106 | 109 | raise RuntimeError(
|
107 | 110 | "Device ID returned is not correct! Please check your wiring."
|
108 | 111 | )
|
109 |
| - |
110 | 112 | # try to start the app
|
111 | 113 | buf = bytearray(1)
|
112 | 114 | buf[0] = 0xF4
|
@@ -156,6 +158,31 @@ def _update_data(self):
|
156 | 158 | if self.error:
|
157 | 159 | raise RuntimeError("Error:" + str(self.error_code))
|
158 | 160 |
|
| 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 | + |
159 | 186 | @property
|
160 | 187 | def tvoc(self): # pylint: disable=invalid-name
|
161 | 188 | """Total Volatile Organic Compound in parts per billion."""
|
|
0 commit comments