-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
136 lines (116 loc) · 3.21 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "EPD_Test.h" // Examples
#include "run_file.h"
#include "led.h"
#include "waveshare_PCF85063.h" // RTC
#include "ff.h"
#include <math.h>
#define enChargingRtc 0
float voltageToPercentage(float voltage)
{
float percentage;
if (voltage > 4.19f) {
percentage = 100;
} else if (voltage < 3.5f){
percentage = 0;
}
else {
percentage = 2808.3808f * powf(voltage, 4) - 43560.9157f * powf(voltage, 3) + 252848.5888f * powf(voltage, 2) - 650767.4615f * voltage + 626532.5703f;
}
return percentage;
}
float measureVBAT(void) {
const float conversion_factor = (3.3f / (1 << 12)) * 3;
uint32_t sum = 0;
for (int i = 0; i < 100; i++) {
uint16_t result = adc_read();
sum += result;
}
float average = (float)sum / 100;
float voltage = average * conversion_factor;
float percentage = voltageToPercentage(voltage);
printf("Raw value: %f, voltage: %fV, percentage: %f\n", average, voltage, percentage);
return voltage;
}
void chargeState_callback()
{
if(gpio_get(VBUS)) {
if(!gpio_get(CHARGE_STATE)) { // is charging
ledCharging();
}
else { // charge complete
ledCharged();
}
}
}
void setTimeFromCard() {
uint8_t buf[6];
FRESULT fr;
FIL fil;
unsigned int br;
fr = f_open(&fil, "time.dat", FA_READ);
if(FR_OK != fr && FR_EXIST != fr) {
printf("time.dat doesn't exist\n");
return;
}
f_read(&fil, &buf, 6, &br);
f_close(&fil);
f_unlink("time.dat");
Time_data time = {buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]};
PCF85063_SetTime(time);
}
void run_display(float voltage)
{
bool hasCard = sdTest();
if(hasCard) {
run_mount();
setTimeFromCard();
EPD_7in3f_display_BMP(voltage);
run_unmount();
}
PCF85063_clear_alarm_flag(); // clear RTC alarm flag
scheduleAlarm(30); // RTC run alarm
}
int main(void)
{
printf("Init\n");
if(DEV_Module_Init() != 0) { // DEV init
return -1;
}
watchdog_enable(8*1000, 1);
gpio_set_irq_enabled_with_callback(CHARGE_STATE, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, chargeState_callback);
float voltage = measureVBAT();
if(voltage < 3.5f) { // battery power is low
printf("low power\n");
PCF85063_alarm_Time_Disable();
ledLowPower(); // LED flash for Low power
powerOff(); // BAT off
return 0;
}
else {
printf("work\n");
ledPowerOn();
}
if(!gpio_get(VBUS)) { // no charge state
run_display(voltage);
}
else { // charge state
chargeState_callback();
while(gpio_get(VBUS)) {
#if enChargingRtc
if(!DEV_Digital_Read(RTC_INT)) { // RTC interrupt trigger
printf("rtc interrupt\n");
run_display(voltage);
}
#endif
if(!gpio_get(BAT_STATE)) { // KEY pressed
printf("key interrupt\n");
voltage = measureVBAT();
run_display(voltage);
}
DEV_Delay_ms(100);
}
}
printf("power off\n");
powerOff(); // BAT off
return 0;
}