main/dcf77tx.c

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 3
849ca14d4a2f
permissions
-rw-r--r--

Use on-board color LED

/**
 * @file dcf77tx.c
 * @brief DCF77 emulator/transmitter
 */
#include "dcf77tx.h"

static const char *TAG = "dcf77tx";

static TaskHandle_t			xTaskWifi = NULL;
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
extern bool				_wifi_ScanDone;
extern int8_t				_wifi_RSSI;
extern SemaphoreHandle_t		xSemaphoreDCF;
extern DCF_State			*dcf_state;			///< DCF77 state
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;

    ESP_LOGI(TAG, "Starting DCF77tx");

    /*
     * Initialize NVS
     */
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    uint8_t mac_addr[8] = {0};
    // Set the configured hostname for the dhcp client.
    esp_efuse_mac_get_default(mac_addr);
    sprintf(hostname, "dcf77tx-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]);

    setenv("TZ", "CET-01CEST-02,M3.4.0,M10.4.0", 1);
    tzset();

    esp_log_level_set("wifi", ESP_LOG_ERROR);
    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);
        if (ready_WiFi()) {
            ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0);
            wait = 0;
        } else {
            wait--;
            if (wait < 1)
		ESP_LOGE(TAG, "Timeout network connection");
        }
    }

    time_t now;
    struct tm timeinfo;
    char strftime_buf[64];
    time(&now);
    localtime_r(&now, &timeinfo);
    strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    ESP_LOGI(TAG, "The current date/time in Amsterdam is: %s", strftime_buf);

    /*
     * Main application loop
     */
    while (1) {
	switch (Main_Loop) {
	    case ML_INIT:	if (ready_WiFi() ) {
				    Main_Loop = ML_SYNC;
				    ESP_LOGI(TAG, "ML_INIT -> ML_SYNC");
				    set_ob_led(5, 0, 0);
				} else {
				    Main_Loop = ML_CONNECT;
				    ESP_LOGI(TAG, "ML_INIT -> ML_CONNECT");
				}
		    		break;
	    case ML_CONNECT:	if (System_TimeOk) {
				    Main_Loop = ML_SYNC;
				    ESP_LOGI(TAG, "ML_CONNECT -> ML_SYNC");
				}
				break;
	    case ML_SYNC:	if (System_TimeOk == false) {
				    Main_Loop = ML_CONNECT;
				    ESP_LOGI(TAG, "ML_SYNC -> ML_CONNECT");
				}
				time(&now);
    				localtime_r(&now, &timeinfo);
				if (timeinfo.tm_sec == 0) {
				    Main_Loop = ML_RUN;
				    ESP_LOGI(TAG, "ML_SYNC -> ML_RUN");
				    request_DCF(true);
				}
				break;
	    case ML_RUN:	if (System_TimeOk == false) {
				    Main_Loop = ML_CONNECT;
				    ESP_LOGI(TAG, "ML_RUN -> ML_CONNECT");
				    request_DCF(false);
				}
				break;
	}

	vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

mercurial