You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.7 KiB
136 lines
3.7 KiB
#include "board_light_ctrl.h"
|
|
|
|
#include "board.h"
|
|
#include "znordic.h"
|
|
|
|
#define BLINK_CNT 1
|
|
#define BLINK_PERIOD_MS (100)
|
|
|
|
#define LIGHT_PWM_DRIVER 1
|
|
|
|
// LED_GREEN_PIN
|
|
|
|
#if LIGHT_PWM_DRIVER
|
|
|
|
typedef struct {
|
|
nrf_drv_pwm_t driver;
|
|
nrf_pwm_values_individual_t seq_values;
|
|
nrf_pwm_sequence_t seq;
|
|
nrf_drv_pwm_config_t config;
|
|
} pwm_light_ctrl_t;
|
|
|
|
#endif
|
|
|
|
static pwm_light_ctrl_t light_ctrl = {
|
|
.driver = NRF_DRV_PWM_INSTANCE(LIGHT_PWM_INSTANCE),
|
|
.seq_values = {0},
|
|
.seq =
|
|
{
|
|
.values.p_individual = &light_ctrl.seq_values,
|
|
.length = NRF_PWM_VALUES_LENGTH(light_ctrl.seq_values),
|
|
.repeats = 0,
|
|
.end_delay = 0,
|
|
},
|
|
.config =
|
|
{
|
|
|
|
.output_pins =
|
|
{
|
|
LED_GREEN_PIN, //
|
|
NRF_DRV_PWM_PIN_NOT_USED,
|
|
NRF_DRV_PWM_PIN_NOT_USED,
|
|
NRF_DRV_PWM_PIN_NOT_USED,
|
|
},
|
|
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
|
|
.base_clock = NRF_PWM_CLK_250kHz,
|
|
.count_mode = NRF_PWM_MODE_UP,
|
|
.top_value = 100, // 125kHz / 46 = 2.717k
|
|
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
|
|
.step_mode = NRF_PWM_STEP_AUTO,
|
|
|
|
},
|
|
};
|
|
|
|
static LightEffect m_light_effect;
|
|
static bool m_led_green_light_state;
|
|
static int m_blink_cnt;
|
|
APP_TIMER_DEF(m_green_light_effect_tmr);
|
|
|
|
void BoardLight_setGreenLightState(bool state) {
|
|
#if LIGHT_PWM_DRIVER
|
|
if (state) {
|
|
light_ctrl.seq_values.channel_0 = 95; // 设置占空比,数值最大不超过 top_value
|
|
nrfx_pwm_simple_playback(&light_ctrl.driver, &light_ctrl.seq, 1, NRF_DRV_PWM_FLAG_LOOP);
|
|
} else {
|
|
nrfx_pwm_stop(&light_ctrl.driver, true);
|
|
}
|
|
#else
|
|
if (state) {
|
|
nrf_gpio_pin_set(LED_GREEN_PIN);
|
|
m_led_green_light_state = true;
|
|
} else {
|
|
nrf_gpio_pin_clear(LED_GREEN_PIN);
|
|
m_led_green_light_state = false;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static void BoardLight_effect_tmr_handler(void* p_context) { //
|
|
if (m_light_effect == kLightEffect_close) {
|
|
if (m_led_green_light_state) {
|
|
BoardLight_setGreenLightState(false);
|
|
}
|
|
} else if (m_light_effect == kLightEffect_open) {
|
|
if (!m_led_green_light_state) {
|
|
BoardLight_setGreenLightState(true);
|
|
}
|
|
} else if (m_light_effect == kLightEffect_slowFlash) {
|
|
if (m_blink_cnt < 2) {
|
|
if (m_blink_cnt % 2 == 0) {
|
|
BoardLight_setGreenLightState(true);
|
|
} else if (m_blink_cnt % 2 == 1) {
|
|
BoardLight_setGreenLightState(false);
|
|
}
|
|
} else {
|
|
if (BLINK_PERIOD_MS * m_blink_cnt >= 1000) {
|
|
m_blink_cnt = 0;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
m_blink_cnt++;
|
|
}
|
|
|
|
void BoardLight_Init() {
|
|
ZERROR_CHECK(app_timer_create(&m_green_light_effect_tmr, APP_TIMER_MODE_REPEATED, BoardLight_effect_tmr_handler));
|
|
#if LIGHT_PWM_DRIVER
|
|
APP_ERROR_CHECK(nrfx_pwm_init(&light_ctrl.driver, &light_ctrl.config, NULL));
|
|
#else
|
|
znrf_gpio_cfg_output(LED_GREEN_PIN, NRF_GPIO_PIN_NOPULL);
|
|
#endif
|
|
}
|
|
void BoardLight_load() {}
|
|
void BoardLight_unload() { BoardLight_setGreenLightEffect(kLightEffect_close); }
|
|
|
|
void BoardLight_toggleGreenLightState() { nrf_gpio_pin_toggle(LED_GREEN_PIN); }
|
|
|
|
void BoardLight_setGreenLightEffect(LightEffect effect) {
|
|
m_light_effect = effect;
|
|
|
|
switch (effect) {
|
|
case kLightEffect_close:
|
|
app_timer_stop(m_green_light_effect_tmr);
|
|
BoardLight_setGreenLightState(false);
|
|
break;
|
|
case kLightEffect_open:
|
|
app_timer_stop(m_green_light_effect_tmr);
|
|
BoardLight_setGreenLightState(true);
|
|
break;
|
|
case kLightEffect_slowFlash:
|
|
app_timer_start(m_green_light_effect_tmr, APP_TIMER_TICKS(BLINK_PERIOD_MS), NULL);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return;
|
|
}
|