Simple led controller

This commit is contained in:
DrZlo13 2021-10-25 05:22:24 +10:00
parent ab75e1f832
commit f8fbb45d4e
5 changed files with 112 additions and 2 deletions

View File

@ -3,6 +3,7 @@ set(SOURCES
"usb-cdc.c"
"nvs.c"
"wifi.c"
"led.c"
)
set(INCLUDES

64
main/led.c Normal file
View File

@ -0,0 +1,64 @@
#include "led.h"
#include <driver/ledc.h>
#include <esp_err.h>
#define LED_PIN_RED (4)
#define LED_PIN_GREEN (5)
#define LED_PIN_BLUE (6)
#define LEDC_MODE LEDC_LOW_SPEED_MODE
typedef enum {
LedChannelRed,
LedChannelGreen,
LedChannelBlue,
} ledc_channel;
void led_init() {
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,
.duty = 255, // Set duty to 100%
.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,
.duty = 255, // Set duty to 100%
.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,
.duty = 255, // Set duty to 100%
.hpoint = 0};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_blue));
}
void led_set(uint8_t red, uint8_t green, uint8_t blue) {
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LedChannelRed, 255U - red));
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LedChannelGreen, 255U - green));
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LedChannelBlue, 255U - blue));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LedChannelRed));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LedChannelGreen));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LedChannelBlue));
}

5
main/led.h Normal file
View File

@ -0,0 +1,5 @@
#include <stdint.h>
void led_init();
void led_set(uint8_t red, uint8_t green, uint8_t blue);

View File

@ -4,6 +4,7 @@
#include "nvs.h"
#include "wifi.h"
#include "gdb_main.h"
#include "led.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <rom/ets_sys.h>
@ -37,6 +38,42 @@ void pins_init() {
gpio_config(&io_conf);
}
void led_thread(void* pvParameters) {
const uint8_t led_colors[8][3] = {
{153, 0, 255},
{255, 0, 0},
{0, 255, 0},
{0, 0, 255},
{0, 255, 136},
{255, 238, 0},
{255, 157, 0},
{255, 255, 255},
};
uint8_t led_color_counter = 0;
while(1) {
ESP_LOGI(
"led_thread",
"Led set to %u, %u, %u",
led_colors[led_color_counter][0],
led_colors[led_color_counter][1],
led_colors[led_color_counter][2]);
led_set(
led_colors[led_color_counter][0],
led_colors[led_color_counter][1],
led_colors[led_color_counter][2]);
led_color_counter++;
if(led_color_counter >= (sizeof(led_colors) / sizeof(led_colors[1]))) {
led_color_counter = 0;
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void app_main(void) {
ESP_LOGI(TAG, "start");
@ -46,7 +83,11 @@ void app_main(void) {
pins_init();
xTaskCreate(&gdb_application_thread, "gdb_thread", 16 * 4096, NULL, 5, NULL);
led_init();
xTaskCreate(&led_thread, "led_thread", 4096, NULL, 5, NULL);
//xTaskCreate(&gdb_application_thread, "gdb_thread", 16 * 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "end");
}

View File

@ -63,7 +63,6 @@ void usb_cdc_rx_callback(int itf, cdcacm_event_t* event) {
if(err == ESP_OK) {
if(rx_size > 0) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
size_t ret =
xStreamBufferSend(furi_hal_vcp.rx_stream, buffer_rx, rx_size, portMAX_DELAY);
ESP_ERROR_CHECK(ret != rx_size);