text: /***************************************************************************//**
   * @file
   * @brief RGB LED driver for BRD2601B
   *******************************************************************************
   * # License
   * <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
   *******************************************************************************
   *
   * SPDX-License-Identifier: Zlib
   *
   * The licensor of this software is Silicon Laboratories Inc.
   *
   * This software is provided 'as-is', without any express or implied
   * warranty. In no event will the authors be held liable for any damages
   * arising from the use of this software.
   *
   * Permission is granted to anyone to use this software for any purpose,
   * including commercial applications, and to alter it and redistribute it
   * freely, subject to the following restrictions:
   *
   * 1. The origin of this software must not be misrepresented; you must not
   *    claim that you wrote the original software. If you use this software
   *    in a product, an acknowledgment in the product documentation would be
   *    appreciated but is not required.
   * 2. Altered source versions must be plainly marked as such, and must not be
   *    misrepresented as being the original software.
   * 3. This notice may not be removed or altered from any source distribution.
   *
   ******************************************************************************/
  
  #include "em_gpio.h"
  #include "sl_simple_rgb_pwm_led_instances.h"
  #include "sl_simple_rgb_pwm_led.h"
  #include "app_log.h"
  #include "board.h"
  
  #define ADV_LED_RED_INTENSITY   127
  #define ADV_LED_GREEN_INTENSITY 0
  #define ADV_LED_BLUE_INTENSITY  0
  
  // -----------------------------------------------------------------------------
  // Private variables
  
  //  Array to linearize the light level of the RGB LEDs
  static const uint8_t light_levels[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
    0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05,
    0x05, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x09, 0x0A, 0x0A, 0x0B, 0x0B,
    0x0C, 0x0C, 0x0D, 0x0D, 0x0E, 0x0F, 0x0F,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FE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF
  };
  
  // -----------------------------------------------------------------------------
  // Public function definitions
  
  void rgb_led_init(void)
  {
  }
  
  void rgb_led_deinit(void)
  {
    sl_simple_rgb_pwm_led_turn_off(sl_led_rgb.led_common.context);
  }
  
  void rgb_led_set(uint8_t m, uint8_t r, uint8_t g, uint8_t b)
  {
    // Make sure the mask corresponds to the RGB LEDs of the board.
    if (m != (m & BOARD_RGBLED_MASK)) {
      m = m & BOARD_RGBLED_MASK;
      app_log_error("Undefined RGB LED is trying to be set." APP_LOG_NL);
    }
  
    if (m != 0) {
      sl_simple_rgb_pwm_led_set_color(sl_led_rgb.led_common.context,
                                      light_levels[r],
                                      light_levels[g],
                                      light_levels[b]);
    } else {
      sl_simple_rgb_pwm_led_turn_off(sl_led_rgb.led_common.context);
    }
  }
  
  void adv_led_turn_on(void)
  {
    sl_simple_rgb_pwm_led_set_color(sl_led_rgb.led_common.context,
                                    light_levels[ADV_LED_RED_INTENSITY],
                                    light_levels[ADV_LED_GREEN_INTENSITY],
                                    light_levels[ADV_LED_BLUE_INTENSITY]);
  }
  
  void adv_led_turn_off(void)
  {
    sl_simple_rgb_pwm_led_turn_off(sl_led_rgb.led_common.context);
  }
  
  void adv_led_toggle(void)
  {
    sl_simple_rgb_pwm_led_toggle(sl_led_rgb.led_common.context);
  }
  