main/dcf77tx.c

Sat, 21 Oct 2023 11:44:46 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 21 Oct 2023 11:44:46 +0200
changeset 3
849ca14d4a2f
parent 1
86b275481021
child 5
676c38f52d08
permissions
-rw-r--r--

Finished coding second bits.

/**
 * @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;


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 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);

    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");
				    gpio_set_level(CONFIG_LED1_PIN, 1);
				} 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);
				    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;
	}

	vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

mercurial