Skip to content

Commit f88c30a

Browse files
[SVE2] Add timer compensation in level control cluster
Signed-off-by: Andrei Menzopol <[email protected]>
1 parent a562b07 commit f88c30a

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/app/clusters/level-control/level-control.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ typedef struct
110110

111111
static EmberAfLevelControlState stateTable[kLevelControlStateTableSize];
112112

113+
static chip::System::Clock::Timestamp nextTimestamp;
114+
113115
static EmberAfLevelControlState * getState(EndpointId endpoint);
114116

115117
static EmberAfStatus moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t level,
@@ -139,6 +141,27 @@ static void timerCallback(System::Layer *, void * callbackContext)
139141
emberAfLevelControlClusterServerTickCallback(static_cast<EndpointId>(reinterpret_cast<uintptr_t>(callbackContext)));
140142
}
141143

144+
static uint32_t calculateNextTimestampMs(int32_t delayMs)
145+
{
146+
int32_t waitTime, latency;
147+
const chip::System::Clock::Timestamp currentTime = chip::System::SystemClock().GetMonotonicTimestamp();
148+
149+
latency = (int32_t)currentTime.count() - (int32_t)nextTimestamp.count();
150+
151+
if(latency > delayMs)
152+
{
153+
waitTime = 0;
154+
}
155+
else
156+
{
157+
waitTime = delayMs - latency;
158+
}
159+
160+
nextTimestamp += chip::System::Clock::Milliseconds32(delayMs);
161+
162+
return (uint32_t)waitTime;
163+
}
164+
142165
static void schedule(EndpointId endpoint, uint32_t delayMs)
143166
{
144167
DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(delayMs), timerCallback,
@@ -271,7 +294,7 @@ void emberAfLevelControlClusterServerTickCallback(EndpointId endpoint)
271294
else
272295
{
273296
writeRemainingTime(endpoint, static_cast<uint16_t>(state->transitionTimeMs - state->elapsedTimeMs));
274-
schedule(endpoint, state->eventDurationMs);
297+
schedule(endpoint, calculateNextTimestampMs(state->eventDurationMs));
275298
}
276299
}
277300

@@ -702,7 +725,8 @@ static EmberAfStatus moveToLevelHandler(EndpointId endpoint, CommandId commandId
702725
state->storedLevel = storedLevel;
703726

704727
// The setup was successful, so mark the new state as active and return.
705-
schedule(endpoint, state->eventDurationMs);
728+
nextTimestamp = chip::System::SystemClock().GetMonotonicTimestamp();
729+
schedule(endpoint, calculateNextTimestampMs(state->eventDurationMs));
706730
status = EMBER_ZCL_STATUS_SUCCESS;
707731

708732
if (commandId == Commands::MoveToLevelWithOnOff::Id)
@@ -828,7 +852,8 @@ static void moveHandler(EndpointId endpoint, CommandId commandId, uint8_t moveMo
828852
state->storedLevel = INVALID_STORED_LEVEL;
829853

830854
// The setup was successful, so mark the new state as active and return.
831-
schedule(endpoint, state->eventDurationMs);
855+
nextTimestamp = chip::System::SystemClock().GetMonotonicTimestamp();
856+
schedule(endpoint, calculateNextTimestampMs(state->eventDurationMs));
832857
status = EMBER_ZCL_STATUS_SUCCESS;
833858

834859
send_default_response:
@@ -954,7 +979,8 @@ static void stepHandler(EndpointId endpoint, CommandId commandId, uint8_t stepMo
954979
state->storedLevel = INVALID_STORED_LEVEL;
955980

956981
// The setup was successful, so mark the new state as active and return.
957-
schedule(endpoint, state->eventDurationMs);
982+
nextTimestamp = chip::System::SystemClock().GetMonotonicTimestamp();
983+
schedule(endpoint, calculateNextTimestampMs(state->eventDurationMs));
958984
status = EMBER_ZCL_STATUS_SUCCESS;
959985

960986
send_default_response:

0 commit comments

Comments
 (0)