#include "PowerManager.h"

PowerManager::PowerManager()
{
  
}

PowerManager::~PowerManager()
{
  
}

PowerManager::PowerInfo PowerManager::getPowerInfo(const uint16_t nbOfMeasures) const
{
  uint16_t batteryMeasures(0), temp(0);
  unsigned char i(0);
  int checkLevel(0);
  PowerInfo p;

  for(;i < nbOfMeasures;i++)
  {
    temp = analogRead(ADC);
    batteryMeasures += temp;
  }
  
  batteryMeasures /= nbOfMeasures;
  
  if(batteryMeasures <= USB_THRESHOLD)
  {
    p.powerType = USB;
    p.level = 0;
  }
  else 
  {
    p.powerType = BATTERY; 
    checkLevel = ((double)(batteryMeasures-BATT_EMPTY)*100.0/(double)BATT_DIFF);
    
    if(checkLevel > 100)
      p.level = 100;
    else if (checkLevel < 0)
      p.level = 0;
    else
      p.level = checkLevel;
  }
  
  return p;
}
