35 lines
574 B
C++
35 lines
574 B
C++
//
|
|
// FILE: Battery.h
|
|
// VERSION: 1.0
|
|
// PURPOSE: Reads battery voltage and returns as a percentage value
|
|
//
|
|
//
|
|
|
|
#include "Battery.h"
|
|
#include "mgos.h"
|
|
#include "mgos_adc.h"
|
|
|
|
#ifdef ESP8266
|
|
extern "C" {
|
|
#include "user_interface.h"
|
|
}
|
|
#endif
|
|
|
|
#define RefVoltage_mV 3200
|
|
#define MaxValueToBeRead 1023
|
|
|
|
Battery::Battery() {
|
|
mgos_adc_enable(0);
|
|
}
|
|
|
|
int Battery::getPercentage() {
|
|
int value = mgos_adc_read(0);
|
|
return (value * 100) / MaxValueToBeRead;
|
|
}
|
|
|
|
int Battery::getVoltage_mV() {
|
|
int value = mgos_adc_read(0);
|
|
return RefVoltage_mV * value / MaxValueToBeRead;
|
|
}
|
|
|