///*
// * Copyright (c) 2019 Nordic Semiconductor ASA
// *
// * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
// */

//#include <bluetooth/mesh/models.h>
//#include <dk_buttons_and_leds.h>
//#include "model_handler.h"
//#include <drivers/pwm.h>
//#include "lc_pwm_led.h"


//#include <logging/log.h>

//#define PWM_NODE            DT_ALIAS(rgb_pwm)
//#define PWM_PIN(channel)    DT_PROP(PWM_NODE, ch##channel##_pin)

//#define PWM_PERIOD 1024
//#define PWM_FLAGS 0

//static const struct device *pwm;

//static const size_t led_pins[3] = {
//	PWM_PIN(0),
//	PWM_PIN(1),
//	PWM_PIN(2),
//};

//void lc_pwm_led_init(void)
//{
//        const char *dev_name = DT_LABEL(PWM_NODE);
//	pwm = device_get_binding(dev_name);
//	if (!pwm) {
//		printk("Error: didn't find %s device\n", dev_name);
//		return;
//	}
//}


//void lc_pwm_led_set(uint16_t desired_lvl, uint16_t desired_hue, uint16_t desired_sat)
//{
//	uint32_t scaled_lvl =
//		(desired_lvl*(desired_hue - desired_sat))/BT_MESH_LIGHT_HSL_MAX;

//	pwm_pin_set_usec(pwm,
//		led_pins[3],
//		PWM_PERIOD,
//		scaled_lvl,
//		PWM_FLAGS);
//}

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <bluetooth/mesh/models.h>
#include <dk_buttons_and_leds.h>
#include "model_handler.h"
#include <drivers/pwm.h>

#define PWM_LED0_NODE	DT_ALIAS(pwm_led0)

/*
 * Devicetree helper macro which gets the 'flags' cell from the node's
 * pwms property, or returns 0 if the property has no 'flags' cell.
 */

#if DT_PHA_HAS_CELL(PWM_LED0_NODE, pwms, flags)
#define PWM_FLAGS DT_PWMS_FLAGS(PWM_LED0_NODE)
#else
#define PWM_FLAGS 0
#endif

#if DT_NODE_HAS_STATUS(PWM_LED0_NODE, okay)
#define PWM_LABEL	DT_PWMS_LABEL(PWM_LED0_NODE)
#define PWM_CHANNEL	DT_PWMS_CHANNEL(PWM_LED0_NODE)
#else
#error "Unsupported board: pwm-led0 devicetree alias is not defined"
#define PWM_LABEL	""
#define PWM_CHANNEL	0
#endif

static const struct device *pwm;
#define PWM_PERIOD 1024

void lc_pwm_led_init(void)
{
	pwm = device_get_binding(PWM_LABEL);
	if (!pwm) {
		printk("Error: didn't find %s device\n", PWM_LABEL);
		return;
	}
}

void lc_pwm_led_set(uint16_t desired_lvl)
{
	uint32_t scaled_lvl =
		(PWM_PERIOD * desired_lvl) /
		BT_MESH_LIGHTNESS_MAX;

	pwm_pin_set_usec(pwm,
		PWM_CHANNEL,
		PWM_PERIOD,
		scaled_lvl,
		PWM_FLAGS);
}












