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.
153 lines
4.5 KiB
153 lines
4.5 KiB
#include "board_light_ctrl.h"
|
|
|
|
#include "board.h"
|
|
#include "znordic.h"
|
|
|
|
#define BLINK_CNT 1
|
|
#define BLINK_PERIOD_MS (100)
|
|
#define QUICK_BLINK_PERIOD_MS (100)
|
|
#define SLOW_BLINK_PERIOD_MS (2000)
|
|
// LED_GREEN_PIN
|
|
|
|
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;
|
|
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_BLUE_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_t m_light_effect;
|
|
static bool m_led_green_light_state;
|
|
static int m_blink_cnt;
|
|
APP_TIMER_DEF(m_green_light_effect_tmr);
|
|
|
|
static bool m_boardlight_greenlightstate = false;
|
|
|
|
void BoardLight_toggleGreenLightState() { BoardLight_setGreenLightState(!m_boardlight_greenlightstate); }
|
|
void BoardLight_setGreenLightState(bool state) {
|
|
m_boardlight_greenlightstate = state;
|
|
if (state) {
|
|
light_ctrl.seq_values.channel_0 = 90; // 设置占空比,数值最大不超过 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);
|
|
}
|
|
}
|
|
|
|
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 >= SLOW_BLINK_PERIOD_MS) {
|
|
m_blink_cnt = 0;
|
|
return;
|
|
}
|
|
}
|
|
} else if (m_light_effect == kLightEffect_quickFlash) {
|
|
if (BLINK_PERIOD_MS * m_blink_cnt >= QUICK_BLINK_PERIOD_MS) {
|
|
BoardLight_toggleGreenLightState();
|
|
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));
|
|
APP_ERROR_CHECK(nrfx_pwm_init(&light_ctrl.driver, &light_ctrl.config, NULL));
|
|
}
|
|
static bool m_boardlight_loaded = false;
|
|
|
|
void BoardLight_load() {
|
|
if (m_boardlight_loaded) {
|
|
return;
|
|
}
|
|
m_boardlight_loaded = true;
|
|
BoardLight_setGreenLightState(false);
|
|
}
|
|
void BoardLight_unload() {
|
|
if (!m_boardlight_loaded) {
|
|
return;
|
|
}
|
|
m_boardlight_loaded = false;
|
|
app_timer_stop(m_green_light_effect_tmr);
|
|
BoardLight_setGreenLightEffect(kLightEffect_close);
|
|
// nrfx_pwm_uninit(&light_ctrl.driver);
|
|
}
|
|
|
|
void BoardLight_setGreenLightEffect(LightEffect_t effect) {
|
|
if (m_light_effect == effect) return;
|
|
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;
|
|
case kLightEffect_quickFlash:
|
|
app_timer_start(m_green_light_effect_tmr, APP_TIMER_TICKS(BLINK_PERIOD_MS), NULL);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return;
|
|
}
|
|
|
|
void BoardLight_blockFlash(int times, int periodms) {
|
|
for (int i = 0; i < times; i++) {
|
|
BoardLight_setGreenLightState(true);
|
|
nrf_delay_ms(periodms);
|
|
BoardLight_setGreenLightState(false);
|
|
nrf_delay_ms(periodms);
|
|
}
|
|
}
|