diff --git a/examples/ChangeI2CAddress/ChangeI2CAddress.ino b/examples/ChangeI2CAddress/ChangeI2CAddress.ino index f5116af..cb1d14b 100644 --- a/examples/ChangeI2CAddress/ChangeI2CAddress.ino +++ b/examples/ChangeI2CAddress/ChangeI2CAddress.ino @@ -35,12 +35,11 @@ void setup() { Serial.print("🔧 Changing device address to 0x"); Serial.print(customI2CAddress, HEX); Serial.println("..."); - device.setDeviceAddress(customI2CAddress); + + // Setting the second parameter to true makes the change persistent + device.setDeviceAddress(customI2CAddress, true); checkConnection(device); - // Store the new address in flash - device.persistSettings(); - Serial.println("🔄 Resetting device to check if change is persistent..."); device.reset(); delay(2000); // Wait for the device to reset diff --git a/examples/UARTRead/UARTRead.ino b/examples/UARTRead/UARTRead.ino index 36f0323..5454d80 100644 --- a/examples/UARTRead/UARTRead.ino +++ b/examples/UARTRead/UARTRead.ino @@ -12,8 +12,8 @@ * * NiclaSenseEnv device; * device.begin(); - * device.setUARTCSVOutputEnabled(true); - * device.persistSettings() # Store the settings so they are not lost after a reset + * The second parameter ensures that the settings are not lost after a reset + * device.setUARTCSVOutputEnabled(true, true); * * Initial author: Sebastian Romero (s.romero@arduino.cc) * diff --git a/src/IndoorAirQualitySensor.cpp b/src/IndoorAirQualitySensor.cpp index eaaa7c7..49b77cc 100644 --- a/src/IndoorAirQualitySensor.cpp +++ b/src/IndoorAirQualitySensor.cpp @@ -52,7 +52,7 @@ IndoorAirQualitySensorMode IndoorAirQualitySensor::mode() { return IndoorAirQualitySensorMode((data >> 1) & 7); } -void IndoorAirQualitySensor::setMode(IndoorAirQualitySensorMode sensorMode) { +bool IndoorAirQualitySensor::setMode(IndoorAirQualitySensorMode sensorMode, bool persist) { uint8_t currentRegisterData = readFromRegister(STATUS_REGISTER_INFO); uint8_t mode = static_cast(sensorMode); // convert to numeric type @@ -60,7 +60,15 @@ void IndoorAirQualitySensor::setMode(IndoorAirQualitySensorMode sensorMode) { if ((currentRegisterData & (7 << 1)) == (mode << 1)) { return; } - writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(7 << 1)) | (mode << 1)); + if(!writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(7 << 1)) | (mode << 1))){ + return false; + } + + if(persist){ + return persistRegister(STATUS_REGISTER_INFO); + } + + return true; } String IndoorAirQualitySensor::modeString() { @@ -85,13 +93,11 @@ bool IndoorAirQualitySensor::enabled() { return mode() != IndoorAirQualitySensorMode::powerDown; } -void IndoorAirQualitySensor::setEnabled(bool isEnabled) { +bool IndoorAirQualitySensor::setEnabled(bool isEnabled, bool persist) { if (isEnabled == enabled()) { return; } - if (isEnabled) { - setMode(IndoorAirQualitySensorMode::defaultMode); - } else { - setMode(IndoorAirQualitySensorMode::powerDown); - } + + auto mode = isEnabled ? IndoorAirQualitySensorMode::indoorAirQuality : IndoorAirQualitySensorMode::powerDown; + return setMode(mode, persist); } diff --git a/src/IndoorAirQualitySensor.h b/src/IndoorAirQualitySensor.h index 1f01389..4bdad87 100644 --- a/src/IndoorAirQualitySensor.h +++ b/src/IndoorAirQualitySensor.h @@ -97,7 +97,6 @@ class IndoorAirQualitySensor : public I2CDevice { /** * @brief Set the mode of the IndoorAirQualitySensor. - * Call persistSettings() on NiclaSenseEnv instance after changing the mode to make the change persistent. * * Note on cleaning mode: * The cleaning mode performs a thermal cleaning cycle of the MOx element. It can eliminate some light pollution @@ -117,8 +116,11 @@ class IndoorAirQualitySensor : public I2CDevice { * For more accurate readings, use the default indoor air quality mode. * * @param sensorMode The mode to set. See the IndoorAirQualitySensorMode enum class for possible values. + * @param persist If true, the change will be saved to flash memory. + * When persist is true, the mode setting of OutdoorAirQualitySensor and TemperatureHumiditySensor will also be persisted. + * */ - void setMode(IndoorAirQualitySensorMode sensorMode); + bool setMode(IndoorAirQualitySensorMode sensorMode, bool persist); /** * @brief Get the mode as a string. @@ -136,10 +138,12 @@ class IndoorAirQualitySensor : public I2CDevice { /** * @brief Set the sensor enabled or disabled. - * Call persistSettings() on NiclaSenseEnv instance after changing the enabled state to make the change persistent. + * When the sensor is enabled after being disabled, the sensor will go back to the indoorAirQuality mode. * @param isEnabled True to enable the sensor, false to disable it. + * @param persist If true, the change will be saved to flash memory. + * When persist is true, the mode setting of IndoorAirQualitySensor and TemperatureHumiditySensor will also be persisted. */ - void setEnabled(bool isEnabled); + bool setEnabled(bool isEnabled, bool persist = false); }; #endif \ No newline at end of file diff --git a/src/NiclaSenseEnv.cpp b/src/NiclaSenseEnv.cpp index c2ef8e2..a15b48e 100644 --- a/src/NiclaSenseEnv.cpp +++ b/src/NiclaSenseEnv.cpp @@ -152,7 +152,7 @@ int NiclaSenseEnv::UARTBaudRate() { return baudRateMap[uartControlRegisterData]; } -void NiclaSenseEnv::setUARTBaudRate(int baudRate) { +bool NiclaSenseEnv::setUARTBaudRate(int baudRate, bool persist) { int baudRateIndex = baudRateNativeValue(baudRate); if (baudRateIndex == -1) { return; // Baud rate not found @@ -162,7 +162,15 @@ void NiclaSenseEnv::setUARTBaudRate(int baudRate) { if ((uartControlRegisterData & 7) == baudRateIndex) { return; // Value is already the same } - writeToRegister(UART_CONTROL_REGISTER_INFO, (uartControlRegisterData & ~7) | baudRateIndex); + if(!writeToRegister(UART_CONTROL_REGISTER_INFO, (uartControlRegisterData & ~7) | baudRateIndex)){ + return false; + } + + if (persist) { + return persistRegister(UART_CONTROL_REGISTER_INFO); + } + + return true; } bool NiclaSenseEnv::isUARTCSVOutputEnabled() { @@ -170,12 +178,20 @@ bool NiclaSenseEnv::isUARTCSVOutputEnabled() { return (boardControlRegisterData & (1 << 1)) != 0; } -void NiclaSenseEnv::setUARTCSVOutputEnabled(bool enabled) { +bool NiclaSenseEnv::setUARTCSVOutputEnabled(bool enabled, bool persist) { uint8_t boardControlRegisterData = readFromRegister(CONTROL_REGISTER_INFO); if ((boardControlRegisterData & 2) == static_cast(enabled)) { return; // Value is already the same } - writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~2) | (enabled << 1)); + if(!writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~2) | (enabled << 1))){ + return false; + } + + if (persist) { + return persistRegister(CONTROL_REGISTER_INFO); + } + + return true; } char NiclaSenseEnv::CSVDelimiter() { @@ -183,7 +199,7 @@ char NiclaSenseEnv::CSVDelimiter() { return static_cast(csvDelimiterRegisterData); } -void NiclaSenseEnv::setCSVDelimiter(char delimiter) { +bool NiclaSenseEnv::setCSVDelimiter(char delimiter, bool persist) { char currentDelimiter = CSVDelimiter(); if (currentDelimiter == delimiter) { return; // Value is already the same @@ -199,7 +215,15 @@ void NiclaSenseEnv::setCSVDelimiter(char delimiter) { } // Use ASCII code of the delimiter character - writeToRegister(CSV_DELIMITER_REGISTER_INFO, static_cast(delimiter)); + if(!writeToRegister(CSV_DELIMITER_REGISTER_INFO, static_cast(delimiter))){ + return false; + } + + if (persist) { + return persistRegister(CSV_DELIMITER_REGISTER_INFO); + } + + return true; } bool NiclaSenseEnv::isDebuggingEnabled() { @@ -207,15 +231,23 @@ bool NiclaSenseEnv::isDebuggingEnabled() { return (boardControlRegisterData & 1) != 0; } -void NiclaSenseEnv::setDebuggingEnabled(bool enabled) { +bool NiclaSenseEnv::setDebuggingEnabled(bool enabled, bool persist) { uint8_t boardControlRegisterData = readFromRegister(CONTROL_REGISTER_INFO); if ((boardControlRegisterData & 1) == static_cast(enabled)) { return; // Value is already the same } - writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~1) | enabled); + if(!writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~1) | enabled)){ + return false; + } + + if(persist){ + return persistRegister(CONTROL_REGISTER_INFO); + } + + return true; } -void NiclaSenseEnv::setDeviceAddress(int address) { +bool NiclaSenseEnv::setDeviceAddress(int address, bool persist) { if (address < 0 || address > 127) { return; // Invalid address } @@ -224,9 +256,18 @@ void NiclaSenseEnv::setDeviceAddress(int address) { if ((addressRegisterData & 127) == address) { return; // Value is already the same } - writeToRegister(SLAVE_ADDRESS_REGISTER_INFO, (addressRegisterData & ~127) | address); + if(!writeToRegister(SLAVE_ADDRESS_REGISTER_INFO, (addressRegisterData & ~127) | address)){ + return false; + } + delayMicroseconds(100); // Wait for the new address to take effect this->i2cDeviceAddress = address; + + if (persist) { + return persistRegister(SLAVE_ADDRESS_REGISTER_INFO); + } + + return true; } // Function to get the index for a given baud rate diff --git a/src/NiclaSenseEnv.h b/src/NiclaSenseEnv.h index 2e8b29e..debbb46 100644 --- a/src/NiclaSenseEnv.h +++ b/src/NiclaSenseEnv.h @@ -163,11 +163,11 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Sets the baud rate for the UART communication. - * Call persistSettings() on NiclaSenseEnv instance after changing the baud rate to make the change persistent. * @param baudRate The desired baud rate for the UART communication. The supported values are: 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 + * @param persist Set to true to store the setting in flash, to false otherwise. */ - void setUARTBaudRate(int baudRate); + bool setUARTBaudRate(int baudRate, bool persist); /** * @brief Checks if UART CSV output is enabled. @@ -179,7 +179,6 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Sets the UART CSV output enabled or disabled. * Enables or disables CSV output over UART. - * Call persistSettings() on NiclaSenseEnv instance after changing the CSV output mode to make the change persistent. * * The column names and their order are: * HS4001 sample counter, HS4001 temperature (degC), HS4001 humidity (%RH), ZMOD4510 status, ZMOD4510 sample counter, @@ -193,8 +192,10 @@ class NiclaSenseEnv : public I2CDevice { * Only the columns for this sensor will be filled, the other columns will be empty. * * @param enabled True to enable UART CSV output, false to disable. + * @param persist True to store the setting in flash, false otherwise. + * When set to True, it will also persist the value set via `setDebuggingEnabled`. */ - void setUARTCSVOutputEnabled(bool enabled); + bool setUARTCSVOutputEnabled(bool enabled, bool persist = false); /** * @brief Gets the CSV delimiter character. @@ -205,10 +206,10 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Sets the CSV delimiter for parsing CSV data. - * Call persistSettings() on NiclaSenseEnv instance after changing the CSV delimiter to make the change persistent. * @param delimiter The character to be used as the CSV delimiter. + * @param persist If true, the change will be saved to flash memory. */ - void setCSVDelimiter(char delimiter); + bool setCSVDelimiter(char delimiter, bool persist = false); /** * @brief Checks if debugging is enabled. @@ -220,18 +221,19 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Toggles the debugging mode. * When debugging mode is enabled, the board will send additional debug messages over UART. - * Call persistSettings() on NiclaSenseEnv instance after changing the debugging mode to make the change persistent. * @param enabled A boolean value indicating whether debugging is enabled or not. + * @param persist If true, the change will be saved to flash memory. + * When setting this to true the value set via `setUARTCSVOutputEnabled` will also be persisted. */ - void setDebuggingEnabled(bool enabled); + bool setDebuggingEnabled(bool enabled, bool persist = false); /** * @brief Sets the I2C address of the device. - * Call persistSettings() on NiclaSenseEnv instance after changing the address to make the change persistent. * * @param address The new I2C address. Valid values are 0 to 127. + * @param persist If true, the change will be saved to flash memory. */ - void setDeviceAddress(int address); + bool setDeviceAddress(int address, bool persist = false); private: /** diff --git a/src/OrangeLED.cpp b/src/OrangeLED.cpp index e28937b..3eacc3c 100644 --- a/src/OrangeLED.cpp +++ b/src/OrangeLED.cpp @@ -11,7 +11,7 @@ uint8_t OrangeLED::brightness() { return map(brightness, 0, 63, 0, 255); } -void OrangeLED::setBrightness(uint8_t brightness) { +bool OrangeLED::setBrightness(uint8_t brightness, bool persist) { if (brightness > 255) { return; // Invalid brightness value } @@ -20,6 +20,12 @@ void OrangeLED::setBrightness(uint8_t brightness) { uint8_t currentRegisterData = readFromRegister(ORANGE_LED_REGISTER_INFO); // Overwrite bits 0 - 5 with the new value writeToRegister(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | mappedBrightness); + + if (persist) { + return persistRegister(ORANGE_LED_REGISTER_INFO); + } + + return true; } bool OrangeLED::errorStatusEnabled() { @@ -28,8 +34,14 @@ bool OrangeLED::errorStatusEnabled() { return data & (1 << 7); } -void OrangeLED::setErrorStatusEnabled(bool enabled) { +bool OrangeLED::setErrorStatusEnabled(bool enabled, bool persist) { uint8_t currentRegisterData = readFromRegister(ORANGE_LED_REGISTER_INFO); // Set bit 7 to 1 if enabled or 0 if disabled while keeping the other bits unchanged writeToRegister(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~(1 << 7)) | (enabled << 7)); + + if (persist) { + return persistRegister(ORANGE_LED_REGISTER_INFO); + } + + return true; } diff --git a/src/OrangeLED.h b/src/OrangeLED.h index 2db7196..6fbd427 100644 --- a/src/OrangeLED.h +++ b/src/OrangeLED.h @@ -32,10 +32,11 @@ class OrangeLED : public I2CDevice { /** * Sets the brightness of the orange LED. - * Call persistSettings() on NiclaSenseEnv instance after changing the orange LED brightness to make the change persistent. - * @param brightness : The brightness of the orange LED. Range is 0 to 63. + * When persist is true, the `errorStatusEnabled` setting will also be persisted. + * @param brightness : The brightness of the orange LED. Range is 0 to 255. + * @param persist : If true, the brightness setting will be saved to flash memory. */ - void setBrightness(uint8_t brightness = 63); + bool setBrightness(uint8_t brightness, bool persist = false); /** * Determines whether the orange LED is used to indicate an error status of one of the sensors. @@ -46,8 +47,9 @@ class OrangeLED : public I2CDevice { /** * Enables or disables the orange LED to indicate an error status of one of the sensors. - * Call persistSettings() on NiclaSenseEnv instance after enabling/disabling the orange LED error status to make the change persistent. + * When persist is true, the brightness setting will also be saved to flash memory. * @param enabled : Whether to enable or disable the orange LED error status. + * @param persist : If true, the change will be saved to flash memory. */ - void setErrorStatusEnabled(bool enabled); + bool setErrorStatusEnabled(bool enabled, bool persist = false); }; diff --git a/src/OutdoorAirQualitySensor.cpp b/src/OutdoorAirQualitySensor.cpp index 05adb34..adcf5af 100644 --- a/src/OutdoorAirQualitySensor.cpp +++ b/src/OutdoorAirQualitySensor.cpp @@ -43,7 +43,7 @@ OutdoorAirQualitySensorMode OutdoorAirQualitySensor::mode() { return OutdoorAirQualitySensorMode((data >> 4) & 3); } -void OutdoorAirQualitySensor::setMode(OutdoorAirQualitySensorMode sensorMode) { +bool OutdoorAirQualitySensor::setMode(OutdoorAirQualitySensorMode sensorMode, bool persist) { uint8_t currentRegisterData = readFromRegister(STATUS_REGISTER_INFO); uint8_t mode = static_cast(sensorMode); // convert to numeric type @@ -53,7 +53,15 @@ void OutdoorAirQualitySensor::setMode(OutdoorAirQualitySensorMode sensorMode) { } // Overwrite bits 4 and 5 with the new value - writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(3 << 4)) | (mode << 4)); + if(!writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(3 << 4)) | (mode << 4))){ + return false; + } + + if(persist){ + return persistRegister(STATUS_REGISTER_INFO); + } + + return true; } String OutdoorAirQualitySensor::modeString() { @@ -73,14 +81,12 @@ bool OutdoorAirQualitySensor::enabled() { return mode() != OutdoorAirQualitySensorMode::powerDown; } -void OutdoorAirQualitySensor::setEnabled(bool isEnabled) { +bool OutdoorAirQualitySensor::setEnabled(bool isEnabled, bool persist) { // Ignore the request if the sensor is already in the desired state to maintain the current mode if (isEnabled == enabled()) { return; } - if (isEnabled) { - setMode(OutdoorAirQualitySensorMode::outdoorAirQuality); - } else { - setMode(OutdoorAirQualitySensorMode::powerDown); - } + + auto mode = isEnabled ? OutdoorAirQualitySensorMode::outdoorAirQuality : OutdoorAirQualitySensorMode::powerDown; + return setMode(mode, persist); } diff --git a/src/OutdoorAirQualitySensor.h b/src/OutdoorAirQualitySensor.h index 357e3c8..a4763a8 100644 --- a/src/OutdoorAirQualitySensor.h +++ b/src/OutdoorAirQualitySensor.h @@ -95,7 +95,6 @@ class OutdoorAirQualitySensor : public I2CDevice { * This function allows you to set the mode of the outdoor air quality sensor. * Possible values are: powerDown, cleaning, outdoorAirQuality. * See OutdoorAirQualitySensorMode for more information. - * Call persistSettings() on NiclaSenseEnv instance after changing the mode to make the change persistent. * * Note on cleaning mode: * The cleaning mode performs a thermal cleaning cycle of the MOx element. It can eliminate some light pollution @@ -106,8 +105,10 @@ class OutdoorAirQualitySensor : public I2CDevice { * The cleaning procedure takes 1 minute (blocking). * * @param sensorMode The mode to set for the sensor. + * @param persist If true, the change will be saved to flash memory. + * When persist is true, the mode setting of IndoorAirQualitySensor and TemperatureHumiditySensor will also be persisted. */ - void setMode(OutdoorAirQualitySensorMode sensorMode); + bool setMode(OutdoorAirQualitySensorMode sensorMode, bool persist = false); /** * @brief Gets the outdoor air quality sensor mode as a string. @@ -127,10 +128,11 @@ class OutdoorAirQualitySensor : public I2CDevice { * @brief Sets the enabled state of the outdoor air quality sensor. * When disabled the sensor goes in power down mode. * When the sensor is enabled after being disabled, the sensor will go back to the outdoorAirQuality mode. - * Call persistSettings() on NiclaSenseEnv instance after changing the enabled state to make the change persistent. * @param isEnabled True to enable the sensor, false to disable it. + * @param persist If true, the change will be saved to flash memory. + * When persist is true, the mode setting of IndoorAirQualitySensor and TemperatureHumiditySensor will also be persisted. */ - void setEnabled(bool isEnabled); + bool setEnabled(bool isEnabled, bool persist = false); }; #endif \ No newline at end of file diff --git a/src/RGBLED.cpp b/src/RGBLED.cpp index 07addc3..baf34dc 100644 --- a/src/RGBLED.cpp +++ b/src/RGBLED.cpp @@ -5,34 +5,36 @@ RGBLED::RGBLED(TwoWire& bus, uint8_t deviceAddress) : I2CDevice(bus, deviceAddre RGBLED::RGBLED(uint8_t deviceAddress) : I2CDevice(deviceAddress) {} -void RGBLED::enableIndoorAirQualityStatus() { - enableIndoorAirQualityStatus(255); // Default maximum brightness +bool RGBLED::enableIndoorAirQualityStatus(uint8_t brightness, bool persist) { + return setColor(0, 0, 0, brightness, persist); } -void RGBLED::enableIndoorAirQualityStatus(uint8_t brightness) { - writeToRegister(RGB_LED_RED_REGISTER_INFO, 0); - writeToRegister(RGB_LED_GREEN_REGISTER_INFO, 0); - writeToRegister(RGB_LED_BLUE_REGISTER_INFO, 0); - writeToRegister(INTENSITY_REGISTER_INFO, brightness); +bool RGBLED::setColor(uint8_t r, uint8_t g, uint8_t b, bool persist) { + return setColor(r, g, b, 255, persist); // Default maximum brightness } -void RGBLED::setColor(uint8_t r, uint8_t g, uint8_t b) { - setColor(r, g, b, 255); // Default maximum brightness -} +bool RGBLED::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness, bool persist) { + if(!writeToRegister(RGB_LED_RED_REGISTER_INFO, r)) return false; + if(!writeToRegister(RGB_LED_GREEN_REGISTER_INFO, g)) return false; + if(!writeToRegister(RGB_LED_BLUE_REGISTER_INFO, b)) return false; + if(!writeToRegister(INTENSITY_REGISTER_INFO, brightness)) return false; + + if (persist) { + return persistRegister(RGB_LED_RED_REGISTER_INFO) && + persistRegister(RGB_LED_GREEN_REGISTER_INFO) && + persistRegister(RGB_LED_BLUE_REGISTER_INFO) && + persistRegister(INTENSITY_REGISTER_INFO); + } -void RGBLED::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness) { - writeToRegister(RGB_LED_RED_REGISTER_INFO, r); - writeToRegister(RGB_LED_GREEN_REGISTER_INFO, g); - writeToRegister(RGB_LED_BLUE_REGISTER_INFO, b); - writeToRegister(INTENSITY_REGISTER_INFO, brightness); + return true; } -void RGBLED::setColor(Color color) { - setColor(color, 255); // Default maximum brightness +bool RGBLED::setColor(Color color, bool persist) { + return setColor(color, 255, persist); // Default maximum brightness } -void RGBLED::setColor(Color color, uint8_t brightness) { - setColor(color.red, color.green, color.blue, brightness); +bool RGBLED::setColor(Color color, uint8_t brightness, bool persist) { + return setColor(color.red, color.green, color.blue, brightness, persist); } Color RGBLED::color() { @@ -46,6 +48,12 @@ uint8_t RGBLED::brightness() { return readFromRegister(INTENSITY_REGISTER_INFO); } -void RGBLED::setBrightness(uint8_t brightness) { - writeToRegister(INTENSITY_REGISTER_INFO, brightness); +bool RGBLED::setBrightness(uint8_t brightness, bool persist) { + if(!writeToRegister(INTENSITY_REGISTER_INFO, brightness)) return false; + + if (persist) { + return persistRegister(INTENSITY_REGISTER_INFO); + } + + return true; } diff --git a/src/RGBLED.h b/src/RGBLED.h index 2b375d4..106e065 100644 --- a/src/RGBLED.h +++ b/src/RGBLED.h @@ -33,56 +33,58 @@ class RGBLED : public I2CDevice { */ RGBLED(uint8_t deviceAddress); - /** - * Enables the indoor air quality status feature on the RGB LED. - * When enabled, the RGB LED will change color based on the air quality (red = bad, green = good) - */ - void enableIndoorAirQualityStatus(); - /** * Enables the indoor air quality status indicator on the RGB LED. * When enabled, the RGB LED will change color based on the air quality (red = bad, green = good) - * Call persistSettings() on NiclaSenseEnv instance after enabling the indoor air quality status to make the change persistent. * @param brightness The brightness level of the indicator (0-255). + * @param persist If true, the change will be saved to flash memory. + * When persist is True, the brightness will also be persisted. + * */ - void enableIndoorAirQualityStatus(uint8_t brightness); + bool enableIndoorAirQualityStatus(uint8_t brightness = 255, bool persist = false); /** * @brief Sets the RGB values of the LED. * * This function sets the red, green, and blue values of the LED using individual values. - * Call persistSettings() on NiclaSenseEnv instance after changing the color to make the change persistent. + * Note: A value of 0, 0, 0 will set the color based on the IAQ value from the Indoor Air Quality sensor. * @param r The red value (0-255). * @param g The green value (0-255). * @param b The blue value (0-255). + * @param persist If true, the change will be saved to flash memory. + * When persist is True, the brightness will also be persisted. */ - void setColor(uint8_t r, uint8_t g, uint8_t b); + bool setColor(uint8_t r, uint8_t g, uint8_t b, bool persist = false); /** * Sets the RGB values of the LED along with the specified brightness. - * Call persistSettings() on NiclaSenseEnv instance after changing the color to make the change persistent. * @param r The red value (0-255). * @param g The green value (0-255). * @param b The blue value (0-255). * @param brightness The brightness value (0-255). + * @param persist If true, the change will be saved to flash memory. + * When persist is True, the brightness will also be persisted. */ - void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness) ; + bool setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness, bool persist = false); ; /** * @brief Sets the RGB color of the LED using a Color object. * The Color object contains the red, green, and blue values that can be changed individually. - * Call persistSettings() on NiclaSenseEnv instance after changing the color to make the change persistent. + * Note: A value of 0, 0, 0 will set the color based on the IAQ value from the Indoor Air Quality sensor. * @param color The RGB color to set. + * @param persist If true, the change will be saved to flash memory. + * When persist is True, the brightness will also be persisted. */ - void setColor(Color color); + bool setColor(Color color, bool persist = false); /** * @brief Sets the RGB color and brightness of the LED using a Color object. - * Call Call persistSettings() on NiclaSenseEnv instance after changing the color / brightness to make the change persistent. * @param color The desired RGB color. * @param brightness The desired brightness level (0-255). + * @param persist If true, the change will be saved to flash memory. + * When persist is True, the brightness will also be persisted. */ - void setColor(Color color, uint8_t brightness); + bool setColor(Color color, uint8_t brightness, bool persist = false); /** * @brief Gets the current RGB color of the LED. @@ -101,8 +103,8 @@ class RGBLED : public I2CDevice { /** * @brief Sets the brightness of the RGB LED. * This function allows you to adjust the brightness of the RGB LED. - * Call persistSettings() on NiclaSenseEnv instance after changing the brightness to make the change persistent. * @param brightness The brightness level to set (0-255). + * @param persist If true, the change will be saved to flash memory. */ - void setBrightness(uint8_t brightness); + boo setBrightness(uint8_t brightness, bool persist = false); }; diff --git a/src/TemperatureHumiditySensor.cpp b/src/TemperatureHumiditySensor.cpp index 8398984..47318b6 100644 --- a/src/TemperatureHumiditySensor.cpp +++ b/src/TemperatureHumiditySensor.cpp @@ -23,7 +23,7 @@ bool TemperatureHumiditySensor::enabled() { return (status & 1) != 0; } -void TemperatureHumiditySensor::setEnabled(bool enabled) { +bool TemperatureHumiditySensor::setEnabled(bool enabled, bool persist) { // Read the current status and update the least significant bit with the new value uint8_t status = this->readFromRegister(STATUS_REGISTER_INFO); @@ -34,4 +34,10 @@ void TemperatureHumiditySensor::setEnabled(bool enabled) { status = enabled ? (status | 1) : (status & 0xFE); this->writeToRegister(STATUS_REGISTER_INFO, status); + + if(persist) { + return this->persistRegister(STATUS_REGISTER_INFO); + } + + return true; } diff --git a/src/TemperatureHumiditySensor.h b/src/TemperatureHumiditySensor.h index 7ba2771..a07dc22 100644 --- a/src/TemperatureHumiditySensor.h +++ b/src/TemperatureHumiditySensor.h @@ -60,10 +60,12 @@ class TemperatureHumiditySensor : public I2CDevice { /** * @brief Sets the enabled state of the temperature and humidity sensor. * When disabled the sensor goes in power down mode. - * Call persistSettings() on NiclaSenseEnv instance after changing the enabled state to make the change persistent. + * When `persist` is true, the mode setting of IndoorAirQualitySensor and OutdoorAirQualitySensor will also be persisted. * @param enabled The desired enabled state. True to enable the sensor, false to disable it. + * @param persist If true, the change will be saved to flash memory. + * @return true if the operation was successful, false otherwise. */ - void setEnabled(bool enabled); + bool setEnabled(bool enabled, bool persist = false); }; #endif \ No newline at end of file