issue #86 - added ESP8266 support + refactor

This commit is contained in:
RobTillaart 2017-12-22 13:05:31 +01:00
parent d006e64419
commit 160b1ba3ce
3 changed files with 30 additions and 19 deletions

View File

@ -1,26 +1,37 @@
//
// FILE: AM232X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: AM232X library for AM2320 for Arduino.
//
// HISTORY:
// 0.1.0 - 2017-12-11 initial version
// 0.1.1 - 2017-12-12 added CRC checking
// 0.1.2 - 2017-12-12 get and set functions.
// 0.1.0: 2017-12-11 initial version
// 0.1.1: 2017-12-12 added CRC checking
// 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
//
#include <AM232X.h>
#define AM232X_ADDRESS ((uint8_t)0x5C)
////////////////////////////////////////////////////////////////////
//
// 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()
@ -122,12 +133,12 @@ int AM232X::setUserRegisterB(int value)
int AM232X::_readRegister(uint8_t reg, uint8_t count)
{
// wake up the sensor - see 8.2
Wire.beginTransmission(_deviceAddress);
Wire.beginTransmission(AM232X_ADDRESS);
int rv = Wire.endTransmission();
delayMicroseconds(1000); // TODO tune
// request the data
Wire.beginTransmission(_deviceAddress);
Wire.beginTransmission(AM232X_ADDRESS);
Wire.write(0x03);
Wire.write(reg);
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
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++)
{
@ -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)
{
// wake up the sensor - see 8.2
Wire.beginTransmission(_deviceAddress);
Wire.beginTransmission(AM232X_ADDRESS);
int rv = Wire.endTransmission();
delayMicroseconds(1000); // TODO tune
@ -193,7 +204,7 @@ int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
// send data
uint8_t length = cnt + 3; // 3 = cmd, startReg, #bytes
Wire.beginTransmission(_deviceAddress);
Wire.beginTransmission(AM232X_ADDRESS);
for (int i=0; i< length; 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;
// wait for the answer
int bytes = Wire.requestFrom(_deviceAddress, length);
int bytes = Wire.requestFrom(AM232X_ADDRESS, length);
for (int i = 0; i < bytes; i++)
{
bits[i] = Wire.read();

View File

@ -4,7 +4,7 @@
// FILE: AM232X.h
// AUTHOR: Rob Tillaart
// PURPOSE: AM232X library for Arduino .
// VERSION: 0.1.2
// VERSION: 0.1.3
// HISTORY: See AM232X.cpp
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/
//
@ -14,7 +14,7 @@
#include "Wire.h"
#include "Arduino.h"
#define AM232X_LIB_VERSION "0.1.2"
#define AM232X_LIB_VERSION "0.1.3"
#define AM232X_OK 0
#define AM232X_ERROR_UNKNOWN -10
@ -36,12 +36,13 @@ from datasheet
0x84: Write disabled
*/
#define AM232X_ADDRESS 0x5C
class AM232X
{
public:
AM232X();
#ifdef ESP8266
void begin(uint8_t sda, uint8_t scl);
#endif
void begin();
int read();
int getModel();
@ -60,7 +61,6 @@ public:
float temperature;
private:
uint8_t _deviceAddress;
uint8_t bits[8];
int _readRegister(uint8_t reg, uint8_t cnt);

View File

@ -1,5 +1,5 @@
name=AM232X
version=0.1.2
version=0.1.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for AM232X Temperature and Humidity sensor.