From 4e06a6e2131415d3c3dfa3346f4fed881f79f1e8 Mon Sep 17 00:00:00 2001 From: cuu Date: Sat, 29 May 2021 13:25:14 +0800 Subject: [PATCH] add check_battery --- Code/thermal_printer/config.h | 10 +- .../thermal_printer/devterm_thermal_printer.c | 123 +++++++++++++++--- Code/thermal_printer/printer.c | 27 +--- 3 files changed, 118 insertions(+), 42 deletions(-) diff --git a/Code/thermal_printer/config.h b/Code/thermal_printer/config.h index 2ca80c2..38b9315 100644 --- a/Code/thermal_printer/config.h +++ b/Code/thermal_printer/config.h @@ -209,7 +209,8 @@ typedef struct _CONFIG uint16_t wordgap:10;//1023 max uint8_t max_pts;// max pts in print_dots_8bit_split - + uint8_t lock; + Margin margin; FONT*font; ImageCache *img; @@ -225,6 +226,13 @@ typedef struct _SerialCache{ uint8_t data[77];//384/5 }SerialCache; +typedef struct _TimeRec{ + unsigned int time; + uint8_t last_status; + uint8_t check; + +}TimeRec; + void PrintDots8bit(uint8_t *Array, uint8_t characters,uint8_t feed_num); uint8_t invert_bit(uint8_t a); diff --git a/Code/thermal_printer/devterm_thermal_printer.c b/Code/thermal_printer/devterm_thermal_printer.c index 9676271..126e5a2 100644 --- a/Code/thermal_printer/devterm_thermal_printer.c +++ b/Code/thermal_printer/devterm_thermal_printer.c @@ -45,6 +45,8 @@ FONT current_font; CONFIG g_config; +TimeRec battery_chk_tm; + int printf_out(CONFIG*cfg, char *format, ...) { va_list args; int rv; @@ -119,8 +121,13 @@ void init_printer(){ g_config.density = 0; g_config.feed_pitch = 2; - g_config.max_pts = 1; + g_config.max_pts = 2; + g_config.lock = 0; + + battery_chk_tm.time = millis(); + battery_chk_tm.last_status = 0; + battery_chk_tm.check = 0; } const char url[] ={"Powered by clockworkpi.com"}; @@ -659,6 +666,88 @@ void parse_serial_stream(CONFIG*cfg,uint8_t input_ch){ socat -d -d pty,link=/tmp/DEVTERM_PRINTER_OUT,raw,echo=0 pty,link=/tmp/DEVTERM_PRINTER_IN,raw,echo=0 */ +int read_bat_cap(CONFIG*cfg) { + long ret; + char c[12]; + FILE *fptr; + if ((fptr = fopen(BAT_CAP, "r")) == NULL) { + PRINTF("Error! Battery File cannot be opened\n"); + // Program exits if the file pointer returns NULL. + return -1; + } + + fscanf(fptr, "%[^\n]", c); + //printf("Data from the file:%s\n", c); + fclose(fptr); + + ret = strtol(c, NULL, 10); + + return (int)ret; + +} + +int bat_cap_to_pts(CONFIG*cfg,int bat) { + int pts; + + pts = (int)round( (float)bat/10.0 ) + 1; + return pts; +} + + +void print_lowpower(CONFIG*cfg) { + + int i; + char*msg = "low power,please charge"; + + reset_cmd(); + + for(i=0;i 200) { + + bat_cap = read_bat_cap(cfg); + + if( bat_cap < 0){ + cfg->max_pts = 1;//no battery ,so set to the slowest + cfg->lock = 0;// unlock printer anyway + } + + if(bat_cap >=0 && bat_cap < 90) { + + if(cfg->lock == 0) { + print_lowpower(cfg); + + } + cfg->lock =1; + reset_cmd(); // clear all serial_cache + }else { + + cfg->max_pts = bat_cap_to_pts(cfg,bat_cap); + + cfg->lock = 0; + } + + battery_chk_tm.time = millis(); + + } + + return 0; + +} + #define FIFO_FILE "/tmp/DEVTERM_PRINTER_OUT" void loop() { @@ -680,20 +769,24 @@ void loop() { if (fp != NULL) { g_config.fp = fp; while(1) - { - fread(readbuf,1, 1,fp); - //printf("read %x",readbuf[0]); - if(g_config.state == PRINT_STATE) { - if(readbuf[0] == ASCII_TAB) { - readbuf[0] = ' '; - parse_serial_stream(&g_config,readbuf[0]); - parse_serial_stream(&g_config,readbuf[0]); - } else {//not a tab - parse_serial_stream(&g_config,readbuf[0]); - } - } else { //cfg->state == PRINT_STATE - parse_serial_stream(&g_config,readbuf[0]); - } + { + check_battery(&g_config); + + if(g_config.lock ==0) { + fread(readbuf,1, 1,fp); + //printf("read %x",readbuf[0]); + if(g_config.state == PRINT_STATE) { + if(readbuf[0] == ASCII_TAB) { + readbuf[0] = ' '; + parse_serial_stream(&g_config,readbuf[0]); + parse_serial_stream(&g_config,readbuf[0]); + } else {//not a tab + parse_serial_stream(&g_config,readbuf[0]); + } + } else { //cfg->state == PRINT_STATE + parse_serial_stream(&g_config,readbuf[0]); + } + } } fclose(fp); g_config.fp = NULL; diff --git a/Code/thermal_printer/printer.c b/Code/thermal_printer/printer.c index d6b4c1a..0c4f163 100644 --- a/Code/thermal_printer/printer.c +++ b/Code/thermal_printer/printer.c @@ -423,29 +423,6 @@ int glob_file(char*av) { #endif -int bat_cap_to_pts(CONFIG*cfg) { - int pts; - long ret; - char c[12]; - FILE *fptr; - if ((fptr = fopen(BAT_CAP, "r")) == NULL) { - printf("Error! Battery File cannot be opened\n"); - // Program exits if the file pointer returns NULL. - return 0; - } - - fscanf(fptr, "%[^\n]", c); - //printf("Data from the file:%s\n", c); - fclose(fptr); - - ret = strtol(c, NULL, 10); - - pts = (int)round( (float)ret/10.0 ) + 1; - return pts; -} - - - uint8_t print_lines8(CONFIG*cfg) { uint8_t i,j,k; int8_t w; @@ -477,8 +454,6 @@ uint8_t print_lines8(CONFIG*cfg) { row = 0; rv = IsPaper(); - cfg->max_pts = bat_cap_to_pts(cfg); - data = (uint8_t*)malloc(sizeof(uint8_t)*(pad+1)); i=0; @@ -607,7 +582,7 @@ uint8_t print_image8(CONFIG*cfg){ addr = 0; rv = IsPaper(); - cfg->max_pts = bat_cap_to_pts(cfg); + while(y < height ) { x=0;