Skip to content

Commit e5bc4a1

Browse files
committed
update libs
1 parent 7661eee commit e5bc4a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1602
-420
lines changed

libraries/AM232X/AM232X.cpp

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: AM232X.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.4
4+
// VERSION: 0.3.1
55
// PURPOSE: AM232X library for AM2320 for Arduino.
66
//
77
// HISTORY:
@@ -19,12 +19,14 @@
1919
// 0.2.3 2020-05-27 update library.json
2020
// 0.2.4 2020-12-09 arduino-ci
2121
// 0.3.0 2021-01-12 isConnected() + Wire0..Wire5 support
22+
// 0.3.1 2021-01-28 fix TODO's in code
2223

2324

2425
#include "AM232X.h"
2526

2627

27-
#define AM232X_ADDRESS ((uint8_t)0x5C)
28+
const uint8_t AM232X_ADDRESS = 0x5C;
29+
2830

2931
////////////////////////////////////////////////////////////////////
3032
//
@@ -173,67 +175,31 @@ int AM232X::setUserRegisterB(int value)
173175
//
174176
int AM232X::_readRegister(uint8_t reg, uint8_t count)
175177
{
176-
// wake up the sensor - see 8.2
177-
_wire->beginTransmission(AM232X_ADDRESS);
178-
int rv = _wire->endTransmission();
179-
delayMicroseconds(1000); // TODO tune
178+
if (! _wakeup() ) return AM232X_ERROR_CONNECT;
180179

181180
// request the data
182181
_wire->beginTransmission(AM232X_ADDRESS);
183182
_wire->write(0x03);
184183
_wire->write(reg);
185184
_wire->write(count);
186-
rv = _wire->endTransmission();
185+
int rv = _wire->endTransmission();
187186
if (rv < 0) return rv;
188187

189188
// request 4 extra, 2 for cmd + 2 for CRC
190-
uint8_t length = count + 4;
191-
int bytes = _wire->requestFrom(AM232X_ADDRESS, length);
192-
193-
for (int i = 0; i < bytes; i++)
194-
{
195-
bits[i] = _wire->read();
196-
}
197-
// ANALYZE ERRORS
198-
// will not detect if we requested 1 byte as that will
199-
// return 5 bytes as requested. E.g. getStatus()
200-
// TODO: design a fix.
201-
if (bytes != length)
202-
{
203-
switch(bits[3])
204-
{
205-
case 0x80: return AM232X_ERROR_FUNCTION;
206-
case 0x81: return AM232X_ERROR_ADDRESS;
207-
case 0x82: return AM232X_ERROR_REGISTER;
208-
case 0x83: return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
209-
case 0x84: return AM232X_ERROR_WRITE_DISABLED;
210-
default: return AM232X_ERROR_UNKNOWN;
211-
}
212-
}
213-
214-
// CRC is LOW Byte first
215-
uint16_t crc = bits[bytes - 1]*256 + bits[bytes - 2];
216-
if (crc16(&bits[0], bytes - 2) != crc)
217-
{
218-
return AM232X_ERROR_CRC_2; // read itself has wrong CRC
219-
}
220-
return AM232X_OK;
189+
rv = _getData(count + 4);
190+
return rv;
221191
}
222192

223193

