Skip to content

Commit d7852da

Browse files
committed
Use new persistRegister function
1 parent 4045b4e commit d7852da

14 files changed

+191
-99
lines changed

examples/ChangeI2CAddress/ChangeI2CAddress.ino

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ void setup() {
3535
Serial.print("🔧 Changing device address to 0x");
3636
Serial.print(customI2CAddress, HEX);
3737
Serial.println("...");
38-
device.setDeviceAddress(customI2CAddress);
38+
39+
// Setting the second parameter to true makes the change persistent
40+
device.setDeviceAddress(customI2CAddress, true);
3941
checkConnection(device);
4042

41-
// Store the new address in flash
42-
device.persistSettings();
43-
4443
Serial.println("🔄 Resetting device to check if change is persistent...");
4544
device.reset();
4645
delay(2000); // Wait for the device to reset

examples/UARTRead/UARTRead.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*
1313
* NiclaSenseEnv device;
1414
* device.begin();
15-
* device.setUARTCSVOutputEnabled(true);
16-
* device.persistSettings() # Store the settings so they are not lost after a reset
15+
* The second parameter ensures that the settings are not lost after a reset
16+
* device.setUARTCSVOutputEnabled(true, true);
1717
*
1818
* Initial author: Sebastian Romero ([email protected])
1919
*

src/IndoorAirQualitySensor.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,23 @@ IndoorAirQualitySensorMode IndoorAirQualitySensor::mode() {
5252
return IndoorAirQualitySensorMode((data >> 1) & 7);
5353
}
5454

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

5959
// Check if the existing value is already the same
6060
if ((currentRegisterData & (7 << 1)) == (mode << 1)) {
6161
return;
6262
}
63-
writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(7 << 1)) | (mode << 1));
63+
if(!writeToRegister(STATUS_REGISTER_INFO, (currentRegisterData & ~(7 << 1)) | (mode << 1))){
64+
return false;
65+
}
66+
67+
if(persist){
68+
return persistRegister(STATUS_REGISTER_INFO);
69+
}
70+
71+
return true;
6472
}
6573

