P2FA/c/src/p2fa.c
2024-11-21 19:33:36 -05:00

202 lines
5.5 KiB
C

//
// Created by steven on 11/14/2024.
//
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ncurses.h>
#include "../libs/go_p2fa.h"
#include "images.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);
}
void home(UWORD *blackImage) {
struct getConfigNames_return configNamesReturn = getConfigNames();
int length = configNamesReturn.r1;
char** configNames = configNamesReturn.r0;
DebugLine(12, 0, "Len: %d\n", length);
if (configNames == NULL || length == 0) {
DebugLine(10, 0, "No configs 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);
for (int i = 0; i < length; i++) {
if (configNames[i] == NULL) {
DebugLine(13, 0, "Null Config: %d\n", i);
}
Paint_DrawString(5, 5 + (i * 20), configNames[i], &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);
int i = 0;
int ch;
while (1) {
ch = getch();
if (ch != ERR) {
switch(ch) {
case KEY_UP:
if (i > 0) i--;
break;
case KEY_DOWN:
if (i < length - 1) i++;
break;
case '\n':
GoString goConfigName;
getCode(configNames[i]);
break;
case 'q':
return;
}
}
Paint_ClearWindows(110, 5 + (i * 20), 117, 17 + (i * 20), WHITE);
Paint_DrawString(115, 5 + (i * 20), "<", &Font12, WHITE, BLACK);
}
freeCStringArray(configNames, length);
DEV_Delay_ms(2000);
}
void code(UWORD *blackImage, char* confName) {
int ch;
codeLoop:
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);
DebugLine(3, 0, "Code: %s\n", code);
Paint_DrawString(5, 5, code, &Font36, WHITE, BLACK);
Paint_DrawString(5, 100, confName, &Font12, WHITE, BLACK);
Paint_DrawRectangle(10, 112, 240, 92, 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();
DebugLine(4, 0, "Time Left: %d\n", i);
double percentLeft = ((double) i / (double) (30 * 1000));
progBarEndIndex = 228 - ((int) (228 * percentLeft));
Paint_ClearWindows(progBarCurrentIndex, 111, progBarEndIndex, 93, WHITE);
int line = 15;
while (progBarCurrentIndex < progBarEndIndex) {
DebugLine(line, 0, "Line: %d,111 - %d,93\n", progBarCurrentIndex, progBarEndIndex);
Paint_DrawLine(progBarCurrentIndex, 111, progBarCurrentIndex, 93, 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);
DebugLine(5, 0, "Elapsed Time %d\n", elapsedTime);
if (elapsedTime < 1000) {
DebugLine(6, 0, "Delay for %dms\n", (1000 - (elapsedTime * 1000)));
DEV_Delay_ms(1000 - (elapsedTime * 1000));
}
lastI = i;
}
}
}
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;
}
home(blackImage);
code(blackImage);
return 0;
}
int main() {
signal(SIGINT, Handler);
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
loadConfigs();
drawLoop();
closeP2FA();
endwin();
return 0;
}