status.c 2.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include "epicardium.h"
#include "modules/modules.h"
#include "modules/log.h"

#include "portexpander.h"
#include "MAX77650-Arduino-Library.h"
#include "FreeRTOS.h"
#include "timers.h"

fleur's avatar
fleur committed
10
#include <stdio.h>
11
12
13
14
15
16
#include <stdint.h>
#define POWERLED 10
#define FREQ 100
#define PULSEWIDTH 2
#define PULSELENGTH 10

fleur's avatar
fleur committed
17
static uint8_t power_status = 0; // 0: discharging, 1: below 3.8V, 2: below 3.6V, 254: charging, 255: full and connected to charger, 128: charge error, 127: undefined
18
19
20
21
static float bat_volt = 0;
static float bat_cur = 0;
static float char_volt = 0;
static float char_cur = 0;
fleur's avatar
fleur committed
22
static bool power_led_on = true;
23

fleur's avatar
fleur committed
24
25
static TimerHandle_t status_timer;
static StaticTimer_t status_timer_buffer;
26
27
28
29
30
31
32
33
34

static void get_power_status(){
	epic_read_battery_voltage(&bat_volt);
	epic_read_battery_current(&bat_cur);
	epic_read_chargein_voltage(&char_volt);
	epic_read_chargein_current(&char_cur);

	if(char_volt > 4){
		if(char_cur > 0.1){
fleur's avatar
fleur committed
35
			power_status = 254;
36
		} else {
fleur's avatar
fleur committed
37
38
39
40
41
			if(bat_volt>4.1){
				power_status = 255;
			} else {
				power_status = 128;
			}
42
43
		}
	} else if(bat_volt>3.8){
fleur's avatar
fleur committed
44
		power_status = 0;
45
	} else if(bat_volt>3.6){
fleur's avatar
fleur committed
46
		power_status = 1;
47
	} else {
fleur's avatar
fleur committed
48
		power_status = 2;
49
50
51
	}
}

fleur's avatar
fleur committed
52
static void update_power_led(){
53
	static uint8_t counter = 0;
fleur's avatar
fleur committed
54
55
56
57
	static uint8_t old_power_status = 127;
	if(old_power_status == power_status && power_status != 2){
		return;
	}
fleur's avatar
fleur committed
58
	switch(power_status){
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		case 0: 
		case 1:
			epic_leds_set(POWERLED,0,0,0);
			return;
		case 254: 
			epic_leds_set(POWERLED,0,0,200);
			return;
		case 255: 
			epic_leds_set(POWERLED,0,200,0);
			return;
		case 2: 
			if(counter<PULSEWIDTH){
				epic_leds_set(POWERLED,200,0,0);
			} else {
				epic_leds_set(POWERLED,0,0,0);
			}	
			counter++;
			counter = counter % PULSELENGTH;
		}
fleur's avatar
fleur committed
78
	old_power_status = power_status;
79
80
81
82
}

static void CALLBACK_led(){
	get_power_status();
fleur's avatar
fleur committed
83
84
	if(power_led_on){
		update_power_led();
85
86
87
88
	}
}

void epic_status_start(){
fleur's avatar
fleur committed
89
90
	if(!status_timer) {
		status_timer = xTimerCreateStatic(
91
92
93
94
95
				"status_led",
				FREQ,
				pdTRUE,
				NULL,
				CALLBACK_led,
fleur's avatar
fleur committed
96
				&status_timer_buffer
97
		);
fleur's avatar
fleur committed
98
		xTimerStart(status_timer, 0);
99
100
101
	}
}

fleur's avatar
fleur committed
102
uint8_t epic_status_get_power_status(){
fleur's avatar
fleur committed
103
	if (!status_timer || !xTimerIsTimerActive(status_timer)) {
104
105
		return -ENODATA;
	}
fleur's avatar
fleur committed
106
	return power_status;
107
108
109
}

void epic_status_stop(){
fleur's avatar
fleur committed
110
111
	if (!status_timer || xTimerIsTimerActive(status_timer) == pdFALSE) { return; }
	xTimerStop(status_timer, 0);
112
113
}

fleur's avatar
fleur committed
114
115
void epic_status_power_led(bool onoff){
	power_led_on = onoff;
116
}