|
@@ -13,35 +13,36 @@
|
|
|
#define CMD_CALIBR2 (uint8_t)0x1c
|
|
|
#define CMD_CALIBR3 (uint8_t)0x1e
|
|
|
|
|
|
-#define STATUS_BUSY (uint8_t)0x80
|
|
|
-#define STATUS_CMD (uint8_t)0x40
|
|
|
-#define STATUS_CYC (uint8_t)0x20
|
|
|
-#define STATUS_CAL (uint8_t)0x08
|
|
|
-#define STATUS_CALB (uint8_t)0x18
|
|
|
+#define STATUS_BUSY (uint8_t)0x80
|
|
|
+#define STATUS_CMD (uint8_t)0x40
|
|
|
+#define STATUS_CYC (uint8_t)0x20
|
|
|
+#define STATUS_CAL (uint8_t)0x08
|
|
|
+#define STATUS_CALB (uint8_t)0x18
|
|
|
|
|
|
-/* Functions prototypes */
|
|
|
-/*
|
|
|
-uint8_t Calc_CRC8(uint8_t *message, uint8_t Num);
|
|
|
-ahtxx_st_t JH_Reset_REG(uint8_t addr);
|
|
|
-*/
|
|
|
-static bool ReadyToRequest;
|
|
|
+/* Private variables */
|
|
|
static ahtxx_t Data;
|
|
|
+static Timer startTimer, getTimer;
|
|
|
+
|
|
|
+/* Private functions prototypes */
|
|
|
+static ahtxx_st_t I2CWriteTo(uint8_t DataToSend);
|
|
|
+//static uint8_t Calc_CRC8(uint8_t *message, uint8_t Num);
|
|
|
+static ahtxx_st_t JH_Reset_REG(uint8_t addr);
|
|
|
+void _Init(void);
|
|
|
+void _GetData(void);
|
|
|
+void _StartMeasure(void);
|
|
|
|
|
|
/**
|
|
|
* Initialization
|
|
|
*/
|
|
|
-AHTxx::AHTxx(void)
|
|
|
-{
|
|
|
+void AHTxx_Init(void) {
|
|
|
// Just powered on, it takes time for the product chip to be ready internally,
|
|
|
// the delay is 100~500ms, and 500ms is recommended
|
|
|
auto timer = new AutoDeleteTimer;
|
|
|
- timer->initializeMs<500>(Init);
|
|
|
+ timer->initializeMs<500>(_Init);
|
|
|
timer->startOnce();
|
|
|
-
|
|
|
- ReadyToRequest = false;
|
|
|
}
|
|
|
|
|
|
-void AHTxx::Init(void) {
|
|
|
+void _Init(void) {
|
|
|
ahtxx_st_t res;
|
|
|
uint8_t data;
|
|
|
|
|
@@ -51,7 +52,7 @@ void AHTxx::Init(void) {
|
|
|
Wire.write(CMD_GET_STATUS);
|
|
|
res = (ahtxx_st_t)Wire.endTransmission();
|
|
|
if (res != St_OK) {
|
|
|
- Data.Error = res;
|
|
|
+ Data.Error = St_NoSensor;
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -80,10 +81,12 @@ void AHTxx::Init(void) {
|
|
|
}
|
|
|
|
|
|
Data.Error = St_OK;
|
|
|
- ReadyToRequest = true;
|
|
|
+
|
|
|
+ // start polling sensors - once per two seconds
|
|
|
+ startTimer.initializeMs(2000, _StartMeasure).start();
|
|
|
}
|
|
|
|
|
|
-void AHTxx::SoftReset(void) {
|
|
|
+void AHTxx_SoftReset(void) {
|
|
|
ahtxx_st_t res;
|
|
|
Data.Error = St_OK;
|
|
|
|
|
@@ -93,13 +96,13 @@ void AHTxx::SoftReset(void) {
|
|
|
Data.Error = res;
|
|
|
return;
|
|
|
}
|
|
|
+ startTimer.restart();
|
|
|
}
|
|
|
|
|
|
-void AHTxx::StartMeasure(void) {
|
|
|
- if (ReadyToRequest == false) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
+/**
|
|
|
+ * @brief Start Sensor measure.
|
|
|
+ */
|
|
|
+void _StartMeasure(void) {
|
|
|
ahtxx_st_t res;
|
|
|
Data.Error = St_OK;
|
|
|
|
|
@@ -112,13 +115,17 @@ void AHTxx::StartMeasure(void) {
|
|
|
Data.Error = res;
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ getTimer.initializeMs(600, _GetData).startOnce();
|
|
|
}
|
|
|
|
|
|
-void AHTxx::GetData(ahtxx_t * data) {
|
|
|
- if (ReadyToRequest == false) {
|
|
|
- return;
|
|
|
- }
|
|
|
+void AHTxx_GetData(ahtxx_t * data) {
|
|
|
+ data->Error = Data.Error;
|
|
|
+ data->Humidity = Data.Humidity;
|
|
|
+ data->Temperature = Data.Temperature;
|
|
|
+}
|
|
|
|
|
|
+void _GetData(void) {
|
|
|
ahtxx_st_t res;
|
|
|
uint8_t buf[8] = {0}, i = 0;
|
|
|
Data.Error = St_OK;
|
|
@@ -131,13 +138,11 @@ void AHTxx::GetData(ahtxx_t * data) {
|
|
|
}
|
|
|
if (i < 7) {
|
|
|
Data.Error = St_NACK_Data;
|
|
|
- data->Error = Data.Error;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if ((buf[0] & STATUS_BUSY) != 0) {
|
|
|
Data.Error = St_Timeout;
|
|
|
- data->Error = Data.Error;
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -170,40 +175,10 @@ void AHTxx::GetData(ahtxx_t * data) {
|
|
|
result /= 256;
|
|
|
result /= 16;
|
|
|
Data.Temperature = (int16_t)(result - 500); // in xx.x *C
|
|
|
-
|
|
|
- data->Error = Data.Error;
|
|
|
- data->Humidity = Data.Humidity;
|
|
|
- data->Temperature = Data.Temperature;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * CRC check type: CRC8/MAXIM
|
|
|
- * Polynomial: X8+X5+X4+1
|
|
|
- * Poly: 0011 0001 0x31
|
|
|
- * When the high bit is placed in the back, it becomes 1000 1100 0x8c
|
|
|
- */
|
|
|
-uint8_t AHTxx::Calc_CRC8(uint8_t *message, uint8_t Num)
|
|
|
-{
|
|
|
- uint8_t i;
|
|
|
- uint8_t byte;
|
|
|
- uint8_t crc = 0xFF;
|
|
|
-
|
|
|
- for (byte=0; byte<Num; byte++) {
|
|
|
- crc ^= (message[byte]);
|
|
|
- for (i=8; i>0; --i) {
|
|
|
- if (crc & 0x80) {
|
|
|
- crc = (crc << 1) ^ 0x31;
|
|
|
- } else {
|
|
|
- crc = (crc << 1);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return crc;
|
|
|
}
|
|
|
|
|
|
/* Reset register */
|
|
|
-ahtxx_st_t AHTxx::JH_Reset_REG(uint8_t addr) {
|
|
|
+static ahtxx_st_t JH_Reset_REG(uint8_t addr) {
|
|
|
ahtxx_st_t res;
|
|
|
uint8_t Byte_first, Byte_second, Byte_third;
|
|
|
|
|
@@ -243,12 +218,36 @@ ahtxx_st_t AHTxx::JH_Reset_REG(uint8_t addr) {
|
|
|
return St_OK;
|
|
|
}
|
|
|
|
|
|
-ahtxx_st_t AHTxx::I2CWriteTo(uint8_t DataToSend) {
|
|
|
+static ahtxx_st_t I2CWriteTo(uint8_t DataToSend) {
|
|
|
Wire.beginTransmission(AHT_I2C_ADDR);
|
|
|
Wire.write(DataToSend);
|
|
|
return (ahtxx_st_t)Wire.endTransmission();
|
|
|
}
|
|
|
|
|
|
-bool AHTxx::IsReadyToRequest(void) {
|
|
|
- return ReadyToRequest;
|
|
|
+/**
|
|
|
+ * CRC check type: CRC8/MAXIM
|
|
|
+ * Polynomial: X8+X5+X4+1
|
|
|
+ * Poly: 0011 0001 0x31
|
|
|
+ * When the high bit is placed in the back, it becomes 1000 1100 0x8c
|
|
|
+ */
|
|
|
+/*
|
|
|
+static uint8_t Calc_CRC8(uint8_t *message, uint8_t Num)
|
|
|
+{
|
|
|
+ uint8_t i;
|
|
|
+ uint8_t byte;
|
|
|
+ uint8_t crc = 0xFF;
|
|
|
+
|
|
|
+ for (byte=0; byte<Num; byte++) {
|
|
|
+ crc ^= (message[byte]);
|
|
|
+ for (i=8; i>0; --i) {
|
|
|
+ if (crc & 0x80) {
|
|
|
+ crc = (crc << 1) ^ 0x31;
|
|
|
+ } else {
|
|
|
+ crc = (crc << 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return crc;
|
|
|
}
|
|
|
+*/
|