/
jpg-render-it8951.cpp
602 lines (513 loc) · 21.8 KB
/
jpg-render-it8951.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Note: Run menuconfig to set the WiFi Credentials -> CALE Configuration
// Requirements: Needs to have an EPD Class defined. Needs PSRAM.
// Needs Lovyan as a component:
// git submodule add https://github.com/lovyan03/LovyanGFX.git components/LovyanGFX
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_types.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_sleep.h"
// WiFi related
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
// HTTP Client + time
#include "esp_netif.h"
#include "esp_http_client.h"
#include "esp_sntp.h"
//#include "soc/rtc_wdt.h"
// JPG decoder
#if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+
#include "esp32/rom/tjpgd.h"
#else // ESP32 Before IDF 4.0
#include "rom/tjpgd.h"
#endif
#include "esp_task_wdt.h"
#include <stdio.h>
#include <string.h>
#include <math.h> // round + pow
// - - - - Display configuration - - - - - - - - -
#define EPD_WIDTH 1872
#define EPD_HEIGHT 1404
// Affects the gamma to calculate gray (lower is darker/higher contrast) Note: That on true is slower
// Nice test values: 0.9 1.2 1.4 higher and is too bright
#define USE_GAMMA_CORRECTION false
double gamma_value = 0.8;
// Image URL and jpg settings. Make sure to update EPD_WIDTH/HEIGHT if using loremflickr
// Note: Only HTTP protocol supported (Check README to use SSL secure URLs) loremflickr
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
//DEMO: https://placekitten.com/ or http://lorempixel.com/
//#define IMG_URL ("https://placekitten.com/" STR(EPD_WIDTH) "/" STR(EPD_HEIGHT))
//#define IMG_URL "http://img.cale.es/jpg/fasani/5ef94f52ad2f6"
//#define IMG_URL "http://img.cale.es/jpg/fasani/5e636b0f39aac"
#define IMG_URL "http://192.168.0.25/capture"
#define LGFX_USE_V1
// Uncomment the define to enable a JPG load in a loop
#define JPG_RELOAD_LOOP
#include <LovyanGFX.hpp>
class LGFX : public lgfx::LGFX_Device
{
lgfx::Panel_IT8951 _panel_instance;
lgfx::Bus_SPI _bus_instance;
lgfx::Light_PWM _light_instance;
public:
// コンストラクタを作成し、ここで各種設定を行います。
// クラス名を変更した場合はコンストラクタも同じ名前を指定してください。
LGFX(void)
{
{ // バス制御の設定を行います。
auto cfg = _bus_instance.config(); // バス設定用の構造体を取得します。
// SPIバスの設定
cfg.spi_host = SPI2_HOST; // 使用するSPIを選択 (VSPI_HOST or HSPI_HOST)
cfg.spi_mode = 0; // SPI通信モードを設定 (0 ~ 3)
cfg.freq_write = 40000000; // 送信時のSPIクロック (最大80MHz, 80MHzを整数で割った値に丸められます)
cfg.freq_read = 16000000; // 受信時のSPIクロック
cfg.spi_3wire = true; // 受信をMOSIピンで行う場合はtrueを設定
cfg.use_lock = true; // トランザクションロックを使用する場合はtrueを設定
cfg.dma_channel = 1; // Set the DMA channel (1 or 2. 0=disable) 使用するDMAチャンネルを設定 (0=DMA不使用)
cfg.pin_sclk = CONFIG_EINK_SPI_CLK; // SPIのSCLKピン番号を設定
cfg.pin_mosi = CONFIG_EINK_SPI_MOSI; // SPIのMOSIピン番号を設定
cfg.pin_miso = CONFIG_EINK_SPI_MISO; // SPIのMISOピン番号を設定 (-1 = disable)
cfg.pin_dc = -1; // SPIのD/Cピン番号を設定 (-1 = disable)
_bus_instance.config(cfg); // 設定値をバスに反映します。
_panel_instance.setBus(&_bus_instance); // バスをパネルにセットします。
}
{ // 表示パネル制御の設定を行います。
auto cfg = _panel_instance.config(); // 表示パネル設定用の構造体を取得します。
cfg.pin_cs = CONFIG_EINK_SPI_CS; // CSが接続されているピン番号 (-1 = disable)
cfg.pin_rst = CONFIG_EINK_RST; // RSTが接続されているピン番号 (-1 = disable)
cfg.pin_busy = CONFIG_EINK_BUSY; // BUSYが接続されているピン番号 (-1 = disable)
// ※ 以下の設定値はパネル毎に一般的な初期値が設定されていますので、不明な項目はコメントアウトして試してみてください。
cfg.memory_width = EPD_WIDTH; // ドライバICがサポートしている最大の幅
cfg.memory_height = EPD_HEIGHT; // ドライバICがサポートしている最大の高さ
cfg.panel_width = EPD_WIDTH; // 実際に表示可能な幅
cfg.panel_height = EPD_HEIGHT; // 実際に表示可能な高さ
cfg.offset_x = 0; // パネルのX方向オフセット量
cfg.offset_y = 0; // パネルのY方向オフセット量
cfg.offset_rotation = 0; // 回転方向の値のオフセット 0~7 (4~7は上下反転)
cfg.dummy_read_pixel = 8; // ピクセル読出し前のダミーリードのビット数
cfg.dummy_read_bits = 1; // ピクセル以外のデータ読出し前のダミーリードのビット数
cfg.readable = true; // データ読出しが可能な場合 trueに設定
cfg.invert = true; // パネルの明暗が反転してしまう場合 trueに設定
cfg.rgb_order = false; // パネルの赤と青が入れ替わってしまう場合 trueに設定
cfg.dlen_16bit = false; // データ長を16bit単位で送信するパネルの場合 trueに設定
cfg.bus_shared = true; // SDカードとバスを共有している場合 trueに設定(drawJpgFile等でバス制御を行います)
_panel_instance.config(cfg);
}
setPanel(&_panel_instance); // 使用するパネルをセットします。
}
};
// 準備したクラスのインスタンスを作成します。
LGFX display;
// - - - - end of Display configuration - - - - -
extern "C"
{
void app_main();
}
// CALE url test (should match width/height of your EPD)
//#define IMG_URL "http://img.cale.es/jpg/fasani/5ea1dec401890" // 800*480 test
// Please check the README to understand how to use an SSL Certificate
// Note: This makes a sntp time sync query for cert validation (It's slower)
#define VALIDATE_SSL_CERTIFICATE false
// As default is 512 without setting buffer_size property in esp_http_client_config_t
#define HTTP_RECEIVE_BUFFER_SIZE 1938
// Load the EMBED_TXTFILES. Then doing (char*) server_cert_pem_start you get the SSL certificate
// Reference: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#embedding-binary-data
extern const uint8_t server_cert_pem_start[] asm("_binary_server_cert_pem_start");
#define DEBUG_VERBOSE true
// JPEG decoder
JDEC jd;
JRESULT rc;
// Buffers
uint8_t *fb; // EPD 2bpp buffer
uint8_t *source_buf; // JPG download buffer
uint8_t *decoded_image; // RAW decoded image
static uint8_t tjpgd_work[3096]; // tjpgd buffer. min size: 3096 bytes
uint32_t buffer_pos = 0;
uint32_t time_download = 0;
uint32_t time_decomp = 0;
uint32_t time_render = 0;
static const char * jd_errors[] = {
"Succeeded",
"Interrupted by output function",
"Device error or wrong termination of input stream",
"Insufficient memory pool for the image",
"Insufficient stream input buffer",
"Parameter error",
"Data format error",
"Right format but not supported",
"Not supported JPEG standard"
};
uint16_t ep_width = EPD_WIDTH;
uint16_t ep_height = EPD_HEIGHT;
uint8_t gamme_curve[256];
static const char *TAG = "TJpeg";
uint16_t countDataEventCalls = 0;
uint32_t countDataBytes = 0;
uint32_t img_buf_pos = 0;
uint32_t dataLenTotal = 0;
uint64_t startTime = 0;
#if VALIDATE_SSL_CERTIFICATE == true
/* Time aware for ESP32: Important to check SSL certs validity */
void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "Notification of a time synchronization event");
}
static void initialize_sntp(void)
{
ESP_LOGI(TAG, "Initializing SNTP");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
#endif
sntp_init();
}
static void obtain_time(void)
{
initialize_sntp();
// wait for time to be set
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int retry_count = 10;
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
time(&now);
localtime_r(&now, &timeinfo);
}
#endif
//====================================================================================
// This sketch contains support functions to render the Jpeg images
//
// Created by Bodmer 15th Jan 2017
// Refactored by @martinberlin for EPDiy as a Jpeg download and render example
//====================================================================================
// Return the minimum of two values a and b
#define minimum(a,b) (((a) < (b)) ? (a) : (b))
uint8_t find_closest_palette_color(uint8_t oldpixel)
{
return oldpixel & 0xF0;
}
void deepsleep(){
esp_deep_sleep(1000000LL * 60 * CONFIG_DEEPSLEEP_MINUTES_AFTER_RENDER);
}
static uint32_t feed_buffer(JDEC *jd,
uint8_t *buff, // Pointer to the read buffer (NULL:skip)
uint32_t nd
) {
uint32_t count = 0;
while (count < nd) {
if (buff != NULL) {
*buff++ = source_buf[buffer_pos];
}
count ++;
buffer_pos++;
}
return count;
}
/* User defined call-back function to output decoded RGB bitmap in decoded_image buffer
In this v2 example the pixels are set directly on tjd_output without using a decoded buffer
*/
static uint32_t
tjd_output(JDEC *jd, /* Decompressor object of current session */
void *bitmap, /* Bitmap data to be output */
JRECT *rect /* Rectangular region to output */
) {
uint32_t render_start = esp_timer_get_time();
uint32_t w = rect->right - rect->left + 1;
uint32_t h = rect->bottom - rect->top + 1;
uint32_t image_width = jd->width;
uint8_t *bitmap_ptr = (uint8_t*)bitmap;
#if USE_GAMMA_CORRECTION == true
uint8_t buf[w*h];
for (uint32_t i = 0; i < w * h; i++) {
uint8_t r = *(bitmap_ptr++);
uint8_t g = *(bitmap_ptr++);
uint8_t b = *(bitmap_ptr++);
buf[i] = gamme_curve[(r*38 + g*75 + b*15)>>7];
// Avoid watchdog timer leaving some delay on each X row of the MCU
if (i%w == 0) {
//rtc_wdt_feed();
vTaskDelay(4 / portTICK_PERIOD_MS);
}
}
display.pushImage(rect->left, rect->top, w, h, (lgfx::grayscale_t*)buf);
#else
// Now working as expected (see image)
display.pushImage(rect->left, rect->top, w, h, (lgfx::bgr888_t*)bitmap);
#endif
//rtc_wdt_feed();
time_render += (esp_timer_get_time() - render_start)/1000;
return 1;
}
//====================================================================================
// This function opens source_buf Jpeg image file and primes the decoder
//====================================================================================
int drawBufJpeg(uint8_t *source_buf, int xpos, int ypos) {
rc = jd_prepare(&jd, feed_buffer, tjpgd_work, sizeof(tjpgd_work), &source_buf);
if (rc != JDR_OK) {
ESP_LOGE(TAG, "JPG jd_prepare error: %s", jd_errors[rc]);
return ESP_FAIL;
}
uint32_t decode_start = esp_timer_get_time();
// Last parameter scales v 1 will reduce the image
rc = jd_decomp(&jd, tjd_output, 0);
if (rc != JDR_OK) {
ESP_LOGE(TAG, "JPG jd_decomp error: %s", jd_errors[rc]);
return ESP_FAIL;
}
time_decomp = (esp_timer_get_time() - decode_start)/1000;
ESP_LOGI("JPG", "width: %d height: %d\n", jd.width, jd.height);
ESP_LOGI("decode", "%d ms . image decompression", time_decomp);
// Render the image onto the screen at given coordinates
//jpegRender(xpos, ypos, jd.width, jd.height);
return 1;
}
// Handles Htpp events and is in charge of buffering source_buf (jpg compressed image)
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
switch (evt->event_id)
{
case HTTP_EVENT_ERROR:
ESP_LOGE(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
#if DEBUG_VERBOSE
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
#endif
break;
case HTTP_EVENT_ON_DATA:
++countDataEventCalls;
#if DEBUG_VERBOSE
if (countDataEventCalls%10==0) {
ESP_LOGI(TAG, "%d len:%d\n", countDataEventCalls, evt->data_len);
}
#endif
dataLenTotal += evt->data_len;
if (countDataEventCalls == 1) startTime = esp_timer_get_time();
// Append received data into source_buf
memcpy(&source_buf[img_buf_pos], evt->data, evt->data_len);
img_buf_pos += evt->data_len;
// Optional hexa dump
//ESP_LOG_BUFFER_HEX(TAG, source_buf, 100);
break;
case HTTP_EVENT_ON_FINISH:
// Do not draw if it's a redirect (302)
if (esp_http_client_get_status_code(evt->client) == 200) {
display.startWrite();
printf("%d bytes read from %s\nIMG_BUF size: %d\n", img_buf_pos, IMG_URL, img_buf_pos);
drawBufJpeg(source_buf, 0, 0);
time_download = (esp_timer_get_time()-startTime)/1000;
ESP_LOGI("www-dw", "%d ms - download", time_download);
// Refresh display
display.endWrite();
ESP_LOGI("draw", "%d ms . image render", time_render);
ESP_LOGI("total", "%d ms - total time spent\n", time_download+time_decomp+time_render);
} else {
printf("HTTP on finish got status code: %d\n", esp_http_client_get_status_code(evt->client));
}
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED\n");
break;
}
return ESP_OK;
}
// Handles http request
static void http_post(void)
{
/**
* NOTE: All the configuration parameters for http_client must be specified
* either in URL or as host and path parameters.
* FIX: Uncommenting cert_pem restarts even if providing the right certificate
*/
esp_http_client_config_t config = {
.url = IMG_URL,
.disable_auto_redirect = false,
.event_handler = _http_event_handler,
.buffer_size = HTTP_RECEIVE_BUFFER_SIZE,
#if VALIDATE_SSL_CERTIFICATE == true
.cert_pem = (char *)server_cert_pem_start
#endif
};
esp_http_client_handle_t client = esp_http_client_init(&config);
#if DEBUG_VERBOSE
printf("Free heap before HTTP download: %d\n", xPortGetFreeHeapSize());
if (esp_http_client_get_transport_type(client) == HTTP_TRANSPORT_OVER_SSL && config.cert_pem) {
printf("SSL CERT:\n%s\n\n", (char *)server_cert_pem_start);
}
#endif
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
ESP_LOGI(TAG, "\nIMAGE URL: %s\n\nHTTP GET Status = %d, content_length = %d\n",
IMG_URL,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
}
else
{
ESP_LOGE(TAG, "\nHTTP GET request failed: %s", esp_err_to_name(err));
}
printf("Go to sleep %d minutes\n", CONFIG_DEEPSLEEP_MINUTES_AFTER_RENDER);
esp_http_client_cleanup(client);
// Reset initialization variables
buffer_pos = 0;
time_download = 0;
time_decomp = 0;
time_render = 0;
countDataEventCalls = 0;
countDataBytes = 0;
img_buf_pos = 0;
dataLenTotal = 0;
startTime = 0;
#ifdef JPG_RELOAD_LOOP
http_post();
#elif
// Uncomment JPG_RELOAD_LOOP to enable a constant JPG load
vTaskDelay(10);
deepsleep();
#endif
}
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static int s_retry_num = 0;
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < 5)
{
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "Retry to connect to the AP");
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
ESP_LOGI(TAG, "Connect to the AP failed %d times. Going to deepsleep %d minutes", 5, CONFIG_DEEPSLEEP_MINUTES_AFTER_RENDER);
deepsleep();
}
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
// Initializes WiFi the ESP-IDF way
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
// C++ wifi config
wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
sprintf(reinterpret_cast<char *>(wifi_config.sta.ssid), CONFIG_ESP_WIFI_SSID);
sprintf(reinterpret_cast<char *>(wifi_config.sta.password), CONFIG_ESP_WIFI_PASSWORD);
wifi_config.sta.pmf_cfg.capable = true;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config((wifi_interface_t)ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually happened. */
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "Connected to ap SSID:%s", CONFIG_ESP_WIFI_SSID);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_ESP_WIFI_SSID);
}
else
{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
}
void app_main() {
// Should be big enough to allocate the JPEG file size, width * height should suffice
source_buf = (uint8_t *)heap_caps_malloc(ep_width * ep_height/2, MALLOC_CAP_SPIRAM);
if (source_buf == NULL) {
ESP_LOGE("main", "Initial alloc source_buf failed!");
}
printf("Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
display.init();
// epd_fast: LovyanGFX uses a 4×4 16pixel tile pattern to display a pseudo 17level grayscale.
// epd_quality: Uses 16 levels of grayscale
display.setEpdMode(epd_mode_t::epd_quality);
double gammaCorrection = 1.0 / gamma_value;
for (int gray_value =0; gray_value<256;gray_value++)
gamme_curve[gray_value]= round (255*pow(gray_value/255.0, gammaCorrection));
// Initialize 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();
}
ESP_ERROR_CHECK(ret);
// WiFi log level set only to Error otherwise outputs too much
esp_log_level_set("wifi", ESP_LOG_ERROR);
// Initialization: WiFi + clean screen while downloading image
printf("Free heap before wifi_init_sta: %d\n", xPortGetFreeHeapSize());
wifi_init_sta();
#if VALIDATE_SSL_CERTIFICATE == true
obtain_time();
#endif
http_post();
}