|
@@ -7,18 +7,33 @@
|
|
|
|
|
|
*/
|
|
|
|
|
|
+float mapfloat(float x, float in_min, float in_max, float out_min, float out_max){
|
|
|
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
|
+}
|
|
|
+
|
|
|
//Update the battery reading from ADC pin
|
|
|
-void updateBatteryReading(){
|
|
|
- batRemain = 19;
|
|
|
+void updateBatteryReading() {
|
|
|
+ int rawValue = analogRead(A0);
|
|
|
+ // rawValue shd be within 0 - 0.854v range
|
|
|
+ // aka 0 - 875 (ADC Output)
|
|
|
+ float voltage = rawValue * (1 / 1023.0);
|
|
|
+ voltage *= 4.91; //Using a 4.7k-1.2k voltage divider
|
|
|
+
|
|
|
+ //Convert voltage to percentage
|
|
|
+ int percentage = mapfloat(voltage, 3.2, 4.2, 0, 100);
|
|
|
+ batRemain = percentage;
|
|
|
+ Serial.println(rawValue);
|
|
|
+ Serial.println(voltage);
|
|
|
+ Serial.println(batRemain);
|
|
|
}
|
|
|
|
|
|
//Draw the battery icon at the bottom right hand corner
|
|
|
void drawBatteryIcon() {
|
|
|
updateBatteryReading();
|
|
|
-
|
|
|
+
|
|
|
//Draw the battery icon
|
|
|
uint16_t barColor = GxEPD_BLACK;
|
|
|
- if (batRemain <= 20){
|
|
|
+ if (batRemain <= 20) {
|
|
|
barColor = GxEPD_RED;
|
|
|
}
|
|
|
int iconBaseX = display.width() - 50;
|
|
@@ -27,7 +42,7 @@ void drawBatteryIcon() {
|
|
|
display.fillRoundRect(iconBaseX + 40, iconBaseY + 5, 4, 10, 3, GxEPD_BLACK);
|
|
|
//Internal bar
|
|
|
int maxBarWidth = 34;
|
|
|
- int barWidth = int(float(batRemain)/100.0 * maxBarWidth);
|
|
|
+ int barWidth = int(float(batRemain) / 100.0 * maxBarWidth);
|
|
|
display.fillRoundRect(iconBaseX + 3, iconBaseY + 3, barWidth, 14, 3, barColor);
|
|
|
|
|
|
//Draw percentage
|