34
34
__version__ = "0.0.0-auto.0"
35
35
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHTC3.git"
36
36
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
39
39
import adafruit_bus_device .i2c_device as i2c_device
40
40
41
41
from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct , Struct
42
42
from adafruit_register .i2c_bit import RWBit , ROBit
43
43
from adafruit_register .i2c_bits import RWBits
44
-
44
+ from struct import unpack_from
45
45
#include "Arduino.h"
46
46
#include <Adafruit_I2CDevice.h>
47
47
#include <Adafruit_Sensor.h>
61
61
_SHTC3_SOFTRESET = 0x805D # Soft Reset
62
62
_SHTC3_SLEEP = 0xB098 # Enter sleep mode
63
63
_SHTC3_WAKEUP = 0x3517 # Wakeup mode
64
+ _SHTC3_CHIP_ID = 0x807
64
65
65
66
class SHTC3 :
66
67
def __init__ (self , i2c_bus , address = _SHTC3_DEFAULT_ADDR ):
67
68
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
68
69
70
+ self ._buffer = bytearray (2 )
69
71
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!" )
72
77
73
- if ((readID () & 0x083F ) != 0x807 ) {
74
- return false ;
75
- }
76
78
77
- # * Internal function to perform and I2C write.
78
- # *
79
- # * @param cmd The 16-bit command ID to send.
80
- # */
81
79
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
87
82
88
- with obj .i2c_device as i2c :
83
+ with self .i2c_device as i2c :
89
84
i2c .write (self ._buffer )
90
85
86
+ @property
87
+ def _chip_id (self ): # readCommand(SHTC3_READID, data, 3);
88
+ out_buf = bytearray (3 )
91
89
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 )
99
91
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 )])
106
97
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 ]
109
99
110
100
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 )
113
104
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
116
109
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 )
117
117
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
131
119
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; }
139
120
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
+ #
147
132
148
- # readCommand(SHTC3_READID, data, 3);
149
133
150
- # uint16_t id = data[0];
151
- # id <<= 8;
152
- # id |= data[1];
153
134
154
- # return id;
155
- # }
156
135
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
- # }
164
136
165
137
# /**************************************************************************/
166
138
# /*!
@@ -171,51 +143,51 @@ def _write_command(self, command):
171
143
# @returns true if the event data was read successfully
172
144
# */
173
145
# /**************************************************************************/
174
- # bool Adafruit_SHTC3::( sensors_event_t *humidity,
146
+ # sensors_event_t *humidity,
175
147
# sensors_event_t *temp) {
176
- # uint32_t t = millis();
148
+ # uint32_t t = millis()
177
149
178
- # uint8_t readbuffer[6];
150
+ # uint8_t readbuffer[6]
179
151
180
- # sleep (false);
152
+ # delay_seconds (false)
181
153
# if (_lpMode) {
182
154
# // 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
+ #
189
161
190
162
# while (!i2c_dev->read(readbuffer, sizeof(readbuffer))) {
191
- # delay(1);
192
- # }
163
+ # delay(1)
164
+ #
193
165
194
166
# if (readbuffer[2] != crc8(readbuffer, 2) ||
195
167
# readbuffer[5] != crc8(readbuffer + 3, 2))
196
- # return false;
168
+ # return false
197
169
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])
199
171
# // 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
203
175
204
- # uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4];
176
+ # uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4]
205
177
# // 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
209
181
210
- # sleep (true);
182
+ # delay_seconds (true)
211
183
212
184
# // use helpers to fill in the events
213
185
# if (temp)
214
- # fillTempEvent(temp, t);
186
+ # fillTempEvent(temp, t)
215
187
# if (humidity)
216
- # fillHumidityEvent(humidity, t);
217
- # return true;
218
- # }
188
+ # fillHumidityEvent(humidity, t)
189
+ # return true
190
+ #
219
191
220
192
221
193
@@ -240,15 +212,15 @@ def _write_command(self, command):
240
212
# * Final XOR 0x00
241
213
# */
242
214
243
- # const uint8_t POLYNOMIAL(0x31);
244
- # uint8_t crc(0xFF);
215
+ # const uint8_t POLYNOMIAL(0x31)
216
+ # uint8_t crc(0xFF)
245
217
246
- # for (int j = len; j; --j) {
247
- # crc ^= *data++;
218
+ # for (int j = len j --j) {
219
+ # crc ^= *data++
248
220
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