main.cpp 1.43 KB
#include <nvs_flash.h>

#include "display.h"
#include "https_request.h"
#include "stock.h"
#include "wifi.h"

//#include "esp_heap_trace.h"

//static heap_trace_record_t trace_record[200];

esp_err_t init_nvs() {
  esp_err_t 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();
  }
  return ret;
}

void init() {
  ESP_ERROR_CHECK(init_display());
  ESP_ERROR_CHECK(init_nvs());
  ESP_ERROR_CHECK(init_wifi());
  ESP_ERROR_CHECK(init_https());
  ESP_ERROR_CHECK(init_stock());
}

extern "C" void app_main() {
  // TODO: Get internet with GUEST_WLAN
  // TODO: Memory leak

  const char* TAG = "app_main";

  init();
  ESP_LOGI(TAG, "Init finished");

  ESP_ERROR_CHECK(connect_wifi(WIFI_SSID, WIFI_PASSWORD));
  ESP_LOGI(TAG, "Wifi finished");

  xTaskCreate(display_stock_task, "display_stock_task", 1024*4, NULL, configMAX_PRIORITIES-1, NULL);
  xTaskCreate(fetch_stock_task, "fetch_stock_task", 1024*4, NULL, configMAX_PRIORITIES-1, NULL);

  /*
  ESP_ERROR_CHECK(heap_trace_init_standalone(trace_record, 200));
  ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));

  int i = 1;
  for(;;) {
    ESP_LOGI("mem", "Free heap=%lu", esp_get_free_heap_size());
    vTaskDelay(60000 / portTICK_PERIOD_MS);
    if(--i == 0) {
      ESP_ERROR_CHECK(heap_trace_stop());
      heap_trace_dump();
    }

  }
  */

  ESP_LOGI(TAG, "main task finished");
}