Skip to content

Commit c9f87a6

Browse files
committed
reading and verifying chip id
1 parent 1738a26 commit c9f87a6

File tree

1 file changed

+85
-113
lines changed

1 file changed

+85
-113
lines changed

adafruit_shtc3.py

Lines changed: 85 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
__version__ = "0.0.0-auto.0"
3535
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHTC3.git"
3636

37-
# Common imports; remove if unused or pylint will complain
38-
from time import sleep
37+
# Common imports remove if unused or pylint will complain
38+
from time import sleep as delay_seconds
3939
import adafruit_bus_device.i2c_device as i2c_device
4040

4141
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct, Struct
4242
from adafruit_register.i2c_bit import RWBit, ROBit
4343
from adafruit_register.i2c_bits import RWBits
44-
44+
from struct import unpack_from
4545
#include "Arduino.h"
4646
#include <Adafruit_I2CDevice.h>
4747
#include <Adafruit_Sensor.h>
@@ -61,106 +61,78 @@
6161
_SHTC3_SOFTRESET = 0x805D # Soft Reset
6262
_SHTC3_SLEEP = 0xB098 # Enter sleep mode
6363
_SHTC3_WAKEUP = 0x3517 # Wakeup mode
64+
_SHTC3_CHIP_ID = 0x807
6465

6566
class SHTC3:
6667
def __init__(self, i2c_bus, address = _SHTC3_DEFAULT_ADDR):
6768
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
6869

70+
self._buffer = bytearray(2)
6971
self.reset()
70-
# reset();
71-
self._sleep = False
72+
self.sleep = False
73+
read_id = self._chip_id
74+
print("got chip_id:", format(read_id, "#010x"))
75+
if (read_id & 0x083F != _SHTC3_CHIP_ID):
76+
raise RuntimeError("Failed to find an ICM20X sensor - check your wiring!")
7277

73-
if ((readID() & 0x083F) != 0x807) {
74-
return false;
75-
}
7678

77-
# * Internal function to perform and I2C write.
78-
# *
79-
# * @param cmd The 16-bit command ID to send.
80-
# */
8179
def _write_command(self, command):
82-
self._buffer.clear() #??
83-
self._buffer = bytearray(command)
84-
# MSBYTE first
85-
self._buffer[0] = command >> 8
86-
self._buffer[1] = command & 0xFF
80+
self._buffer[0] = command >> 8
81+
self._buffer[1] = command & 0xFF
8782

88-
with obj.i2c_device as i2c:
83+
with self.i2c_device as i2c:
8984
i2c.write(self._buffer)
9085

86+
@property
87+
def _chip_id(self): # readCommand(SHTC3_READID, data, 3);
88+
out_buf = bytearray(3)
9189

92-
# /**
93-
# * Internal function to perform an I2C read.
94-
# *
95-
# * @param cmd The 16-bit command ID to send.
96-
# */
97-
# bool Adafruit_SHTC3::readCommand(uint16_t command, uint8_t *buffer,
98-
# uint8_t num_bytes) {
90+
self._write_command(_SHTC3_READID)
9991

100-
def _write_command(self, command):
101-
self._buffer.clear() #??
102-
self._buffer = bytearray(command)
103-
# MSBYTE first
104-
self._buffer[0] = command >> 8
105-
self._buffer[1] = command & 0xFF
92+
with self.i2c_device as i2c:
93+
i2c.readinto(out_buf)
94+
# i2c.write_then_readinto(self._buffer, self._buffer, out_end=1, in_start=1)
95+
print("buf in:", out_buf)
96+
print("vuf a:", [hex(i) for i in list(out_buf)])
10697

107-
with obj.i2c_device as i2c:
108-
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
98+
return unpack_from(">H", out_buf)[0]
10999

110100

111-
# cmd[0] = command >> 8;
112-
# cmd[1] = command & 0xFF;
101+
def reset(self):
102+
self._write_command(_SHTC3_SOFTRESET)
103+
delay_seconds(0.001)
113104

114-
# return i2c_dev->write_then_read(cmd, 2, buffer, num_bytes);
115-
# }
105+
@property
106+
def sleep(self):
107+
"""Determines the sleep state of the sensor"""
108+
return self._cached_sleep
116109

110+
@sleep.setter
111+
def sleep(self, sleep_enabled):
112+
if sleep_enabled:
113+
self._write_command(_SHTC3_SLEEP)
114+
else:
115+
self._write_command(_SHTC3_WAKEUP)
116+
delay_seconds(0.001)
117117

118-
# /**
119-
# * @brief Brings the SHTC3 in or out of sleep mode
120-
# *
121-
# * @param sleepmode If true, go into sleep mode. Else, wakeup
122-
# */
123-
# void Adafruit_SHTC3::sleep(bool sleepmode) {
124-
# if (sleepmode) {
125-
# writeCommand(SHTC3_SLEEP);
126-
# } else {
127-
# writeCommand(SHTC3_WAKEUP);
128-
# delayMicroseconds(250);
129-
# }
130-
# }
118+
# lowPowerMode(bool readmode) { _lpMode = readmode
131119

