// // Created by steven on 11/14/2024. // #include #include #include #include #include "../libs/go_p2fa.h" #include "../libs/e-Paper/EPD_2in13_V4.h" #include "../libs/GUI/GUI_Paint.h" #include "../libs/GUI/GUI_BMPfile.h" #include "../libs/Config/Debug.h" void closeP2FA() { EPD_2in13_V4_Init(); EPD_2in13_V4_Clear(); EPD_2in13_V4_Sleep(); DEV_Delay_ms(2000);//important, at least 2s DEV_Module_Exit(); } void Handler(int sigNum) { DebugLine(28, 0, "Caught signal %d\n", sigNum); closeP2FA(); endwin(); exit(0); } int computeWidth(char* text, sFONT *font) { return (font->Width) * strlen(text); } //TODO Put display to sleep after void code(UWORD *blackImage, char* confName) { int ch; while(1) { ch = getch(); if (ch != ERR) { if (ch == 'q') break; DebugLine(2, 0, "Key Pressed: %c\n", ch); } Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 90, WHITE); EPD_2in13_V4_Init(); Paint_SelectImage(blackImage); Paint_Clear(WHITE); GoString confStr = {confName, strlen(confName)}; const char *code = getCode(confStr); Paint_DrawString(5, 20, code, &Font36, WHITE, BLACK); Paint_DrawString(8, 5, confName, &Font12, WHITE, BLACK); Paint_DrawRectangle(10, 114, 240, 94, BLACK, DOT_PIXEL_1X1, DRAW_FILL_EMPTY); EPD_2in13_V4_Display_Base(blackImage); Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 90, WHITE); Paint_SelectImage(blackImage); clock_t startTime; clock_t endTime; int elapsedTime; int progBarCurrentIndex = 11; int progBarEndIndex; int lastI = 30 * 1000; for (int i = getTimeRemainingMS(30);; i = getTimeRemainingMS(30)) { ch = getch(); if (ch != ERR) { if (ch == 'q') return; } if (i > lastI) break; startTime = clock(); double percentLeft = (double) i / (double) (30 * 1000); progBarEndIndex = 228 - (int) (228 * percentLeft); Paint_ClearWindows(progBarCurrentIndex, 113, progBarEndIndex, 95, WHITE); int line = 15; while (progBarCurrentIndex < progBarEndIndex) { Paint_DrawLine(progBarCurrentIndex, 113, progBarCurrentIndex, 95, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID); ++progBarCurrentIndex; ++line; if (line == 21) line = 15; } EPD_2in13_V4_Display_Partial(blackImage); endTime = clock(); elapsedTime = (int) ((endTime - startTime) / CLOCKS_PER_SEC); if (elapsedTime < 1000) { DEV_Delay_ms(1000 - (elapsedTime * 1000)); } lastI = i; } } } void config(UWORD *blackImage, char* configName) { GoString goConfName = { configName, strlen(configName) }; const char *config = getConfig(goConfName); if (config == NULL) { DebugLine(10, 0, "Config not found\n"); return; } Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); EPD_2in13_V4_Init(); Paint_SelectImage(blackImage); Paint_Clear(WHITE); DebugLine(14, 10, "MADE IT TO HERE --------------------------------------"); Paint_DrawString(5, 5, "key: JBSWY3DPEHPK3PXP\ndigits: 4", &Font12, WHITE, BLACK); //Paint_DrawString(5, 5, getConfigReturn.r0, &Font12, WHITE, BLACK); EPD_2in13_V4_Display(blackImage); int ch = getch(); if (ch == 'q') { return; } } void drawTotpHome(UWORD *blackImage, char** configNames, int *length) { Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); Paint_SelectImage(blackImage); Paint_Clear(WHITE); for (int j = 0; j < *length; j++) { if (configNames[j] == NULL) { DebugLine(13, 0, "Null Config: %d\n", j); } Paint_DrawString(5, 5 + j * 20, configNames[j], &Font12, WHITE, BLACK); } EPD_2in13_V4_Display_Base(blackImage); Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); Paint_SelectImage(blackImage); } void totpHome(UWORD *blackImage) { struct getConfigNames_return configNamesReturn = getConfigNames(); int length = configNamesReturn.r1; char** configNames = configNamesReturn.r0; if (configNames == NULL || length == 0) { DebugLine(10, 0, "No configs found\n"); return; } drawTotpHome(blackImage, configNames, &length); int i = 0; Paint_DrawString(115, 5 + (i * 20), "<", &Font12, WHITE, BLACK); EPD_2in13_V4_Display_Partial(blackImage); int ch; while (1) { timeout(-1); ch = getch(); if (ch != ERR) { switch(ch) { case KEY_UP: if (i > 0) i--; Paint_ClearWindows(115, 5 + (i + 1) * 20, 122, 17 + (i + 1) * 20, WHITE); break; case KEY_DOWN: if (i < length - 1) i++; Paint_ClearWindows(115, 5 + (i - 1) * 20, 122, 17 + (i - 1) * 20, WHITE); break; case '\n': timeout(0); code(blackImage, configNames[i]); drawTotpHome(blackImage, configNames, &length); break; case 'c': config(blackImage, configNames[i]); drawTotpHome(blackImage, configNames, &length); break; case 'q': return; } } Paint_DrawString(115, 5 + (i * 20), "<", &Font12, WHITE, BLACK); EPD_2in13_V4_Display_Partial(blackImage); } freeCStringArray(configNames, length); DEV_Delay_ms(2000); } int drawLoop() { if (DEV_Module_Init() != 0) { return -1; } EPD_2in13_V4_Init(); EPD_2in13_V4_Clear(); UBYTE *blackImage; UWORD ImageSize = ((EPD_2in13_V4_WIDTH % 8 == 0) ? (EPD_2in13_V4_WIDTH / 8) : (EPD_2in13_V4_WIDTH / 8 + 1)) * EPD_2in13_V4_HEIGHT; if ((blackImage = (UBYTE *) malloc(ImageSize)) == NULL) { DebugLine(8, 0, "Failed to apply for black memory...\n"); return -1; } totpHome(blackImage); return 0; } int main() { signal(SIGINT, Handler); signal(SIGTERM, Handler); initscr(); cbreak(); noecho(); nodelay(stdscr, TRUE); keypad(stdscr, TRUE); loadConfigs(); drawLoop(); closeP2FA(); endwin(); return 0; }