main/brewboard.c

Sat, 20 Oct 2018 13:23:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 20 Oct 2018 13:23:15 +0200
changeset 0
b74b0e4902c3
child 1
ad2c8b13eb88
permissions
-rw-r--r--

Initial checkin brewboard

/* BrewVoard

*/

#include "config.h"


static const char *TAG = "brewboard";

static TaskHandle_t		xTaskTFT     = NULL;
static TaskHandle_t		xTaskDS18B20 = NULL;
static TaskHandle_t		xTaskDriver  = NULL;
static TaskHandle_t		xTaskSound   = NULL;
static TaskHandle_t		xTaskSDcard  = NULL;
static TaskHandle_t		xTaskWifi    = NULL;

extern SemaphoreHandle_t	xSemaphoreDS18B20;
extern SemaphoreHandle_t	xSemaphoreDriver;
extern SemaphoreHandle_t	xSemaphoreWiFi;
extern WIFI_State		*wifi_state;


int 				Main_Screen = MAIN_MODE_UNKNOWN;
int				Old_Screen = MAIN_MODE_UNKNOWN;
bool				System_TimeOk = false;



void app_main()
{
    int		tempy;
    char	temp[64];

    ESP_LOGI(TAG, "Starting");
    init_tft_display();

    TFT_setFont(DEJAVU24_FONT, NULL);
    _fg = TFT_YELLOW;
    tempy = TFT_getfontheight() + 4;
    sprintf(temp, "BrewBoard %s", VERSION);
    TFT_print(temp, CENTER, 4);

    TFT_setFont(DEJAVU18_FONT, NULL);
    _fg = TFT_CYAN;
    TFT_print("Mount /spiffs ", 0, LASTY+tempy);
    ESP_LOGI(TAG, "Initializing SPIFFS");
        
    esp_vfs_spiffs_conf_t conf = {
	.base_path = "/spiffs",
	.partition_label = NULL,
	.max_files = 5,
	.format_if_mount_failed = true
    };
	    
    // Use settings defined above to initialize and mount SPIFFS filesystem.
    // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
    esp_err_t ret = esp_vfs_spiffs_register(&conf);

    if (ret != ESP_OK) {
	if (ret == ESP_FAIL) {
	    ESP_LOGE(TAG, "Failed to mount or format filesystem");
	} else if (ret == ESP_ERR_NOT_FOUND) {
	    ESP_LOGE(TAG, "Failed to find SPIFFS partition");
	} else {
	    ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret);
	}
	_fg = TFT_RED;
	TFT_print("error\r\n", LASTX, LASTY);
	return; // Stop application.
    }

    size_t total = 0, used = 0;
    ret = esp_spiffs_info(NULL, &total, &used);
    if (ret != ESP_OK) {
	ESP_LOGE(TAG, "Failed to get SPIFFS partition information");
	_fg = TFT_RED;
	TFT_print("error\r\n", LASTX, LASTY);
	return; // Stop application.
    } else {
	ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total);
    }
    TFT_print("Ok\r\n", LASTX, LASTY);

    // Just to debug, list the /spiffs filesystem.
#if 1
    DIR *dir = opendir("/spiffs");
    struct dirent* de = readdir(dir);
    while (de) {
	if (de->d_type == DT_REG) {
	    printf("F ");
	}
	if (de->d_type == DT_DIR) {
	    printf("D ");
	}
	printf("%s\n", de->d_name);
	de = readdir(dir);
    }
    closedir(dir);
#endif

    /*
     * Read or create configuration
     */
    TFT_print("Ophalen configuratie ", LASTX, LASTY);
    read_config();
    read_equipment(config.EquipmentRec);
    read_runtime();
//    unlink("/spiffs/etc/recipe.conf");
    read_recipe(config.RecipeRec);
    TFT_print("Ok\r\n", LASTX, LASTY);

    // Set the Touchscreen calibration/
    TS_set_calibration(config.ts_xleft, config.ts_xright, config.ts_ytop, config.ts_ybottom);

    /*
     * TZ names don't work, so set the TZ the hard way.
     * This is the setting for Europe/Amsterdam.
     */
    setenv("TZ", "CET-01CEST-02,M3.4.0,M10.4.0", 1);
    tzset();

    xSemaphoreDS18B20 = xSemaphoreCreateMutex();
    xSemaphoreDriver  = xSemaphoreCreateMutex();

    TFT_print("Starten taken ", LASTX, LASTY);
    xTaskCreate(&task_tft,     "task_tft",      6144, NULL, 4, &xTaskTFT);
    vTaskDelay(400 / portTICK_PERIOD_MS);
    xTaskCreate(&task_ds18b20, "task_ds18b20",  2560, NULL, 8, &xTaskDS18B20);
    xTaskCreate(&task_driver,  "task_driver",   2560, NULL, 8, &xTaskDriver);
    xTaskCreate(&task_sound,   "task_sound",    3072, NULL,15, &xTaskSound);
    xTaskCreate(&task_sdcard,  "task_sdcard",   8192, NULL,10, &xTaskSDcard);
    /* disable the default wifi logging */
    esp_log_level_set("wifi", ESP_LOG_NONE);
    xTaskCreate(&task_wifi,    "task_wifi",     4096, NULL, 3, &xTaskWifi);
    // Task for MQTT
    TFT_print(" Ok\r\nConnecting ", LASTX, LASTY);

    int wait = 20;
    while (wait) {
	vTaskDelay(500 / portTICK_PERIOD_MS);
	TFT_print(".", LASTX, LASTY);
    	if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
	    if (wifi_state->STA_connected == true)
		wait = 0;
	    else
		wait--;
	    xSemaphoreGive(xSemaphoreWiFi);
    	}
    }

    Main_Screen = MAIN_MODE_FREE;
    TFT_print(" Ok\r\n", LASTX, LASTY);
    SoundPlay(SOUND_StartUp);

    /* Do not write to the TFT during VNC startup to avoid race conditions */
    VncStartup();
    start_http_websocket();
    vTaskDelay(1000 / portTICK_PERIOD_MS);

    /*
     * A small useless delay
     */
    vTaskDelay(1000 / portTICK_PERIOD_MS);

//    static char cBuffer[ 1024 ];

    /*
     * Main application loop.
     */
    while (1) {
	vTaskDelay(20000 / portTICK_PERIOD_MS);

//	vTaskList( cBuffer );
//	printf("Name            State   Prio    Stack   Num\n");
//	printf("--------------- ------- ------- ------- -------\n");
//	printf("%s\n", cBuffer);
    }
    // Not reached.
}

mercurial