/*
 * Charger.cpp
 *
 *  Created on: 19.11.2018
 *      Author: Hendrik Schutter
 */

#include "openChargeMicro.h"

charger::charger(const struct s_charger pCharger) {
	charger_settings = pCharger;
	active = false;
	capacity = 0;
}

charger::~charger() {
}

/* returns current charge-current in mA */
int charger::getCurrent() {
	mux.setChannel(charger_settings.chI);
	int tmp = (int) io.readAdc(0);
	int ret = (int) (tmp * 1.0); //TODO calibration and testing
	return ret;
}

/* returns current battery voltage in V */
double charger::getVoltage() {
	mux.setChannel(charger_settings.chU);
	int tmp = (int) io.readAdc(0);
	double ret = ((double) tmp) * 0.00615;
	return ret;
}

/* returns summed up charged capacity since charge start in mAh */
unsigned int charger::getCapacity() {
	return capacity;
}

void charger::setStartTime(struct time_t pTime) {
	startTime = pTime;
}
struct time_t charger::getStartTime() {
	return startTime;
}

void charger::getInfo() {
	char buffer[50];
	sprintf(buffer, "Nr: %i - Uch %i - Ich %i\r\n", charger_settings.nr,
			charger_settings.chU, charger_settings.chI);
	serialSend(buffer);
}
void charger::setStatus(bool pBool) {
	active = pBool;
}
bool charger::getStatus() {
	return active;
}

/* resets the capacity */
void charger::reset() {

}

/* updates the capacity */
void charger::update() {
	// 1sec / 3600
	const double div = 0.000277778;
	capacity = capacity + ((unsigned int) (div * getCurrent()));
	//serialSend("update\r\n");
/*
	char charVal[10];
	dtostrf(getCurrent(), 4, 0, charVal);
	serialSend(charVal);
	serialSend(" mA\r\n");
*/
}

