From 3b84959ee650235730d10283f5a5589dfb55eb9a Mon Sep 17 00:00:00 2001 From: cuu Date: Mon, 21 Apr 2025 15:46:18 +0800 Subject: [PATCH] Update picocalc_helloworld battery info now has charging status flag --- Code/picocalc_helloworld/i2ckbd/i2ckbd.h | 2 ++ Code/picocalc_helloworld/main.c | 27 +++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Code/picocalc_helloworld/i2ckbd/i2ckbd.h b/Code/picocalc_helloworld/i2ckbd/i2ckbd.h index b9414f2..0a162b0 100644 --- a/Code/picocalc_helloworld/i2ckbd/i2ckbd.h +++ b/Code/picocalc_helloworld/i2ckbd/i2ckbd.h @@ -17,4 +17,6 @@ void init_i2c_kbd(); int read_i2c_kbd(); int read_battery(); +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) +#define bitClear(value, bit) ((value) &= ~(1 << (bit))) #endif \ No newline at end of file diff --git a/Code/picocalc_helloworld/main.c b/Code/picocalc_helloworld/main.c index f9f44c4..f2d2a76 100644 --- a/Code/picocalc_helloworld/main.c +++ b/Code/picocalc_helloworld/main.c @@ -202,9 +202,25 @@ void pwm_interrupt_handler() { // } } +void test_battery(){ + char buf[64]; + int bat_pcnt = read_battery(); + bat_pcnt = bat_pcnt>>8; + int bat_charging = bitRead(bat_pcnt,7); + bitClear(bat_pcnt,7); + sprintf(buf,"battery percent is %d\n",bat_pcnt); + printf("%s", buf); + lcd_print_string(buf); + if(bat_charging ==0 ){ + sprintf(buf,"battery is not charging\n"); + }else{ + sprintf(buf,"battery is charging\n"); + } + printf("%s", buf); + lcd_print_string(buf); +} int main() { - char buf[64]; set_sys_clock_khz(133000, true); stdio_init_all(); @@ -235,18 +251,13 @@ int main() { // otherwise we won't get working battery infos sleep_ms(2000); - int bat_pcnt = read_battery(); - - sprintf(buf,"battery percent is %d\n",bat_pcnt>>8); - printf(buf); - lcd_print_string(buf); + test_battery(); while (1) { - int c = lcd_getc(0); if(c != -1 && c > 0) { lcd_putc(0,c); } - sleep_ms(10); + sleep_ms(20); } }