updateChargers via Timer IS

This commit is contained in:
Hendrik Schutter 2018-11-25 22:04:09 +01:00
parent 32879835ca
commit 6c3d5a7184
7 changed files with 147 additions and 42 deletions

View File

@ -28,6 +28,7 @@ int charger::getCurrent() {
double charger::getVoltage() {
mux.setChannel(charger_settings.chU);
int tmp = (int) io.readAdc(0);
if(tmp == 0) return 0;
double ret = ((double) tmp) * 0.00615;
return ret;
}
@ -45,10 +46,12 @@ struct time_t charger::getStartTime() {
}
void charger::getInfo() {
#ifdef DEBUG
char buffer[50];
sprintf(buffer, "Nr: %i - Uch %i - Ich %i\r\n", charger_settings.nr,
charger_settings.chU, charger_settings.chI);
serialSend(buffer);
#endif
}
void charger::setStatus(bool pBool) {
active = pBool;

View File

@ -7,14 +7,16 @@
#include "openChargeMicro.h"
uint32_t systemTime; // 0 to 4294967295 sec
unsigned long int systemTime; // 0 to 4294967295 sec
/* Only call once */
clock::clock() {
//TODO Singleton
systemTime = 0;
clock_init();
#ifdef DEBUG
serialSend("init clock\r\n");
#endif
}
clock::~clock() {
@ -23,6 +25,11 @@ clock::~clock() {
void clock::clock_init() {
OCR1A = 0x3D08; // --> 1 sec
//OCR1A = 0x7A11; // --> 2 sec
TCCR1B |= (1 << WGM12);
// Mode 4, CTC on OCR1A
TIMSK1 |= (1 << OCIE1A);
@ -32,7 +39,7 @@ void clock::clock_init() {
sei(); //enable interrupts
}
/* get seconds since boot*/
uint32_t clock::getTime() {
unsigned long int clock::getTime() {
return systemTime;
}
@ -69,12 +76,12 @@ struct time_t clock::getTimeStamp() {
ret.diffHour = 0;
while (ret.diffSecond > 59) {
ret.diffSecond = -60;
ret.diffSecond = ret.diffSecond - 60;
ret.diffMinute++;
}
while (ret.diffMinute > 59) {
ret.diffMinute = -60;
ret.diffMinute = ret.diffMinute - 60;
ret.diffHour++;
}
@ -85,9 +92,15 @@ ISR (TIMER1_COMPA_vect) {
// action to be done every 1 sec
systemTime++; //increase system time
updateChargers();
}
}
/*
ISR(__vector_default) {
}
*/

View File

@ -9,9 +9,9 @@
#define SRC_CLOCK_H_
struct time_t {
unsigned long int diffHour;
unsigned long int diffMinute;
unsigned long int diffSecond;
int diffHour;
int diffMinute;
int diffSecond;
unsigned long int seconds;
};
@ -23,7 +23,7 @@ private:
public:
clock();
~clock();
uint32_t getTime(); //time in sec since boot
unsigned long int getTime(); //time in sec since boot
struct time_t getTime(struct time_t pTime); //time in sec since time stamp
struct time_t getTimeStamp(); // returns a timestamp

View File

@ -9,12 +9,30 @@
gui::gui() {
oled.oled_init();
}
gui::~gui() {
}
void gui::gui_init(){
oled.oled_init();
}
void gui::gui_info(){
oled.oled_clear_screen();
oled.oled_clear_screen();
oled.oled_gotoxy(0,0);
oled.oled_write("OpenChargeMicro");
oled.oled_gotoxy(0,2);
oled.oled_write("HW: 0.1 SW: dev");
oled.oled_gotoxy(0,6);
oled.oled_write("Akkus einstecken");
}
void gui::gui_print(int pNr, bool pStatus, struct time_t pTime, double pVoltage,
int pCurrent, unsigned long int pCapacity) {
@ -39,7 +57,9 @@ void gui::gui_print(int pNr, bool pStatus, struct time_t pTime, double pVoltage,
/* time */
oled.oled_gotoxy(0, 2);
sprintf(buffer, "----%02d:%02d:%02d----", (int) pTime.diffHour, (int) pTime.diffMinute, (int) pTime.diffSecond);
sprintf(buffer, "----%02d:%02d:%02d----", pTime.diffHour, pTime.diffMinute, pTime.diffSecond);
//sprintf(buffer, "%lu", pTime);
oled.oled_write_str(buffer);
@ -71,3 +91,9 @@ void gui::gui_print(int pNr, bool pStatus, struct time_t pTime, double pVoltage,
oled.oled_write_str(buffer);
}
void gui::gui_write(char* str){
oled.oled_clear_screen();
oled.oled_gotoxy(0,0);
oled.oled_write_str(str);
}

View File

@ -19,7 +19,11 @@ private:
public:
gui();
~gui();
void gui_init();
void gui_print(int pNr, bool pStatus, struct time_t pTime, double pVoltage, int pCurrent, unsigned long int pCapacity);
void gui_write(char* str);
void gui_info();
};

View File

@ -2,22 +2,38 @@
charger* chargers;
clock* clk;
gui* ui;
ioController* io;
void createChargers();
void printStatus();
void checkForBattery();
void updateGUI();
bool everySec();
unsigned short int indexCount = 0;
int indexCount = 0;
int main(void) {
#ifdef DEBUG
serialSetup();
serialSend("Hello World - ");
serialSend(__DATE__ __TIME__);
serialSend("\r\n");
#endif
ui = (gui*) malloc(sizeof(gui));
ui->gui_init();
ui->gui_info();
_delay_ms(1000);
io = (ioController*) malloc(sizeof(ioController));
io->setBuzzer(false);
io->deactivateChargers();
io->setActiveLED(true);
io->activateChargers();
chargers = (charger *) malloc(CHARGER_SIZE * sizeof(charger));
@ -25,37 +41,58 @@ int main(void) {
clock tmp;
clk = &tmp;
ioController io;
io.setBuzzer(false);
io.deactivateChargers();
io.setActiveLED(true);
io.activateChargers();
gui ui;
createChargers();
io.setWS2812(0, 255, 255);
//io.setWS2812(0, 255, 255);
//loop till power off
while (true) {
checkForBattery();
printStatus();
//updateChargers();
updateGUI();
checkForBattery();
}
return 0;
}
void updateGUI() {
if (everySec()) { //updates the ui every sec
bool next = true;
bool notfound = true;
static bool found = true;
int loops = 0;
//if (everySec()) { //updates the ui every sec
//finds one or more active charges or aborts after CHARGER_SIZE
while (next && (loops < CHARGER_SIZE)) {
loops++;
if (chargers[indexCount].getStatus()) {
#ifdef DEBUG
char c[50];
sprintf(c, "updating: %i\r\n", (int) indexCount);
serialSend(c);
#endif
//void gui_print(int pNr, bool pStatus, struct time_t pTime, double pVoltage, int pCurrent, unsigned long int pCapacity);
serialSend("-\r\n");
ui->gui_print((indexCount + 1), true,
clk->getTime(chargers[indexCount].getStartTime()),
chargers[indexCount].getVoltage(),
chargers[indexCount].getCurrent(),
chargers[indexCount].getCapacity());
notfound = false;
next = false;
found = true;
}
indexCount = (indexCount + 1) % CHARGER_SIZE;
} //end while
if (notfound && found) {
ui->gui_info();
found = false;
}
} //end everySec
//}
}
bool everySec() {
@ -70,17 +107,20 @@ bool everySec() {
void updateChargers() {
//serialSend("updateChargers\r\n");
if (everySec()) { //updates the chargers every sec
for (int i = 0; i < CHARGER_SIZE; i++) {
if (chargers[i].getStatus()) {
//charger active --> battery pluged on
chargers[i].update();
}
//if (everySec()) { //updates the chargers every sec
for (int i = 0; i < CHARGER_SIZE; i++) {
if (chargers[i].getStatus()) {
//charger active --> battery pluged on
chargers[i].update();
}
}
//}
}
void checkForBattery() {
bool activeChargers[CHARGER_SIZE];
for (int l = 0; l < CHARGER_SIZE; l++) {
bool zero = false;
@ -106,10 +146,19 @@ void checkForBattery() {
serialSend(" Volt\r\n");
*/
chargers[l].setStatus(true);
//io->setActiveLED(true);
if (!activeChargers[l]) {
chargers[l].setStartTime(clk->getTimeStamp());
}
activeChargers[l] = true;
} else {
//serialSend("blocked\r\n");
//io->setActiveLED(false);
chargers[l].setStatus(false);
chargers[l].reset();
activeChargers[l] = false;
}
}
@ -158,6 +207,8 @@ void printStatus() {
}
}
#ifdef DEBUG
void serialSetup(void) {
//Register settings
//High and low bits
@ -165,7 +216,7 @@ void serialSetup(void) {
UBRR0L = BUAD_RATE_CALC;
//transimit and recieve enable
UCSR0B = (1 << TXEN0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); //8 bit data format
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);//8 bit data format
}
void serialSend(const char* sendString) {
@ -176,3 +227,4 @@ void serialSend(const char* sendString) {
}
}
#endif

View File

@ -1,3 +1,7 @@
//#define DEBUG
/* system header */
#include <avr/io.h>
#include <string.h>
@ -15,10 +19,6 @@
#include "gui.h"
#include "charger.h"
/* Pins */
#define WS2812B PD2
#define ACTIVELED PB5
@ -33,13 +33,16 @@
#define S1 PD6
#define S0 PD5
#ifdef DEBUG
/* Serial */
#define BUAD 9600
#define BUAD_RATE_CALC ((F_CPU/16/BUAD) - 1)
void serialSetup(void);
void serialSend(const char* sendString);
#endif
/* Charger Config */
#define CHARGER_SIZE 4
@ -68,6 +71,10 @@ void serialSend(const char* sendString);
void updateChargers();
void updateGUI();
void checkForBattery();