Skip to content

Commit

Permalink
Display battery level in percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Jul 11, 2021
1 parent 621d6a5 commit f708c4b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion examples/M5_CoreInk/M5_CoreInk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "epaper_fonts.h"
#include "forecast_record.h"
#include "M5CoreInk.h"
#include "esp_adc_cal.h"

#define SCREEN_WIDTH 200
#define SCREEN_HEIGHT 200
Expand Down Expand Up @@ -144,11 +145,31 @@ void DisplayTempHumiSection(int x, int y) {
display.setTextSize(1);
drawString(x + 60, y + 83, String(WxConditions[0].Humidity, 0) + "% RH", CENTER); // Show Humidity
}


float getBatVoltage()
{
analogSetPinAttenuation(35,ADC_11db);
esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 3600, adc_chars);
uint16_t ADCValue = analogRead(35);

uint32_t BatVolmV = esp_adc_cal_raw_to_voltage(ADCValue,adc_chars);
float BatVol = float(BatVolmV) * 25.1 / 5.1 / 1000;
return BatVol;
}


//#########################################################################################
void DisplayHeadingSection() {
drawString(2, 2, Time_str, LEFT);
drawString(SCREEN_WIDTH - 2, 0, Date_str, RIGHT);
drawString(SCREEN_WIDTH / 2, 0, version, CENTER);

char batteryStrBuff[64];
int batPerc = (int)(123-(123/(powf(1+powf(getBatVoltage()/3.7f, 80), 0.165f))));
sprintf(batteryStrBuff,"%d %%", batPerc);
Serial.println(batteryStrBuff);
drawString(SCREEN_WIDTH / 2, 0, batteryStrBuff, CENTER);
display.drawLine(0, 12, SCREEN_WIDTH, 12, GxEPD_BLACK);
}
//#########################################################################################
Expand Down

0 comments on commit f708c4b

Please sign in to comment.