From abf982ec977c6352f6dbf3eed6fc27f11d200836 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 9 Aug 2021 13:47:12 +0200 Subject: display blue channel on GPIO02 (built-in led) working with pwm :tada: --- main/main.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/main/main.c b/main/main.c index 0343f14..27bb6fc 100644 --- a/main/main.c +++ b/main/main.c @@ -10,12 +10,34 @@ #include "nvs_flash.h" #include "protocol_examples_common.h" +#include "driver/pwm.h" + #include +#define GPIO_OUTPUT_CHANNEL_RED 0 +#define GPIO_OUTPUT_CHANNEL_GREEN 0 +#define GPIO_OUTPUT_CHANNEL_BLUE 2 + +#define PWM_PERIOD 1000 + static const char *TAG = "rgbstrip"; int color[3]; static httpd_handle_t server = NULL; +const uint32_t pin_num[] = {GPIO_OUTPUT_CHANNEL_RED, GPIO_OUTPUT_CHANNEL_GREEN, + GPIO_OUTPUT_CHANNEL_BLUE}; + +uint32_t duties[] = {0, 0, 0}; +float phase[] = {0, 0, 0}; + +void update_strip() { + duties[0] = PWM_PERIOD - (int)((float)(PWM_PERIOD * color[0]) / 0xff); + duties[1] = PWM_PERIOD - (int)((float)(PWM_PERIOD * color[1]) / 0xff); + duties[2] = PWM_PERIOD - (int)((float)(PWM_PERIOD * color[2]) / 0xff); + pwm_set_duties(duties); + pwm_start(); +} + char *color_to_str() { char *value = (char *)malloc(sizeof(char) * (6 + 1)); // 6 char + 1 \0; sprintf(value, "%02x%02x%02x", color[0], color[1], color[2]); @@ -37,6 +59,7 @@ esp_err_t post_handler(httpd_req_t *req) { if (remaining != 6) return ESP_OK; + httpd_req_recv(req, buf, 6); char r_hex[] = {buf[0], buf[1], '\0'}; @@ -47,6 +70,8 @@ esp_err_t post_handler(httpd_req_t *req) { sscanf(g_hex, "%x", &color[1]); sscanf(b_hex, "%x", &color[2]); + update_strip(); + httpd_resp_send_chunk(req, NULL, 0); return ESP_OK; } @@ -102,5 +127,9 @@ void app_main() { ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); + pwm_init(PWM_PERIOD, duties, 3, pin_num); + pwm_set_phases(phase); + pwm_start(); + server = start_webserver(); } -- cgit v1.2.3