Clean code and add comments

This commit is contained in:
Phat Nguyen
2024-02-04 15:04:38 +07:00
parent 990f2482bb
commit 335fad3f0d
36 changed files with 3785 additions and 1636 deletions

View File

@ -1,6 +1,6 @@
#include "AirGradient.h"
#define AG_LIB_VER "3.0.0"
#define AG_LIB_VER "2.5.0"
AirGradient::AirGradient(BoardType type)
: pms5003(type), pms5003t_1(type), pms5003t_2(type), s8(type), sht(type), sgp41(type),

View File

@ -1,18 +1,22 @@
#ifndef _AIR_GRADIENT_H_
#define _AIR_GRADIENT_H_
#include "bsp/BoardDef.h"
#include "bsp/LedBar.h"
#include "bsp/PushButton.h"
#include "bsp/StatusLed.h"
#include "bsp/HardwareWatchdog.h"
#include "co2/s8.h"
#include "main/BoardDef.h"
#include "main/HardwareWatchdog.h"
#include "main/LedBar.h"
#include "main/PushButton.h"
#include "main/StatusLed.h"
#include "s8/s8.h"
#include "display/oled.h"
#include "pm/pms5003.h"
#include "pm/pms5003t.h"
#include "sgp/sgp41.h"
#include "sht/sht4x.h"
#include "pms/pms5003.h"
#include "pms/pms5003t.h"
#include "sgp41/sgp41.h"
#include "sht4x/sht4x.h"
/**
* @brief Class with define all the sensor has supported by Airgradient. Each
* sensor usage must be init before use.
*/
class AirGradient {
public:
AirGradient(BoardType type);
@ -21,7 +25,13 @@ public:
* @brief Plantower PMS5003 sensor
*/
PMS5003 pms5003;
/**
* @brief Plantower PMS5003T sensor: connect to PM1 connector on outdoor board.
*/
PMS5003T pms5003t_1;
/**
* @brief Plantower PMS5003T sensor: connect to PM2 connector on outdoor board
*/
PMS5003T pms5003t_2;
/**
@ -30,18 +40,18 @@ public:
S8 s8;
/**
* @brief Temperature and humidity sensor
* @brief SHT41 Temperature and humidity sensor
*/
Sht sht;
/**
* @brief TVOC and NOx sensor
* @brief SGP41 TVOC and NOx sensor
*
*/
Sgp41 sgp41;
/**
* @brief Display
* @brief OLED Display
*
*/
Display display;
@ -55,18 +65,43 @@ public:
* @brief LED
*/
StatusLed statusLed;
/**
* @brief RGB LED array
*
*/
LedBar ledBar;
/**
* @brief Hardware watchdog
* @brief External hardware watchdog
*/
HardwareWatchdog watchdog;
/**
* @brief Get I2C SDA pin has of board supported
*
* @return int Pin number if -1 invalid
*/
int getI2cSdaPin(void);
/**
* @brief Get I2C SCL pin has of board supported
*
* @return int Pin number if -1 invalid
*/
int getI2cSclPin(void);
/**
* @brief Get the Board Type
*
* @return BoardType @ref BoardType
*/
BoardType getBoardType(void);
/**
* @brief Get the library version string
*
* @return String
*/
String getVersion(void);
private:

View File

@ -3,7 +3,7 @@
#include "../library/Adafruit_SSD1306_Wemos_OLED/Adafruit_SSD1306.h"
#define disp(func) \
if (this->_boardType == BOARD_DIY_BASIC_KIT) { \
if (this->_boardType == DIY_BASIC) { \
((Adafruit_SSD1306 *)(this->oled))->func; \
} else { \
((Adafruit_SH110X *)(this->oled))->func; \
@ -19,7 +19,18 @@ void Display::begin(TwoWire &wire, Stream &debugStream) {
Display::Display(BoardType type) : _boardType(type) {}
/**
* @brief Initialize display, should be call this function before call of ther,
* if not it's always return failure.
*
* @param wire TwoWire instance, Must be initialized
*/
void Display::begin(TwoWire &wire) {
if (_isBegin) {
AgLog("Initialized, call end() then try again");
return;
}
this->_bsp = getBoardDef(this->_boardType);
if ((this->_bsp == nullptr) || (this->_bsp->I2C.supported == false) ||
(this->_bsp->OLED.supported == false)) {
@ -28,67 +39,106 @@ void Display::begin(TwoWire &wire) {
}
/** Init OLED */
if (this->_boardType == BOARD_DIY_BASIC_KIT) {
if (this->_boardType == DIY_BASIC) {
AgLog("Init Adafruit_SSD1306");
Adafruit_SSD1306 *_oled = new Adafruit_SSD1306();
_oled->begin(wire, SSD1306_SWITCHCAPVCC, this->_bsp->OLED.addr);
this->oled = _oled;
} else {
AgLog("Init Adafruit_SH1106G");
Adafruit_SH1106G *_oled = new Adafruit_SH1106G(this->_bsp->OLED.width, this->_bsp->OLED.height, &wire);
Adafruit_SH1106G *_oled = new Adafruit_SH1106G(
this->_bsp->OLED.width, this->_bsp->OLED.height, &wire);
_oled->begin(this->_bsp->OLED.addr, false);
this->oled = _oled;
}
this->_isInit = true;
this->_isBegin = true;
disp(clearDisplay());
AgLog("Init");
AgLog("Initialize");
}
/**
* @brief Clear display buffer
*
*/
void Display::clear(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(clearDisplay());
}
/**
* @brief Invert display color
*
* @param i 0: black, other is white
*/
void Display::invertDisplay(uint8_t i) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(invertDisplay(i));
}
/**
* @brief Send display frame buffer to OLED
*
*/
void Display::show() {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(display());
}
/**
* @brief Set display contract
*
* @param value Contract (0;255);
*/
void Display::setContrast(uint8_t value) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(setContrast(value));
}
/**
* @brief Draw pixel into display frame buffer, call show to draw to
* display(OLED)
*
* @param x X Position
* @param y Y Position
* @param color Color (0: black, other white)
*/
void Display::drawPixel(int16_t x, int16_t y, uint16_t color) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(drawPixel(x, y, color));
}
/**
* @brief Set text size, it's scale default font instead of point to multiple
* font has define for special size
*
* @param size Size of text (default = 1)
*/
void Display::setTextSize(int size) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(setTextSize(size));
}
/**
* @brief Move draw cursor into new position
*
* @param x X Position
* @param y Y Position
*/
void Display::setCursor(int16_t x, int16_t y) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(setCursor(x, y));
@ -100,7 +150,7 @@ void Display::setCursor(int16_t x, int16_t y) {
* @param color 0:black, 1: While
*/
void Display::setTextColor(uint16_t color) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(setTextColor(color));
@ -113,66 +163,120 @@ void Display::setTextColor(uint16_t color) {
* @param backGroundColor Text background color
*/
void Display::setTextColor(uint16_t foreGroundColor, uint16_t backGroundColor) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(setTextColor(foreGroundColor, backGroundColor));
}
/**
* @brief Draw text to display framebuffer, call show() to draw to display
* (OLED)
*
* @param text String
*/
void Display::setText(String text) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(print(text));
}
/**
* @brief Draw bitmap into display framebuffer, call show() to draw to display
* (OLED)
*
* @param x X Position
* @param y Y Position
* @param bitmap Bitmap buffer
* @param w Bitmap width
* @param h Bitmap hight
* @param color Bitmap color
*/
void Display::drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[],
int16_t w, int16_t h, uint16_t color) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(drawBitmap(x, y, bitmap, w, h, color));
}
/**
* @brief Set text to display framebuffer, call show() to draw into to display
* (OLED)
*
* @param text Character buffer
*/
void Display::setText(const char text[]) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(print(text));
}
/**
* @brief Draw line to display framebuffer, call show() to draw to
* display(OLED)
*
* @param x0 Start X position
* @param y0 Start Y position
* @param x1 End X Position
* @param y1 End Y Position
* @param color Color (0: black, otherwise white)
*/
void Display::drawLine(int x0, int y0, int x1, int y1, uint16_t color) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(drawLine(x0, y0, x1, y1, color));
}
/**
* @brief Draw circle to display framebuffer,
*
* @param x
* @param y
* @param r
* @param color
*/
void Display::drawCircle(int x, int y, int r, uint16_t color) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(drawCircle(x, y, r, color));
}
void Display::drawRect(int x0, int y0, int x1, int y1, uint16_t color) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
disp(drawRect(x0, y0, x1, y1, color));
}
bool Display::checkInit(void) {
if (this->_isInit) {
bool Display::isBegin(void) {
if (this->_isBegin) {
return true;
}
AgLog("OLED is not init");
AgLog("Display not-initialized");
return false;
}
void Display::setRotation(uint8_t r) {
if (checkInit() == false) {
if (isBegin() == false) {
return;
}
disp(setRotation(r));
}
void Display::end(void) {
if (this->_isBegin == false) {
return;
}
_isBegin = false;
if (this->_boardType == DIY_BASIC) {
delete ((Adafruit_SSD1306 *)(this->oled));
} else {
delete ((Adafruit_SH110X *)(this->oled));
}
AgLog("De-initialize");
}

View File

@ -1,10 +1,14 @@
#ifndef _AIR_GRADIENT_OLED_H_
#define _AIR_GRADIENT_OLED_H_
#include "../bsp/BoardDef.h"
#include "../main/BoardDef.h"
#include <Arduino.h>
#include <Wire.h>
/**
* @brief The class define how to handle the OLED display on Airgradient has
* attached or support OLED display like: ONE-V9, Basic-V4
*/
class Display {
public:
const uint16_t COLOR_WHILTE = 1;
@ -15,10 +19,11 @@ public:
#endif
Display(BoardType type);
void begin(TwoWire &wire);
void end(void);
void clear(void); // .clear
void clear(void);
void invertDisplay(uint8_t i);
void show(); // .show()
void show();
void setContrast(uint8_t value);
void drawPixel(int16_t x, int16_t y, uint16_t color);
@ -39,14 +44,14 @@ private:
BoardType _boardType;
const BoardDef *_bsp = nullptr;
void *oled;
bool _isInit = false;
bool _isBegin = false;
#if defined(ESP8266)
const char *TAG = "oled";
Stream *_debugStream = nullptr;
#else
#endif
bool checkInit(void);
bool isBegin(void);
};
#endif /** _AIR_GRADIENT_OLED_H_ */

View File

@ -3,9 +3,9 @@
#include "esp32-hal-log.h"
#endif
const BoardDef bsps[BOARD_DEF_MAX] = {
/** BOARD_DIY_BASIC_KIT */
[BOARD_DIY_BASIC_KIT] =
const BoardDef bsps[_BOARD_MAX] = {
/** DIY_BASIC */
[DIY_BASIC] =
{
.SenseAirS8 =
{
@ -17,7 +17,7 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
.supported = false,
#endif
},
.PMS5003 =
.Pms5003 =
{
.uart_tx_pin = 14,
.uart_rx_pin = 12,
@ -70,10 +70,10 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
.resetPin = -1,
.supported = false,
},
.name = "BOARD_DIY_BASIC_KIT",
.name = "DIY_BASIC",
},
/** BOARD_DIY_PRO_INDOOR_V4_2 */
[BOARD_DIY_PRO_INDOOR_V4_2] =
/** DIY_PRO_INDOOR_V4_2 */
[DIY_PRO_INDOOR_V4_2] =
{
.SenseAirS8 =
{
@ -85,7 +85,7 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
.supported = false,
#endif
},
.PMS5003 =
.Pms5003 =
{
.uart_tx_pin = 14,
.uart_rx_pin = 12,
@ -144,10 +144,10 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
.resetPin = -1,
.supported = false,
},
.name = "BOARD_DIY_PRO_INDOOR_V4_2",
.name = "DIY_PRO_INDOOR_V4_2",
},
/** BOARD_ONE_INDOOR_MONITOR_V9_0 */
[BOARD_ONE_INDOOR_MONITOR_V9_0] =
/** ONE_INDOOR */
[ONE_INDOOR] =
{
.SenseAirS8 =
{
@ -160,7 +160,7 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
#endif
},
/** Use UART0 don't use define pin number */
.PMS5003 =
.Pms5003 =
{
.uart_tx_pin = -1,
.uart_rx_pin = -1,
@ -232,10 +232,10 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
.supported = true,
#endif
},
.name = "BOARD_ONE_INDOOR_MONITOR_V9_0",
.name = "ONE_INDOOR",
},
/** BOARD_OUTDOOR_MONITOR_V1_3 */
[BOARD_OUTDOOR_MONITOR_V1_3] = {
/** OPEN_AIR_OUTDOOR */
[OPEN_AIR_OUTDOOR] = {
.SenseAirS8 =
{
.uart_tx_pin = 1,
@ -247,7 +247,7 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
#endif
},
/** Use UART0 don't use define pin number */
.PMS5003 =
.Pms5003 =
{
.uart_tx_pin = -1,
.uart_rx_pin = -1,
@ -319,7 +319,7 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
.supported = true,
#endif
},
.name = "BOARD_OUTDOOR_MONITOR_V1_3",
.name = "OPEN_AIR_OUTDOOR",
}};
/**
@ -329,7 +329,7 @@ const BoardDef bsps[BOARD_DEF_MAX] = {
* @return const BoardDef*
*/
const BoardDef *getBoardDef(BoardType def) {
if (def >= BOARD_DEF_MAX) {
if (def >= _BOARD_MAX) {
return NULL;
}
return &bsps[def];
@ -356,7 +356,7 @@ void printBoardDef(Stream *_debug) {
}
#endif
for (int i = 0; i < BOARD_DEF_MAX; i++) {
for (int i = 0; i < _BOARD_MAX; i++) {
bspPrintf("Board name: %s", bsps[i].name);
bspPrintf("\tSensor CO2 S8:");
bspPrintf("\t\tSupported: %d", bsps[i].SenseAirS8.supported);
@ -366,10 +366,10 @@ void printBoardDef(Stream *_debug) {
}
bspPrintf("\tSensor PMS5003:");
bspPrintf("\t\tSupported: %d", bsps[i].PMS5003.supported);
if (bsps[i].PMS5003.supported) {
bspPrintf("\t\tUART Tx: %d", bsps[i].PMS5003.uart_tx_pin);
bspPrintf("\t\tUART Rx: %d", bsps[i].PMS5003.uart_rx_pin);
bspPrintf("\t\tSupported: %d", bsps[i].Pms5003.supported);
if (bsps[i].Pms5003.supported) {
bspPrintf("\t\tUART Tx: %d", bsps[i].Pms5003.uart_tx_pin);
bspPrintf("\t\tUART Rx: %d", bsps[i].Pms5003.uart_rx_pin);
}
bspPrintf("\tI2C");

View File

@ -13,14 +13,21 @@
#define AgLog(c, ...) log_i(c, ##__VA_ARGS__)
#endif
/**
* @brief Define Airgradient supported board type
*/
enum BoardType {
BOARD_DIY_BASIC_KIT = 0x00,
BOARD_DIY_PRO_INDOOR_V4_2 = 0x01,
BOARD_ONE_INDOOR_MONITOR_V9_0 = 0x02,
BOARD_OUTDOOR_MONITOR_V1_3 = 0x03,
BOARD_DEF_MAX
DIY_BASIC = 0x00,
DIY_PRO_INDOOR_V4_2 = 0x01,
ONE_INDOOR = 0x02,
OPEN_AIR_OUTDOOR = 0x03,
_BOARD_MAX
};
/**
* @brief Board definitions
*
*/
struct BoardDef {
/** Board Support CO2 SenseS8 */
struct {
@ -34,7 +41,7 @@ struct BoardDef {
const int uart_tx_pin; /** UART tx pin */
const int uart_rx_pin; /** UART rx pin */
const bool supported; /** Is BSP supported for this sensor */
} PMS5003;
} Pms5003;
/** I2C Bus */
struct {

View File

@ -5,6 +5,10 @@
#include "BoardDef.h"
/**
* @brief The class define how to control external watchdog on ONE-V9 and
* Outdoor
*/
class HardwareWatchdog {
public:
HardwareWatchdog(BoardType type);

View File

@ -18,7 +18,7 @@ LedBar::LedBar(BoardType type) : _boardType(type) {}
*
*/
void LedBar::begin(void) {
if (this->_isInit) {
if (this->_isBegin) {
return;
}
@ -36,9 +36,9 @@ void LedBar::begin(void) {
pixel()->begin();
pixel()->clear();
this->_isInit = true;
this->_isBegin = true;
AgLog("Init");
AgLog("Initialize");
}
/**
@ -64,7 +64,7 @@ void LedBar::setColor(uint8_t red, uint8_t green, uint8_t blue, int ledNum) {
* @param brightness Brightness (0 - 255)
*/
void LedBar::setBrighness(uint8_t brightness) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
pixel()->setBrightness(brightness);
@ -76,15 +76,15 @@ void LedBar::setBrighness(uint8_t brightness) {
* @return int Number of LED
*/
int LedBar::getNumberOfLed(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return 0;
}
return this->_bsp->LED.rgbNum;
}
bool LedBar::checkInit(void) {
if (this->_isInit) {
bool LedBar::isBegin(void) {
if (this->_isBegin) {
return true;
}
AgLog("LED is not initialized");
@ -92,7 +92,7 @@ bool LedBar::checkInit(void) {
}
bool LedBar::ledNumInvalid(int ledNum) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return true;
}

View File

@ -5,6 +5,10 @@
#include "BoardDef.h"
/**
* @brief The class define how to handle the RGB LED bar
*
*/
class LedBar {
public:
#if defined(ESP8266)
@ -22,7 +26,7 @@ public:
private:
const BoardDef *_bsp;
bool _isInit = false;
bool _isBegin = false;
uint8_t _ledState = 0;
BoardType _boardType;
void *pixels = nullptr;
@ -31,7 +35,7 @@ private:
const char *TAG = "LED";
#else
#endif
bool checkInit(void);
bool isBegin(void);
bool ledNumInvalid(int ledNum);
};

View File

@ -15,7 +15,8 @@ void PushButton::begin(Stream &debugStream) {
*
*/
void PushButton::begin(void) {
if (this->_isInit) {
if (this->_isBegin) {
AgLog("Initialized, call end() then try again");
return;
}
@ -25,14 +26,14 @@ void PushButton::begin(void) {
return;
}
if (this->_boardType == BOARD_DIY_PRO_INDOOR_V4_2) {
if (this->_boardType == DIY_PRO_INDOOR_V4_2) {
pinMode(this->_bsp->SW.pin, INPUT_PULLUP);
} else {
pinMode(this->_bsp->SW.pin, INPUT);
}
this->_isInit = true;
AgLog("Init");
this->_isBegin = true;
AgLog("Initialize");
}
/**
@ -41,7 +42,7 @@ void PushButton::begin(void) {
* @return PushButton::State
*/
PushButton::State PushButton::getState(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return State::BUTTON_RELEASED;
}
@ -64,8 +65,8 @@ String PushButton::toString(PushButton::State state) {
return "Released";
}
bool PushButton::checkInit(void) {
if (this->_isInit) {
bool PushButton::isBegin(void) {
if (this->_isBegin) {
return true;
}
AgLog("Switch not initialized");

View File

@ -4,6 +4,10 @@
#include "BoardDef.h"
#include <Arduino.h>
/**
* @brief The class define how to handle the Push button
*
*/
class PushButton {
public:
/**
@ -26,7 +30,7 @@ private:
/** Board type */
BoardType _boardType;
/** Is inititalize flag */
bool _isInit = false;
bool _isBegin = false;
/** Special variable for ESP8266 */
#if defined(ESP8266)
@ -37,7 +41,7 @@ private:
/** Method */
bool checkInit(void);
bool isBegin(void);
};
#endif /** _AIR_GRADIENT_SW_H_ */

View File

@ -15,6 +15,10 @@ void StatusLed::begin(Stream &debugStream) {
*
*/
void StatusLed::begin(void) {
if (this->_isBegin) {
AgLog("Initialized, call end() then try again");
return;
}
bsp = getBoardDef(this->boardType);
if ((bsp == nullptr) || (bsp->LED.supported == false)) {
AgLog("Board not support StatusLed");
@ -25,9 +29,9 @@ void StatusLed::begin(void) {
digitalWrite(bsp->LED.pin, !bsp->LED.onState);
this->state = LED_OFF;
this->isInit = true;
this->_isBegin = true;
AgLog("Init");
AgLog("Initialize");
}
/**
@ -35,7 +39,7 @@ void StatusLed::begin(void) {
*
*/
void StatusLed::setOn(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
digitalWrite(bsp->LED.pin, bsp->LED.onState);
@ -48,7 +52,7 @@ void StatusLed::setOn(void) {
*
*/
void StatusLed::setOff(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return;
}
digitalWrite(bsp->LED.pin, !bsp->LED.onState);
@ -88,11 +92,24 @@ String StatusLed::toString(StatusLed::State state) {
return "Off";
}
bool StatusLed::checkInit(void) {
if (this->isInit == false) {
AgLog("No-Initialized");
bool StatusLed::isBegin(void) {
if (this->_isBegin == false) {
AgLog("Not-Initialized");
return false;
}
return true;
}
void StatusLed::end(void) {
if (_isBegin == false) {
return;
}
#if defined(ESP8266)
_debugStream = nullptr;
#endif
setOff();
_isBegin = false;
AgLog("De-initialize");
}

View File

@ -4,6 +4,10 @@
#include "BoardDef.h"
#include <Arduino.h>
/**
* @brief The class define how to handle the LED
*
*/
class StatusLed {
public:
enum State {
@ -17,6 +21,7 @@ public:
#else
#endif
void begin(void);
void end(void);
void setOn(void);
void setOff(void);
void setToggle(void);
@ -26,7 +31,7 @@ public:
private:
const BoardDef *bsp = nullptr;
BoardType boardType;
bool isInit = false;
bool _isBegin = false;
State state;
#if defined(ESP8266)
Stream *_debugStream;
@ -34,7 +39,7 @@ private:
#else
#endif
bool checkInit(void);
bool isBegin(void);
};
#endif /** _STATUS_LED_H_ */

View File

@ -1,7 +1,5 @@
#include "PMS.h"
// PMS::PMS(Stream &stream) { this->_stream = &stream; }
bool PMS::begin(Stream *stream) {
_stream = stream;

View File

@ -3,10 +3,15 @@
#include <Arduino.h>
/**
* @brief Class define how to handle plantower PMS sensor it's upport for
* PMS5003 and PMS5003T series. The data @ref AMB_TMP and @ref AMB_HUM only
* valid on PMS5003T
*/
class PMS {
public:
static const uint16_t SINGLE_RESPONSE_TIME = 1000;
static const uint16_t TOTAL_RESPONSE_TIME = 1000 * 10;
static const uint16_t TOTAL_RESPONSE_TIME = 1000 * 10;
static const uint16_t STEADY_RESPONSE_TIME = 1000 * 30;
// static const uint16_t BAUD_RATE = 9600;
@ -38,7 +43,7 @@ public:
uint16_t AMB_HUM;
};
bool begin(Stream* stream);
bool begin(Stream *stream);
void sleep();
void wakeUp();
void activeMode();

View File

@ -43,32 +43,27 @@ PMS5003::PMS5003(BoardType def) : _boardDef(def) {}
* @return false Failure
*/
bool PMS5003::begin(void) {
if (this->_isInit) {
if (this->_isBegin) {
AgLog("Initialized, call end() then try again");
return true;
}
#if defined(ESP32)
// if (this->_serial != &Serial) {
// AgLog("Hardware serial must be Serial(0)");
// return false;
// }
#endif
this->bsp = getBoardDef(this->_boardDef);
if (bsp == NULL) {
AgLog("Board [%d] not supported", this->_boardDef);
return false;
}
if (bsp->PMS5003.supported == false) {
if (bsp->Pms5003.supported == false) {
AgLog("Board [%d] PMS50035003 not supported", this->_boardDef);
return false;
}
#if defined(ESP8266)
bsp->PMS5003.uart_tx_pin;
SoftwareSerial *uart =
new SoftwareSerial(bsp->PMS5003.uart_tx_pin, bsp->PMS5003.uart_rx_pin);
bsp->Pms5003.uart_tx_pin;
SoftwareSerial *uart = new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin);
uart->begin(9600);
if (pms.begin(uart) == false) {
AgLog("PMS failed");
@ -82,7 +77,7 @@ bool PMS5003::begin(void) {
}
#endif
this->_isInit = true;
this->_isBegin = true;
return true;
}
@ -119,7 +114,7 @@ int PMS5003::pm25ToAQI(int pm02) {
* @return false Failure
*/
bool PMS5003::readData(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -168,10 +163,26 @@ int PMS5003::convertPm25ToUsAqi(int pm25) { return this->pm25ToAQI(pm25); }
* @return true Initialized
* @return false No-initialized
*/
bool PMS5003::checkInit(void) {
if (this->_isInit == false) {
AgLog("No initialized");
bool PMS5003::isBegin(void) {
if (this->_isBegin == false) {
AgLog("Not-initialized");
return false;
}
return true;
}
/**
* @brief De-initialize sensor
*/
void PMS5003::end(void) {
if (_isBegin == false) {
return;
}
_isBegin = false;
#if defined(ESP8266)
_debugStream = NULL;
#else
delete _serial;
#endif
AgLog("De-initialize");
}

View File

@ -1,10 +1,13 @@
#ifndef _AIR_GRADIENT_PMS5003_H_
#define _AIR_GRADIENT_PMS5003_H_
#include "../bsp/BoardDef.h"
#include "../main/BoardDef.h"
#include "Stream.h"
#include "PMS.h"
/**
* @brief The class define how to handle PMS5003 sensor bas on @ref PMS class
*/
class PMS5003 {
public:
PMS5003(BoardType def);
@ -13,6 +16,7 @@ public:
#else
bool begin(HardwareSerial &serial);
#endif
void end(void);
bool readData(void);
int getPm01Ae(void);
@ -22,7 +26,7 @@ public:
int convertPm25ToUsAqi(int pm25);
private:
bool _isInit = false;
bool _isBegin = false;
BoardType _boardDef;
PMS pms;
const BoardDef *bsp;
@ -36,7 +40,7 @@ private:
PMS::DATA pmsData;
bool begin(void);
bool checkInit(void);
bool isBegin(void);
int pm25ToAQI(int pm02);
};
#endif /** _AIR_GRADIENT_PMS5003_H_ */

View File

@ -43,7 +43,7 @@ PMS5003T::PMS5003T(BoardType def) : _boardDef(def) {}
* @return false Failure
*/
bool PMS5003T::begin(void) {
if (this->_isInit) {
if (this->_isBegin) {
return true;
}
@ -60,15 +60,15 @@ bool PMS5003T::begin(void) {
return false;
}
if (bsp->PMS5003.supported == false) {
if (bsp->Pms5003.supported == false) {
AgLog("Board [%d] PMS5003 not supported", this->_boardDef);
return false;
}
#if defined(ESP8266)
bsp->PMS5003.uart_tx_pin;
bsp->Pms5003.uart_tx_pin;
SoftwareSerial *uart =
new SoftwareSerial(bsp->PMS5003.uart_tx_pin, bsp->PMS5003.uart_rx_pin);
new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin);
uart->begin(9600);
if (pms.begin(uart) == false) {
AgLog("PMS failed");
@ -82,8 +82,8 @@ bool PMS5003T::begin(void) {
if (this->_serial == &Serial) {
#endif
AgLog("Init Serial");
this->_serial->begin(9600, SERIAL_8N1, bsp->PMS5003.uart_rx_pin,
bsp->PMS5003.uart_tx_pin);
this->_serial->begin(9600, SERIAL_8N1, bsp->Pms5003.uart_rx_pin,
bsp->Pms5003.uart_tx_pin);
} else {
if (bsp->SenseAirS8.supported == false) {
AgLog("Board [%d] PMS5003T_2 not supported", this->_boardDef);
@ -101,7 +101,7 @@ bool PMS5003T::begin(void) {
}
#endif
this->_isInit = true;
this->_isBegin = true;
return true;
}
@ -138,7 +138,7 @@ int PMS5003T::pm25ToAQI(int pm02) {
* @return false Failure
*/
bool PMS5003T::readData(void) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -207,9 +207,9 @@ float PMS5003T::getRelativeHumidity(void) {
* @return true Initialized
* @return false No-initialized
*/
bool PMS5003T::checkInit(void) {
if (this->_isInit == false) {
AgLog("No initialized");
bool PMS5003T::isBegin(void) {
if (this->_isBegin == false) {
AgLog("Not-initialized");
return false;
}
return true;
@ -221,3 +221,16 @@ float PMS5003T::correctionTemperature(float inTemp) {
}
return inTemp * 1.181f - 5.113f;
}
void PMS5003T::end(void) {
if (_isBegin == false) {
return;
}
_isBegin = false;
#if defined(ESP8266)
_debugStream = NULL;
#else
delete _serial;
#endif
AgLog("De-initialize");
}

View File

@ -2,10 +2,13 @@
#define _PMS5003T_H_
#include <HardwareSerial.h>
#include "../bsp/BoardDef.h"
#include "../main/BoardDef.h"
#include "PMS.h"
#include "Stream.h"
/**
* @brief The class define how to handle PMS5003T sensor bas on @ref PMS class
*/
class PMS5003T {
public:
PMS5003T(BoardType def);
@ -14,6 +17,7 @@ public:
#else
bool begin(HardwareSerial &serial);
#endif
void end(void);
bool readData(void);
int getPm01Ae(void);
@ -25,7 +29,7 @@ public:
float getRelativeHumidity(void);
private:
bool _isInit = false;
bool _isBegin = false;
bool _isSleep = false;
BoardType _boardDef;
@ -41,7 +45,7 @@ private:
int pm25ToAQI(int pm02);
PMS pms;
PMS::DATA pmsData;
bool checkInit(void);
bool isBegin(void);
float correctionTemperature(float inTemp);
};

View File

@ -6,7 +6,7 @@
#endif
/**
* @brief Construct a new Sense Air S 8:: Sense Air S 8 object
* @brief Construct a new Sense Air S8:: Sense Air S8 object
*
* @param def
*/
@ -18,7 +18,7 @@ S8::S8(BoardType def) : _boardDef(def) {}
* @return true = success, otherwise is failure
*/
bool S8::begin(void) {
if (this->_isInit) {
if (this->_isBegin) {
AgLog("Initialized, Call end() then try again");
return true;
}
@ -27,8 +27,8 @@ bool S8::begin(void) {
}
/**
* @brief Init sensor has print debug log, if class create without serial debug
* before it's override last define
* @brief Init S8 sensor, this methos should be call before other, if not it's
* always return the failure status
*
* @param _debugStream Serial print debug log, NULL if don't use
* @return true = success, otherwise is failure
@ -56,13 +56,12 @@ bool S8::begin(HardwareSerial &serial) {
*
*/
void S8::end(void) {
if (this->_isInit == false) {
AgLog("Senor is not initialized");
if (this->_isBegin == false) {
return;
}
// Deinit
AgLog("De-Inititlized");
AgLog("De-Inititlize");
}
/**
@ -71,7 +70,7 @@ void S8::end(void) {
* @param firmver String buffer, len = 10 char
*/
void S8::getFirmwareVersion(char firmver[]) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return;
}
@ -103,7 +102,7 @@ void S8::getFirmwareVersion(char firmver[]) {
* @return int32_t Return ID
*/
int32_t S8::getSensorTypeId(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -149,7 +148,7 @@ int32_t S8::getSensorTypeId(void) {
* @return int32_t ID
*/
int32_t S8::getSensorId(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -195,7 +194,7 @@ int32_t S8::getSensorId(void) {
* @return int16_t
*/
int16_t S8::getMemoryMapVersion(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -225,7 +224,7 @@ int16_t S8::getMemoryMapVersion(void) {
* @return int16_t (PPM), -1 if invalid.
*/
int16_t S8::getCo2(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -266,7 +265,7 @@ bool S8::setBaselineCalibration(void) {
/**
* @brief Wait for background calibration done
*
*
* @return true Done
* @return false On calib
*/
@ -286,7 +285,7 @@ bool S8::isBaseLineCalibrationDone(void) {
* @return int16_t PWM
*/
int16_t S8::getOutputPWM(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -318,7 +317,7 @@ int16_t S8::getOutputPWM(void) {
* @return int16_t Hour
*/
int16_t S8::getCalibPeriodABC(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -350,7 +349,7 @@ int16_t S8::getCalibPeriodABC(void) {
* @return false Failure
*/
bool S8::setCalibPeriodABC(int16_t period) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -391,7 +390,7 @@ bool S8::setCalibPeriodABC(int16_t period) {
* @return false Failure
*/
bool S8::manualCalib(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -416,7 +415,7 @@ bool S8::manualCalib(void) {
* @return int16_t Flags
*/
int16_t S8::getAcknowledgement(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -446,7 +445,7 @@ int16_t S8::getAcknowledgement(void) {
* @return false Failure
*/
bool S8::clearAcknowledgement(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -480,7 +479,7 @@ bool S8::clearAcknowledgement(void) {
* @return int16_t Alarm status
*/
int16_t S8::getAlarmStatus(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -509,7 +508,7 @@ int16_t S8::getAlarmStatus(void) {
* @return S8::Status Sensor status
*/
S8::Status S8::getStatus(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return (Status)0;
}
@ -538,7 +537,7 @@ S8::Status S8::getStatus(void) {
* @return int16_t Output status
*/
int16_t S8::getOutputStatus(void) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return -1;
}
@ -569,7 +568,7 @@ int16_t S8::getOutputStatus(void) {
* @return false Failure
*/
bool S8::sendSpecialCommand(CalibrationSpecialComamnd command) {
if (this->isInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -656,16 +655,16 @@ bool S8::init(int txPin, int rxPin, uint32_t baud) {
/** Check communication by get firmware version */
char fwVers[11];
this->_isInit = true;
this->_isBegin = true;
this->getFirmwareVersion(fwVers);
if (strlen(fwVers) == 0) {
this->_isInit = false;
this->_isBegin = false;
return false;
}
AgLog("Firmware version: %s", fwVers);
AgLog("Sensor successfully initialized. Heating up for 10s");
this->_isInit = true;
this->_isBegin = true;
this->_lastInitTime = millis();
return true;
}
@ -676,11 +675,11 @@ bool S8::init(int txPin, int rxPin, uint32_t baud) {
* @return true Initialized
* @return false No-Initialized
*/
bool S8::isInit(void) {
if (this->_isInit) {
bool S8::isBegin(void) {
if (this->_isBegin) {
return true;
}
AgLog("Sensor no-initialized");
AgLog("Sensor not-initialized");
return false;
}

View File

@ -1,9 +1,12 @@
#ifndef _S8_H_
#define _S8_H_
#include "../bsp/BoardDef.h"
#include "../main/BoardDef.h"
#include "Arduino.h"
/**
* @brief The class define how to handle the senseair S8 sensor (CO2 sensor)
*/
class S8 {
public:
const int S8_BAUDRATE =
@ -86,7 +89,7 @@ private:
#if defined(ESP32)
HardwareSerial *_serial;
#endif
bool _isInit = false;
bool _isBegin = false;
uint32_t _lastInitTime;
bool isCalib = false;
@ -95,7 +98,7 @@ private:
bool init(const BoardDef *bsp);
bool init(int txPin, int rxPin);
bool init(int txPin, int rxPin, uint32_t baud);
bool isInit(void);
bool isBegin(void);
void uartWriteBytes(uint8_t size); // Send bytes to sensor
uint8_t

View File

@ -7,6 +7,11 @@
#define vocAlgorithm() ((VOCGasIndexAlgorithm *)(this->_vocAlgorithm))
#define noxAlgorithm() ((NOxGasIndexAlgorithm *)(this->_noxAlgorithm))
/**
* @brief Construct a new Sgp 4 1:: Sgp 4 1 object
*
* @param type Board type @ref BoardType
*/
Sgp41::Sgp41(BoardType type) : _boardType(type) {}
/**
@ -18,9 +23,13 @@ Sgp41::Sgp41(BoardType type) : _boardType(type) {}
* @return false Failure
*/
bool Sgp41::begin(TwoWire &wire) {
if (this->_isInit) {
/** Ignore next step if initialized */
if (this->_isBegin) {
AgLog("Initialized, call end() then try again");
return true;
}
/** Check that board has supported this sensor */
if (this->boardSupported() == false) {
return false;
}
@ -55,8 +64,8 @@ bool Sgp41::begin(TwoWire &wire) {
conditioningCount = 0;
#endif
this->_isInit = true;
AgLog("Init");
this->_isBegin = true;
AgLog("Initialize");
return true;
}
@ -88,7 +97,11 @@ void Sgp41::handle(void) {
}
}
#else
#else
/**
* @brief Handle the sensor conditioning and run time udpate value, This method
* must not call, it's called on private task
*/
void Sgp41::_handle(void) {
/** NOx conditionning */
uint16_t err;
@ -115,18 +128,26 @@ void Sgp41::_handle(void) {
}
#endif
/**
* @brief De-Initialize sensor
*/
void Sgp41::end(void) {
if (this->_isInit == false) {
if (this->_isBegin == false) {
return;
}
#ifdef ESP32
vTaskDelete(pollTask);
#else
_debugStream = nullptr;
#endif
this->_isInit = false;
bsp = NULL;
this->_isBegin = false;
delete sgpSensor();
delete vocAlgorithm();
delete noxAlgorithm();
AgLog("De-Init");
AgLog("De-initialize");
}
/**
@ -153,6 +174,12 @@ int Sgp41::getNoxIndex(void) {
return nox;
}
/**
* @brief Check that board has supported sensor
*
* @return true Supported
* @return false Not-supported
*/
bool Sgp41::boardSupported(void) {
if (this->bsp == nullptr) {
this->bsp = getBoardDef(this->_boardType);
@ -165,25 +192,19 @@ bool Sgp41::boardSupported(void) {
return true;
}
int Sgp41::sdaPin(void) {
if (this->bsp) {
return this->bsp->I2C.sda_pin;
}
AgLog("sdaPin(): board not supported I2C");
return -1;
}
int Sgp41::sclPin(void) {
if (this->bsp) {
return this->bsp->I2C.scl_pin;
}
AgLog("sdlPin(): board not supported I2C");
return -1;
}
/**
* @brief Get raw signal
*
* @param raw_voc Raw VOC output
* @param row_nox Raw NOx output
* @param defaultRh
* @param defaultT
* @return true Success
* @return false Failure
*/
bool Sgp41::getRawSignal(uint16_t &raw_voc, uint16_t &row_nox,
uint16_t defaultRh, uint16_t defaultT) {
if (this->checkInit() == false) {
if (this->isBegin() == false) {
return false;
}
@ -195,43 +216,25 @@ bool Sgp41::getRawSignal(uint16_t &raw_voc, uint16_t &row_nox,
}
/**
* @brief This command turns the hotplate off and stops the measurement.
* Subsequently, the sensor enters the idle mode.
* @brief Check that sensor is initialized
*
* @return true Initialized
* @return false Not-initialized
*/
bool Sgp41::isBegin(void) {
if (this->_isBegin) {
return true;
}
AgLog("Sensor not-initialized");
return false;
}
/**
* @brief Handle nox conditioning process
*
* @return true Success
* @return false Failure
*/
bool Sgp41::turnHeaterOff(void) {
if (this->checkInit() == false) {
return false;
}
if (sgpSensor()->turnHeaterOff() == 0) {
return true;
}
return false;
}
bool Sgp41::getSerialNumber(uint16_t serialNumbers[],
uint8_t serialNumberSize) {
if (this->checkInit() == false) {
return false;
}
if (sgpSensor()->getSerialNumber(serialNumbers) == 0) {
return true;
}
return false;
}
bool Sgp41::checkInit(void) {
if (this->_isInit) {
return true;
}
AgLog("Sensor no-initialized");
return false;
}
bool Sgp41::_noxConditioning(void) {
uint16_t err;
uint16_t srawVoc;

View File

@ -1,10 +1,15 @@
#ifndef _AIR_GRADIENT_SGP4X_H_
#define _AIR_GRADIENT_SGP4X_H_
#include "../bsp/BoardDef.h"
#include "../main/BoardDef.h"
#include <Arduino.h>
#include <Wire.h>
/**
* @brief The class define how to handle Sensirion sensor SGP41 (VOC and NOx
* sensor)
*
*/
class Sgp41 {
public:
Sgp41(BoardType type);
@ -12,7 +17,7 @@ public:
#if defined(ESP8266)
bool begin(TwoWire &wire, Stream &stream);
void handle(void);
#else
#else
void _handle(void);
#endif
void end(void);
@ -22,7 +27,7 @@ public:
private:
bool onConditioning = true;
bool ready = false;
bool _isInit = false;
bool _isBegin = false;
void *_sensor;
void *_vocAlgorithm;
void *_noxAlgorithm;
@ -40,14 +45,10 @@ private:
#else
TaskHandle_t pollTask;
#endif
bool checkInit(void);
bool isBegin(void);
bool boardSupported(void);
int sdaPin(void);
int sclPin(void);
bool getRawSignal(uint16_t &raw_voc, uint16_t &raw_nox,
uint16_t defaultRh = 0x8000, uint16_t defaultT = 0x6666);
bool turnHeaterOff(void);
bool getSerialNumber(uint16_t serialNumbers[], uint8_t serialNumberSize);
bool _noxConditioning(void);
};

View File

@ -1,210 +0,0 @@
#include "sht4x.h"
#include "../library/SensirionSHT4x/src/SensirionI2CSht4x.h"
#define shtSensor() ((SensirionI2CSht4x *)(this->_sensor))
#if defined(ESP8266)
bool Sht::begin(TwoWire &wire, Stream &debugStream) {
this->_debugStream = &debugStream;
return this->begin(wire);
}
#else
#endif
Sht::Sht(BoardType type) : _boardType(type) {}
/**
* @brief Init sensor, Ifthis funciton not call the other funtion call will
* always return false or value invalid
*
* @param wire TwoWire instance, Must be initialized
* @return true Success
* @return false Failure
*/
bool Sht::begin(TwoWire &wire) {
if (this->_isInit) {
return true;
}
if (this->boardSupported() == false) {
return false;
}
this->_sensor = new SensirionI2CSht4x();
shtSensor()->begin(wire, SHT40_I2C_ADDR_44);
if (shtSensor()->softReset() != 0) {
AgLog("Reset sensor fail, look like sensor is not on I2C bus");
return false;
}
delay(10);
this->_isInit = true;
AgLog("Init");
return true;
}
void Sht::end(void) {
if (this->_isInit == false) {
return;
}
this->_isInit = false;
delete shtSensor();
}
/**
* @brief Get temperature degrees celsius
*
* @return float value <= 256.0f is invalid, That mean sensor has issue or
* communication to sensor not worked as well.
*/
float Sht::getTemperature(void) {
float temperature;
float humidity;
if (this->measureMediumPrecision(temperature, humidity)) {
return temperature;
}
return -256.0f;
}
/**
* @brief Get humidity
*
* @return float Percent(0 - 100), value < 0 is invalid.
*/
float Sht::getRelativeHumidity(void) {
float temperature;
float humidity;
if (this->measureMediumPrecision(temperature, humidity)) {
return humidity;
}
return -1.0f;
}
bool Sht::boardSupported(void) {
if (this->_bsp == NULL) {
this->_bsp = getBoardDef(this->_boardType);
}
if ((this->_bsp == NULL) || (this->_bsp->I2C.supported == false)) {
AgLog("Board not supported");
return false;
}
return true;
}
int Sht::sdaPin(void) {
if (this->_bsp) {
return this->_bsp->I2C.sda_pin;
}
AgLog("sdaPin(): board not supported I2C");
return -1;
}
int Sht::sclPin(void) {
if (this->_bsp) {
return this->_bsp->I2C.scl_pin;
}
AgLog("sdlPin(): board not supported I2C");
return -1;
}
bool Sht::checkInit(void) {
if (this->_isInit) {
return true;
}
AgLog("Sensor no-initialized");
return false;
}
bool Sht::measureHighPrecision(float &temperature, float &humidity) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->measureHighPrecision(temperature, humidity) == 0) {
return true;
}
return false;
}
bool Sht::measureMediumPrecision(float &temperature, float &humidity) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->measureMediumPrecision(temperature, humidity) == 0) {
return true;
}
return false;
}
bool Sht::measureLowestPrecision(float &temperature, float &humidity) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->measureLowestPrecision(temperature, humidity) == 0) {
return true;
}
return false;
}
bool Sht::activateHighestHeaterPowerShort(float &temperature, float &humidity) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->activateHighestHeaterPowerShort(temperature, humidity) ==
0) {
return true;
}
return false;
}
bool Sht::activateMediumHeaterPowerLong(float &temperature, float &humidity) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->activateMediumHeaterPowerLong(temperature, humidity) == 0) {
return true;
}
return false;
}
bool Sht::activateLowestHeaterPowerLong(float &temperature, float &humidity) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->activateLowestHeaterPowerLong(temperature, humidity) == 0) {
return true;
}
return false;
}
bool Sht::getSerialNumber(uint32_t &serialNumber) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->serialNumber(serialNumber) == 0) {
return true;
}
return false;
}
bool Sht::softReset(void) {
if (this->checkInit() == false) {
return false;
}
if (shtSensor()->softReset() == 0) {
return true;
}
return false;
}

165
src/sht4x/sht4x.cpp Normal file
View File

@ -0,0 +1,165 @@
#include "sht4x.h"
#include "../library/SensirionSHT4x/src/SensirionI2CSht4x.h"
/** Cast _sensor to SensirionI2CSht4x */
#define shtSensor() ((SensirionI2CSht4x *)(this->_sensor))
#if defined(ESP8266)
/**
* @brief Init sensor, Ifthis funciton not call the other funtion call will
* always return false or value invalid
*
* @param wire wire TwoWire instance, Must be initialized
* @param debugStream Point to debug Serial to print debug log
* @return true Sucecss
* @return false Failure
*/
bool Sht::begin(TwoWire &wire, Stream &debugStream) {
this->_debugStream = &debugStream;
return this->begin(wire);
}
#else
#endif
/**
* @brief Construct a new Sht:: Sht object
*
* @param type Board type @ref BoardType
*/
Sht::Sht(BoardType type) : _boardType(type) {}
/**
* @brief Init sensor, Ifthis funciton not call the other funtion call will
* always return false or value invalid
*
* @param wire TwoWire instance, Must be initialized
* @return true Success
* @return false Failure
*/
bool Sht::begin(TwoWire &wire) {
/** Ignore next step if sensor has intiialized */
if (this->_isBegin) {
AgLog("Initialized, call end() then try again");
return true;
}
/** Check sensor has supported on board */
if (this->boardSupported() == false) {
return false;
}
/** Create new SensirionI2CSht4x and init */
this->_sensor = new SensirionI2CSht4x();
shtSensor()->begin(wire, SHT40_I2C_ADDR_44);
if (shtSensor()->softReset() != 0) {
AgLog("Reset sensor fail, look like sensor is not on I2C bus");
return false;
}
delay(10);
this->_isBegin = true;
AgLog("Initialize");
return true;
}
/**
* @brief De-initialize SHT41 sensor
*
*/
void Sht::end(void) {
if (this->_isBegin == false) {
return;
}
this->_isBegin = false;
_bsp = NULL;
delete shtSensor();
#if defined(ESP8266)
_debugStream = nullptr;
#endif
AgLog("De-initialize");
}
/**
* @brief Get temperature degrees celsius
*
* @return float value <= 256.0f is invalid, That mean sensor has issue or
* communication to sensor not worked as well.
*/
float Sht::getTemperature(void) {
float temperature;
float humidity;
if (this->measureMediumPrecision(temperature, humidity)) {
return temperature;
}
return -256.0f;
}
/**
* @brief Get humidity
*
* @return float Percent(0 - 100), value < 0 is invalid.
*/
float Sht::getRelativeHumidity(void) {
float temperature;
float humidity;
if (this->measureMediumPrecision(temperature, humidity)) {
return humidity;
}
return -1.0f;
}
/**
* @brief Check sensor has supported by board
*
* @return true Supported
* @return false Not supported
*/
bool Sht::boardSupported(void) {
if (this->_bsp == NULL) {
this->_bsp = getBoardDef(this->_boardType);
}
if ((this->_bsp == NULL) || (this->_bsp->I2C.supported == false)) {
AgLog("Board not supported");
return false;
}
return true;
}
/**
* @brief Check that sensor has initialized
*
* @return true Initialized
* @return false Not-initialized
*/
bool Sht::isBegin(void) {
if (this->_isBegin) {
return true;
}
AgLog("Sensor not-initialized");
return false;
}
/**
* @brief Ge SHT41 temperature and humidity value with medium meaure precision
*
* @param temperature Read out temperarure
* @param humidity Read humidity
* @return true Success
* @return false Failure
*/
bool Sht::measureMediumPrecision(float &temperature, float &humidity) {
if (this->isBegin() == false) {
return false;
}
if (shtSensor()->measureMediumPrecision(temperature, humidity) == 0) {
return true;
}
return false;
}

View File

@ -4,8 +4,12 @@
#include <Arduino.h>
#include <Wire.h>
#include "../bsp/BoardDef.h"
#include "../main/BoardDef.h"
/**
* @brief The class with define how to handle the Sensirion sensor SHT41
* (temperature and humidity sensor).
*/
class Sht {
public:
#if defined(ESP8266)
@ -15,13 +19,12 @@ public:
Sht(BoardType type);
bool begin(TwoWire &wire);
void end(void);
float getTemperature(void);
float getRelativeHumidity(void);
private:
BoardType _boardType;
bool _isInit = false;
bool _isBegin = false; /** Flag indicate that sensor initialized or not */
void *_sensor;
const BoardDef *_bsp = NULL;
#if defined(ESP8266)
@ -29,21 +32,9 @@ private:
const char *TAG = "SHT4x";
#else
#endif
bool checkInit(void);
bool isBegin(void);
bool boardSupported(void);
int sdaPin(void);
int sclPin(void);
bool measureHighPrecision(float &temperature, float &humidity);
bool measureMediumPrecision(float &temperature, float &humidity);
bool measureLowestPrecision(float &temperature, float &humidity);
bool activateHighestHeaterPowerShort(float &temperature, float &humidity);
bool activateMediumHeaterPowerLong(float &temperature, float &humidity);
bool activateLowestHeaterPowerLong(float &temperature, float &humidity);
bool getSerialNumber(uint32_t &serialNumber);
bool softReset(void);
};
#endif /** _AIR_GRADIENT_SHT_H_ */