From b232a42eba8e3e194359e32b2cc37b7b4fda1596 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 11 Apr 2023 14:38:54 +0200 Subject: [PATCH] Added isClose to test if the adc changes brutally, and getGauge which simplifies the battery bar. --- implementation/battery.c | 56 ++++++++++++++++++++++++---------------- implementation/battery.h | 2 ++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/implementation/battery.c b/implementation/battery.c index f3f5329..0730561 100644 --- a/implementation/battery.c +++ b/implementation/battery.c @@ -2,43 +2,55 @@ extern MyUART_Struct_Typedef uartCool; int actualMinutes =-1; +uint32_t oldAdc =0; void battery(uint32_t data) { MyRTC_Struct_TypeDef rtcBattery; MyRTC_GetTime(&rtcBattery); - if(actualMinutes == rtcBattery.minutes) + + if((actualMinutes == rtcBattery.minutes) && isClose(oldAdc,data,50)) //pas de precision/10 % { return; } + + oldAdc = data; actualMinutes = rtcBattery.minutes; float percentBattery = ((float)data)/MAX_BAT; - uint8_t batteryBar[13]={'[','.','.','.','.','.','.','.','.','.','.',']',0x20}, batteryGauge=percentBattery*10, i; - if(batteryGauge>10) - { - batteryGauge = 10; - } - char timerClock[8] = {'[','.','.','h','.','.',']',0x20}; - sprintf(timerClock+1,"%.*d",2,rtcBattery.hours); - timerClock[3]=':'; - sprintf(timerClock+4,"%.*d",2,rtcBattery.minutes); - timerClock[3]='\t'; - MyUART_SendArray(&uartCool, (uint8_t *)timerClock, 8); + char batteryBar[13]="[__________]"; + char testChar[24]; - for(i=(10-batteryGauge); i<11; i++) - { - batteryBar[i]='#'; - } - MyUART_SendArray(&uartCool, batteryBar, 13); - - char batteryPercentString[3]; - sprintf(batteryPercentString,"%.*g",2,percentBattery*100); - batteryPercentString[2]='%'; - MyUART_SendArray(&uartCool, (uint8_t *)batteryPercentString, 6); + getGauge(batteryBar, percentBattery); + sprintf(testChar,"[%.2d:%.2d] %s %.2d%%",rtcBattery.hours,rtcBattery.minutes,batteryBar,(int)(percentBattery*100)); + MyUART_SendArray(&uartCool, (uint8_t *)testChar, 24); MyUART_Send(&uartCool, '\n'); } +void getGauge(char gauge[], float percent) +{ + int i; + percent=percent*10; + if(percent>10) + { + percent = 10.0; + } + for(i=(10-percent)+1; i<11; i++) + { + gauge[i]='#'; + } + gauge[12]='\0'; +} + +char isClose(uint32_t data, uint32_t compare, int precision) +{ + if(data < precision) + { + return !(data >= compare+precision); + } + return !((data >= compare+precision) || (data <= compare-precision)); +} + void initBattery(void) { MyADC_Init_Periph(battery); diff --git a/implementation/battery.h b/implementation/battery.h index 88b754b..d593a80 100644 --- a/implementation/battery.h +++ b/implementation/battery.h @@ -8,6 +8,8 @@ #define MAX_BAT 1145 void battery(uint32_t data); +void getGauge(char gauge[], float percent); void initBattery(void); +char isClose(uint32_t data, uint32_t compare, int precision); #endif