Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the timing inside on-off server cluster more accurate #21755

Merged
merged 4 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/app/clusters/on-off-server/on-off-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,26 @@ bool OnOffServer::OnWithRecallGlobalSceneCommand(app::CommandHandler * commandOb
return true;
}

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;

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

nextDesiredOnWithTimedOffTimestamp += UPDATE_TIME_MS;

return (uint32_t) waitTime.count();
}

bool OnOffServer::OnWithTimedOffCommand(const app::ConcreteCommandPath & commandPath,
const Commands::OnWithTimedOff::DecodableType & commandData)
{
Expand Down Expand Up @@ -503,7 +523,8 @@ bool OnOffServer::OnWithTimedOffCommand(const app::ConcreteCommandPath & command

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

exit:
Expand All @@ -526,7 +547,7 @@ void OnOffServer::updateOnOffTimeCommand(chip::EndpointId endpoint)
if (isOn) // OnOff On case
{
// Restart Timer
emberEventControlSetDelayMS(configureEventControl(endpoint), UPDATE_TIME_MS);
emberEventControlSetDelayMS(configureEventControl(endpoint), calculateNextWaitTimeMS());

// Update onTime values
uint16_t onTime = MIN_TIME_VALUE;
Expand Down Expand Up @@ -565,7 +586,7 @@ void OnOffServer::updateOnOffTimeCommand(chip::EndpointId endpoint)
if (offWaitTime > 0)
{
// Restart Timer
emberEventControlSetDelayMS(configureEventControl(endpoint), UPDATE_TIME_MS);
emberEventControlSetDelayMS(configureEventControl(endpoint), calculateNextWaitTimeMS());
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions src/app/clusters/on-off-server/on-off-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ using chip::app::Clusters::OnOff::OnOffFeature;
* Defines and Macros
*********************************************************/

static constexpr uint8_t UPDATE_TIME_MS = 100;
static constexpr uint16_t TRANSITION_TIME_1S = 10;
static constexpr chip::System::Clock::Milliseconds32 UPDATE_TIME_MS = chip::System::Clock::Milliseconds32(100);
static constexpr uint16_t TRANSITION_TIME_1S = 10;

static constexpr uint16_t MAX_TIME_VALUE = 0xFFFF;
static constexpr uint8_t MIN_TIME_VALUE = 1;
Expand Down Expand Up @@ -79,12 +79,14 @@ class OnOffServer
EmberEventControl * getEventControl(chip::EndpointId endpoint);
EmberEventControl * configureEventControl(chip::EndpointId endpoint);

uint32_t calculateNextWaitTimeMS(void);
/**********************************************************
* Attributes Declaration
*********************************************************/

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

struct OnOffEffect
Expand Down