|
| 1 | +#include "battery.h" |
| 2 | + |
| 3 | + |
| 4 | +void indicator_led_on(){ |
| 5 | + digitalWrite(PC13, LOW); |
| 6 | +} |
| 7 | + |
| 8 | +void indicator_led_off(){ |
| 9 | + digitalWrite(PC13, HIGH); |
| 10 | +} |
| 11 | + |
| 12 | +void flash_one_time(int ts,int restore_status) { |
| 13 | + for(int i=0;i<ts;i++) { |
| 14 | + indicator_led_on(); |
| 15 | + delay(400); |
| 16 | + indicator_led_off(); |
| 17 | + delay(200); |
| 18 | + } |
| 19 | + digitalWrite(PC13,restore_status); |
| 20 | +} |
| 21 | + |
| 22 | +void show_bat_segs(){ |
| 23 | + if(!PMU.isBatteryConnect()) return; |
| 24 | + |
| 25 | + int pcnt = PMU.getBatteryPercent(); |
| 26 | + int last_d201_status = digitalRead(PC13); |
| 27 | + |
| 28 | + if(pcnt >0 && pcnt < 33) { |
| 29 | + //show one time |
| 30 | + flash_one_time(1,last_d201_status); |
| 31 | + }else if(pcnt >= 33 && pcnt <66){ |
| 32 | + //show 2 times |
| 33 | + flash_one_time(2,last_d201_status); |
| 34 | + }else if(pcnt >=66 && pcnt <= 100){ |
| 35 | + //show 3 times |
| 36 | + flash_one_time(3,last_d201_status); |
| 37 | + } |
| 38 | + |
| 39 | + if(PMU.isCharging()){ |
| 40 | + start_chg(); |
| 41 | + } |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +void low_bat(){ |
| 46 | + if(PMU.isBatteryConnect() && !PMU.isCharging()){ |
| 47 | + int pcnt = PMU.getBatteryPercent(); |
| 48 | + if(pcnt <= LOW_BAT_VAL){ |
| 49 | + //This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery. |
| 50 | + indicator_led_off(); |
| 51 | + if(pcnt <= 1) {//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery. |
| 52 | + PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_4HZ); |
| 53 | + if(pcnt==0){//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery. |
| 54 | + PMU.shutdown();//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery. |
| 55 | + } |
| 56 | + }else{ |
| 57 | + PMU.setChargingLedMode(XPOWERS_CHG_LED_ON); |
| 58 | + } |
| 59 | + }else{ |
| 60 | + indicator_led_on(); |
| 61 | + PMU.setChargingLedMode(XPOWERS_CHG_LED_OFF); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void start_chg(){ |
| 67 | + indicator_led_on(); |
| 68 | + PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_1HZ); |
| 69 | +} |
| 70 | + |
| 71 | +void stop_chg(){ |
| 72 | + PMU.setChargingLedMode(XPOWERS_CHG_LED_OFF); |
| 73 | + low_bat(); |
| 74 | +} |
0 commit comments