Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ enum WIFI_CONNECT_RETRY_TIME // Time to sleep before trying to connect to the Wi

#define PIN_RESET 9
#define PIN_INTERRUPT 2

#ifdef EPDIY
#define PIN_BATTERY 1
#else
#define PIN_BATTERY 3
#endif

#define BUTTON_HOLD_TIME 5000

Expand Down
2 changes: 2 additions & 0 deletions lib/trmnl/src/bmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ bmp_err_e parseBMPHeader(uint8_t *data, bool &reversed)
uint32_t imageDataSize = *(uint32_t *)&data[34];
uint32_t colorTableEntries = *(uint32_t *)&data[46];

#ifndef EPDIY
if (width != 800 || height != 480 || bitsPerPixel != 1 || imageDataSize != 48000 || colorTableEntries != 2)
return BMP_BAD_SIZE;
#endif
Comment on lines +28 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifndef EPDIY
if (width != 800 || height != 480 || bitsPerPixel != 1 || imageDataSize != 48000 || colorTableEntries != 2)
return BMP_BAD_SIZE;
#endif
if (width != DISPLAY_WIDTH ||
height != DISPLAY_HEIGHT ||
bitsPerPixel != IMAGE_BPP ||
imageDataSize != (DISPLAY_WIDTH*DISPLAY_HEIGHT*IMAGE_BPP/8) ||
colorTableEntries != (1<<IMAGE_BPP))
return BMP_BAD_SIZE;

// Get the offset of the pixel data
uint32_t dataOffset = *(uint32_t *)&data[10];

Expand Down
27 changes: 24 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ board_build.partitions = min_spiffs.csv
board_build.filesystem = spiffs
;board_build.partitions = custom_partition.csv
upload_speed = 460800
build_flags =
build_flags =
-D ARDUINO_USB_MODE=0
-D ARDUINO_USB_CDC_ON_BOOT=0
-D CORE_DEBUG_LEVEL=0
lib_ldf_mode=deep
lib_deps =
lib_deps =
;WiFiManager@^2.0.16-rc.2
esphome/[email protected]
bblanchon/[email protected]
Expand Down Expand Up @@ -60,4 +60,25 @@ lib_deps =
fabiobatsilva/ArduinoFake@^0.4.0
build_flags =
-D ARDUINOJSON_ENABLE_ARDUINO_STRING=1
-std=gnu++11
-std=gnu++11


[env:epdiy]
platform = [email protected]
board = esp32-s3-devkitm-1
framework = arduino
build_flags =
-DBOARD_HAS_PSRAM
-DEPDIY
-DEPDIY_TYPE=ED060XC3
-DEPDIY_WIDTH=1024
-DEPDIY_HEIGHT=758
Comment on lines +74 to +75

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-DEPDIY_WIDTH=1024
-DEPDIY_HEIGHT=758
-DDISPLAY_WIDTH=1024
-DDISPLAY_HEIGHT=758
-DDISPLAY_BPP=4

board_build.arduino.memory_type = qio_opi
board_build.partitions = min_spiffs.csv
board_build.filesystem = spiffs
monitor_speed = 115200
lib_deps =
esphome/[email protected]
bblanchon/[email protected]
https://github.com/thijse/Arduino-Log.git#3f4fcf5a345c1d542e56b88c0ffcb2bd2a5b0bd0
https://github.com/vroland/epdiy.git#ba7a4190102ebc430f4002009f3787ad5cbb321e
18 changes: 16 additions & 2 deletions src/bl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@

bool pref_clear = false;

#ifdef EPDIY
// image buffer (62 bytes for metadata
uint8_t buffer[((uint32_t) EPDIY_WIDTH * EPDIY_HEIGHT / 8) + 62];
#else
uint8_t buffer[48062]; // image buffer
#endif
Comment on lines +31 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(does not need to be addressed in this PR, but could be a general improvement)

48062 could also be expanded into meaningful constant calculation,
and EPDIY specifics can be eliminated here that way.

eg;

Suggested change
#ifdef EPDIY
// image buffer (62 bytes for metadata
uint8_t buffer[((uint32_t) EPDIY_WIDTH * EPDIY_HEIGHT / 8) + 62];
#else
uint8_t buffer[48062]; // image buffer
#endif
// in build settings:
// TODO: define DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_BPP with TRMNL defaults (800x480 1bpp)
// TODO: define IMAGE_BPP (default = DISPLAY_BPP)
// in `bl.h`:
// in bytes - 62 bytes - header; 48000 bytes - bitmap (480*800 1bpp) / 8
#define DISPLAY_BMP_IMAGE_SIZE (((uint32_t)DISPLAY_WIDTH * DISPLAY_HEIGHT * IMAGE_BPP / 8) + 62)
uint8_t buffer[DISPLAY_BMP_IMAGE_SIZE];

