From f7f722f62f82ccf8c19e7574a76a8854c9fe30da Mon Sep 17 00:00:00 2001
From: Hussein Elsherbini <Hussein.Elsherbini@silabs.com>
Date: Mon, 19 Sep 2022 19:30:32 +0000
Subject: [PATCH] Pull request #116: re-enabled Jenkin custom example builds,
 fixed various issue with RGB example functionality

Merge in WMN_TOOLS/matter from custom_examples to silabs

Squashed commit of the following:

commit a2b4829c9babddf87d6330fe0b5a46cc8b92636d
Author: Hussein Elsherbini <huelsher@silabs.com>
Date:   Mon Sep 19 14:58:14 2022 -0400

    added BRD4166A to custom build boards
    JIRA: MATTER-693

commit 1949cd27b62c1e9b302cff363a90ce49008709ac
Author: Hussein Elsherbini <huelsher@silabs.com>
Date:   Mon Sep 19 14:40:40 2022 -0400

    re-enabled Jenkin custom example builds, fixed various issue with RGB example functionality
    JIRA: MATTER-693
---
 .../efr32/light_modules/led_widget_rgb.cpp    | 59 ++++++++++++++++---
 1 file changed, 51 insertions(+), 8 deletions(-)

diff --git a/silabs_examples/sl-newLight/efr32/light_modules/led_widget_rgb.cpp b/silabs_examples/sl-newLight/efr32/light_modules/led_widget_rgb.cpp
index 921ad9fe59ba74..30f755ad0ce8c5 100644
--- a/silabs_examples/sl-newLight/efr32/light_modules/led_widget_rgb.cpp
+++ b/silabs_examples/sl-newLight/efr32/light_modules/led_widget_rgb.cpp
@@ -40,8 +40,11 @@
 #include "sl_led.h"
 #include "sl_pwm_led.h"
 #include "sl_simple_rgb_pwm_led.h"
+#include "AppTask.h"
+
+using namespace chip;
+using namespace ::chip::DeviceLayer;
 
-using namespace chip::app::Clusters::OnOff;
 /**
  * @brief Red led instance.
  */
@@ -168,7 +171,6 @@ void LEDWidgetRGB::Init(const sl_led_rgb_pwm_t* led)
 void LEDWidgetRGB::SetLevel(uint8_t level)
 {
     bool currentValue;
-    EmberAfStatus status;
     /* 1. Check if the input value is correct. */
     if (level > ATTRIBUTE_LEVEL_MAX)
     {
@@ -184,7 +186,15 @@ void LEDWidgetRGB::SetLevel(uint8_t level)
     }
 
     /* 2. Update the color. */
-    this->SetColor(this->current_hue_, this->current_saturation_, this->current_level_);
+    chip::DeviceLayer::PlatformMgr().LockChipStack();
+    OnOffServer::Instance().getOnOffValue(1, &currentValue);
+    chip::DeviceLayer::PlatformMgr().UnlockChipStack();
+
+    if(currentValue){
+        
+        this->SetColor(this->current_hue_, this->current_saturation_, this->current_level_);
+    }
+    
 }
 
 
@@ -229,25 +239,44 @@ void LEDWidgetRGB::GetLevel(ColorElements* rgb)
 
 void LEDWidgetRGB::SetHue(uint8_t hue)
 {
+    bool currentValue;
     // Hue takes a value [0, 360] and is expressed in degrees.
     // See appclusters, section 3.2.7.1.
+    chip::DeviceLayer::PlatformMgr().LockChipStack();
+    OnOffServer::Instance().getOnOffValue(1, &currentValue);
+    chip::DeviceLayer::PlatformMgr().UnlockChipStack();
+
     this->current_hue_ = static_cast<uint16_t>((hue * 360) / ATTRIBUTE_LEVEL_MAX);
-    this->SetColor(this->current_hue_, this->current_saturation_, this->current_level_); 
+    if(currentValue){
+        
+        this->SetColor(this->current_hue_, this->current_saturation_, this->current_level_);
+    }
+    
+   
 }
 
 
 void LEDWidgetRGB::SetSaturation(uint8_t sat)
 {
+    bool currentValue;
     // Saturation takes a value [0, 1] representing a percentage.
     // The Color Control cluster accepts saturation values 0 to 254.
     // See appclusters, section 3.2.7.2.
-    this->current_saturation_ = sat/254.0;
-    this->SetColor(this->current_hue_, this->current_saturation_, this->current_level_);
+    chip::DeviceLayer::PlatformMgr().LockChipStack();
+    OnOffServer::Instance().getOnOffValue(1, &currentValue);
+    chip::DeviceLayer::PlatformMgr().UnlockChipStack();
+    
+    this->current_saturation_ = static_cast<uint16_t>((sat * 360) / ATTRIBUTE_LEVEL_MAX);
+    if(currentValue){
+        
+        this->SetColor(this->current_hue_, this->current_saturation_, this->current_level_);
+    }
 }
 
 
 void LEDWidgetRGB::SetColor(uint8_t hue, float saturation, uint8_t level)
 {
+    bool currentValue;
     /* 1. Convert the hue and saturation input to RGB values. (HSV to RGB conversion) */
     ColorElements rgb = 
     {
@@ -257,14 +286,20 @@ void LEDWidgetRGB::SetColor(uint8_t hue, float saturation, uint8_t level)
     };
 
     HueToRGB(hue, saturation, level, &rgb, PWM_MAX_VALUE);
+    chip::DeviceLayer::PlatformMgr().LockChipStack();
+    OnOffServer::Instance().getOnOffValue(1, &currentValue);
+    chip::DeviceLayer::PlatformMgr().UnlockChipStack();
 
     /* 2. Set the color values. */
-    this->SetColorRGB(&rgb);
+    if(currentValue){
+        this->SetColorRGB(&rgb);      
+    }
 }
 
 
 void LEDWidgetRGB::SetColorRGB(ColorElements* rgb)
 {
+    bool currentValue;
     /* 1. Verify that the struct argument is not null. */
     if (rgb == nullptr)
     {
@@ -273,7 +308,15 @@ void LEDWidgetRGB::SetColorRGB(ColorElements* rgb)
     }
 
     /* 2. Call the PWM driver to set the new values. */
-    this->led_rgb_->set_rgb_color(this->led_rgb_->led_common.context, rgb->red_value, rgb->green_value, rgb->blue_value);
+    
+    chip::DeviceLayer::PlatformMgr().LockChipStack();
+    OnOffServer::Instance().getOnOffValue(1, &currentValue);
+    chip::DeviceLayer::PlatformMgr().UnlockChipStack();
+
+    /* 2. Set the color values. */
+    if(currentValue){
+        this->led_rgb_->set_rgb_color(this->led_rgb_->led_common.context, rgb->red_value, rgb->green_value, rgb->blue_value);   
+    }
 }