mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
issue #86 - added ESP8266 support + refactor
This commit is contained in:
parent
d006e64419
commit
160b1ba3ce
@ -1,26 +1,37 @@
|
|||||||
//
|
//
|
||||||
// FILE: AM232X.cpp
|
// FILE: AM232X.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.2
|
// VERSION: 0.1.3
|
||||||
// PURPOSE: AM232X library for AM2320 for Arduino.
|
// PURPOSE: AM232X library for AM2320 for Arduino.
|
||||||
//
|
//
|
||||||
// HISTORY:
|
// HISTORY:
|
||||||
// 0.1.0 - 2017-12-11 initial version
|
// 0.1.0: 2017-12-11 initial version
|
||||||
// 0.1.1 - 2017-12-12 added CRC checking
|
// 0.1.1: 2017-12-12 added CRC checking
|
||||||
// 0.1.2 - 2017-12-12 get and set functions.
|
// 0.1.2: 2017-12-12 get and set functions.
|
||||||
|
// 0.1.3: 2017-12-19 added ESP8266 - issue #86
|
||||||
|
// tested by Viktor Balint
|
||||||
//
|
//
|
||||||
// Released to the public domain
|
// Released to the public domain
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <AM232X.h>
|
#include <AM232X.h>
|
||||||
|
|
||||||
|
#define AM232X_ADDRESS ((uint8_t)0x5C)
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// PUBLIC
|
// PUBLIC
|
||||||
//
|
//
|
||||||
AM232X::AM232X()
|
#ifdef ESP8266
|
||||||
|
void AM232X::begin(uint8_t sda, uint8_t scl)
|
||||||
{
|
{
|
||||||
_deviceAddress = AM232X_ADDRESS;
|
Wire.begin(sda, scl);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void AM232X::begin()
|
||||||
|
{
|
||||||
|
Wire.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
int AM232X::read()
|
int AM232X::read()
|
||||||
@ -122,12 +133,12 @@ int AM232X::setUserRegisterB(int value)
|
|||||||
int AM232X::_readRegister(uint8_t reg, uint8_t count)
|
int AM232X::_readRegister(uint8_t reg, uint8_t count)
|
||||||
{
|
{
|
||||||
// wake up the sensor - see 8.2
|
// wake up the sensor - see 8.2
|
||||||
Wire.beginTransmission(_deviceAddress);
|
Wire.beginTransmission(AM232X_ADDRESS);
|
||||||
int rv = Wire.endTransmission();
|
int rv = Wire.endTransmission();
|
||||||
delayMicroseconds(1000); // TODO tune
|
delayMicroseconds(1000); // TODO tune
|
||||||
|
|
||||||
// request the data
|
// request the data
|
||||||
Wire.beginTransmission(_deviceAddress);
|
Wire.beginTransmission(AM232X_ADDRESS);
|
||||||
Wire.write(0x03);
|
Wire.write(0x03);
|
||||||
Wire.write(reg);
|
Wire.write(reg);
|
||||||
Wire.write(count);
|
Wire.write(count);
|
||||||
@ -136,7 +147,7 @@ int AM232X::_readRegister(uint8_t reg, uint8_t count)
|
|||||||
|
|
||||||
// request 4 extra, 2 for cmd + 2 for CRC
|
// request 4 extra, 2 for cmd + 2 for CRC
|
||||||
uint8_t length = count + 4;
|
uint8_t length = count + 4;
|
||||||
int bytes = Wire.requestFrom(_deviceAddress, length);
|
int bytes = Wire.requestFrom(AM232X_ADDRESS, length);
|
||||||
|
|
||||||
for (int i = 0; i < bytes; i++)
|
for (int i = 0; i < bytes; i++)
|
||||||
{
|
{
|
||||||
@ -171,7 +182,7 @@ int AM232X::_readRegister(uint8_t reg, uint8_t count)
|
|||||||
int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
|
int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
|
||||||
{
|
{
|
||||||
// wake up the sensor - see 8.2
|
// wake up the sensor - see 8.2
|
||||||
Wire.beginTransmission(_deviceAddress);
|
Wire.beginTransmission(AM232X_ADDRESS);
|
||||||
int rv = Wire.endTransmission();
|
int rv = Wire.endTransmission();
|
||||||
delayMicroseconds(1000); // TODO tune
|
delayMicroseconds(1000); // TODO tune
|
||||||
|
|
||||||
@ -193,7 +204,7 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
|
|||||||
|
|
||||||
// send data
|
// send data
|
||||||
uint8_t length = cnt + 3; // 3 = cmd, startReg, #bytes
|
uint8_t length = cnt + 3; // 3 = cmd, startReg, #bytes
|
||||||
Wire.beginTransmission(_deviceAddress);
|
Wire.beginTransmission(AM232X_ADDRESS);
|
||||||
for (int i=0; i< length; i++)
|
for (int i=0; i< length; i++)
|
||||||
{
|
{
|
||||||
Wire.write(bits[i]);
|
Wire.write(bits[i]);
|
||||||
@ -207,7 +218,7 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
|
|||||||
if (rv < 0) return rv;
|
if (rv < 0) return rv;
|
||||||
|
|
||||||
// wait for the answer
|
// wait for the answer
|
||||||
int bytes = Wire.requestFrom(_deviceAddress, length);
|
int bytes = Wire.requestFrom(AM232X_ADDRESS, length);
|
||||||
for (int i = 0; i < bytes; i++)
|
for (int i = 0; i < bytes; i++)
|
||||||
{
|
{
|
||||||
bits[i] = Wire.read();
|
bits[i] = Wire.read();
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
// FILE: AM232X.h
|
// FILE: AM232X.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// PURPOSE: AM232X library for Arduino .
|
// PURPOSE: AM232X library for Arduino .
|
||||||
// VERSION: 0.1.2
|
// VERSION: 0.1.3
|
||||||
// HISTORY: See AM232X.cpp
|
// HISTORY: See AM232X.cpp
|
||||||
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/
|
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/
|
||||||
//
|
//
|
||||||
@ -14,7 +14,7 @@
|
|||||||
#include "Wire.h"
|
#include "Wire.h"
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
#define AM232X_LIB_VERSION "0.1.2"
|
#define AM232X_LIB_VERSION "0.1.3"
|
||||||
|
|
||||||
#define AM232X_OK 0
|
#define AM232X_OK 0
|
||||||
#define AM232X_ERROR_UNKNOWN -10
|
#define AM232X_ERROR_UNKNOWN -10
|
||||||
@ -36,12 +36,13 @@ from datasheet
|
|||||||
0x84: Write disabled
|
0x84: Write disabled
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define AM232X_ADDRESS 0x5C
|
|
||||||
|
|
||||||
class AM232X
|
class AM232X
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AM232X();
|
#ifdef ESP8266
|
||||||
|
void begin(uint8_t sda, uint8_t scl);
|
||||||
|
#endif
|
||||||
|
void begin();
|
||||||
|
|
||||||
int read();
|
int read();
|
||||||
int getModel();
|
int getModel();
|
||||||
@ -60,7 +61,6 @@ public:
|
|||||||
float temperature;
|
float temperature;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _deviceAddress;
|
|
||||||
uint8_t bits[8];
|
uint8_t bits[8];
|
||||||
|
|
||||||
int _readRegister(uint8_t reg, uint8_t cnt);
|
int _readRegister(uint8_t reg, uint8_t cnt);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=AM232X
|
name=AM232X
|
||||||
version=0.1.2
|
version=0.1.3
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
sentence=Library for AM232X Temperature and Humidity sensor.
|
sentence=Library for AM232X Temperature and Humidity sensor.
|
||||||
|
Loading…
Reference in New Issue
Block a user