Skip to content

Commit

Permalink
[QPG6100] Fix sync of cluster attributes on manual change of light st…
Browse files Browse the repository at this point in the history
…atus (#7878)

* * Fix sync of cluster state on manual change of light status
* Cleanup logging LightingManager/Attribute updates

* Kick GH actions - ESP32/QEMU test
  • Loading branch information
tima-q authored and pull[bot] committed Jul 12, 2021
1 parent de0884c commit 1015947
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
37 changes: 27 additions & 10 deletions examples/lighting-app/qpg6100/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ void AppTask::AppTaskMain(void * pvParameter)
void AppTask::LightingActionEventHandler(AppEvent * aEvent)
{
LightingManager::Action_t action;

if (aEvent->Type == AppEvent::kEventType_Button)
{
// Toggle light
if (LightingMgr().IsTurnedOn())
{
action = LightingManager::OFF_ACTION;
Expand All @@ -195,48 +197,58 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent)
{
action = LightingManager::ON_ACTION;
}

sAppTask.mSyncClusterToButtonAction = true;
LightingMgr().InitiateAction(action, 0, 0, 0);
}
if (aEvent->Type == AppEvent::kEventType_Level && aEvent->ButtonEvent.Action != 0)
{
// Toggle Dimming of light between 2 fixed levels
uint8_t val = 0x0;
val = LightingMgr().GetLevel() == 0x7f ? 0x1 : 0x7f;
action = LightingManager::LEVEL_ACTION;

sAppTask.mSyncClusterToButtonAction = true;
LightingMgr().InitiateAction(action, 0, 1, &val);
}
return;
}

void AppTask::ButtonEventHandler(uint8_t btnIdx, bool btnPressed)
{
ChipLogProgress(NotSpecified, "ButtonEventHandler %d, %d", btnIdx, btnPressed);
if (btnIdx != APP_ON_OFF_BUTTON && btnIdx != APP_FUNCTION_BUTTON && btnIdx != APP_LEVEL_BUTTON)
{
return;
}

ChipLogProgress(NotSpecified, "ButtonEventHandler %d, %d", btnIdx, btnPressed);

AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.ButtonIdx = btnIdx;
button_event.ButtonEvent.Action = btnPressed;

if (btnIdx == APP_ON_OFF_BUTTON && btnPressed == true)
{
// Hand off to Light handler - On/Off light
button_event.Handler = LightingActionEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (btnIdx == APP_LEVEL_BUTTON)
{
// Hand off to Light handler - Change level of light
button_event.Type = AppEvent::kEventType_Level;
button_event.Handler = LightingActionEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (btnIdx == APP_FUNCTION_BUTTON)
{
button_event.Type = AppEvent::kEventType_Level;
// Hand off to Functionality handler - depends on duration of press
button_event.Handler = FunctionHandler;
sAppTask.PostEvent(&button_event);
}
else
{
return;
}

sAppTask.PostEvent(&button_event);
}

void AppTask::TimerEventHandler(chip::System::Layer * aLayer, void * aAppState, chip::System::Error aError)
Expand Down Expand Up @@ -430,21 +442,26 @@ void AppTask::DispatchEvent(AppEvent * aEvent)
}
}

/**
* Update cluster status after application level changes
*/
void AppTask::UpdateClusterState(void)
{
uint8_t newValue = !LightingMgr().IsTurnedOn();
uint8_t newValue;

ChipLogProgress(NotSpecified, "UpdateClusterState");

// write the new on/off value
newValue = LightingMgr().IsTurnedOn();
EmberAfStatus status = emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
(uint8_t *) &newValue, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "ERR: updating on/off %" PRIx32, status);
}

ChipLogProgress(NotSpecified, "UpdateClusterState");
newValue = LightingMgr().GetLevel();
// TODO understand well enough to implement the level cluster ZCL_CURRENT_LEVEL_ATTRIBUTE_ID
status = emberAfWriteAttribute(1, ZCL_LEVEL_CONTROL_CLUSTER_ID, ZCL_CURRENT_LEVEL_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
status = emberAfWriteAttribute(1, ZCL_LEVEL_CONTROL_CLUSTER_ID, ZCL_CURRENT_LEVEL_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
(uint8_t *) &newValue, ZCL_DATA8_ATTRIBUTE_TYPE);

if (status != EMBER_ZCL_STATUS_SUCCESS)
Expand Down
8 changes: 4 additions & 4 deletions examples/lighting-app/qpg6100/src/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint16_t
switch (aAction)
{
case ON_ACTION:
ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(ON_ACTION)");
ChipLogProgress(NotSpecified, "LightMgr:ON: %s->ON", mState == kState_On ? "ON" : "OFF");
break;
case OFF_ACTION:
ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(OFF_ACTION)");
ChipLogProgress(NotSpecified, "LightMgr:OFF: %s->OFF", mState == kState_On ? "ON" : "OFF");
break;
case LEVEL_ACTION:
ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(LEVEL_ACTION)");
ChipLogProgress(NotSpecified, "LightMgr:LEVEL: lev:%u->%u", mLevel, *value);
break;
default:
ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(unknown)");
ChipLogProgress(NotSpecified, "LightMgr:Unknown");
break;
}

Expand Down
7 changes: 3 additions & 4 deletions examples/lighting-app/qpg6100/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId clusterId
return;
}

LightingMgr().InitiateAction(*value ? LightingManager::ON_ACTION : LightingManager::OFF_ACTION, AppEvent::kEventType_Level,
size, value);
LightingMgr().InitiateAction(*value ? LightingManager::ON_ACTION : LightingManager::OFF_ACTION, 0, size, value);
}
else if (clusterId == ZCL_LEVEL_CONTROL_CLUSTER_ID)
{
Expand All @@ -53,10 +52,10 @@ void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId clusterId
return;
}

ChipLogProgress(Zcl, "Value: %u, length %u", *value, size);
if (size == 1)
{
LightingMgr().InitiateAction(LightingManager::LEVEL_ACTION, AppEvent::kEventType_Level, size, value);
ChipLogProgress(Zcl, "New level: %u", *value);
LightingMgr().InitiateAction(LightingManager::LEVEL_ACTION, 0, size, value);
}
else
{
Expand Down

0 comments on commit 1015947

Please sign in to comment.