Skip to content

Commit 987e701

Browse files
authored
Merge pull request #26 from caternuson/iss25
Update set_environmental_data function.
2 parents 09430db + 9a8fac7 commit 987e701

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

adafruit_ccs811.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"""
3535
import time
3636
import math
37+
import struct
3738

3839
from micropython import const
3940
from adafruit_bus_device.i2c_device import I2CDevice
@@ -192,21 +193,16 @@ def set_environmental_data(self, humidity, temperature):
192193
# Humidity is stored as an unsigned 16 bits in 1/512%RH. The default
193194
# value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61,
194195
# 0x00.
195-
hum_perc = int(humidity) << 1
196+
humidity = int(humidity * 512)
196197

197198
# Temperature is stored as an unsigned 16 bits integer in 1/512 degrees
198199
# there is an offset: 0 maps to -25C. The default value is 25C = 0x64,
199200
# 0x00. As an example 23.5% temperature would be 0x61, 0x00.
200-
parts = math.fmod(temperature)
201-
fractional = parts[0]
202-
temperature = parts[1]
201+
temperature = int((temperature + 25) * 512)
203202

204-
temp_high = ((temperature + 25) << 9)
205-
temp_low = ((fractional / 0.001953125) & 0x1FF)
206-
207-
temp_conv = (temp_high | temp_low)
208-
209-
buf = bytearray([_ENV_DATA, hum_perc, 0x00, ((temp_conv >> 8) & 0xFF), (temp_conv & 0xFF)])
203+
buf = bytearray(5)
204+
buf[0] = _ENV_DATA
205+
struct.pack_into(">HH", buf, 1, humidity, temperature)
210206

211207
with self.i2c_device as i2c:
212208
i2c.write(buf)

0 commit comments

Comments
 (0)