Skip to content

Commit

Permalink
even simpler: nextDesiredOnWithTimedOffTimestamp reflects the desired…
Browse files Browse the repository at this point in the history
… time stamp for the next state machine step. This variable gets adjusted by UPDATE_TIME_MS and the wait time gets calculated by UPDATE_TIME_MS - last latency
  • Loading branch information
bauerschwan authored and woody-apple committed Aug 10, 2022
1 parent a2dcce4 commit 914248b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions src/app/clusters/on-off-server/on-off-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,22 +457,17 @@ uint32_t OnOffServer::calculateNextWaitTimeMS(void)
const chip::System::Clock::Timestamp currentTime = chip::System::SystemClock().GetMonotonicTimestamp();
chip::System::Clock::Timestamp waitTime = UPDATE_TIME_MS;
chip::System::Clock::Timestamp latency;
chip::System::Clock::Timestamp elapsed;

if (currentTime > onWithTimedOffStartTimestamp)
if (currentTime > nextDesiredOnWithTimedOffTimestamp)
{
elapsed = currentTime - onWithTimedOffStartTimestamp;
if (elapsed >= UPDATE_TIME_MS)
{
latency = elapsed - UPDATE_TIME_MS;
if (waitTime > latency)
waitTime -= latency;
else if (waitTime == latency)
waitTime = chip::System::Clock::Milliseconds32(1);
}
latency = currentTime - nextDesiredOnWithTimedOffTimestamp;
if (latency >= UPDATE_TIME_MS)
waitTime = chip::System::Clock::Milliseconds32(1);
else
waitTime -= latency;
}

onWithTimedOffStartTimestamp = currentTime;
nextDesiredOnWithTimedOffTimestamp += UPDATE_TIME_MS;

return (uint32_t)waitTime.count();
}
Expand Down Expand Up @@ -528,7 +523,7 @@ bool OnOffServer::OnWithTimedOffCommand(const app::ConcreteCommandPath & command

if (currentOnTime < MAX_TIME_VALUE && currentOffWaitTime < MAX_TIME_VALUE)
{
onWithTimedOffStartTimestamp = chip::System::SystemClock().GetMonotonicTimestamp();
nextDesiredOnWithTimedOffTimestamp = chip::System::SystemClock().GetMonotonicTimestamp() + UPDATE_TIME_MS;
emberEventControlSetDelayMS(configureEventControl(endpoint), (uint32_t)UPDATE_TIME_MS.count());
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/clusters/on-off-server/on-off-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class OnOffServer

static OnOffServer instance;
EmberEventControl eventControls[EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT];
chip::System::Clock::Timestamp onWithTimedOffStartTimestamp;
chip::System::Clock::Timestamp nextDesiredOnWithTimedOffTimestamp;
};

struct OnOffEffect
Expand Down

0 comments on commit 914248b

Please sign in to comment.