It failed to run attributes set function in the timer callback handler.

Software version: nRF CONNECT SDK v2.4.2.  Project: Matter light bulb

I have created a soft timer, and defined a function named "autoAdjustPWMLevel" as callback handler, like the pic shows.

Everything is fine but when I want to set the attribute value and call this "Set" function, it run wrong even if it compile and build normally.

chip::app::Clusters::IlluminanceMeasurement::Attributes::MeasuredValue::Set()

Parents
  • Barry, 

    My colleague who is more experience with Matter said that you should use events in addition to timers as is done in the Adding clusters to Matter application guide (step 3). So their implementation should be more like this (I have not tested or verified this code, just based it on the guide + other samples and the customer's code)

    /*----------- src/app_event.h -----------*/
    
    enum class AppEventType {
         None = 0,
         Button,
         ButtonPushed,
         ButtonReleased,
         Timer,
         UpdateLedState,
         GLAppActivate,
         GLAppDeactivate,
         AdjustPWMLevel
    };
    
    /*----------- src/app_task.cpp -----------*/
    
    // Timers
    
    void GLApp::audoAdjustPWMLevel(k_timer *timer)
    {
        AppEvent event;
        event.Type = AppEventType::AdjustPWMLevel;
        event.Handler = AppTask::AdjustPWMLevelHandler;
        AppTask::Instance().PostEvent(event);
    }
    
    void StartGLAppTimer()
    {
        k_timer_start(&glAppTimer, K_MSEC(500), K_MSEC(500));
    }
    
    void StopGLAppTimer()
    {
        k_timer_stop(&glAppTimer);
    }
    
    void GLApp::initGLAppTimer()
    {
        k_timer_init(&glAppTimer, &GLApp::audoAdjustPWMLevel, nullptr);
        k_timer_user_data_set(&glAppTimer, this);
        k_timer_start(&glAppTimer, K_MSEC(500), K_MSEC(500));
    }
    
    
    int AppTask::Init()
    {
        /*
        ... Original content
        */
    
        k_timer_init(&glAppTimer, &GLApp::audoAdjustPWMLevel, nullptr);
        k_timer_user_data_set(&glAppTimer, this);
        return 0;
    }
    
    // Event handlers
    
    void AppTask::GLAppActivateHandler(const AppEvent &)
    {
        StartGLAppTimer();
    }
    
    void AppTask::GLAppDeactivateHandler(const AppEvent &)
    {
        StopGLAppTimer()
    }
    
    void AppTask::AdjustPWMLevelHandler(const AppEvent &)
    {
        chip::app::Clusters::IlluminanceMeasurement::Attributes::MeasuredValue::Set(1, testValue);
    }

    Not sure if this fixes the issue at hand, but this is suggested anyway to be implemented in your design.

Reply
  • Barry, 

    My colleague who is more experience with Matter said that you should use events in addition to timers as is done in the Adding clusters to Matter application guide (step 3). So their implementation should be more like this (I have not tested or verified this code, just based it on the guide + other samples and the customer's code)

    /*----------- src/app_event.h -----------*/
    
    enum class AppEventType {
         None = 0,
         Button,
         ButtonPushed,
         ButtonReleased,
         Timer,
         UpdateLedState,
         GLAppActivate,
         GLAppDeactivate,
         AdjustPWMLevel
    };
    
    /*----------- src/app_task.cpp -----------*/
    
    // Timers
    
    void GLApp::audoAdjustPWMLevel(k_timer *timer)
    {
        AppEvent event;
        event.Type = AppEventType::AdjustPWMLevel;
        event.Handler = AppTask::AdjustPWMLevelHandler;
        AppTask::Instance().PostEvent(event);
    }
    
    void StartGLAppTimer()
    {
        k_timer_start(&glAppTimer, K_MSEC(500), K_MSEC(500));
    }
    
    void StopGLAppTimer()
    {
        k_timer_stop(&glAppTimer);
    }
    
    void GLApp::initGLAppTimer()
    {
        k_timer_init(&glAppTimer, &GLApp::audoAdjustPWMLevel, nullptr);
        k_timer_user_data_set(&glAppTimer, this);
        k_timer_start(&glAppTimer, K_MSEC(500), K_MSEC(500));
    }
    
    
    int AppTask::Init()
    {
        /*
        ... Original content
        */
    
        k_timer_init(&glAppTimer, &GLApp::audoAdjustPWMLevel, nullptr);
        k_timer_user_data_set(&glAppTimer, this);
        return 0;
    }
    
    // Event handlers
    
    void AppTask::GLAppActivateHandler(const AppEvent &)
    {
        StartGLAppTimer();
    }
    
    void AppTask::GLAppDeactivateHandler(const AppEvent &)
    {
        StopGLAppTimer()
    }
    
    void AppTask::AdjustPWMLevelHandler(const AppEvent &)
    {
        chip::app::Clusters::IlluminanceMeasurement::Attributes::MeasuredValue::Set(1, testValue);
    }

    Not sure if this fixes the issue at hand, but this is suggested anyway to be implemented in your design.

Children
No Data
Related