-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathso_power.h
294 lines (272 loc) · 7.41 KB
/
so_power.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#ifndef POWER_H
#define POWER_H
#include <Arduino.h>
#include "so_declare.h"
#include "so_parameter.h"
#include "so_display.h"
#include "so_gyro.h"
#include "so_espnow.h"
const int windowSize = 1000;
float batteryLevels[windowSize];
int readIndex = 0;
// ADC Characteristics
const float batteryMaxVoltage = 4.2; // Maximum voltage of battery
const float showFullBatteryAboveVoltage = 4.1; // Maximum voltage of battery
const float showEmptyBatteryBelowVoltage = 3.4; // Maximum voltage of battery
const float dividerRatio = 2.0; // Ratio of voltage divider (100kΩ/100kΩ)
const float adcResolution = 4095.0; // ESP32 ADC resolution for 12-bit (0-4095)
const float referenceVoltage = 3.3; // Reference voltage of ESP32 ADC
// Low battery threshold
const float lowBatteryThreshold = 3.3; // Battery voltage threshold for low battery action
void (*resetFunc)(void) = 0; //AVR重启函数
void reset() {
#ifdef ESP32
ESP.restart();
#endif // ESP32
#ifdef AVR
resetFunc();
#endif // AVR
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_MBED_RP2040)
NVIC_SystemReset();
#endif // defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_MBED_RP2040)
}
#ifdef ESP32
void esp32_wakeup() {
#ifndef ESP32C3
detachInterrupt(GPIO_NUM_BUTTON_POWER);
#endif
u8g2.setPowerSave(0);
scale.powerUp();
mpu.enableCycle(false);
}
void esp32_sleep() {
//beep(4, 50);
u8g2.setPowerSave(1);
mpu.enableCycle(true);
scale.powerDown();
#ifdef ESP32C3
esp_deep_sleep_enable_gpio_wakeup(1 << GPIO_NUM_BUTTON_POWER, ESP_GPIO_WAKEUP_GPIO_LOW);
#else
attachInterrupt(GPIO_NUM_BUTTON_POWER, esp32_wakeup, FALLING);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_BUTTON_POWER, LOW);
#endif
esp_deep_sleep_start();
}
#endif //ESP32
void shut_down_now() {
Serial.println("power off");
buzzer.beep(4, 50);
#ifdef CHINESE
refreshOLED((char*)"关机", FONT_M);
#endif
#ifdef ENGLISH
refreshOLED((char*)"Power off", FONT_M);
#endif
#ifdef ESPNOW
if (b_f_espnow) {
b_f_power_off = 1;
updateEspnow(1);
}
#endif
delay(1000);
#ifdef ESP32
esp32_sleep();
#else
pinMode(BUTTON_KEY, OUTPUT);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, OUTPUT);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(1000);
#endif //ESP32
}
void shut_down_low_battery(float voltage) {
Serial.print("Low battery, voltage:");
Serial.println(voltage);
#ifdef CHINESE
refreshOLED((char*)"电量不足", FONT_M);
#endif
#ifdef ENGLISH
refreshOLED((char*)"Low battery", FONT_M);
#endif
#ifdef ESPNOW
if (b_f_espnow) {
b_f_power_off = 1;
updateEspnow(1);
}
#endif
delay(1000);
#ifdef ESP32
esp32_sleep();
#else
pinMode(BUTTON_KEY, OUTPUT);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, OUTPUT);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(1000);
#endif
}
void shut_down_now_nobeep() {
Serial.println("power off no beep");
#ifdef CHINESE
refreshOLED((char*)"关机", FONT_M);
#endif
#ifdef ENGLISH
refreshOLED((char*)"Power off", FONT_M);
#endif
#ifdef ESPNOW
if (b_f_espnow) {
b_f_power_off = 1;
updateEspnow(1);
}
#endif
delay(1000);
#ifdef ESP32
esp32_sleep();
#else
pinMode(BUTTON_KEY, OUTPUT);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, OUTPUT);
delay(BUTTON_KEY_DELAY);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(1000);
#endif
}
void shut_down_now_accidentTouch() {
Serial.println("accdient on, power off...");
#ifdef ESPNOW
if (b_f_espnow) {
b_f_power_off = 1;
updateEspnow(1);
}
#endif
#ifdef ESP32
esp32_sleep();
#else
pinMode(BUTTON_KEY, OUTPUT);
delay(300);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(300);
pinMode(BUTTON_KEY, OUTPUT);
delay(300);
pinMode(BUTTON_KEY, INPUT_PULLUP);
delay(1000);
#endif
}
float getVoltage(int batteryPin) {
int adcValue = analogRead(batteryPin); // Read the value from ADC
float voltageAtPin = (adcValue / adcResolution) * referenceVoltage; // Calculate voltage at ADC pin
float batteryVoltage = voltageAtPin * dividerRatio; // Calculate the actual battery voltage
float correctedVoltage = batteryVoltage * f_batteryCalibrationFactor;
return correctedVoltage;
}
void power_off(int min) {
if (getVoltage(BATTERY_PIN) < BATTERY_VOLTAGE_MIN) {
//shut_down_low_battery(getVoltage(BATTERY_PIN));
return;
}
if (!b_f_is_charging) {
if (min == -1) {
t_power_off = millis();
//Serial.println("power off timer reset");
}
if (min > 0) {
double d_timeleft = min * 60 - (millis() - t_power_off) / 1000;
// Serial.print(d_timeleft);
// Serial.println(" seconds to power off");
if (d_timeleft <= 0) {
shut_down_now();
}
}
}
}
void power_off_gyro(int sec) {
if (!b_f_is_charging) {
if (sec == -1) {
t_power_off_gyro = millis();
//Serial.println("power off by gyro timer reset");
}
if (sec > 0) {
double d_timeleft = sec - (millis() - t_power_off_gyro) / 1000;
//Serial.print(d_timeleft);
//Serial.println(" seconds to power off by gyro");
if (d_timeleft <= 0) {
shut_down_now();
}
}
}
}
void power_off(double sec) {
if (!b_f_is_charging) {
if (sec == -1) {
t_power_off = millis();
//Serial.println("power off timer reset");
}
if (sec > 0) {
double d_timeleft = sec - (millis() - t_power_off) / 1000;
//Serial.print(d_timeleft);
//Serial.println(" seconds to power off");
if (d_timeleft <= 0) {
shut_down_now();
}
}
}
}
#ifdef CHECKBATTERY
float get_usb_voltage() {
Serial.print("get_usb_voltage");
Serial.println(analogRead(USB_PIN) * f_vref / (pow(2, ADC_BIT) - 1) * f_divider_factor);
return analogRead(USB_PIN) * f_vref / (pow(2, ADC_BIT) - 1) * f_divider_factor;
}
float get_bat_voltage() {
Serial.print("get_bat_voltage");
Serial.println(analogRead(USB_PIN) * f_vref / (pow(2, ADC_BIT) - 1) * f_divider_factor);
return analogRead(BATTERY_PIN) * f_vref / (pow(2, ADC_BIT) - 1) * f_divider_factor;
}
#endif //CHECKBATTERY
void checkBattery() {
// Serial.print("Battery Voltage:");
// Serial.print(getVoltage(BATTERY_PIN));
float perc = map(getVoltage(BATTERY_PIN) * 1000, showEmptyBatteryBelowVoltage * 1000, showFullBatteryAboveVoltage * 1000, 0, 100);//map funtion doesn't take float as input.
// Serial.print(" ");
// Serial.print(perc);
// Serial.print("\%,");
i_icon = map(perc, 0, 100, 1, 5);
// Serial.print(" i_icon =");
// Serial.println(i_icon);
switch (i_icon) {
case 0:
c_battery = (char*)"0";
break;
case 1:
c_battery = (char*)"1";
break;
case 2:
c_battery = (char*)"2";
break;
case 3:
c_battery = (char*)"3";
break;
case 4:
c_battery = (char*)"4";
break;
case 5:
c_battery = (char*)"5";
break;
default:
c_battery = (char*)"5";
break;
}
// if (getVoltage(BATTERY_PIN) > 4.20) {
// b_f_is_charging = true;
// //Serial.println("is charging");
// }
}
#endif