char filename[1024]; // image URL
char binUrl[1024]; // update URL
char log_array[1024]; // log
Expand Down Expand Up @@ -1087,6 +1092,7 @@ static https_request_err_e downloadAndShow()
Log.info("%s [%d]: Content size: %d\r\n", __FILE__, __LINE__, https.getSize());

uint32_t counter = 0;
#ifndef EPDIY
if (https.getSize() != DISPLAY_BMP_IMAGE_SIZE)
{
Log.error("%s [%d]: Receiving failed. Bad file size\r\n", __FILE__, __LINE__);
Expand All @@ -1095,6 +1101,8 @@ static https_request_err_e downloadAndShow()

return HTTPS_REQUEST_FAILED;
}
#endif
uint32_t content_size = https.getSize();
WiFiClient *stream = https.getStreamPtr();
Log.info("%s [%d]: RSSI: %d\r\n", __FILE__, __LINE__, WiFi.RSSI());
Log.info("%s [%d]: Stream timeout: %d\r\n", __FILE__, __LINE__, stream->getTimeout());
Expand All @@ -1112,7 +1120,7 @@ static https_request_err_e downloadAndShow()
int iteration_counter = 0;

Log.info("%s [%d]: Starting a download at: %d\r\n", __FILE__, __LINE__, getTime());
while (counter != DISPLAY_BMP_IMAGE_SIZE && millis() - download_start < 10000)
while (counter != content_size && millis() - download_start < 10000)
{
if (stream->available())
{
Expand All @@ -1125,7 +1133,7 @@ static https_request_err_e downloadAndShow()
}
Log.info("%s [%d]: Ending a download at: %d, in %d iterations\r\n", __FILE__, __LINE__, getTime(), iteration_counter);

if (counter != DISPLAY_BMP_IMAGE_SIZE)
if (counter != content_size)
{

Log.error("%s [%d]: Receiving failed. Readed: %d\r\n", __FILE__, __LINE__, counter);
Expand Down Expand Up @@ -1599,8 +1607,10 @@ static void goToSleep(void)
preferences.putUInt(PREFERENCES_LAST_SLEEP_TIME, getTime());
preferences.end();
esp_sleep_enable_timer_wakeup(time_to_sleep * SLEEP_uS_TO_S_FACTOR);
#ifndef EPDIY
esp_deep_sleep_enable_gpio_wakeup(1 << PIN_INTERRUPT,
ESP_GPIO_WAKEUP_GPIO_LOW);
#endif
esp_deep_sleep_start();
}

Expand Down Expand Up @@ -1649,7 +1659,11 @@ static float readBatteryVoltage(void)
adc += analogReadMilliVolts(PIN_BATTERY);
}

#ifdef EPDIY
int32_t sensorValue = (adc / 128) * 1.402;
#else
int32_t sensorValue = (adc / 128) * 2;
#endif

float voltage = sensorValue / 1000.0;
return voltage;
Expand Down
113 changes: 107 additions & 6 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
#include <display.h>
#include <ArduinoLog.h>
#include "DEV_Config.h"
#include "EPD.h"

#include "GUI_Paint.h"
#include <config.h>
#include <ImageData.h>

#ifdef EPDIY
#include "epd_highlevel.h"
#include "epdiy.h"

EpdiyHighlevelState hl;
#else
#include "EPD.h"
#endif

/**
* @brief Function to init the display
* @param none
Expand All @@ -16,11 +25,21 @@
void display_init(void)
{
Log.info("%s [%d]: dev module start\r\n", __FILE__, __LINE__);
#ifdef EPDIY
epd_init(&epd_board_v7, &EPDIY_TYPE, EPD_LUT_64K);
epd_set_vcom(2550);
#else
DEV_Module_Init();
#endif
Log.info("%s [%d]: dev module end\r\n", __FILE__, __LINE__);


Log.info("%s [%d]: screen hw start\r\n", __FILE__, __LINE__);
#ifdef EPDIY
hl = epd_hl_init(EPD_BUILTIN_WAVEFORM);
#else
EPD_7IN5_V2_Init_New();
#endif
Log.info("%s [%d]: screen hw end\r\n", __FILE__, __LINE__);
}

Expand All @@ -32,7 +51,13 @@ void display_init(void)
void display_reset(void)
{
Log.info("%s [%d]: e-Paper Clear start\r\n", __FILE__, __LINE__);
#ifdef EPDIY
int temperature = 25;
epd_poweron();
epd_fullclear(&hl, temperature);
#else
EPD_7IN5_V2_Clear();
#endif
Log.info("%s [%d]: e-Paper Clear end\r\n", __FILE__, __LINE__);
// DEV_Delay_ms(500);
}
Expand All @@ -43,7 +68,11 @@ void display_reset(void)
*/
uint16_t display_height()
{
#ifdef EPDIY
return EPDIY_HEIGHT;
#else
return EPD_7IN5_V2_HEIGHT;
#endif
Comment on lines +71 to +75

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef EPDIY
return EPDIY_HEIGHT;
#else
return EPD_7IN5_V2_HEIGHT;
#endif
return DISPLAY_HEIGHT;

}

