blackmagic-esp32-s2/main/led.c

91 lines
2.8 KiB
C
Raw Normal View History

2021-10-24 20:22:24 +01:00
#include "led.h"
#include <driver/ledc.h>
2021-11-22 11:24:16 +00:00
#include <esp_log.h>
2021-10-24 20:22:24 +01:00
#include <esp_err.h>
2021-11-18 05:47:44 +00:00
#define LED_PIN_RED (6)
2021-10-24 20:22:24 +01:00
#define LED_PIN_GREEN (5)
2021-11-18 05:47:44 +00:00
#define LED_PIN_BLUE (4)
2021-10-24 20:22:24 +01:00
#define LEDC_MODE LEDC_LOW_SPEED_MODE
2021-11-22 11:24:16 +00:00
#define TAG "led"
2021-11-23 21:22:18 +00:00
#define LED_PWM_MAX_VAL 256U
#define LED_RED_MAX_VAL 20U
#define LED_GREEN_MAX_VAL 20U
#define LED_BLUE_MAX_VAL 20U
2021-10-24 20:22:24 +01:00
typedef enum {
LedChannelRed,
LedChannelGreen,
LedChannelBlue,
} ledc_channel;
void led_init() {
2021-11-22 11:24:16 +00:00
ESP_LOGI(TAG, "init");
2021-10-24 20:22:24 +01:00
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_8_BIT,
.freq_hz = 5000, // Set output frequency at 5 kHz
.clk_cfg = LEDC_AUTO_CLK};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
ledc_channel_config_t ledc_channel_red = {
.speed_mode = LEDC_MODE,
.channel = LedChannelRed,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LED_PIN_RED,
2021-11-23 21:22:18 +00:00
.duty = LED_PWM_MAX_VAL, // Set duty to 100%
2021-10-24 20:22:24 +01:00
.hpoint = 0};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_red));
ledc_channel_config_t ledc_channel_green = {
.speed_mode = LEDC_MODE,
.channel = LedChannelGreen,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LED_PIN_GREEN,
2021-11-23 21:22:18 +00:00
.duty = LED_PWM_MAX_VAL, // Set duty to 100%
2021-10-24 20:22:24 +01:00
.hpoint = 0};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_green));
ledc_channel_config_t ledc_channel_blue = {
.speed_mode = LEDC_MODE,
.channel = LedChannelBlue,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LED_PIN_BLUE,
2021-11-23 21:22:18 +00:00
.duty = LED_PWM_MAX_VAL, // Set duty to 100%
2021-10-24 20:22:24 +01:00
.hpoint = 0};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_blue));
2021-11-22 11:24:16 +00:00
ESP_LOGI(TAG, "init done");
2021-10-24 20:22:24 +01:00
}
void led_set(uint8_t red, uint8_t green, uint8_t blue) {
2021-10-28 06:03:33 +01:00
led_set_red(red);
led_set_green(green);
led_set_blue(blue);
}
void led_set_red(uint8_t value) {
2021-11-23 21:22:18 +00:00
uint32_t pwm_value = ((uint32_t)value * LED_RED_MAX_VAL) / 255;
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LedChannelRed, LED_PWM_MAX_VAL - pwm_value));
2021-10-24 20:22:24 +01:00
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LedChannelRed));
2021-10-28 06:03:33 +01:00
}
void led_set_green(uint8_t value) {
2021-11-23 21:22:18 +00:00
uint32_t pwm_value = ((uint32_t)value * LED_GREEN_MAX_VAL) / 255;
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LedChannelGreen, LED_PWM_MAX_VAL - pwm_value));
2021-10-24 20:22:24 +01:00
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LedChannelGreen));
2021-10-28 06:03:33 +01:00
}
void led_set_blue(uint8_t value) {
2021-11-23 21:22:18 +00:00
uint32_t pwm_value = ((uint32_t)value * LED_BLUE_MAX_VAL) / 255;
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LedChannelBlue, LED_PWM_MAX_VAL - pwm_value));
2021-10-24 20:22:24 +01:00
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LedChannelBlue));
}