add check_battery

This commit is contained in:
cuu 2021-05-29 13:25:14 +08:00
parent 0824e292fd
commit 4e06a6e213
3 changed files with 118 additions and 42 deletions

View File

@ -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);

View File

@ -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<strlen(url);i++) {
parse_serial_stream(cfg,msg[i]);
}
parse_serial_stream(cfg,10);
reset_cmd();
}
int check_battery(CONFIG*cfg){
int bat_cap;
bat_cap = 0;
if( millis() - battery_chk_tm.time > 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;

View File

@ -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;