/**
Expand All @@ -52,8 +81,34 @@ uint16_t display_height()
*/
uint16_t display_width()
{
#ifdef EPDIY
return EPDIY_WIDTH;
#else
return EPD_7IN5_V2_WIDTH;
#endif
Comment on lines +84 to +88

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef EPDIY
return EPDIY_WIDTH;
#else
return EPD_7IN5_V2_WIDTH;
#endif
return DISPLAY_WIDTH;

}

#ifdef EPDIY
void convert_1bit_to_4bit(const uint8_t *fb_1bit, uint8_t *fb_4bit, int width, int height) {
int byte_width = ((width + 31) / 32) * 4; // Each byte in 1-bit framebuffer stores 8 pixels
int row_size_out = width / 2; // Each row in the 4-bit framebuffer (2 pixels per byte)

for (int y = 0; y < height; y++) {
int out_row_index = (height - 1 - y) * row_size_out; // Flip Y-axis

for (int x = 0; x < width; x += 2) {
int byte_index = (y * byte_width) + (x / 8);
int bit_index1 = 7 - (x % 8);
int bit_index2 = 7 - ((x + 1) % 8);

uint8_t pixel1 = (fb_1bit[byte_index] >> bit_index1) & 1;
uint8_t pixel2 = (fb_1bit[byte_index] >> bit_index2) & 1;

fb_4bit[out_row_index++] = (pixel1 ? 0x0F : 0x00) << 4 | (pixel2 ? 0x0F : 0x00);
}
}
}
#endif
Comment on lines +91 to +111

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional is not necessary. Compiler would eliminate dead code if unused anyway.

Suggested change
#ifdef EPDIY
void convert_1bit_to_4bit(const uint8_t *fb_1bit, uint8_t *fb_4bit, int width, int height) {
int byte_width = ((width + 31) / 32) * 4; // Each byte in 1-bit framebuffer stores 8 pixels
int row_size_out = width / 2; // Each row in the 4-bit framebuffer (2 pixels per byte)
for (int y = 0; y < height; y++) {
int out_row_index = (height - 1 - y) * row_size_out; // Flip Y-axis
for (int x = 0; x < width; x += 2) {
int byte_index = (y * byte_width) + (x / 8);
int bit_index1 = 7 - (x % 8);
int bit_index2 = 7 - ((x + 1) % 8);
uint8_t pixel1 = (fb_1bit[byte_index] >> bit_index1) & 1;
uint8_t pixel2 = (fb_1bit[byte_index] >> bit_index2) & 1;
fb_4bit[out_row_index++] = (pixel1 ? 0x0F : 0x00) << 4 | (pixel2 ? 0x0F : 0x00);
}
}
}
#endif
void convert_1bit_to_4bit(const uint8_t *fb_1bit, uint8_t *fb_4bit, int width, int height) {
int byte_width = ((width + 31) / 32) * 4; // Each byte in 1-bit framebuffer stores 8 pixels
int row_size_out = width / 2; // Each row in the 4-bit framebuffer (2 pixels per byte)
for (int y = 0; y < height; y++) {
int out_row_index = (height - 1 - y) * row_size_out; // Flip Y-axis
for (int x = 0; x < width; x += 2) {
int byte_index = (y * byte_width) + (x / 8);
int bit_index1 = 7 - (x % 8);
int bit_index2 = 7 - ((x + 1) % 8);
uint8_t pixel1 = (fb_1bit[byte_index] >> bit_index1) & 1;
uint8_t pixel2 = (fb_1bit[byte_index] >> bit_index2) & 1;
fb_4bit[out_row_index++] = (pixel1 ? 0x0F : 0x00) << 4 | (pixel2 ? 0x0F : 0x00);
}
}
}


