Add averaging to reading battery levels
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Compile / Compile (push) Successful in 1m21s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Compile / Compile (push) Successful in 1m21s
				
			This commit is contained in:
		
							parent
							
								
									32cd26fccf
								
							
						
					
					
						commit
						5993fda85e
					
				
					 2 changed files with 15 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -2,6 +2,9 @@
 | 
			
		|||
#include "Battery.h"
 | 
			
		||||
#include <Wire.h>
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
#include <limits>
 | 
			
		||||
 | 
			
		||||
float WatchFeatures::Battery::m_previousVoltage = std::numeric_limits<float>::infinity();
 | 
			
		||||
 | 
			
		||||
WatchFeatures::Battery::Battery()
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -9,13 +12,20 @@ WatchFeatures::Battery::Battery()
 | 
			
		|||
 | 
			
		||||
float WatchFeatures::Battery::GetVoltage()
 | 
			
		||||
{
 | 
			
		||||
  return analogReadMilliVolts(BATT_ADC_PIN) / 1000.0f * 2.0f;
 | 
			
		||||
  float voltage = analogReadMilliVolts(BATT_ADC_PIN) / 1000.0f * 2.0f;
 | 
			
		||||
 | 
			
		||||
  if (m_previousVoltage == std::numeric_limits<float>::infinity()) {
 | 
			
		||||
    m_previousVoltage = voltage;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float averageVoltage = (m_previousVoltage + voltage) / 2.0f;
 | 
			
		||||
  m_previousVoltage = voltage;
 | 
			
		||||
  return averageVoltage;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t WatchFeatures::Battery::GetPercentage()
 | 
			
		||||
{
 | 
			
		||||
  float voltage = GetVoltage();
 | 
			
		||||
  Serial.println(voltage);
 | 
			
		||||
 | 
			
		||||
  float level = (GetVoltage() - 3.6f) / 0.6f;
 | 
			
		||||
  if (level < 0.0f) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,4 +13,7 @@ public:
 | 
			
		|||
  Battery();
 | 
			
		||||
  float GetVoltage();
 | 
			
		||||
  uint8_t GetPercentage();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
  static float m_previousVoltage;
 | 
			
		||||
};
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue