Skip to content

Commit 4045b4e

Browse files
committed
Add function to persist register
1 parent d1502ed commit 4045b4e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/I2CDevice.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ I2CDevice::I2CDevice(TwoWire& bus, uint8_t deviceAddress)
88
I2CDevice::I2CDevice(uint8_t deviceAddress)
99
: bus(Wire), i2cDeviceAddress(deviceAddress) {}
1010

11+
bool I2CDevice::persistRegister(RegisterInfo registerInfo){
12+
writeToRegister(DEFAULTS_REGISTER_INFO, registerInfo.address | (1 << 7));
13+
14+
// Read bit 7 to check if the write is complete. When the write is complete, bit 7 will be 0.
15+
// Try 10 times with increasing delay between each try
16+
for (int i = 0; i < 10; ++i) {
17+
uint8_t defaultsRegisterData = readFromRegister<uint8_t>(DEFAULTS_REGISTER_INFO);
18+
if (!(defaultsRegisterData & (1 << 7))) {
19+
return true;
20+
}
21+
// Even a value of 1 us seems to work, but we start with 100 us to be safe.
22+
Serial.println("⌛️ Waiting for flash write to complete...");
23+
// Exponential sleep duration
24+
delayMicroseconds(100 * (2 << i));
25+
}
26+
27+
return false;
28+
}
29+
1130
bool I2CDevice::connected() {
1231
bus.beginTransmission(i2cDeviceAddress);
1332
return bus.endTransmission() == 0;

src/I2CDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ class I2CDevice {
141141
return bus.endTransmission() == 0;
142142
}
143143

144+
/**
145+
* @brief Makes the value of a given register persistent.
146+
* @param registerInfo The register to make persistent.
147+
*/
148+
bool persistRegister(RegisterInfo registerInfo);
149+
144150
/**
145151
* @brief Reference to the I2C bus used by the device.
146152
*/

src/registers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ constexpr RegisterInfo ZMOD4410_RHTR_REGISTER_INFO{0xC4, "float", 4};
4747
constexpr RegisterInfo ZMOD4410_TEMP_REGISTER_INFO{0xC8, "float", 4};
4848
constexpr RegisterInfo ZMOD4410_INTENSITY_REGISTER_INFO{0xCC, "float", 4};
4949
constexpr RegisterInfo ZMOD4410_ODOR_CLASS_REGISTER_INFO{0xD0, "uint8", 1};
50+
constexpr RegisterInfo DEFAULTS_REGISTER_INFO{0xD4, "uint8", 1};
5051

5152
#endif

0 commit comments

Comments
 (0)