/**
* @brief Function to show the image on the display
Expand All @@ -63,13 +118,47 @@ uint16_t display_width()
*/
void display_show_image(uint8_t *image_buffer, bool reverse)
{
auto width = display_width();
auto height = display_height();
uint16_t width = display_width();
uint16_t height = display_height();

#ifdef EPDIY

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef EPDIY
#if IMAGE_BPP == 1 && DISPLAY_BPP == 4

uint32_t width_b = *(uint32_t *)&image_buffer[18];
uint32_t height_b = *(uint32_t *)&image_buffer[22];
uint32_t dataOffset = *(uint32_t *)&image_buffer[10];

// set to default value if header is faulty
if(width == 0 || height == 0) {
width = 800;
height = 480;
}
Comment on lines +130 to +133

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(width == 0 || height == 0) {
width = 800;
height = 480;
}
if(width_b == 0 || height_b == 0) {
width_b = width;
height_b = height;
}


if(dataOffset == 0) {
dataOffset = 62;
}
Log.info("%s [%d]: Image width: %d, height: %d!\r\n", __FILE__, __LINE__, width_b, height_b);
Log.info("%s [%d]: Data offset: %d!\r\n", __FILE__, __LINE__, dataOffset);

EpdRect paint_area = { .x = (width - width_b) / 2, .y = (height - height_b) / 2, .width = width_b, .height = height_b };
uint8_t *image_buffer_4bpp = new uint8_t[width_b * height_b / 2];
convert_1bit_to_4bit(image_buffer + dataOffset, image_buffer_4bpp, width_b, height_b);

uint8_t temperature = 22;

epd_poweron();
epd_fullclear(&hl, temperature);

epd_copy_to_framebuffer(paint_area, image_buffer_4bpp, epd_hl_get_framebuffer(&hl));

enum EpdDrawError _err = epd_hl_update_screen(&hl, MODE_EPDIY_WHITE_TO_GL16, temperature);
Log.info("%s [%d]: Paint_NewImage %s\r\n", __FILE__, __LINE__, _err);
delete[] image_buffer_4bpp;
epd_poweroff();
#else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#else
#elseif IMAGE_BPP != DISPLAY_BPP
#error Unsupported display configuration for BPP conversion
#else

// Create a new image cache
UBYTE *BlackImage;
/* you have to edit the startup_stm32fxxx.s file and set a big enough heap size */
UWORD Imagesize = ((width % 8 == 0) ? (width / 8) : (width / 8 + 1)) * height;

Log.error("%s [%d]: free heap - %d\r\n", __FILE__, __LINE__, ESP.getFreeHeap());
Log.error("%s [%d]: free alloc heap - %d\r\n", __FILE__, __LINE__, ESP.getMaxAllocHeap());
if ((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL)
Expand All @@ -81,7 +170,7 @@ void display_show_image(uint8_t *image_buffer, bool reverse)
// if (reverse)
// Paint_NewImage(BlackImage, EPD_7IN5_V2_WIDTH, EPD_7IN5_V2_HEIGHT, 0, BLACK);
// else

Paint_NewImage(BlackImage, width, height, 0, WHITE);

Log.info("%s [%d]: show image for array\r\n", __FILE__, __LINE__);
Expand All @@ -97,10 +186,12 @@ void display_show_image(uint8_t *image_buffer, bool reverse)
}
Paint_DrawBitMap(image_buffer + 62);
EPD_7IN5_V2_Display(BlackImage);
Log.info("%s [%d]: display\r\n", __FILE__, __LINE__);

free(BlackImage);
BlackImage = NULL;
#endif
Log.info("%s [%d]: display\r\n", __FILE__, __LINE__);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}

/**
Expand All @@ -111,6 +202,8 @@ void display_show_image(uint8_t *image_buffer, bool reverse)
*/
void display_show_msg(uint8_t *image_buffer, MSG message_type)
{
#ifdef EPDIY
#else
Comment on lines +205 to +206

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef EPDIY
#else
#ifndef EPDIY

auto width = display_width();
auto height = display_height();
UBYTE *BlackImage;
Expand Down Expand Up @@ -243,6 +336,7 @@ void display_show_msg(uint8_t *image_buffer, MSG message_type)
Log.info("%s [%d]: display\r\n", __FILE__, __LINE__);
free(BlackImage);
BlackImage = NULL;
#endif
}

/**
Expand All @@ -257,6 +351,8 @@ void display_show_msg(uint8_t *image_buffer, MSG message_type)
*/
void display_show_msg(uint8_t *image_buffer, MSG message_type, String friendly_id, bool id, const char *fw_version, String message)
{
#ifdef EPDIY
#else
Comment on lines +354 to +355

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef EPDIY
#else
#ifndef EPDIY

if (message_type == WIFI_CONNECT)
{
Log.info("%s [%d]: Display set to white\r\n", __FILE__, __LINE__);
Expand Down Expand Up @@ -326,6 +422,7 @@ void display_show_msg(uint8_t *image_buffer, MSG message_type, String friendly_i
Log.info("%s [%d]: display\r\n", __FILE__, __LINE__);
free(BlackImage);
BlackImage = NULL;
#endif
}

/**
Expand All @@ -336,5 +433,9 @@ void display_show_msg(uint8_t *image_buffer, MSG message_type, String friendly_i
void display_sleep(void)
{
Log.info("%s [%d]: Goto Sleep...\r\n", __FILE__, __LINE__);
#ifdef EPDIY
epd_poweroff();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
epd_poweroff();
epd_poweroff();

#else
EPD_7IN5B_V2_Sleep();
#endif
}