From 1c38a7257d9689ff5d813940df29e3b9418bf03e Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Thu, 13 Jun 2024 15:38:11 +0200 Subject: [PATCH 1/8] Rename function to persistSettings --- examples/ChangeI2CAddress/ChangeI2CAddress.ino | 2 +- examples/UARTRead/UARTRead.ino | 2 +- src/IndoorAirQualitySensor.h | 4 ++-- src/NiclaSenseEnv.cpp | 4 ++-- src/NiclaSenseEnv.h | 14 +++++++------- src/OrangeLED.h | 5 +++-- src/OutdoorAirQualitySensor.h | 5 +++-- src/RGBLED.h | 12 ++++++------ src/TemperatureHumiditySensor.h | 2 +- 9 files changed, 26 insertions(+), 24 deletions(-) diff --git a/examples/ChangeI2CAddress/ChangeI2CAddress.ino b/examples/ChangeI2CAddress/ChangeI2CAddress.ino index 81a117c..f5116af 100644 --- a/examples/ChangeI2CAddress/ChangeI2CAddress.ino +++ b/examples/ChangeI2CAddress/ChangeI2CAddress.ino @@ -39,7 +39,7 @@ void setup() { checkConnection(device); // Store the new address in flash - device.storeSettingsInFlash(); + device.persistSettings(); Serial.println("🔄 Resetting device to check if change is persistent..."); device.reset(); diff --git a/examples/UARTRead/UARTRead.ino b/examples/UARTRead/UARTRead.ino index db18684..36f0323 100644 --- a/examples/UARTRead/UARTRead.ino +++ b/examples/UARTRead/UARTRead.ino @@ -13,7 +13,7 @@ * NiclaSenseEnv device; * device.begin(); * device.setUARTCSVOutputEnabled(true); - * device.storeSettingsInFlash() # Store the settings so they are not lost after a reset + * device.persistSettings() # Store the settings so they are not lost after a reset * * Initial author: Sebastian Romero (s.romero@arduino.cc) * diff --git a/src/IndoorAirQualitySensor.h b/src/IndoorAirQualitySensor.h index f00a961..1f01389 100644 --- a/src/IndoorAirQualitySensor.h +++ b/src/IndoorAirQualitySensor.h @@ -97,7 +97,7 @@ class IndoorAirQualitySensor : public I2CDevice { /** * @brief Set the mode of the IndoorAirQualitySensor. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the mode to make the change persistent. + * 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 @@ -136,7 +136,7 @@ class IndoorAirQualitySensor : public I2CDevice { /** * @brief Set the sensor enabled or disabled. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the enabled state to make the change persistent. + * 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. */ void setEnabled(bool isEnabled); diff --git a/src/NiclaSenseEnv.cpp b/src/NiclaSenseEnv.cpp index 3e1bc7c..c2ef8e2 100644 --- a/src/NiclaSenseEnv.cpp +++ b/src/NiclaSenseEnv.cpp @@ -73,7 +73,7 @@ void NiclaSenseEnv::end() { } } -bool NiclaSenseEnv::storeSettingsInFlash() { +bool NiclaSenseEnv::persistSettings() { uint8_t controlRegisterData = readFromRegister(CONTROL_REGISTER_INFO); writeToRegister(CONTROL_REGISTER_INFO, controlRegisterData | (1 << 7)); @@ -137,7 +137,7 @@ bool NiclaSenseEnv::restoreFactorySettings() { boardControlRegisterData = readFromRegister(CONTROL_REGISTER_INFO); if ((boardControlRegisterData & (1 << 5)) == 0) { - return storeSettingsInFlash(); + return persistSettings(); } Serial.println("⌛️ Waiting for factory reset to complete..."); // Exponential sleep duration diff --git a/src/NiclaSenseEnv.h b/src/NiclaSenseEnv.h index 77d5865..2e8b29e 100644 --- a/src/NiclaSenseEnv.h +++ b/src/NiclaSenseEnv.h @@ -106,7 +106,7 @@ class NiclaSenseEnv : public I2CDevice { * * @return true if the settings were successfully stored, false otherwise. */ - bool storeSettingsInFlash(); + bool persistSettings(); /** * @brief Retrieves the serial number of the device. @@ -148,7 +148,7 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Restores the factory settings. * This will reset among other properties the device address to the default value. - * See storeSettingsInFlash() for a complete list of properties that are affected by this method. + * See persistSettings() for a complete list of properties that are affected by this method. * * @return true if the factory settings were successfully restored, false otherwise. */ @@ -163,7 +163,7 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Sets the baud rate for the UART communication. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the baud rate to make the change persistent. + * 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 */ @@ -179,7 +179,7 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Sets the UART CSV output enabled or disabled. * Enables or disables CSV output over UART. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the CSV output mode to make the change persistent. + * 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, @@ -205,7 +205,7 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Sets the CSV delimiter for parsing CSV data. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the CSV delimiter to make the change persistent. + * 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. */ void setCSVDelimiter(char delimiter); @@ -220,14 +220,14 @@ class NiclaSenseEnv : public I2CDevice { /** * @brief Toggles the debugging mode. * When debugging mode is enabled, the board will send additional debug messages over UART. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the debugging mode to make the change persistent. + * 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. */ void setDebuggingEnabled(bool enabled); /** * @brief Sets the I2C address of the device. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the address to make the change persistent. + * 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. */ diff --git a/src/OrangeLED.h b/src/OrangeLED.h index d79e2cb..2db7196 100644 --- a/src/OrangeLED.h +++ b/src/OrangeLED.h @@ -32,20 +32,21 @@ class OrangeLED : public I2CDevice { /** * Sets the brightness of the orange LED. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the orange LED brightness to make the change persistent. + * 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. */ void setBrightness(uint8_t brightness = 63); /** * Determines whether the orange LED is used to indicate an error status of one of the sensors. + * When a board error condition occurs the LED blinks, independently of the brightness setting. * @return True if the orange LED is used for error status, false otherwise. */ bool errorStatusEnabled(); /** * Enables or disables the orange LED to indicate an error status of one of the sensors. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after enabling/disabling the orange LED error status to make the change persistent. + * Call persistSettings() on NiclaSenseEnv instance after enabling/disabling the orange LED error status to make the change persistent. * @param enabled : Whether to enable or disable the orange LED error status. */ void setErrorStatusEnabled(bool enabled); diff --git a/src/OutdoorAirQualitySensor.h b/src/OutdoorAirQualitySensor.h index 794b537..8c3a027 100644 --- a/src/OutdoorAirQualitySensor.h +++ b/src/OutdoorAirQualitySensor.h @@ -95,7 +95,7 @@ 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 storeSettingsInFlash() on NiclaSenseEnv instance after changing the mode to make the change persistent. + * 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 @@ -126,7 +126,8 @@ class OutdoorAirQualitySensor : public I2CDevice { /** * @brief Sets the enabled state of the outdoor air quality sensor. * When disabled the sensor goes in power down mode. - * Call storeSettingsInFlash() 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 default 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. */ void setEnabled(bool isEnabled); diff --git a/src/RGBLED.h b/src/RGBLED.h index d5fb0be..2b375d4 100644 --- a/src/RGBLED.h +++ b/src/RGBLED.h @@ -42,7 +42,7 @@ class RGBLED : public I2CDevice { /** * 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 storeSettingsInFlash() on NiclaSenseEnv instance after enabling the indoor air quality status to make the change persistent. + * 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). */ void enableIndoorAirQualityStatus(uint8_t brightness); @@ -51,7 +51,7 @@ class RGBLED : public I2CDevice { * @brief Sets the RGB values of the LED. * * This function sets the red, green, and blue values of the LED using individual values. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the color to make the change persistent. + * 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). @@ -60,7 +60,7 @@ class RGBLED : public I2CDevice { /** * Sets the RGB values of the LED along with the specified brightness. - * Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the color to make the change persistent. + * 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). @@ -71,14 +71,14 @@ class RGBLED : public I2CDevice { /** * @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 storeSettingsInFlash() on NiclaSenseEnv instance after changing the color to make the change persistent. + * Call persistSettings() on NiclaSenseEnv instance after changing the color to make the change persistent. * @param color The RGB color to set. */ void setColor(Color color); /** * @brief Sets the RGB color and brightness of the LED using a Color object. - * Call Call storeSettingsInFlash() on NiclaSenseEnv instance after changing the color / brightness to make the change persistent. + * 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). */ @@ -101,7 +101,7 @@ 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 storeSettingsInFlash() on NiclaSenseEnv instance after changing the brightness to make the change persistent. + * Call persistSettings() on NiclaSenseEnv instance after changing the brightness to make the change persistent. * @param brightness The brightness level to set (0-255). */ void setBrightness(uint8_t brightness); diff --git a/src/TemperatureHumiditySensor.h b/src/TemperatureHumiditySensor.h index 2377f60..7ba2771 100644 --- a/src/TemperatureHumiditySensor.h +++ b/src/TemperatureHumiditySensor.h @@ -60,7 +60,7 @@ 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 storeSettingsInFlash() on NiclaSenseEnv instance after changing the enabled state to make the change persistent. + * Call persistSettings() on NiclaSenseEnv instance after changing the enabled state to make the change persistent. * @param enabled The desired enabled state. True to enable the sensor, false to disable it. */ void setEnabled(bool enabled); From d1502ede004b023ba8d8c048dcd3411d15467a13 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Thu, 13 Jun 2024 15:41:19 +0200 Subject: [PATCH 2/8] Use outdoorAirQuality mode when enabling sensor --- src/OutdoorAirQualitySensor.cpp | 2 +- src/OutdoorAirQualitySensor.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OutdoorAirQualitySensor.cpp b/src/OutdoorAirQualitySensor.cpp index 21ec01b..05adb34 100644 --- a/src/OutdoorAirQualitySensor.cpp +++ b/src/OutdoorAirQualitySensor.cpp @@ -79,7 +79,7 @@ void OutdoorAirQualitySensor::setEnabled(bool isEnabled) { return; } if (isEnabled) { - setMode(OutdoorAirQualitySensorMode::defaultMode); + setMode(OutdoorAirQualitySensorMode::outdoorAirQuality); } else { setMode(OutdoorAirQualitySensorMode::powerDown); } diff --git a/src/OutdoorAirQualitySensor.h b/src/OutdoorAirQualitySensor.h index 8c3a027..357e3c8 100644 --- a/src/OutdoorAirQualitySensor.h +++ b/src/OutdoorAirQualitySensor.h @@ -10,7 +10,7 @@ enum class OutdoorAirQualitySensorMode { powerDown = 0, ///< Mode to turn off the sensor and reduce power consumption cleaning = 1, ///< Cleaning mode to perform a thermal cleaning cycle of the MOx element outdoorAirQuality = 2, ///< Mode to measure outdoor air quality - defaultMode = powerDown // Can't use default as it's a reserved keyword + defaultMode = powerDown // Can't use 'default' as it's a reserved keyword }; /** @@ -126,7 +126,7 @@ 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 default 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. */ From 4045b4e5d8e8f228aedf5b49a31e478d1c9524f9 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Thu, 13 Jun 2024 16:46:58 +0200 Subject: [PATCH 3/8] Add function to persist register --- src/I2CDevice.cpp | 19 +++++++++++++++++++ src/I2CDevice.h | 6 ++++++ src/registers.h | 1 + 3 files changed, 26 insertions(+) diff --git a/src/I2CDevice.cpp b/src/I2CDevice.cpp index a2fa03c..4651663 100644 --- a/src/I2CDevice.cpp +++ b/src/I2CDevice.cpp @@ -8,6 +8,25 @@ I2CDevice::I2CDevice(TwoWire& bus, uint8_t deviceAddress) I2CDevice::I2CDevice(uint8_t deviceAddress) : bus(Wire), i2cDeviceAddress(deviceAddress) {} +bool I2CDevice::persistRegister(RegisterInfo registerInfo){ + writeToRegister(DEFAULTS_REGISTER_INFO, registerInfo.address | (1 << 7)); + + // Read bit 7 to check if the write is complete. When the write is complete, bit 7 will be 0. + // Try 10 times with increasing delay between each try + for (int i = 0; i < 10; ++i) { + uint8_t defaultsRegisterData = readFromRegister(DEFAULTS_REGISTER_INFO); + if (!(defaultsRegisterData & (1 << 7))) { + return true; + } + // Even a value of 1 us seems to work, but we start with 100 us to be safe. + Serial.println("⌛️ Waiting for flash write to complete..."); + // Exponential sleep duration + delayMicroseconds(100 * (2 << i)); + } + + return false; +} + bool I2CDevice::connected() { bus.beginTransmission(i2cDeviceAddress); return bus.endTransmission() == 0; diff --git a/src/I2CDevice.h b/src/I2CDevice.h index 03ad4d7..268c283 100644 --- a/src/I2CDevice.h +++ b/src/I2CDevice.h @@ -141,6 +141,12 @@ class I2CDevice { return bus.endTransmission() == 0; } + /** + * @brief Makes the value of a given register persistent. + * @param registerInfo The register to make persistent. + */ + bool persistRegister(RegisterInfo registerInfo); + /** * @brief Reference to the I2C bus used by the device. */ diff --git a/src/registers.h b/src/registers.h index 94bc032..8ea4214 100644 --- a/src/registers.h +++ b/src/registers.h @@ -47,5 +47,6 @@ constexpr RegisterInfo ZMOD4410_RHTR_REGISTER_INFO{0xC4, "float", 4}; constexpr RegisterInfo ZMOD4410_TEMP_REGISTER_INFO{0xC8, "float", 4}; constexpr RegisterInfo ZMOD4410_INTENSITY_REGISTER_INFO{0xCC, "float", 4}; constexpr RegisterInfo ZMOD4410_ODOR_CLASS_REGISTER_INFO{0xD0, "uint8", 1}; +constexpr RegisterInfo DEFAULTS_REGISTER_INFO{0xD4, "uint8", 1}; #endif \ No newline at end of file From d7852da1f03f83e0dee8fcf65dff1ff875e6ee01 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Thu, 13 Jun 2024 17:26:33 +0200 Subject: [PATCH 4/8] Use new persistRegister function --- .../ChangeI2CAddress/ChangeI2CAddress.ino | 7 +-- examples/UARTRead/UARTRead.ino | 4 +- src/IndoorAirQualitySensor.cpp | 22 ++++--- src/IndoorAirQualitySensor.h | 12 ++-- src/NiclaSenseEnv.cpp | 61 ++++++++++++++++--- src/NiclaSenseEnv.h | 22 ++++--- src/OrangeLED.cpp | 16 ++++- src/OrangeLED.h | 12 ++-- src/OutdoorAirQualitySensor.cpp | 22 ++++--- src/OutdoorAirQualitySensor.h | 10 +-- src/RGBLED.cpp | 50 ++++++++------- src/RGBLED.h | 38 ++++++------ src/TemperatureHumiditySensor.cpp | 8 ++- src/TemperatureHumiditySensor.h | 6 +- 14 files changed, 191 insertions(+), 99 deletions(-) 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 From ee02d07a9081a4210ac2e62cbfda23762cb0d15b Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 14 Jun 2024 11:01:27 +0200 Subject: [PATCH 5/8] Use setEnabled instead of setting the mode manually --- examples/OutdoorAirQuality/OutdoorAirQuality.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/OutdoorAirQuality/OutdoorAirQuality.ino b/examples/OutdoorAirQuality/OutdoorAirQuality.ino index 49412df..7083a5a 100644 --- a/examples/OutdoorAirQuality/OutdoorAirQuality.ino +++ b/examples/OutdoorAirQuality/OutdoorAirQuality.ino @@ -40,7 +40,8 @@ void setup() { // Enable outdoor air quality sensor (disabled by default) // Please note that it may take some time for the sensor to deliver the first data - outdoorAirQualitySensor.setMode(OutdoorAirQualitySensorMode::outdoorAirQuality); + // Use setEnabled(true, true) make the change persistent + outdoorAirQualitySensor.setEnabled(true); displaySensorData(outdoorAirQualitySensor); // Optionally disable the sensor From 3be47d6a4d483211905b15dc89edf96f6d9ec175 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 14 Jun 2024 11:14:52 +0200 Subject: [PATCH 6/8] Fix typo --- src/RGBLED.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RGBLED.h b/src/RGBLED.h index 106e065..394aca5 100644 --- a/src/RGBLED.h +++ b/src/RGBLED.h @@ -106,5 +106,5 @@ class RGBLED : public I2CDevice { * @param brightness The brightness level to set (0-255). * @param persist If true, the change will be saved to flash memory. */ - boo setBrightness(uint8_t brightness, bool persist = false); + bool setBrightness(uint8_t brightness, bool persist = false); }; From bfd0a2d86bbe968d031cbf48b0e96ee0e9b58c2a Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 14 Jun 2024 11:25:23 +0200 Subject: [PATCH 7/8] Add documentation for return types --- src/I2CDevice.h | 1 + src/IndoorAirQualitySensor.h | 3 ++- src/NiclaSenseEnv.h | 5 +++++ src/OrangeLED.h | 2 ++ src/OutdoorAirQualitySensor.h | 2 ++ src/RGBLED.h | 7 ++++++- 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/I2CDevice.h b/src/I2CDevice.h index 268c283..44b3b58 100644 --- a/src/I2CDevice.h +++ b/src/I2CDevice.h @@ -144,6 +144,7 @@ class I2CDevice { /** * @brief Makes the value of a given register persistent. * @param registerInfo The register to make persistent. + * @return Whether or not the register value was successfully persisted. */ bool persistRegister(RegisterInfo registerInfo); diff --git a/src/IndoorAirQualitySensor.h b/src/IndoorAirQualitySensor.h index 4bdad87..e3276a3 100644 --- a/src/IndoorAirQualitySensor.h +++ b/src/IndoorAirQualitySensor.h @@ -118,7 +118,7 @@ class IndoorAirQualitySensor : public I2CDevice { * @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. - * + * @return True if the mode was set successfully, false otherwise. */ bool setMode(IndoorAirQualitySensorMode sensorMode, bool persist); @@ -142,6 +142,7 @@ class IndoorAirQualitySensor : public I2CDevice { * @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. + * @return True if the the sensor was enabled successfully. */ bool setEnabled(bool isEnabled, bool persist = false); }; diff --git a/src/NiclaSenseEnv.h b/src/NiclaSenseEnv.h index debbb46..e5a46d9 100644 --- a/src/NiclaSenseEnv.h +++ b/src/NiclaSenseEnv.h @@ -166,6 +166,7 @@ class NiclaSenseEnv : public I2CDevice { * @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. + * @return True if the baud rate was set successfully, false otherwise. */ bool setUARTBaudRate(int baudRate, bool persist); @@ -194,6 +195,7 @@ class NiclaSenseEnv : public I2CDevice { * @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`. + * @return True if the CSV output mode was set successfully, false otherwise. */ bool setUARTCSVOutputEnabled(bool enabled, bool persist = false); @@ -208,6 +210,7 @@ class NiclaSenseEnv : public I2CDevice { * @brief Sets the CSV delimiter for parsing CSV data. * @param delimiter The character to be used as the CSV delimiter. * @param persist If true, the change will be saved to flash memory. + * @return True if the delimiter was set successfully, false otherwise. */ bool setCSVDelimiter(char delimiter, bool persist = false); @@ -224,6 +227,7 @@ class NiclaSenseEnv : public I2CDevice { * @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. + * @return True if the debugging mode was set successfully, false otherwise. */ bool setDebuggingEnabled(bool enabled, bool persist = false); @@ -232,6 +236,7 @@ class NiclaSenseEnv : public I2CDevice { * * @param address The new I2C address. Valid values are 0 to 127. * @param persist If true, the change will be saved to flash memory. + * @return True if the address was set successfully, false otherwise. */ bool setDeviceAddress(int address, bool persist = false); diff --git a/src/OrangeLED.h b/src/OrangeLED.h index 6fbd427..7128f4b 100644 --- a/src/OrangeLED.h +++ b/src/OrangeLED.h @@ -35,6 +35,7 @@ class OrangeLED : public I2CDevice { * 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. + * @return True if the brightness was set successfully, false otherwise. */ bool setBrightness(uint8_t brightness, bool persist = false); @@ -50,6 +51,7 @@ class OrangeLED : public I2CDevice { * 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. + * @return True if the mode was set successfully, false otherwise. */ bool setErrorStatusEnabled(bool enabled, bool persist = false); }; diff --git a/src/OutdoorAirQualitySensor.h b/src/OutdoorAirQualitySensor.h index a4763a8..23bcdb5 100644 --- a/src/OutdoorAirQualitySensor.h +++ b/src/OutdoorAirQualitySensor.h @@ -107,6 +107,7 @@ class OutdoorAirQualitySensor : public I2CDevice { * @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. + * @return True if the mode was set successfully, false otherwise. */ bool setMode(OutdoorAirQualitySensorMode sensorMode, bool persist = false); @@ -131,6 +132,7 @@ class OutdoorAirQualitySensor : public I2CDevice { * @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. + * @return True if the enabled state was set successfully, false otherwise. */ bool setEnabled(bool isEnabled, bool persist = false); }; diff --git a/src/RGBLED.h b/src/RGBLED.h index 394aca5..3624470 100644 --- a/src/RGBLED.h +++ b/src/RGBLED.h @@ -39,7 +39,7 @@ class RGBLED : public I2CDevice { * @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. - * + * @return True if the mode was set successfully, false otherwise. */ bool enableIndoorAirQualityStatus(uint8_t brightness = 255, bool persist = false); @@ -53,6 +53,7 @@ class RGBLED : public I2CDevice { * @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. + * @return True if the color was set successfully, false otherwise. */ bool setColor(uint8_t r, uint8_t g, uint8_t b, bool persist = false); @@ -64,6 +65,7 @@ class RGBLED : public I2CDevice { * @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. + * @return True if the color was set successfully, false otherwise. */ bool setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness, bool persist = false); ; @@ -74,6 +76,7 @@ class RGBLED : public I2CDevice { * @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. + * @return True if the color was set successfully, false otherwise. */ bool setColor(Color color, bool persist = false); @@ -83,6 +86,7 @@ class RGBLED : public I2CDevice { * @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. + * @return True if the color was set successfully, false otherwise. */ bool setColor(Color color, uint8_t brightness, bool persist = false); @@ -105,6 +109,7 @@ class RGBLED : public I2CDevice { * This function allows you to adjust the brightness of the RGB LED. * @param brightness The brightness level to set (0-255). * @param persist If true, the change will be saved to flash memory. + * @return True if the brightness was set successfully, false otherwise. */ bool setBrightness(uint8_t brightness, bool persist = false); }; From db4c8824e9ed1356da794640b96660b86ba70f4f Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 14 Jun 2024 12:07:52 +0200 Subject: [PATCH 8/8] Fix compiler issues --- examples/RGBLED/RGBLED.ino | 3 +-- src/IndoorAirQualitySensor.cpp | 4 ++-- src/IndoorAirQualitySensor.h | 2 +- src/NiclaSenseEnv.cpp | 16 ++++++++-------- src/OrangeLED.cpp | 2 +- src/OutdoorAirQualitySensor.cpp | 4 ++-- src/RGBLED.cpp | 16 +++------------- src/RGBLED.h | 24 ------------------------ src/TemperatureHumiditySensor.cpp | 2 +- 9 files changed, 19 insertions(+), 54 deletions(-) diff --git a/examples/RGBLED/RGBLED.ino b/examples/RGBLED/RGBLED.ino index eeaaa4f..c55ea84 100644 --- a/examples/RGBLED/RGBLED.ino +++ b/examples/RGBLED/RGBLED.ino @@ -21,8 +21,7 @@ void pulseLED(RGBLED &led) { } void pulseColors(RGBLED &led) { - // Brightness can also be set via 4th struct element - led.setColor({255, 0, 0}, 255); // Red + led.setColor({255, 0, 0}); // Red pulseLED(led); // Color can be set via Color struct or 3 separate uint8_t values diff --git a/src/IndoorAirQualitySensor.cpp b/src/IndoorAirQualitySensor.cpp index 49b77cc..b38a89d 100644 --- a/src/IndoorAirQualitySensor.cpp +++ b/src/IndoorAirQualitySensor.cpp @@ -58,7 +58,7 @@ bool IndoorAirQualitySensor::setMode(IndoorAirQualitySensorMode sensorMode, bool // Check if the existing value is already the same if ((currentRegisterData & (7 << 1)) == (mode << 1)) { - return; + return true; } if(!writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(7 << 1)) | (mode << 1))){ return false; @@ -95,7 +95,7 @@ bool IndoorAirQualitySensor::enabled() { bool IndoorAirQualitySensor::setEnabled(bool isEnabled, bool persist) { if (isEnabled == enabled()) { - return; + return true; } auto mode = isEnabled ? IndoorAirQualitySensorMode::indoorAirQuality : IndoorAirQualitySensorMode::powerDown; diff --git a/src/IndoorAirQualitySensor.h b/src/IndoorAirQualitySensor.h index e3276a3..7b94d47 100644 --- a/src/IndoorAirQualitySensor.h +++ b/src/IndoorAirQualitySensor.h @@ -120,7 +120,7 @@ class IndoorAirQualitySensor : public I2CDevice { * When persist is true, the mode setting of OutdoorAirQualitySensor and TemperatureHumiditySensor will also be persisted. * @return True if the mode was set successfully, false otherwise. */ - bool setMode(IndoorAirQualitySensorMode sensorMode, bool persist); + bool setMode(IndoorAirQualitySensorMode sensorMode, bool persist = false); /** * @brief Get the mode as a string. diff --git a/src/NiclaSenseEnv.cpp b/src/NiclaSenseEnv.cpp index a15b48e..6a3e139 100644 --- a/src/NiclaSenseEnv.cpp +++ b/src/NiclaSenseEnv.cpp @@ -155,12 +155,12 @@ int NiclaSenseEnv::UARTBaudRate() { bool NiclaSenseEnv::setUARTBaudRate(int baudRate, bool persist) { int baudRateIndex = baudRateNativeValue(baudRate); if (baudRateIndex == -1) { - return; // Baud rate not found + return false; // Baud rate not found } uint8_t uartControlRegisterData = readFromRegister(UART_CONTROL_REGISTER_INFO); if ((uartControlRegisterData & 7) == baudRateIndex) { - return; // Value is already the same + return true; // Value is already the same } if(!writeToRegister(UART_CONTROL_REGISTER_INFO, (uartControlRegisterData & ~7) | baudRateIndex)){ return false; @@ -181,7 +181,7 @@ bool NiclaSenseEnv::isUARTCSVOutputEnabled() { 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 + return true; // Value is already the same } if(!writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~2) | (enabled << 1))){ return false; @@ -202,7 +202,7 @@ char NiclaSenseEnv::CSVDelimiter() { bool NiclaSenseEnv::setCSVDelimiter(char delimiter, bool persist) { char currentDelimiter = CSVDelimiter(); if (currentDelimiter == delimiter) { - return; // Value is already the same + return true; // Value is already the same } // Define prohibited delimiters @@ -210,7 +210,7 @@ bool NiclaSenseEnv::setCSVDelimiter(char delimiter, bool persist) { for (auto prohibitedDelimiter : prohibitedDelimiters) { if (delimiter == prohibitedDelimiter) { - return; // Delimiter is prohibited + return false; // Delimiter is prohibited } } @@ -234,7 +234,7 @@ bool NiclaSenseEnv::isDebuggingEnabled() { 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 + return true; // Value is already the same } if(!writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~1) | enabled)){ return false; @@ -249,12 +249,12 @@ bool NiclaSenseEnv::setDebuggingEnabled(bool enabled, bool persist) { bool NiclaSenseEnv::setDeviceAddress(int address, bool persist) { if (address < 0 || address > 127) { - return; // Invalid address + return false; // Invalid address } uint8_t addressRegisterData = readFromRegister(SLAVE_ADDRESS_REGISTER_INFO); // Check bits 0 - 6 if ((addressRegisterData & 127) == address) { - return; // Value is already the same + return true; // Value is already the same } if(!writeToRegister(SLAVE_ADDRESS_REGISTER_INFO, (addressRegisterData & ~127) | address)){ return false; diff --git a/src/OrangeLED.cpp b/src/OrangeLED.cpp index 3eacc3c..9ad1fce 100644 --- a/src/OrangeLED.cpp +++ b/src/OrangeLED.cpp @@ -13,7 +13,7 @@ uint8_t OrangeLED::brightness() { bool OrangeLED::setBrightness(uint8_t brightness, bool persist) { if (brightness > 255) { - return; // Invalid brightness value + return false; // Invalid brightness value } uint8_t mappedBrightness = map(brightness, 0, 255, 0, 63); diff --git a/src/OutdoorAirQualitySensor.cpp b/src/OutdoorAirQualitySensor.cpp index adcf5af..fb27dea 100644 --- a/src/OutdoorAirQualitySensor.cpp +++ b/src/OutdoorAirQualitySensor.cpp @@ -49,7 +49,7 @@ bool OutdoorAirQualitySensor::setMode(OutdoorAirQualitySensorMode sensorMode, bo // Check if the existing value is already the same if ((currentRegisterData & (3 << 4)) == (mode << 4)) { - return; + return true; } // Overwrite bits 4 and 5 with the new value @@ -84,7 +84,7 @@ bool OutdoorAirQualitySensor::enabled() { 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; + return true; } auto mode = isEnabled ? OutdoorAirQualitySensorMode::outdoorAirQuality : OutdoorAirQualitySensorMode::powerDown; diff --git a/src/RGBLED.cpp b/src/RGBLED.cpp index baf34dc..df0229e 100644 --- a/src/RGBLED.cpp +++ b/src/RGBLED.cpp @@ -6,35 +6,25 @@ RGBLED::RGBLED(TwoWire& bus, uint8_t deviceAddress) : I2CDevice(bus, deviceAddre RGBLED::RGBLED(uint8_t deviceAddress) : I2CDevice(deviceAddress) {} bool RGBLED::enableIndoorAirQualityStatus(uint8_t brightness, bool persist) { - return setColor(0, 0, 0, brightness, persist); + return setColor(0, 0, 0, persist) && setBrightness(brightness, persist); } bool RGBLED::setColor(uint8_t r, uint8_t g, uint8_t b, bool persist) { - return setColor(r, g, b, 255, persist); // 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); + persistRegister(RGB_LED_BLUE_REGISTER_INFO); } return true; } bool RGBLED::setColor(Color color, bool persist) { - return setColor(color, 255, persist); // Default maximum brightness -} - -bool RGBLED::setColor(Color color, uint8_t brightness, bool persist) { - return setColor(color.red, color.green, color.blue, brightness, persist); + return setColor(color.red, color.green, color.blue, persist); } Color RGBLED::color() { diff --git a/src/RGBLED.h b/src/RGBLED.h index 3624470..cf8d71f 100644 --- a/src/RGBLED.h +++ b/src/RGBLED.h @@ -52,44 +52,20 @@ class RGBLED : public I2CDevice { * @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. * @return True if the color was set successfully, false otherwise. */ 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. - * @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. - * @return True if the color was set successfully, false otherwise. - */ - 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. * 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. * @return True if the color was set successfully, false otherwise. */ bool setColor(Color color, bool persist = false); - /** - * @brief Sets the RGB color and brightness of the LED using a Color object. - * @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. - * @return True if the color was set successfully, false otherwise. - */ - bool setColor(Color color, uint8_t brightness, bool persist = false); - /** * @brief Gets the current RGB color of the LED. * diff --git a/src/TemperatureHumiditySensor.cpp b/src/TemperatureHumiditySensor.cpp index 47318b6..8245048 100644 --- a/src/TemperatureHumiditySensor.cpp +++ b/src/TemperatureHumiditySensor.cpp @@ -29,7 +29,7 @@ bool TemperatureHumiditySensor::setEnabled(bool enabled, bool persist) { // Check if current value is already the desired value if (static_cast(status & 1) == enabled) { - return; + return true; } status = enabled ? (status | 1) : (status & 0xFE);