Skip to content

Commit 006e1e3

Browse files
committed
refactor: improve i2c peripherals responsiveness
1 parent 8431b27 commit 006e1e3

16 files changed

Lines changed: 344 additions & 219 deletions

File tree

app/firmware/src/io/i2c/instance/impl/deps.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ namespace opendeck::firmware::io::i2c
108108
*/
109109
virtual bool update() = 0;
110110

111+
/**
112+
* @brief Returns the compile-time update interval for this peripheral.
113+
*
114+
* @return Minimum time between update() calls in milliseconds.
115+
*/
116+
virtual int64_t update_interval_ms()
117+
{
118+
return 1;
119+
}
120+
111121
/**
112122
* @brief Returns the peripheral name used in diagnostics.
113123
*

app/firmware/src/io/i2c/instance/impl/i2c.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ void I2c::update_peripheral(PeripheralState& state, int64_t now_ms)
135135
return;
136136
}
137137

138-
state.address_index = address_index.value();
139-
state.initialized = peripheral->init(state.address_index);
138+
state.address_index = address_index.value();
139+
state.initialized = peripheral->init(state.address_index);
140+
state.next_update_ms = now_ms;
140141

141142
if (!state.initialized)
142143
{
@@ -151,10 +152,24 @@ void I2c::update_peripheral(PeripheralState& state, int64_t now_ms)
151152
}
152153
}
153154

154-
if (state.initialized && !peripheral->update())
155+
if (!state.initialized)
156+
{
157+
return;
158+
}
159+
160+
if (now_ms < state.next_update_ms)
161+
{
162+
return;
163+
}
164+
165+
state.next_update_ms = now_ms + peripheral->update_interval_ms();
166+
167+
if (!peripheral->update())
155168
{
156169
LOG_WRN("I2C peripheral %s update failed, deinitializing", peripheral->name().data());
157170
peripheral->deinit();
158-
state.initialized = false;
171+
state.initialized = false;
172+
state.next_probe_ms = now_ms + DEVICE_PROBE_INTERVAL_MS;
173+
state.next_update_ms = 0;
159174
}
160175
}

app/firmware/src/io/i2c/instance/impl/i2c.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ namespace opendeck::firmware::io::i2c
5454

5555
struct PeripheralState
5656
{
57-
Peripheral* instance = nullptr;
58-
bool initialized = false;
59-
size_t address_index = 0;
60-
int64_t next_probe_ms = 0;
57+
Peripheral* instance = nullptr;
58+
bool initialized = false;
59+
size_t address_index = 0;
60+
int64_t next_probe_ms = 0;
61+
int64_t next_update_ms = 0;
6162
};
6263

6364
static inline std::vector<PeripheralState> peripherals = {};

app/firmware/src/io/i2c/peripherals/sensor_apds9960/instance/impl/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace opendeck::firmware::io::i2c::sensor_apds9960
7373
constexpr inline uint8_t APDS9960_CONTROL_LED_DRIVE_SHIFT = 6;
7474
constexpr inline uint8_t APDS9960_CONTROL_PROXIMITY_GAIN_SHIFT = 2;
7575

76-
constexpr inline uint8_t APDS9960_DEFAULT_ATIME = 219;
76+
constexpr inline uint8_t APDS9960_DEFAULT_ATIME = 246;
7777
constexpr inline uint8_t APDS9960_DEFAULT_WTIME = 246;
7878
constexpr inline uint8_t APDS9960_DEFAULT_PPULSE = 0x87;
7979
constexpr inline uint8_t APDS9960_DEFAULT_POFFSET_UR = 0;

0 commit comments

Comments
 (0)