224194
int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
225195
{
226-
// wake up the sensor - see 8.2
227-
_wire->beginTransmission(AM232X_ADDRESS);
228-
int rv = _wire->endTransmission();
229-
delayMicroseconds(1000); // TODO tune
196+
if (! _wakeup() ) return AM232X_ERROR_CONNECT;
230197

231198
// prepare data to send
232199
bits[0] = 0x10;
233200
bits[1] = reg;
234201
bits[2] = cnt;
235202

236-
// TODO: is the order correct? MSB LSB
237203
if (cnt == 2)
238204
{
239205
bits[4] = value & 0xFF;
@@ -247,20 +213,43 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
247213
// send data
248214
uint8_t length = cnt + 3; // 3 = cmd, startReg, #bytes
249215
_wire->beginTransmission(AM232X_ADDRESS);
250-
for (int i=0; i< length; i++)
216+
for (int i = 0; i < length; i++)
251217
{
252218
_wire->write(bits[i]);
253219
}
254220
// send the CRC
255-
uint16_t crc = crc16(bits, length);
221+
uint16_t crc = _crc16(bits, length);
256222
_wire->write(crc & 0xFF);
257223
_wire->write(crc >> 8);
258224

259-
rv = _wire->endTransmission();
225+
int rv = _wire->endTransmission();
260226
if (rv < 0) return rv;
261227

262228
// wait for the answer
229+
rv = _getData(length);
230+
return rv;
231+
}
232+
233+
234+
bool AM232X::_wakeup()
235+
{
236+
// wake up the sensor - see 8.2
237+
// min 800 us max 3000 us
238+
uint32_t start = micros();
239+
while (! isConnected())
240+
{
241+
if (micros() - start > 3000) return false;
242+
yield();
243+
delayMicroseconds(100);
244+
}
245+
return true;
246+
}
247+
248+
249+
int AM232X::_getData(uint8_t length)
250+
{
263251
int bytes = _wire->requestFrom(AM232X_ADDRESS, length);
252+
264253
for (int i = 0; i < bytes; i++)
265254
{
266255
bits[i] = _wire->read();
@@ -269,23 +258,23 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
269258
// ANALYZE ERRORS
270259
// will not detect if we requested 1 byte as that will
271260
// return 5 bytes as requested. E.g. getStatus()
272-
// TODO: design a fix.
261+
// design a fix if it becomes a problem.
273262
if (bytes != length)
274263
{
275-
switch(bits[3])
264+
switch (bits[3])
276265
{
277-
case 0x80: return AM232X_ERROR_FUNCTION;
278-
case 0x81: return AM232X_ERROR_ADDRESS;
279-
case 0x82: return AM232X_ERROR_REGISTER;
280-
case 0x83: return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
281-
case 0x84: return AM232X_ERROR_WRITE_DISABLED;
282-
default: return AM232X_ERROR_UNKNOWN;
266+
case 0x80: return AM232X_ERROR_FUNCTION;
267+
case 0x81: return AM232X_ERROR_ADDRESS;
268+
case 0x82: return AM232X_ERROR_REGISTER;
269+
case 0x83: return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
270+
case 0x84: return AM232X_ERROR_WRITE_DISABLED;
271+
default: return AM232X_ERROR_UNKNOWN;
283272
}
284273
}
285274

286275
// CRC is LOW Byte first
287-
crc = bits[bytes - 1]*256 + bits[bytes - 2];
288-
if (crc16(&bits[0], bytes - 2) != crc)
276+
uint16_t crc = bits[bytes - 1] * 256 + bits[bytes - 2];
277+
if (_crc16(&bits[0], bytes - 2) != crc)
289278
{
290279
return AM232X_ERROR_CRC_2; // read itself has wrong CRC
291280
}
@@ -294,14 +283,14 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
294283
}
295284

296285

297-
uint16_t AM232X::crc16(uint8_t *ptr, uint8_t len)
286+
uint16_t AM232X::_crc16(uint8_t *ptr, uint8_t len)
298287
{
299-
uint16_t crc =0xFFFF;
288+
uint16_t crc = 0xFFFF;
300289

301290
while (len--)
302291
{
303292
crc ^= *ptr++;
304-
for(int i = 0; i < 8; i++)
293+
for (int i = 0; i < 8; i++)
305294
{
306295
if (crc & 0x01)
307296
{

libraries/AM232X/AM232X.h

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// FILE: AM232X.h
44
// AUTHOR: Rob Tillaart
55
// PURPOSE: AM232X library for Arduino
6-
// VERSION: 0.3.0
6+
// VERSION: 0.3.1
77
// HISTORY: See AM232X.cpp
88
// URL: https://github.com/RobTillaart/AM232X
99
//
1010

11-
// Bottom view
11+
// Bottom view
1212
// +---+
1313
// VDD |o |
1414
// SDA |o |
@@ -21,7 +21,7 @@
2121
#include "Wire.h"
2222

2323

24-
#define AM232X_LIB_VERSION (F("0.3.0"))
24+
#define AM232X_LIB_VERSION (F("0.3.1"))
2525

2626

2727

@@ -39,51 +39,54 @@
3939

4040

4141
/*
42-
* from datasheet
43-
* 0x80: not support function code
44-
* 0x81: Read an illegal address
45-
* 0x82: write data beyond the scope
46-
* 0x83: CRC checksum error
47-
* 0x84: Write disabled
42+
from datasheet
43+
0x80: not support function code
44+
0x81: Read an illegal address
45+
0x82: write data beyond the scope
46+
0x83: CRC checksum error
47+
0x84: Write disabled
4848
*/
4949

5050
class AM232X
5151
{
52-
public:
53-
explicit AM232X(TwoWire *wire = &Wire);
52+
public:
53+
explicit AM232X(TwoWire *wire = &Wire);
5454

5555
#if defined (ESP8266) || defined(ESP32)
56-
bool begin(uint8_t sda, uint8_t scl);
56+
bool begin(uint8_t sda, uint8_t scl);
5757
#endif
58-
bool begin();
59-
bool isConnected();
60-
61-
int read();
62-
int getModel();
63-
int getVersion();
64-
uint32_t getDeviceID();
65-
66-
int getStatus();
67-
int getUserRegisterA();
68-
int getUserRegisterB();
69-
70-
int setStatus(uint8_t value);
71-
int setUserRegisterA(int value);
72-
int setUserRegisterB(int value);
73-
74-
inline float getHumidity() { return humidity; };
75-
inline float getTemperature() { return temperature; };
76-
77-
private:
78-
uint8_t bits[8];
79-
float humidity;
80-
float temperature;
81-
82-
int _readRegister(uint8_t reg, uint8_t cnt);
83-
int _writeRegister(uint8_t reg, uint8_t cnt, int16_t value);
84-
uint16_t crc16(uint8_t *ptr, uint8_t len);
85-
86-
TwoWire* _wire;
58+
bool begin();
59+
bool isConnected();
60+
61+
int read();
62+
int getModel();
63+
int getVersion();
64+
uint32_t getDeviceID();
65+
66+
int getStatus();
67+
int getUserRegisterA();
68+
int getUserRegisterB();
69+
70+
int setStatus(uint8_t value);
71+
int setUserRegisterA(int value);
72+
int setUserRegisterB(int value);
73+
74+
inline float getHumidity() { return humidity; };
75+
inline float getTemperature() { return temperature; };
76+
77+
private:
78+
uint8_t bits[8];
79+
float humidity;
80+
float temperature;
81+
82+
int _readRegister(uint8_t reg, uint8_t cnt);
83+
int _writeRegister(uint8_t reg, uint8_t cnt, int16_t value);
84+
bool _wakeup();
85+
int _getData(uint8_t length);
86+
87+
uint16_t _crc16(uint8_t *ptr, uint8_t len);
88+
89+
TwoWire* _wire;
8790
};
8891

8992
// -- END OF FILE --

libraries/AM232X/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,12 @@ variables temperature and humidity.
7676
To access these values one must use **getTemperature()** and **getHumidity()**.
7777

7878

79-
## Planned changes
79+
## Future
8080

81-
Fix several TODO's in the code.
8281

8382

8483
## Warning
8584

86-
The library has several open ends so use at own risk.
85+
The library has not been tested extensively yet so use at own risk.
8786

8887
See also LICENSE

0 commit comments

Comments
 (0)