clarifying method/variable/class names

This commit is contained in:
Phat Nguyen
2024-04-07 16:39:01 +07:00
parent 9a03fb2bd7
commit 4577082731
21 changed files with 1372 additions and 1279 deletions

View File

@ -5,14 +5,16 @@
#define LED_SHORT_BLINK_DELAY 500 /** ms */
#define LED_LONG_BLINK_DELAY 2000 /** ms */
#define SENSOR_CO2_CALIB_COUNTDOWN_MAX 5 /** sec */
/**
* @brief Animation LED bar with color
*
* @param r
* @param g
* @param b
*
* @param r
* @param g
* @param b
*/
void AgStateMachine::ledBarSingleLedAnimation(uint8_t r, uint8_t g, uint8_t b) {
void StateMachine::ledBarSingleLedAnimation(uint8_t r, uint8_t g, uint8_t b) {
if (ledBarAnimationCount < 0) {
ledBarAnimationCount = 0;
ag->ledBar.setColor(r, g, b, ledBarAnimationCount);
@ -27,10 +29,10 @@ void AgStateMachine::ledBarSingleLedAnimation(uint8_t r, uint8_t g, uint8_t b) {
/**
* @brief LED status blink with delay
*
*
* @param ms Miliseconds
*/
void AgStateMachine::ledStatusBlinkDelay(uint32_t ms) {
void StateMachine::ledStatusBlinkDelay(uint32_t ms) {
ag->statusLed.setOn();
delay(ms);
ag->statusLed.setOff();
@ -39,15 +41,15 @@ void AgStateMachine::ledStatusBlinkDelay(uint32_t ms) {
/**
* @brief Led bar show led color status
*
*
*/
void AgStateMachine::sensorLedHandle(void) {
void StateMachine::sensorhandleLeds(void) {
switch (config.getLedBarMode()) {
case LedBarMode::LedBarModeCO2:
co2LedHandle();
co2handleLeds();
break;
case LedBarMode::LedBarModePm:
pm25LedHandle();
pm25handleLeds();
break;
default:
ag->ledBar.clear();
@ -57,9 +59,9 @@ void AgStateMachine::sensorLedHandle(void) {
/**
* @brief Show CO2 LED status
*
*
*/
void AgStateMachine::co2LedHandle(void) {
void StateMachine::co2handleLeds(void) {
int co2Value = value.CO2;
if (co2Value <= 400) {
/** G; 1 */
@ -140,10 +142,10 @@ void AgStateMachine::co2LedHandle(void) {
/**
* @brief Show PM2.5 LED status
*
*
*/
void AgStateMachine::pm25LedHandle(void) {
int pm25Value = value.PM25;
void StateMachine::pm25handleLeds(void) {
int pm25Value = value.pm25_1;
if (pm25Value <= 5) {
/** G; 1 */
ag->ledBar.setColor(0, 255, 0, ag->ledBar.getNumberOfLeds() - 1);
@ -221,29 +223,182 @@ void AgStateMachine::pm25LedHandle(void) {
}
}
void StateMachine::co2Calibration(void) {
if (config.isCo2CalibrationRequested() && config.hasSensorS8) {
logInfo("CO2 Calibration");
/** Count down to 0 then start */
for (int i = 0; i < SENSOR_CO2_CALIB_COUNTDOWN_MAX; i++) {
if (ag->isOne()) {
String str =
"after " + String(SENSOR_CO2_CALIB_COUNTDOWN_MAX - i) + " sec";
disp.setText("Start CO2 calib", str.c_str(), "");
} else {
logInfo("Start CO2 calib after " +
String(SENSOR_CO2_CALIB_COUNTDOWN_MAX - i) + " sec");
}
delay(1000);
}
if (ag->s8.setBaselineCalibration()) {
if (ag->isOne()) {
disp.setText("Calibration", "success", "");
} else {
logInfo("CO2 Calibration: success");
}
delay(1000);
if (ag->isOne()) {
disp.setText("Wait for", "calib finish", "...");
} else {
logInfo("CO2 Calibration: Wait for calibration finish...");
}
/** Count down wait for finish */
int count = 0;
while (ag->s8.isBaseLineCalibrationDone() == false) {
delay(1000);
count++;
}
if (ag->isOne()) {
String str = "after " + String(count);
disp.setText("Calib finish", str.c_str(), "sec");
} else {
logInfo("CO2 Calibration: finish after " + String(count) + " sec");
}
delay(2000);
} else {
if (ag->isOne()) {
disp.setText("Calibration", "failure!!!", "");
} else {
logInfo("CO2 Calibration: failure!!!");
}
delay(2000);
}
}
if (config.getCO2CalibrationAbcDays() > 0 && config.hasSensorS8) {
int newHour = config.getCO2CalibrationAbcDays() * 24;
logInfo("Requested abcDays setting: " +
String(config.getCO2CalibrationAbcDays()) + "days (" +
String(newHour) + "hours)");
int curHour = ag->s8.getAbcPeriod();
logInfo("Current S8 abcDays setting: " + String(curHour) + "(hours)");
if (curHour == newHour) {
logInfo("'abcDays' unchanged");
} else {
if (ag->s8.setAbcPeriod(config.getCO2CalibrationAbcDays() * 24) ==
false) {
logError("Set S8 abcDays period failed");
} else {
logInfo("Set S8 abcDays period success");
}
}
} else {
logWarning("CO2 S8 not available, set 'abcDays' ignored");
}
}
void StateMachine::ledBarTest(void) {
if (config.isLedBarTestRequested()) {
if (config.getCountry() == "TH") {
uint32_t tstart = millis();
logInfo("Start run LED test for 2 min");
while (1) {
ledBarRunTest();
uint32_t ms = (uint32_t)(millis() - tstart);
if (ms >= (60 * 1000 * 2)) {
logInfo("LED test after 2 min finish");
break;
}
}
} else {
ledBarRunTest();
}
}
}
void StateMachine::ledBarRunTest(void) {
disp.setText("LED Test", "running", ".....");
runLedTest('r');
ag->ledBar.show();
delay(1000);
runLedTest('g');
ag->ledBar.show();
delay(1000);
runLedTest('b');
ag->ledBar.show();
delay(1000);
runLedTest('w');
ag->ledBar.show();
delay(1000);
runLedTest('n');
ag->ledBar.show();
delay(1000);
}
void StateMachine::runLedTest(char color) {
int r = 0;
int g = 0;
int b = 0;
switch (color) {
case 'g':
g = 255;
break;
case 'y':
r = 255;
g = 255;
break;
case 'o':
r = 255;
g = 128;
break;
case 'r':
r = 255;
break;
case 'b':
b = 255;
break;
case 'w':
r = 255;
g = 255;
b = 255;
break;
case 'p':
r = 153;
b = 153;
break;
case 'z':
r = 102;
break;
case 'n':
default:
break;
}
ag->ledBar.setColor(r, g, b);
}
/**
* @brief Construct a new Ag State Machine:: Ag State Machine object
*
* @param disp AgOledDisplay
*
* @param disp OledDisplay
* @param log Serial Stream
* @param value AgValue
* @param config AgConfigure
* @param value Measurements
* @param config Configuration
*/
AgStateMachine::AgStateMachine(AgOledDisplay &disp, Stream &log, AgValue &value,
AgConfigure &config)
: PrintLog(log, "AgStateMachine"), disp(disp), value(value),
config(config) {}
StateMachine::StateMachine(OledDisplay &disp, Stream &log, Measurements &value,
Configuration &config)
: PrintLog(log, "StateMachine"), disp(disp), value(value), config(config) {}
AgStateMachine::~AgStateMachine() {}
StateMachine::~StateMachine() {}
/**
* @brief OLED display show content from state value
*
* @param state
*
* @param state
*/
void AgStateMachine::displayHandle(AgStateMachineState state) {
void StateMachine::displayHandle(AgStateMachineState state) {
// Ignore handle if not ONE_INDOOR board
if (!ag->isOneIndoor()) {
if (!ag->isOne()) {
return;
}
@ -320,6 +475,9 @@ void AgStateMachine::displayHandle(AgStateMachineState state) {
disp.showDashboard();
break;
}
case AgStateMachineCo2Calibration:
co2Calibration();
break;
default:
break;
}
@ -327,51 +485,49 @@ void AgStateMachine::displayHandle(AgStateMachineState state) {
/**
* @brief OLED display show content as previous state updated
*
*
*/
void AgStateMachine::displayHandle(void) { displayHandle(dispState); }
void StateMachine::displayHandle(void) { displayHandle(dispState); }
/**
* @brief Update status add to dashboard
*
*
*/
void AgStateMachine::displaySetAddToDashBoard(void) {
void StateMachine::displaySetAddToDashBoard(void) {
addToDashBoard = true;
addToDashboardTime = millis();
}
void AgStateMachine::displayClearAddToDashBoard(void) {
addToDashBoard = false;
}
void StateMachine::displayClearAddToDashBoard(void) { addToDashBoard = false; }
/**
* @brief Set WiFi connection coundown on dashboard
*
*
* @param count Seconds
*/
void AgStateMachine::displayWiFiConnectCountDown(int count) {
void StateMachine::displayWiFiConnectCountDown(int count) {
wifiConnectCountDown = count;
}
/**
* @brief Init before start LED bar animation
*
*
*/
void AgStateMachine::ledAnimationInit(void) { ledBarAnimationCount = -1; }
void StateMachine::ledAnimationInit(void) { ledBarAnimationCount = -1; }
/**
* @brief Handle LED from state
*
* @param state
*
* @param state
*/
void AgStateMachine::ledHandle(AgStateMachineState state) {
void StateMachine::handleLeds(AgStateMachineState state) {
if (state > AgStateMachineNormal) {
logError("ledHandle: state invalid");
return;
}
ledState = state;
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.clear(); // Set all LED OFF
}
switch (state) {
@ -379,7 +535,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
/** In WiFi Manager Mode */
/** Turn LED OFF */
/** Turn midle LED Color */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(0, 0, 255, ag->ledBar.getNumberOfLeds() / 2);
} else {
ag->statusLed.setToggle();
@ -388,7 +544,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
}
case AgStateMachineWiFiManagerPortalActive: {
/** WiFi Manager has connected to mobile phone */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(0, 0, 255);
} else {
ag->statusLed.setOn();
@ -398,7 +554,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
case AgStateMachineWiFiManagerStaConnecting: {
/** after SSID and PW entered and OK clicked, connection to WiFI network is
* attempted */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ledBarSingleLedAnimation(255, 255, 255);
} else {
ag->statusLed.setOff();
@ -407,7 +563,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
}
case AgStateMachineWiFiManagerStaConnected: {
/** Connecting to WiFi worked */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(255, 255, 255);
} else {
ag->statusLed.setOff();
@ -416,7 +572,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
}
case AgStateMachineWiFiOkServerConnecting: {
/** once connected to WiFi an attempt to reach the server is performed */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ledBarSingleLedAnimation(0, 255, 0);
} else {
ag->statusLed.setOff();
@ -425,7 +581,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
}
case AgStateMachineWiFiOkServerConnected: {
/** Server is reachable, all fine */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(0, 255, 0);
} else {
ag->statusLed.setOff();
@ -441,7 +597,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
}
case AgStateMachineWiFiManagerConnectFailed: {
/** Cannot connect to WiFi (e.g. wrong password, WPA Enterprise etc.) */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(255, 0, 0);
} else {
ag->statusLed.setOff();
@ -459,7 +615,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
case AgStateMachineWiFiOkServerConnectFailed: {
/** Connected to WiFi but server not reachable, e.g. firewall block/
* whitelisting needed etc. */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(233, 183, 54); /** orange */
} else {
ag->statusLed.setOff();
@ -475,7 +631,7 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
}
case AgStateMachineWiFiOkServerOkSensorConfigFailed: {
/** Server reachable but sensor not configured correctly */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(139, 24, 248); /** violet */
} else {
ag->statusLed.setOff();
@ -492,12 +648,12 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
case AgStateMachineWiFiLost: {
/** Connection to WiFi network failed credentials incorrect encryption not
* supported etc. */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
/** WIFI failed status LED color */
ag->ledBar.setColor(255, 0, 0, 0);
/** Show CO2 or PM color status */
// sensorLedColorHandler();
sensorLedHandle();
sensorhandleLeds();
} else {
ag->statusLed.setOff();
}
@ -506,11 +662,11 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
case AgStateMachineServerLost: {
/** Connected to WiFi network but the server cannot be reached through the
* internet, e.g. blocked by firewall */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(233, 183, 54, 0);
/** Show CO2 or PM color status */
sensorLedHandle();
sensorhandleLeds();
// sensorLedColorHandler();
} else {
ag->statusLed.setOff();
@ -520,66 +676,77 @@ void AgStateMachine::ledHandle(AgStateMachineState state) {
case AgStateMachineSensorConfigFailed: {
/** Server is reachable but there is some configuration issue to be fixed on
* the server side */
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.setColor(139, 24, 248, 0);
/** Show CO2 or PM color status */
sensorLedHandle();
sensorhandleLeds();
} else {
ag->statusLed.setOff();
}
break;
}
case AgStateMachineNormal: {
if (ag->isOneIndoor()) {
sensorLedHandle();
if (ag->isOne()) {
sensorhandleLeds();
} else {
ag->statusLed.setOff();
}
break;
}
case AgStateMachineLedBarTest:
ledBarTest();
break;
default:
break;
}
// Show LED bar color
if (ag->isOneIndoor()) {
if (ag->isOne()) {
ag->ledBar.show();
}
}
/**
* @brief Handle LED as previous state updated
*
*
*/
void AgStateMachine::ledHandle(void) { ledHandle(ledState); }
void StateMachine::handleLeds(void) { handleLeds(ledState); }
/**
* @brief Set display state
*
* @param state
*
* @param state
*/
void AgStateMachine::setDisplayState(AgStateMachineState state) {
void StateMachine::setDisplayState(AgStateMachineState state) {
dispState = state;
}
/**
* @brief Get current display state
*
* @return AgStateMachineState
*
* @return AgStateMachineState
*/
AgStateMachineState AgStateMachine::getDisplayState(void) { return dispState; }
AgStateMachineState StateMachine::getDisplayState(void) { return dispState; }
/**
* @brief Set AirGradient instance
*
*
* @param ag Point to AirGradient instance
*/
void AgStateMachine::setAirGradient(AirGradient *ag) { this->ag = ag; }
void StateMachine::setAirGradient(AirGradient *ag) { this->ag = ag; }
/**
* @brief Get current LED state
*
* @return AgStateMachineState
*
* @return AgStateMachineState
*/
AgStateMachineState AgStateMachine::getLedState(void) { return ledState; }
AgStateMachineState StateMachine::getLedState(void) { return ledState; }
void StateMachine::executeCo2Calibration(void) {
displayHandle(AgStateMachineCo2Calibration);
}
void StateMachine::executeLedBarTest(void) {
handleLeds(AgStateMachineLedBarTest);
}