Skip to content

Commit 9bcffd9

Browse files
committed
reading stuff
1 parent 82e3414 commit 9bcffd9

File tree

2 files changed

+70
-73
lines changed

2 files changed

+70
-73
lines changed

adafruit_shtc3.py

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
# Common imports remove if unused or pylint will complain
3838
from time import sleep as delay_seconds
39+
from time import monotonic
3940
import adafruit_bus_device.i2c_device as i2c_device
4041

4142
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct, Struct
@@ -67,7 +68,7 @@ class SHTC3:
6768
def __init__(self, i2c_bus, address = _SHTC3_DEFAULT_ADDR):
6869
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
6970

70-
self._buffer = bytearray(3)
71+
self._buffer = bytearray(6)
7172
self.reset()
7273
self.sleep = False
7374
if (self._chip_id & 0x083F != _SHTC3_CHIP_ID):
@@ -91,7 +92,6 @@ def _chip_id(self): # readCommand(SHTC3_READID, data, 3);
9192

9293
return unpack_from(">H", self._buffer)[0]
9394

94-
9595
def reset(self):
9696
self._write_command(_SHTC3_SOFTRESET)
9797
delay_seconds(0.001)
@@ -111,76 +111,65 @@ def sleep(self, sleep_enabled):
111111

112112
# lowPowerMode(bool readmode) { _lpMode = readmode
113113

114+
@property
115+
def humidity(self):
116+
"""Current relative humidity in % rH"""
117+
# sensors_event_t *humidity,
118+
# sensors_event_t *temp) {
119+
t = monotonic()
120+
print("reading hum/temp")
121+
readbuffer = bytearray(6)
114122

115-
# readID(void) {
116-
# uint8_t data[3]
117-
118-
# readCommand(SHTC3_READID, data, 3)
119-
120-
# uint16_t id = data[0]
121-
# id <<= 8
122-
# id |= data[1]
123-
124-
# return id
125-
#
126-
127-
128-
129-
130-
131-
# /**************************************************************************/
132-
# /*!
133-
# @brief Gets the humidity sensor and temperature values as sensor events
134-
# @param humidity Sensor event object that will be populated with humidity
135-
# data
136-
# @param temp Sensor event object that will be populated with temp data
137-
# @returns true if the event data was read successfully
138-
# */
139-
# /**************************************************************************/
140-
# sensors_event_t *humidity,
141-
# sensors_event_t *temp) {
142-
# uint32_t t = millis()
143-
144-
# uint8_t readbuffer[6]
145-
146-
# delay_seconds(false)
147-
# if (_lpMode) {
148-
# // low power
149-
# writeCommand(SHTC3_LOWPOW_MEAS_TFIRST)
150-
# delay(1)
151-
# else {
152-
# writeCommand(SHTC3_NORMAL_MEAS_TFIRST)
153-
# delay(13)
154-
#
155-
156-
# while (!i2c_dev->read(readbuffer, sizeof(readbuffer))) {
157-
# delay(1)
158-
#
123+
self.sleep = False
124+
read_bytes = []
159125

160-
# if (readbuffer[2] != crc8(readbuffer, 2) ||
161-
# readbuffer[5] != crc8(readbuffer + 3, 2))
162-
# return false
163-
164-
# int32_t stemp = (int32_t)(((uint32_t)readbuffer[0] << 8) | readbuffer[1])
165-
# // simplified (65536 instead of 65535) integer version of:
166-
# // temp = (stemp * 175.0f) / 65535.0f - 45.0f
167-
# stemp = ((4375 * stemp) >> 14) - 4500
168-
# _temperature = (float)stemp / 100.0f
169-
170-
# uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4]
171-
# // simplified (65536 instead of 65535) integer version of:
172-
# // humidity = (shum * 100.0f) / 65535.0f
173-
# shum = (625 * shum) >> 12
174-
# _humidity = (float)shum / 100.0f
175-
176-
# delay_seconds(true)
177-
178-
# // use helpers to fill in the events
179-
# if (temp)
180-
# fillTempEvent(temp, t)
181-
# if (humidity)
182-
# fillHumidityEvent(humidity, t)
183-
# return true
126+
# self._write_command(_SHTC3_LOWPOW_MEAS_TFIRST)
127+
if False: #lowPower
128+
self._write_command(_SHTC3_LOWPOW_MEAS_TFIRST)
129+
delay_seconds(0.001)
130+
else:
131+
self._write_command(_SHTC3_NORMAL_MEAS_TFIRST)
132+
delay_seconds(0.013)
133+
134+
# while (!i2c_dev->read(readbuffer, sizeof(readbuffer))) {
135+
# delay(1)
136+
while True:
137+
with self.i2c_device as i2c:
138+
print("reading")
139+
i2c.readinto(self._buffer)
140+
print("buf:", [hex(i) for i in self._buffer])
141+
read_bytes = unpack_from(">hbh", self._buffer)
142+
print("unpacked:", read_bytes)
143+
if read_bytes[0] != 0 and read_bytes[1] != 0 and read_bytes[2] != 0:
144+
print ("not all zeros, breaking")
145+
break
146+
print ("all zeros, continuing")
147+
148+
# if (self._buffer[2] != crc8(self._buffer, 2) or
149+
# self._buffer[5] != crc8(self._buffer + 3, 2)):
150+
# print("NOT CHECKING")
151+
152+
stemp = read_bytes[0]
153+
stemp = ((4375 * stemp) >> 14) - 4500
154+
temperature = stemp / 100.0
155+
print("temp:", temperature, "stemp:", stemp)
156+
157+
shum = read_bytes[2]
158+
shum = (625 * shum) >> 12
159+
humidity = shum / 100.0
160+
161+
self.sleep = True
162+
163+
print("shum:", shum, "humidity:", humidity)
164+
return (temperature, humidity)
165+
# delay_seconds(true)
166+
167+
# // use helpers to fill in the events
168+
# if (temp)
169+
# fillTempEvent(temp, t)
170+
# if (humidity)
171+
# fillHumidityEvent(humidity, t)
172+
# return true
184173
#
185174

186175

examples/shtc3_simpletest.py

100644100755
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2-
#
3-
# SPDX-License-Identifier: Unlicense
1+
#!/usr/bin/env python
2+
import time
3+
import busio
4+
import board
5+
import adafruit_shtc3
6+
from adafruit_debug_i2c import DebugI2C
7+
i2c = busio.I2C(board.SCL, board.SDA)
8+
i2c = DebugI2C(i2c)
9+
sht = adafruit_shtc3.SHTC3(i2c)
10+
print("got out")
11+
print("hum:", sht.humidity)

0 commit comments

Comments
 (0)