Skip to content

Commit

Permalink
Use new persistRegister function
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Jun 14, 2024
1 parent 4045b4e commit d7852da
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 99 deletions.
7 changes: 3 additions & 4 deletions examples/ChangeI2CAddress/ChangeI2CAddress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/UARTRead/UARTRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
*
Expand Down
22 changes: 14 additions & 8 deletions src/IndoorAirQualitySensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,23 @@ IndoorAirQualitySensorMode IndoorAirQualitySensor::mode() {
return IndoorAirQualitySensorMode((data >> 1) & 7);
}

void IndoorAirQualitySensor::setMode(IndoorAirQualitySensorMode sensorMode) {
bool IndoorAirQualitySensor::setMode(IndoorAirQualitySensorMode sensorMode, bool persist) {
uint8_t currentRegisterData = readFromRegister<uint8_t>(STATUS_REGISTER_INFO);
uint8_t mode = static_cast<uint8_t>(sensorMode); // convert to numeric type

// Check if the existing value is already the same
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() {
Expand All @@ -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);
}
12 changes: 8 additions & 4 deletions src/IndoorAirQualitySensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
61 changes: 51 additions & 10 deletions src/NiclaSenseEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -162,28 +162,44 @@ 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() {
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
return (boardControlRegisterData & (1 << 1)) != 0;
}

void NiclaSenseEnv::setUARTCSVOutputEnabled(bool enabled) {
bool NiclaSenseEnv::setUARTCSVOutputEnabled(bool enabled, bool persist) {
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
if ((boardControlRegisterData & 2) == static_cast<int>(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() {
uint8_t csvDelimiterRegisterData = readFromRegister<uint8_t>(CSV_DELIMITER_REGISTER_INFO);
return static_cast<char>(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
Expand All @@ -199,23 +215,39 @@ void NiclaSenseEnv::setCSVDelimiter(char delimiter) {
}

// Use ASCII code of the delimiter character
writeToRegister(CSV_DELIMITER_REGISTER_INFO, static_cast<uint8_t>(delimiter));
if(!writeToRegister(CSV_DELIMITER_REGISTER_INFO, static_cast<uint8_t>(delimiter))){
return false;
}

if (persist) {
return persistRegister(CSV_DELIMITER_REGISTER_INFO);
}

return true;
}

bool NiclaSenseEnv::isDebuggingEnabled() {
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
return (boardControlRegisterData & 1) != 0;
}

void NiclaSenseEnv::setDebuggingEnabled(bool enabled) {
bool NiclaSenseEnv::setDebuggingEnabled(bool enabled, bool persist) {
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
if ((boardControlRegisterData & 1) == static_cast<int>(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
}
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/NiclaSenseEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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:
/**
Expand Down
16 changes: 14 additions & 2 deletions src/OrangeLED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -20,6 +20,12 @@ void OrangeLED::setBrightness(uint8_t brightness) {
uint8_t currentRegisterData = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
// Overwrite bits 0 - 5 with the new value
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | mappedBrightness);

if (persist) {
return persistRegister(ORANGE_LED_REGISTER_INFO);
}

return true;
}

bool OrangeLED::errorStatusEnabled() {
Expand All @@ -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<uint8_t>(ORANGE_LED_REGISTER_INFO);
// Set bit 7 to 1 if enabled or 0 if disabled while keeping the other bits unchanged
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~(1 << 7)) | (enabled << 7));

if (persist) {
return persistRegister(ORANGE_LED_REGISTER_INFO);
}

return true;
}
12 changes: 7 additions & 5 deletions src/OrangeLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
};
Loading

0 comments on commit d7852da

Please sign in to comment.