6674
String IndoorAirQualitySensor::modeString() {
@@ -85,13 +93,11 @@ bool IndoorAirQualitySensor::enabled() {
8593
return mode() != IndoorAirQualitySensorMode::powerDown;
8694
}
8795

88-
void IndoorAirQualitySensor::setEnabled(bool isEnabled) {
96+
bool IndoorAirQualitySensor::setEnabled(bool isEnabled, bool persist) {
8997
if (isEnabled == enabled()) {
9098
return;
9199
}
92-
if (isEnabled) {
93-
setMode(IndoorAirQualitySensorMode::defaultMode);
94-
} else {
95-
setMode(IndoorAirQualitySensorMode::powerDown);
96-
}
100+
101+
auto mode = isEnabled ? IndoorAirQualitySensorMode::indoorAirQuality : IndoorAirQualitySensorMode::powerDown;
102+
return setMode(mode, persist);
97103
}

src/IndoorAirQualitySensor.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class IndoorAirQualitySensor : public I2CDevice {
9797

9898
/**
9999
* @brief Set the mode of the IndoorAirQualitySensor.
100-
* Call persistSettings() on NiclaSenseEnv instance after changing the mode to make the change persistent.
101100
*
102101
* Note on cleaning mode:
103102
* 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 {
117116
* For more accurate readings, use the default indoor air quality mode.
118117
*
119118
* @param sensorMode The mode to set. See the IndoorAirQualitySensorMode enum class for possible values.
119+
* @param persist If true, the change will be saved to flash memory.
120+
* When persist is true, the mode setting of OutdoorAirQualitySensor and TemperatureHumiditySensor will also be persisted.
121+
*
120122
*/
121-
void setMode(IndoorAirQualitySensorMode sensorMode);
123+
bool setMode(IndoorAirQualitySensorMode sensorMode, bool persist);
122124

123125
/**
124126
* @brief Get the mode as a string.
@@ -136,10 +138,12 @@ class IndoorAirQualitySensor : public I2CDevice {
136138

137139
/**
138140
* @brief Set the sensor enabled or disabled.
139-
* Call persistSettings() on NiclaSenseEnv instance after changing the enabled state to make the change persistent.
141+
* When the sensor is enabled after being disabled, the sensor will go back to the indoorAirQuality mode.
140142
* @param isEnabled True to enable the sensor, false to disable it.
143+
* @param persist If true, the change will be saved to flash memory.
144+
* When persist is true, the mode setting of IndoorAirQualitySensor and TemperatureHumiditySensor will also be persisted.
141145
*/
142-
void setEnabled(bool isEnabled);
146+
bool setEnabled(bool isEnabled, bool persist = false);
143147
};
144148

145149
#endif

src/NiclaSenseEnv.cpp

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ int NiclaSenseEnv::UARTBaudRate() {
152152
return baudRateMap[uartControlRegisterData];
153153
}
154154

155-
void NiclaSenseEnv::setUARTBaudRate(int baudRate) {
155+
bool NiclaSenseEnv::setUARTBaudRate(int baudRate, bool persist) {
156156
int baudRateIndex = baudRateNativeValue(baudRate);
157157
if (baudRateIndex == -1) {
158158
return; // Baud rate not found
@@ -162,28 +162,44 @@ void NiclaSenseEnv::setUARTBaudRate(int baudRate) {
162162
if ((uartControlRegisterData & 7) == baudRateIndex) {
163163
return; // Value is already the same
164164
}
165-
writeToRegister(UART_CONTROL_REGISTER_INFO, (uartControlRegisterData & ~7) | baudRateIndex);
165+
if(!writeToRegister(UART_CONTROL_REGISTER_INFO, (uartControlRegisterData & ~7) | baudRateIndex)){
166+
return false;
167+
}
168+
169+
if (persist) {
170+
return persistRegister(UART_CONTROL_REGISTER_INFO);
171+
}
172+
173+
return true;
166174
}
167175

168176
bool NiclaSenseEnv::isUARTCSVOutputEnabled() {
169177
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
170178
return (boardControlRegisterData & (1 << 1)) != 0;
171179
}
172180

173-
void NiclaSenseEnv::setUARTCSVOutputEnabled(bool enabled) {
181+
bool NiclaSenseEnv::setUARTCSVOutputEnabled(bool enabled, bool persist) {
174182
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
175183
if ((boardControlRegisterData & 2) == static_cast<int>(enabled)) {
176184
return; // Value is already the same
177185
}
178-
writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~2) | (enabled << 1));
186+
if(!writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~2) | (enabled << 1))){
187+
return false;
188+
}
189+
190+
if (persist) {
191+
return persistRegister(CONTROL_REGISTER_INFO);
192+
}
193+
194+
return true;
179195
}
180196

181197
char NiclaSenseEnv::CSVDelimiter() {
182198
uint8_t csvDelimiterRegisterData = readFromRegister<uint8_t>(CSV_DELIMITER_REGISTER_INFO);
183199
return static_cast<char>(csvDelimiterRegisterData);
184200
}
185201

186-
void NiclaSenseEnv::setCSVDelimiter(char delimiter) {
202+
bool NiclaSenseEnv::setCSVDelimiter(char delimiter, bool persist) {
187203
char currentDelimiter = CSVDelimiter();
188204
if (currentDelimiter == delimiter) {
189205
return; // Value is already the same
@@ -199,23 +215,39 @@ void NiclaSenseEnv::setCSVDelimiter(char delimiter) {
199215
}
200216

201217
// Use ASCII code of the delimiter character
202-
writeToRegister(CSV_DELIMITER_REGISTER_INFO, static_cast<uint8_t>(delimiter));
218+
if(!writeToRegister(CSV_DELIMITER_REGISTER_INFO, static_cast<uint8_t>(delimiter))){
219+
return false;
220+
}
221+
222+
if (persist) {
223+
return persistRegister(CSV_DELIMITER_REGISTER_INFO);
224+
}
225+
226+
return true;
203227
}
204228

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

210-
void NiclaSenseEnv::setDebuggingEnabled(bool enabled) {
234+
bool NiclaSenseEnv::setDebuggingEnabled(bool enabled, bool persist) {
211235
uint8_t boardControlRegisterData = readFromRegister<uint8_t>(CONTROL_REGISTER_INFO);
212236
if ((boardControlRegisterData & 1) == static_cast<int>(enabled)) {
213237
return; // Value is already the same
214238
}
215-
writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~1) | enabled);
239+
if(!writeToRegister(CONTROL_REGISTER_INFO, (boardControlRegisterData & ~1) | enabled)){
240+
return false;
241+
}
242+
243+
if(persist){
244+
return persistRegister(CONTROL_REGISTER_INFO);
245+
}
246+
247+
return true;
216248
}
217249

218-
void NiclaSenseEnv::setDeviceAddress(int address) {
250+
bool NiclaSenseEnv::setDeviceAddress(int address, bool persist) {
219251
if (address < 0 || address > 127) {
220252
return; // Invalid address
221253
}
@@ -224,9 +256,18 @@ void NiclaSenseEnv::setDeviceAddress(int address) {
224256
if ((addressRegisterData & 127) == address) {
225257
return; // Value is already the same
226258
}
227-
writeToRegister(SLAVE_ADDRESS_REGISTER_INFO, (addressRegisterData & ~127) | address);
259+
if(!writeToRegister(SLAVE_ADDRESS_REGISTER_INFO, (addressRegisterData & ~127) | address)){
260+
return false;
261+
}
262+
228263
delayMicroseconds(100); // Wait for the new address to take effect
229264
this->i2cDeviceAddress = address;
265+
266+
if (persist) {
267+
return persistRegister(SLAVE_ADDRESS_REGISTER_INFO);
268+
}
269+
270+
return true;
230271
}
231272

232273
// Function to get the index for a given baud rate

src/NiclaSenseEnv.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ class NiclaSenseEnv : public I2CDevice {
163163

164164
/**
165165
* @brief Sets the baud rate for the UART communication.
166-
* Call persistSettings() on NiclaSenseEnv instance after changing the baud rate to make the change persistent.
167166
* @param baudRate The desired baud rate for the UART communication.
168167
The supported values are: 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
168+
* @param persist Set to true to store the setting in flash, to false otherwise.
169169
*/
170-
void setUARTBaudRate(int baudRate);
170+
bool setUARTBaudRate(int baudRate, bool persist);
171171

172172
/**
173173
* @brief Checks if UART CSV output is enabled.
@@ -179,7 +179,6 @@ class NiclaSenseEnv : public I2CDevice {
179179
/**
180180
* @brief Sets the UART CSV output enabled or disabled.
181181
* Enables or disables CSV output over UART.
182-
* Call persistSettings() on NiclaSenseEnv instance after changing the CSV output mode to make the change persistent.
183182
*
184183
* The column names and their order are:
185184
* HS4001 sample counter, HS4001 temperature (degC), HS4001 humidity (%RH), ZMOD4510 status, ZMOD4510 sample counter,
@@ -193,8 +192,10 @@ class NiclaSenseEnv : public I2CDevice {
193192
* Only the columns for this sensor will be filled, the other columns will be empty.
194193
*
195194
* @param enabled True to enable UART CSV output, false to disable.
195+
* @param persist True to store the setting in flash, false otherwise.
196+
* When set to True, it will also persist the value set via `setDebuggingEnabled`.
196197
*/
197-
void setUARTCSVOutputEnabled(bool enabled);
198+
bool setUARTCSVOutputEnabled(bool enabled, bool persist = false);
198199

199200
/**
200201
* @brief Gets the CSV delimiter character.
@@ -205,10 +206,10 @@ class NiclaSenseEnv : public I2CDevice {
205206

206207
/**
207208
* @brief Sets the CSV delimiter for parsing CSV data.
208-
* Call persistSettings() on NiclaSenseEnv instance after changing the CSV delimiter to make the change persistent.
209209
* @param delimiter The character to be used as the CSV delimiter.
210+
* @param persist If true, the change will be saved to flash memory.
210211
*/
211-
void setCSVDelimiter(char delimiter);
212+
bool setCSVDelimiter(char delimiter, bool persist = false);
212213

213214
/**
214215
* @brief Checks if debugging is enabled.
@@ -220,18 +221,19 @@ class NiclaSenseEnv : public I2CDevice {
220221
/**
221222
* @brief Toggles the debugging mode.
222223
* When debugging mode is enabled, the board will send additional debug messages over UART.
223-
* Call persistSettings() on NiclaSenseEnv instance after changing the debugging mode to make the change persistent.
224224
* @param enabled A boolean value indicating whether debugging is enabled or not.
225+
* @param persist If true, the change will be saved to flash memory.
226+
* When setting this to true the value set via `setUARTCSVOutputEnabled` will also be persisted.
225227
*/
226-
void setDebuggingEnabled(bool enabled);
228+
bool setDebuggingEnabled(bool enabled, bool persist = false);
227229

228230
/**
229231
* @brief Sets the I2C address of the device.
230-
* Call persistSettings() on NiclaSenseEnv instance after changing the address to make the change persistent.
231232
*
232233
* @param address The new I2C address. Valid values are 0 to 127.
234+
* @param persist If true, the change will be saved to flash memory.
233235
*/
234-
void setDeviceAddress(int address);
236+
bool setDeviceAddress(int address, bool persist = false);
235237

236238
private:
237239
/**

src/OrangeLED.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ uint8_t OrangeLED::brightness() {
1111
return map(brightness, 0, 63, 0, 255);
1212
}
1313

14-
void OrangeLED::setBrightness(uint8_t brightness) {
14+
bool OrangeLED::setBrightness(uint8_t brightness, bool persist) {
1515
if (brightness > 255) {
1616
return; // Invalid brightness value
1717
}
@@ -20,6 +20,12 @@ void OrangeLED::setBrightness(uint8_t brightness) {
2020
uint8_t currentRegisterData = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
2121
// Overwrite bits 0 - 5 with the new value
2222
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | mappedBrightness);
23+
24+
if (persist) {
25+
return persistRegister(ORANGE_LED_REGISTER_INFO);
26+
}
27+
28+
return true;
2329
}
2430

2531
bool OrangeLED::errorStatusEnabled() {
@@ -28,8 +34,14 @@ bool OrangeLED::errorStatusEnabled() {
2834
return data & (1 << 7);
2935
}
3036

31-
void OrangeLED::setErrorStatusEnabled(bool enabled) {
37+
bool OrangeLED::setErrorStatusEnabled(bool enabled, bool persist) {
3238
uint8_t currentRegisterData = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
3339
// Set bit 7 to 1 if enabled or 0 if disabled while keeping the other bits unchanged
3440
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~(1 << 7)) | (enabled << 7));
41+
42+
if (persist) {
43+
return persistRegister(ORANGE_LED_REGISTER_INFO);
44+
}
45+
46+
return true;
3547
}

src/OrangeLED.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class OrangeLED : public I2CDevice {
3232

3333
/**
3434
* Sets the brightness of the orange LED.
35-
* Call persistSettings() on NiclaSenseEnv instance after changing the orange LED brightness to make the change persistent.
36-
* @param brightness : The brightness of the orange LED. Range is 0 to 63.
35+
* When persist is true, the `errorStatusEnabled` setting will also be persisted.
36+
* @param brightness : The brightness of the orange LED. Range is 0 to 255.
37+
* @param persist : If true, the brightness setting will be saved to flash memory.
3738
*/
38-
void setBrightness(uint8_t brightness = 63);
39+
bool setBrightness(uint8_t brightness, bool persist = false);
3940

4041
/**
4142
* 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 {
4647

4748
/**
4849
* Enables or disables the orange LED to indicate an error status of one of the sensors.
49-
* Call persistSettings() on NiclaSenseEnv instance after enabling/disabling the orange LED error status to make the change persistent.
50+
* When persist is true, the brightness setting will also be saved to flash memory.
5051
* @param enabled : Whether to enable or disable the orange LED error status.
52+
* @param persist : If true, the change will be saved to flash memory.
5153
*/
52-
void setErrorStatusEnabled(bool enabled);
54+
bool setErrorStatusEnabled(bool enabled, bool persist = false);
5355
};

0 commit comments

Comments
 (0)