132-
# /**
133-
# * @brief Tells the SHTC3 to read future data in low power (fast) or normal
134-
# * (precise)
135-
# *
136-
# * @param readmode If true, use low power mode for reads
137-
# */
138-
# void Adafruit_SHTC3::lowPowerMode(bool readmode) { _lpMode = readmode; }
139120

140-
# /**
141-
# * Gets the ID register contents.
142-
# *
143-
# * @return The 16-bit ID register.
144-
# */
145-
# uint16_t Adafruit_SHTC3::readID(void) {
146-
# uint8_t data[3];
121+
# readID(void) {
122+
# uint8_t data[3]
123+
124+
# readCommand(SHTC3_READID, data, 3)
125+
126+
# uint16_t id = data[0]
127+
# id <<= 8
128+
# id |= data[1]
129+
130+
# return id
131+
#
147132

148-
# readCommand(SHTC3_READID, data, 3);
149133

150-
# uint16_t id = data[0];
151-
# id <<= 8;
152-
# id |= data[1];
153134

154-
# return id;
155-
# }
156135

157-
# /**
158-
# * Performs a reset of the sensor to put it into a known state.
159-
# */
160-
# void Adafruit_SHTC3::reset(void) {
161-
# writeCommand(SHTC3_SOFTRESET);
162-
# delay(1);
163-
# }
164136

165137
# /**************************************************************************/
166138
# /*!
@@ -171,51 +143,51 @@ def _write_command(self, command):
171143
# @returns true if the event data was read successfully
172144
# */
173145
# /**************************************************************************/
174-
# bool Adafruit_SHTC3::(sensors_event_t *humidity,
146+
# sensors_event_t *humidity,
175147
# sensors_event_t *temp) {
176-
# uint32_t t = millis();
148+
# uint32_t t = millis()
177149

178-
# uint8_t readbuffer[6];
150+
# uint8_t readbuffer[6]
179151

180-
# sleep(false);
152+
# delay_seconds(false)
181153
# if (_lpMode) {
182154
# // low power
183-
# writeCommand(SHTC3_LOWPOW_MEAS_TFIRST);
184-
# delay(1);
185-
# } else {
186-
# writeCommand(SHTC3_NORMAL_MEAS_TFIRST);
187-
# delay(13);
188-
# }
155+
# writeCommand(SHTC3_LOWPOW_MEAS_TFIRST)
156+
# delay(1)
157+
# else {
158+
# writeCommand(SHTC3_NORMAL_MEAS_TFIRST)
159+
# delay(13)
160+
#
189161

190162
# while (!i2c_dev->read(readbuffer, sizeof(readbuffer))) {
191-
# delay(1);
192-
# }
163+
# delay(1)
164+
#
193165

194166
# if (readbuffer[2] != crc8(readbuffer, 2) ||
195167
# readbuffer[5] != crc8(readbuffer + 3, 2))
196-
# return false;
168+
# return false
197169

198-
# int32_t stemp = (int32_t)(((uint32_t)readbuffer[0] << 8) | readbuffer[1]);
170+
# int32_t stemp = (int32_t)(((uint32_t)readbuffer[0] << 8) | readbuffer[1])
199171
# // simplified (65536 instead of 65535) integer version of:
200-
# // temp = (stemp * 175.0f) / 65535.0f - 45.0f;
201-
# stemp = ((4375 * stemp) >> 14) - 4500;
202-
# _temperature = (float)stemp / 100.0f;
172+
# // temp = (stemp * 175.0f) / 65535.0f - 45.0f
173+
# stemp = ((4375 * stemp) >> 14) - 4500
174+
# _temperature = (float)stemp / 100.0f
203175

204-
# uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4];
176+
# uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4]
205177
# // simplified (65536 instead of 65535) integer version of:
206-
# // humidity = (shum * 100.0f) / 65535.0f;
207-
# shum = (625 * shum) >> 12;
208-
# _humidity = (float)shum / 100.0f;
178+
# // humidity = (shum * 100.0f) / 65535.0f
179+
# shum = (625 * shum) >> 12
180+
# _humidity = (float)shum / 100.0f
209181

210-
# sleep(true);
182+
# delay_seconds(true)
211183

212184
# // use helpers to fill in the events
213185
# if (temp)
214-
# fillTempEvent(temp, t);
186+
# fillTempEvent(temp, t)
215187
# if (humidity)
216-
# fillHumidityEvent(humidity, t);
217-
# return true;
218-
# }
188+
# fillHumidityEvent(humidity, t)
189+
# return true
190+
#
219191

220192

221193

@@ -240,15 +212,15 @@ def _write_command(self, command):
240212
# * Final XOR 0x00
241213
# */
242214

243-
# const uint8_t POLYNOMIAL(0x31);
244-
# uint8_t crc(0xFF);
215+
# const uint8_t POLYNOMIAL(0x31)
216+
# uint8_t crc(0xFF)
245217

246-
# for (int j = len; j; --j) {
247-
# crc ^= *data++;
218+
# for (int j = len j --j) {
219+
# crc ^= *data++
248220

249-
# for (int i = 8; i; --i) {
250-
# crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
251-
# }
252-
# }
253-
# return crc;
254-
# }
221+
# for (int i = 8 i --i) {
222+
# crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1)
223+
#
224+
#
225+
# return crc
226+
#

0 commit comments

Comments
 (0)