Use on-board color LED

Sat, 21 Oct 2023 17:22:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 21 Oct 2023 17:22:37 +0200
changeset 5
676c38f52d08
parent 4
426defedd402
child 6
60ae555eff0b

Use on-board color LED

main/dcf77tx.c file | annotate | diff | comparison | revisions
main/dcf77tx.h file | annotate | diff | comparison | revisions
main/task_dcf.c file | annotate | diff | comparison | revisions
--- a/main/dcf77tx.c	Sat Oct 21 16:22:20 2023 +0200
+++ b/main/dcf77tx.c	Sat Oct 21 17:22:37 2023 +0200
@@ -10,7 +10,7 @@
 static TaskHandle_t			xTaskDCF = NULL;
 char					hostname[32];
 int					Main_Loop = ML_INIT;
-
+static led_strip_handle_t		led_strip;			///< ESP32-C3 onboard LED
 
 extern SemaphoreHandle_t		xSemaphoreWiFi;
 extern WIFI_State			*wifi_state;			///< WiFi state
@@ -21,6 +21,17 @@
 extern bool				System_TimeOk;
 
 
+void set_ob_led(uint32_t red, uint32_t green, uint32_t blue)
+{
+    if ((red + green + blue) > 0) {
+    	led_strip_set_pixel(led_strip, 0, red, green, blue);
+    	led_strip_refresh(led_strip);
+    } else {
+	led_strip_clear(led_strip);
+    }
+}
+
+
 void app_main(void)
 {
     esp_err_t	ret;
@@ -49,6 +60,17 @@
     xTaskCreate(&task_wifi,    "task_wifi",     4096, NULL, 3, &xTaskWifi);
     xTaskCreate(&task_DCF,     "task_DCF",      4096, NULL, 4, &xTaskDCF);
 
+    led_strip_config_t strip_config = {
+        .strip_gpio_num = 7,
+        .max_leds = 1, // at least one LED on board
+    };
+    led_strip_rmt_config_t rmt_config = {
+        .resolution_hz = 10 * 1000 * 1000, // 10MHz
+    };
+    ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
+    /* Set all LED off to clear all pixels */
+    led_strip_clear(led_strip);
+
     int wait = 150;
     while (wait) {
         vTaskDelay(100 / portTICK_PERIOD_MS);
@@ -78,7 +100,7 @@
 	    case ML_INIT:	if (ready_WiFi() ) {
 				    Main_Loop = ML_SYNC;
 				    ESP_LOGI(TAG, "ML_INIT -> ML_SYNC");
-				    gpio_set_level(CONFIG_LED1_PIN, 1);
+				    set_ob_led(5, 0, 0);
 				} else {
 				    Main_Loop = ML_CONNECT;
 				    ESP_LOGI(TAG, "ML_INIT -> ML_CONNECT");
@@ -99,16 +121,12 @@
 				    Main_Loop = ML_RUN;
 				    ESP_LOGI(TAG, "ML_SYNC -> ML_RUN");
 				    request_DCF(true);
-				    gpio_set_level(CONFIG_LED2_PIN, 1);
-				    gpio_set_level(CONFIG_LED1_PIN, 0);
 				}
 				break;
 	    case ML_RUN:	if (System_TimeOk == false) {
 				    Main_Loop = ML_CONNECT;
 				    ESP_LOGI(TAG, "ML_RUN -> ML_CONNECT");
 				    request_DCF(false);
-				    gpio_set_level(CONFIG_LED1_PIN, 0);
-				    gpio_set_level(CONFIG_LED2_PIN, 0);
 				}
 				break;
 	}
--- a/main/dcf77tx.h	Sat Oct 21 16:22:20 2023 +0200
+++ b/main/dcf77tx.h	Sat Oct 21 17:22:37 2023 +0200
@@ -22,8 +22,6 @@
 #include "freertos/queue.h"
 #include "driver/gpio.h"
 #include "driver/ledc.h"
-//#include "driver/rtc_io.h"
-//#include "soc/rtc.h"
 #include "esp_log.h"
 #include "esp_system.h"
 #include "esp_mac.h"
@@ -32,6 +30,7 @@
 #include "esp_netif_sntp.h"
 #include "esp_sntp.h"
 #include "esp_timer.h"
+#include "led_strip.h"
 #include "nvs_flash.h"
 
 
@@ -46,5 +45,6 @@
     ML_RUN			///< Run transmitter
 } ML;
 
+void set_ob_led(uint32_t red, uint32_t green, uint32_t blue);
 
 #endif
--- a/main/task_dcf.c	Sat Oct 21 16:22:20 2023 +0200
+++ b/main/task_dcf.c	Sat Oct 21 17:22:37 2023 +0200
@@ -109,9 +109,9 @@
 			ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 512);
 			ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
 			if (impulseArray[actualSecond] == 1) {
-			    gpio_set_level(CONFIG_LED1_PIN, 1);
+			    set_ob_led(5, 5, 0);
 			} else if (impulseArray[actualSecond] == 2) {
-			    gpio_set_level(CONFIG_LED2_PIN, 1);
+			    set_ob_led(0, 5, 5);
 			}
 			break;
 	case 1:		if (impulseArray[actualSecond] == 1) {
@@ -120,7 +120,7 @@
     			    ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
     			    ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
     			    ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
-			    gpio_set_level(CONFIG_LED1_PIN, 0);
+			    set_ob_led(0, 2, 0);
 			}
 			break;
 	case 2:		/* Carrier back to 100% */
@@ -128,8 +128,7 @@
                         ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
                         ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
                         ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
-			gpio_set_level(CONFIG_LED1_PIN, 0);
-			gpio_set_level(CONFIG_LED2_PIN, 0);
+			set_ob_led(0, 2, 0);
 			break;
 	case 9:		impulseCount = 0;
     			/*
@@ -279,11 +278,6 @@
     dcf_state = malloc(sizeof(DCF_State));
     memset(dcf_state, 0x00, sizeof(DCF_State));
 
-    gpio_reset_pin(LED1);
-    gpio_reset_pin(LED2);
-    gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
-    gpio_set_direction(LED2, GPIO_MODE_OUTPUT);
-
     esp_timer_create_args_t timerDCF = {
         .callback = &DCFout,
         .name = "DCF timer"

mercurial