Skip to content

Commit

Permalink
Fix jpegdec render for idf >5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
martinberlin committed Dec 18, 2024
1 parent df958d9 commit 9e8a51b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/www-image/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(
app_sources "jpg-render.c"
#app_sources "jpgdec-render.cpp"
#app_sources "jpg-render.c"
app_sources "jpgdec-render.cpp"
)

idf_component_register(SRCS ${app_sources}
Expand Down
60 changes: 32 additions & 28 deletions examples/www-image/main/jpgdec-render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ idf_component_register(SRCS ${srcs}
#include <math.h> // round + pow
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
extern "C" {
#include "epd_highlevel.h"
#include "epdiy.h"
#include "epd_highlevel.h"
#include "epdiy.h"
}
EpdiyHighlevelState hl;
// JPG decoder from @bitbank2
Expand Down Expand Up @@ -75,17 +76,17 @@ uint8_t* fb; // EPD 2bpp buffer
uint8_t* source_buf; // JPG download buffer

uint32_t buffer_pos = 0;
uint32_t time_download = 0;
uint32_t time_decomp = 0;
uint32_t time_render = 0;
uint16_t ep_width = 0;
uint16_t ep_height = 0;

static const char* TAG = "Jpgdec";
uint16_t countDataEventCalls = 0;
uint32_t countDataBytes = 0;
uint32_t img_buf_pos = 0;
uint64_t startTime = 0;
uint64_t start_time = 0;
uint64_t time_download = 0;
uint64_t time_decomp = 0;
uint64_t time_render = 0;

#if VALIDATE_SSL_CERTIFICATE == true
/* Time aware for ESP32: Important to check SSL certs validity */
Expand Down Expand Up @@ -113,7 +114,7 @@ static void obtain_time(void) {
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);
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", (int)retry, (int)retry_count);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
time(&now);
Expand Down Expand Up @@ -190,18 +191,18 @@ int decodeJpeg(uint8_t* source_buf, int xpos, int ypos) {
time_decomp = (esp_timer_get_time() - decode_start) / 1000 - time_render;
ESP_LOGI(
"decode",
"%d ms - %dx%d image MCUs:%d",
"%lld ms - %dx%d image MCUs:%d",
time_decomp,
jpeg.getWidth(),
jpeg.getHeight(),
mcu_count
(int)jpeg.getWidth(),
(int)jpeg.getHeight(),
(int)mcu_count
);
} else {
ESP_LOGE("jpeg.decode", "Failed with error: %d", jpeg.getLastError());
ESP_LOGE("jpeg.decode", "Failed with error: %d", (int)jpeg.getLastError());
}

} else {
ESP_LOGE("jpeg.openRAM", "Failed with error: %d", jpeg.getLastError());
ESP_LOGE("jpeg.openRAM", "Failed with error: %d", (int)jpeg.getLastError());
}
jpeg.close();

Expand Down Expand Up @@ -236,44 +237,47 @@ esp_err_t _http_event_handler(esp_http_client_event_t* evt) {
#endif

if (countDataEventCalls == 1)
startTime = esp_timer_get_time();
start_time = esp_timer_get_time();
// Append received data into source_buf
memcpy(&source_buf[img_buf_pos], evt->data, evt->data_len);
memcpy(&source_buf[img_buf_pos], evt->data, (int)evt->data_len);
img_buf_pos += evt->data_len;
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) {
printf("%d bytes read from %s\n", img_buf_pos, IMG_URL);
time_download = (esp_timer_get_time() - startTime) / 1000;
printf("%ld bytes read from %s\n", img_buf_pos, IMG_URL);
time_download = (esp_timer_get_time() - start_time) / 1000;

decodeJpeg(source_buf, 0, 0);

ESP_LOGI("www-dw", "%d ms - download", time_download);
ESP_LOGI("www-dw", "%lld ms - download", time_download);
ESP_LOGI(
"render",
"%d ms - copying pix (JPEG_CPY_FRAMEBUFFER:%d)",
"%lld ms - copying pix (JPEG_CPY_FRAMEBUFFER:%d)",
time_render,
JPEG_CPY_FRAMEBUFFER
(int)JPEG_CPY_FRAMEBUFFER
);
// Refresh display
epd_hl_update_screen(&hl, MODE_GC16, 25);

ESP_LOGI(
"total", "%d ms - total time spent\n", time_download + time_decomp + time_render
"total", "%lld 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)
(int)esp_http_client_get_status_code(evt->client)
);
}
break;

case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED\n");
break;
default:
ESP_LOGI(TAG, "HTTP_EVENT_UNKNOWN or redirect\n");
break;
}
return ESP_OK;
}
Expand Down Expand Up @@ -306,7 +310,7 @@ static void http_post(void) {
if (err == ESP_OK) {
ESP_LOGI(
TAG,
"\nIMAGE URL: %s\n\nHTTP GET Status = %d, content_length = %d\n",
"\nIMAGE URL: %s\n\nHTTP GET Status = %d, content_length = %lld\n",
IMG_URL,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client)
Expand Down Expand Up @@ -350,8 +354,8 @@ static void event_handler(
ESP_LOGI(
TAG,
"Connect to the AP failed %d times. Going to deepsleep %d minutes",
5,
DEEPSLEEP_MINUTES_AFTER_RENDER
(int)5,
(int)DEEPSLEEP_MINUTES_AFTER_RENDER
);
deepsleep();
}
Expand Down Expand Up @@ -392,7 +396,6 @@ void wifi_init_sta(void) {
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
Expand All @@ -408,7 +411,7 @@ void wifi_init_sta(void) {
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s", ESP_WIFI_SSID);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
ESP_LOGE(TAG, "UNEXPECTED EVENT in wifi_init");
}

/* The event will not be processed after unregister */
Expand All @@ -422,7 +425,8 @@ void wifi_init_sta(void) {
}

void app_main() {
epd_init(EPD_OPTIONS_DEFAULT);
// EPD_DISPLAY is in settings header
epd_init(&epd_board_v7, &EPD_DISPLAY, EPD_LUT_64K);
hl = epd_hl_init(WAVEFORM);
fb = epd_hl_get_framebuffer(&hl);

Expand Down
6 changes: 4 additions & 2 deletions examples/www-image/main/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define ESP_WIFI_SSID ""
#define ESP_WIFI_PASSWORD ""

// Define DISPLAY
#define EPD_DISPLAY ED052TC4
// Affects the gamma to calculate gray (lower is darker/higher contrast)
// Nice test values: 0.9 1.2 1.4 higher and is too bright
double gamma_value = 0.7;
Expand All @@ -18,7 +20,7 @@ double gamma_value = 0.7;

// Image URL and jpg settings. Make sure to update WIDTH/HEIGHT if using loremflickr
// Note: Only HTTP protocol supported (Check README to use SSL secure URLs) loremflickr
#define IMG_URL ("https://loremflickr.com/1024/768")
#define IMG_URL ("http://img.cale.es/jpg/fasani/5ea1dec401890")

// idf >= 4.3 needs VALIDATE_SSL_CERTIFICATE set to true for https URLs
// Please check the README to understand how to use an SSL Certificate
Expand All @@ -27,7 +29,7 @@ double gamma_value = 0.7;
// verification
// heading ESP-TLS in
// https://newreleases.io/project/github/espressif/esp-idf/release/v4.3-beta1
#define VALIDATE_SSL_CERTIFICATE true
#define VALIDATE_SSL_CERTIFICATE false
// To make an insecure request please check Readme

// Alternative non-https URL:
Expand Down

0 comments on commit 9e8a51b

Please sign in to comment.