summaryrefslogtreecommitdiffstats
path: root/esp32/main/main.c
blob: 681b59872d4861850f9c2ce80f0426a0b7cb9686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include <time.h>
#include <esp_log.h>
#include <esp_event.h>
#include <esp_netif.h>
#include <nvs_flash.h>

#include "dht.h"
#include "epd.h"
#include "gui.h"
#include "ntp.h"
#include "news.h"
#include "scrn.h"
#include "wifi.h"
#include "stock.h"

const static char *TAG = "app";

void app_main(void)
{
	time_t t;
	char ts[20];

	struct tm now;
	struct scrn sc;
	struct news_item *news;

	struct stock_data stock;
	stock.prices_maxlen = 90; /* days */
	stock.prices = malloc(sizeof(int) * stock.prices_maxlen);

	int ntp_rc = 0;

	ESP_ERROR_CHECK(nvs_flash_init());
	ESP_ERROR_CHECK(esp_event_loop_create_default());

	sc.width = EPD_WIDTH;
	sc.height = EPD_HEIGHT;
	sc.fb = heap_caps_malloc(sizeof(sc.fb[0]) * MAXLEN, MALLOC_CAP_DMA);

	wifi_connect();

	ntp_init();
	dht_init();
	news_init();
	epd_init();

	for (;;) {
		if (!ntp_rc)
			ntp_rc = ntp_sync();

		t = time(NULL);
		now = *localtime(&t);
		strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", &now);

		gui_draw_layout(&sc);
		gui_draw_temp(&sc);
		gui_draw_humid(&sc);
		gui_draw_date(&sc, &now);

		stock_get_data(&stock);
		if (stock.prices_len > 0)
			gui_plot_stocks(&sc, &stock);

		news = news_local_get();
		if (news)
			gui_draw_str(&sc, news->title, 335, 40, 785, 184, 0);

		news = news_world_get();
		if (news)
			gui_draw_str(&sc, news->title, 335, 214, 785, 340, 0);

		epd_wake();
		vTaskDelay(500 / portTICK_PERIOD_MS);	

		epd_draw(sc.fb, MAXLEN);
		vTaskDelay(1000 / portTICK_PERIOD_MS);	

		epd_sleep();

		ESP_LOGI(TAG, "last updated at %s", ts);
		vTaskDelay(5 * 60 * 1000 / portTICK_PERIOD_MS);	
	}
}