Scene Management

Last Updated on : 2022-11-24 09:20:26

After a scene is created, the setting data will be saved, which will be read when this scene is called later. dev_scene_add_callback is used to save the data of user-defined scenes.

static uint8_t g_onoff_status = 0; // Assume it indicates the switch status.
void dev_scene_add_callback(uint8_t endpoint, uint8_t *out_data, uint8_t *in_out_len)
{
    out_data[0] = g_onoff_status;
    *in_out_len = 1;

    //TODO: Call this function after a scene is created to save the user-defined scene data.
}

void dev_scene_recall_callback(uint8_t endpoint, const scene_save_data_t *in_data)
{
    //TODO: Run a scene.
    switch(in_data->type) {
        case SCENE_DATA_TYPE_USER_DEFINE: {
            if(in_data->data[0] == 1) {
                g_onoff_status = 1;
                //TODO: Turn on the switch.
            }
            else if(in_data->data[0] == 0) {
                g_onoff_status = 1;
                //TODO: Turn off the switch.
            }
            break;
        }
        default: {
            break;
        }
    }
    return;
}

If your device is a scene control panel rather than the device that runs a scene, the following operation is required:

Determine whether to clear all the existing scenes of an endpoint before a new scene is added. If FALSE is returned, retain all the existing scenes. If TRUE is returned, clear all the existing scenes. FALSE is the default return value.

bool_t zigbee_sdk_scene_remove_before_add(uint8_t endpoint)
{
   return TRUE; // The return value is fixed to `TRUE` for the scene control panel.
}

The scene control panel should call dev_scene_recall_send_command when it recalls a scene.

bool_t devGetSceneCallback(uint16_t endpoint, uint16_t* groupId, uint8_t* sceneId);
bool_t dev_scene_recall_send_command(uint8_t endpoint, uint16_t groupId, uint8_t sceneId);

static void __scene_panel_demo(void)
{
    bool_t result = FALSE;
    uint16_t group_id = 0;
    uint8_t  scene_id = 0;

    result = devGetSceneCallback(1, &group_id, &scene_id);  /// Get the scene ID and group ID if the endpoint is 1.
    if(result) {
        dev_scene_recall_send_command(1, group_id, scene_id); /// Send the scenes of endpoint 1.
    }
}