Skip to content

Commit

Permalink
Posting SensorTimerEventHandler as an event to AppTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
arun-silabs committed Oct 3, 2024
1 parent c669c0c commit 90db04b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
9 changes: 2 additions & 7 deletions examples/thermostat/silabs/include/SensorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,15 @@ class SensorManager
{
public:
CHIP_ERROR Init();
struct AttributeUpdateInfo
{
int16_t temperature;
uint16_t endPoint;
};

private:
static void UpdateTemperatureAttribute(intptr_t context);
friend SensorManager & SensorMgr();

osTimerId_t mSensorTimer;

// Reads new generated sensor value, stores it, and updates local temperature attribute
static void SensorTimerEventHandler(void * arg);
// Reads new generated sensor value, stores it, and updates local temperature attribute
static void TemperatureUpdateEventHandler(AppEvent * aEvent);

static SensorManager sSensorManager;
};
Expand Down
26 changes: 13 additions & 13 deletions examples/thermostat/silabs/src/SensorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ CHIP_ERROR SensorManager::Init()
}

void SensorManager::SensorTimerEventHandler(void * arg)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.Handler = TemperatureUpdateEventHandler;

AppTask::GetAppTask().PostEvent(&event);
}

void SensorManager::TemperatureUpdateEventHandler(AppEvent * aEvent)
{
int16_t temperature = 0;
static int16_t lastTemperature = 0;
Expand Down Expand Up @@ -115,20 +124,11 @@ void SensorManager::SensorTimerEventHandler(void * arg)

if ((temperature >= (lastTemperature + kMinTemperatureDelta)) || temperature <= (lastTemperature - kMinTemperatureDelta))
{
lastTemperature = temperature;
AttributeUpdateInfo * data = chip::Platform::New<AttributeUpdateInfo>();
data->endPoint = kThermostatEndpoint;
data->temperature = temperature;
lastTemperature = temperature;
PlatformMgr().LockChipStack();
// The SensorMagager shouldn't be aware of the Endpoint ID TODO Fix this.
// TODO Per Spec we should also apply the Offset stored in the same cluster before saving the temp

chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateTemperatureAttribute, reinterpret_cast<intptr_t>(data));
app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kThermostatEndpoint, temperature);
PlatformMgr().UnlockChipStack();
}
}

void SensorManager::UpdateTemperatureAttribute(intptr_t context)
{
SensorManager::AttributeUpdateInfo * data = reinterpret_cast<SensorManager::AttributeUpdateInfo *>(context);
app::Clusters::Thermostat::Attributes::LocalTemperature::Set(data->endPoint, data->temperature);
chip::Platform::Delete(data);
}

0 comments on commit 90db04b

Please sign in to comment.