215 lines
9.8 KiB
C

//
// Created by Kai Jan Kriegel on 03.02.23.
//
#include "epd.h"
#include "EPD_2in66.h"
#include "platform.h"
#include <esp_err.h>
#include <string.h>
display_t epd_display;
esp_err_t epd_init(display_t display) {
if (DEV_Module_Init() != 0) {
return ESP_FAIL;
}
EPD_2IN66_Init();
epd_display = display;
UWORD Imagesize =
((EPD_2IN66_WIDTH % 8 == 0) ? (EPD_2IN66_WIDTH / 8) : (EPD_2IN66_WIDTH / 8 + 1)) * EPD_2IN66_HEIGHT;
if ((epd_display.canvas = (UBYTE *) malloc(Imagesize)) == NULL) {
ESP_LOGE("EPD", "Failed to allocate epd canvas memory");
return ESP_FAIL;
}
Paint_NewImage(epd_display.canvas, EPD_2IN66_WIDTH, EPD_2IN66_HEIGHT, 270, WHITE);
// Initialise the display
Paint_SelectImage(epd_display.canvas);
Paint_Clear(WHITE);
char readerid[DISPLAY_BUFFER_SIZE];
snprintf(readerid, DISPLAY_BUFFER_SIZE, "Reader ID: %05d", epd_display.reader_info.reader_id);
char batterlevel[DISPLAY_BUFFER_SIZE];
snprintf(batterlevel, DISPLAY_BUFFER_SIZE, "Battery: %d%%", epd_display.reader_info.battery_level);
//Draw border
Paint_DrawRectangle(1, 1, EPD_2IN66_HEIGHT - 1, EPD_2IN66_WIDTH, BLACK, DOT_PIXEL_1X1, DRAW_FILL_EMPTY);
// Draw reader id
Paint_DrawString_EN(17, 2, readerid, &Font12, WHITE, BLACK);
// draw divider
Paint_DrawLine(EPD_2IN66_HEIGHT / 2, 1, EPD_2IN66_HEIGHT / 2, 15, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
// Draw battery level
Paint_DrawString_EN(175, 2, batterlevel, &Font12, WHITE, BLACK);
// Draw header horizontal divider
Paint_DrawLine(1, 15, EPD_2IN66_HEIGHT, 15, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
// draw vertical button divider
ESP_LOGD("EPD", "Drawing vertical button divider");
Paint_DrawLine(40, 15, 40, EPD_2IN66_WIDTH, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
Paint_DrawLine(EPD_2IN66_HEIGHT - 40, 15, EPD_2IN66_HEIGHT - 40, EPD_2IN66_WIDTH, BLACK, DOT_PIXEL_1X1,
LINE_STYLE_SOLID);
// Draw horizontal button divider
ESP_LOGD("EPD", "Drawing horizontal button divider");
Paint_DrawLine(1, ((EPD_2IN66_WIDTH - 15) / 2) + 15, 40, ((EPD_2IN66_WIDTH - 15) / 2) + 15, BLACK, DOT_PIXEL_1X1,
LINE_STYLE_SOLID);
Paint_DrawLine(EPD_2IN66_HEIGHT - 40, ((EPD_2IN66_WIDTH - 15) / 2) + 15, EPD_2IN66_HEIGHT,
((EPD_2IN66_WIDTH - 15) / 2) + 15, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
// Draw buttons
ESP_LOGD("EPD", "Drawing buttons");
Paint_DrawString_EN(12, ((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2), epd_display.buttons.btn0, &Font24, WHITE, BLACK);
Paint_DrawString_EN(12, ((EPD_2IN66_WIDTH - 15) / 2) + 40, epd_display.buttons.btn1, &Font24, WHITE, BLACK);
Paint_DrawString_EN(EPD_2IN66_HEIGHT - 12 - Font24.Width, ((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2), epd_display.buttons.btn2, &Font24, WHITE, BLACK);
Paint_DrawString_EN(EPD_2IN66_HEIGHT - 12 - Font24.Width, ((EPD_2IN66_WIDTH - 15) / 2) + 40, epd_display.buttons.btn3, &Font24, WHITE, BLACK);
// Draw machine name
ESP_LOGD("EPD", "Drawing machine name");
Paint_DrawString_EN(42, 20, epd_display.machine_name, &Font16, WHITE, BLACK);
// Draw machine state
ESP_LOGD("EPD", "Drawing machine state");
Paint_DrawString_EN(60, ((EPD_2IN66_WIDTH - 15) / 2) + 15 - (Font24.Height / 2), epd_display.state, &Font24, WHITE,
BLACK);
// Draw QR code
ESP_LOGD("EPD", "Drawing QR code");
Paint_DrawBitMap_Paste(gImage_qrurn, EPD_2IN66_HEIGHT - 142,
((EPD_2IN66_WIDTH - 15) / 2) - 35, 100, 100, TRUE);
ESP_LOGI("EPD", "Displaying initial layout");
EPD_2IN66_Display(epd_display.canvas);
ESP_LOGD("EPD", "Sleeping");
EPD_2IN66_Sleep();
DEV_Module_Exit();
return ESP_OK;
}
void epd_clear() {
EPD_2IN66_Init();
EPD_2IN66_Clear();
EPD_2IN66_Sleep();
}
void epd_update_reader_info(reader_info_t reader_info) {
if (memcmp(&epd_display.reader_info, &reader_info, sizeof(reader_info_t)) != 0) {
memcpy(&epd_display.reader_info, &reader_info, sizeof(reader_info_t));
DEV_Module_Init();
EPD_2IN66_Init();
EPD_2IN66_Init_Partial();
Paint_SelectImage(epd_display.canvas);
char readerid[DISPLAY_BUFFER_SIZE];
snprintf(readerid, DISPLAY_BUFFER_SIZE, "%05d", epd_display.reader_info.reader_id);
char batterlevel[DISPLAY_BUFFER_SIZE];
snprintf(batterlevel, DISPLAY_BUFFER_SIZE, "%d%%", epd_display.reader_info.battery_level);
Paint_ClearWindows(94, 2, 129, 14, WHITE);
Paint_DrawString_EN(94, 2, readerid, &Font12, WHITE, BLACK);
// blank out battery level
Paint_ClearWindows(238, 2, 266, 14, WHITE);
// Draw battery level
Paint_DrawString_EN(238, 2, batterlevel, &Font12, WHITE, BLACK);
EPD_2IN66_Display(epd_display.canvas);
EPD_2IN66_Sleep();
DEV_Module_Exit();
}
}
void epd_update_state(char* state) {
if (strcmp(epd_display.state, state) != 0) {
ESP_LOGI("EPD", "Updating state to %s", state);
strncpy(epd_display.state, state, DISPLAY_BUFFER_SIZE);
DEV_Module_Init();
EPD_2IN66_Init();
EPD_2IN66_Init_Partial();
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(60, ((EPD_2IN66_WIDTH - 15) / 2) + 15 - (Font24.Height / 2), EPD_2IN66_HEIGHT - 142,
((EPD_2IN66_WIDTH - 15) / 2) + 15 - (Font24.Height / 2) + Font24.Height, WHITE);
Paint_DrawString_EN(60, ((EPD_2IN66_WIDTH - 15) / 2) + 15 - (Font24.Height / 2), epd_display.state, &Font24,
WHITE,
BLACK);
EPD_2IN66_Display(epd_display.canvas);
EPD_2IN66_Sleep();
DEV_Module_Exit();
}
}
void epd_update_buttons(buttons_t* buttons) {
DEV_Module_Init();
EPD_2IN66_Init();
EPD_2IN66_Init_Partial();
if (strcmp(epd_display.buttons.btn0, buttons->btn0) != 0) {
strncpy(epd_display.buttons.btn0, buttons->btn0, DISPLAY_BUFFER_SIZE);
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(12, ((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2), 12 + Font24.Width,
((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2) + Font24.Height, WHITE);
Paint_DrawString_EN(12, ((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2), epd_display.buttons.btn0, &Font24,
WHITE, BLACK);
EPD_2IN66_Display(epd_display.canvas);
}
if (strcmp(epd_display.buttons.btn1, buttons->btn1) != 0) {
strncpy(epd_display.buttons.btn1, buttons->btn1, DISPLAY_BUFFER_SIZE);
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(12, ((EPD_2IN66_WIDTH - 15) / 2) + 40, 12 + Font24.Width,
((EPD_2IN66_WIDTH - 15) / 2) + 40 + Font24.Height, WHITE);
Paint_DrawString_EN(12, ((EPD_2IN66_WIDTH - 15) / 2) + 40, epd_display.buttons.btn1, &Font24, WHITE, BLACK);
EPD_2IN66_Display(epd_display.canvas);
}
if (strcmp(epd_display.buttons.btn2, buttons->btn2) != 0) {
strncpy(epd_display.buttons.btn2, buttons->btn2, DISPLAY_BUFFER_SIZE);
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(EPD_2IN66_HEIGHT - 12 - Font24.Width,
((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2), EPD_2IN66_HEIGHT - 12,
((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2) + Font24.Height, WHITE);
Paint_DrawString_EN(EPD_2IN66_HEIGHT - 12 - Font24.Width,
((EPD_2IN66_WIDTH - 15) / 2) - 20 - (Font24.Height / 2), epd_display.buttons.btn2, &Font24,
WHITE, BLACK);
EPD_2IN66_Display(epd_display.canvas);
}
if (strcmp(epd_display.buttons.btn3, buttons->btn3) != 0) {
strncpy(epd_display.buttons.btn3, buttons->btn3, DISPLAY_BUFFER_SIZE);
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(EPD_2IN66_HEIGHT - 12 - Font24.Width,
((EPD_2IN66_WIDTH - 15) / 2) + 40, EPD_2IN66_HEIGHT - 12,
((EPD_2IN66_WIDTH - 15) / 2) + 40 + Font24.Height, WHITE);
Paint_DrawString_EN(EPD_2IN66_HEIGHT - 12 - Font24.Width,
((EPD_2IN66_WIDTH - 15) / 2) + 40, epd_display.buttons.btn3, &Font24, WHITE, BLACK);
EPD_2IN66_Display(epd_display.canvas);
}
EPD_2IN66_Sleep();
DEV_Module_Exit();
}
void epd_update_machine_name(char* machine_name) {
if (strcmp(epd_display.machine_name, machine_name) != 0) {
strncpy(epd_display.machine_name, machine_name, DISPLAY_BUFFER_SIZE);
DEV_Module_Init();
EPD_2IN66_Init();
EPD_2IN66_Init_Partial();
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(42, 20, 251,
20 + Font16.Height, WHITE);
Paint_DrawString_EN(42, 20, epd_display.machine_name, &Font16, WHITE, BLACK);
EPD_2IN66_Display(epd_display.canvas);
EPD_2IN66_Sleep();
DEV_Module_Exit();
}
}
void epd_update_image(UBYTE* image) {
if (memcmp(epd_display.image, image, DISPLAY_BUFFER_SIZE) != 0) {
memcpy(epd_display.image, image, DISPLAY_BUFFER_SIZE);
DEV_Module_Init();
EPD_2IN66_Init();
EPD_2IN66_Init_Partial();
Paint_SelectImage(epd_display.canvas);
Paint_ClearWindows(EPD_2IN66_HEIGHT - 142,
((EPD_2IN66_WIDTH - 15) / 2) - 35,
EPD_2IN66_HEIGHT - 142 + 100,
((EPD_2IN66_WIDTH - 15) / 2) - 35 + 100, WHITE);
Paint_DrawBitMap_Paste(epd_display.image, EPD_2IN66_HEIGHT - 142,
((EPD_2IN66_WIDTH - 15) / 2) - 35, 100, 100, TRUE);
EPD_2IN66_Display(epd_display.canvas);
EPD_2IN66_Sleep();
DEV_Module_Exit();
}
}