Skip to content

Commit 164dd5a

Browse files
committed
✨ feat: Add battery level display to SSD1306 OLED
Add battery level display to SSD1306 OLED screen for ESP32. Closes #4
1 parent f612c12 commit 164dd5a

File tree

3 files changed

+31
-56
lines changed

3 files changed

+31
-56
lines changed

include/components/module.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct Battery_conf {
1111
};
1212

1313
void setup_battery(Battery_conf *conf);
14-
uint8_t get_electricity(Battery_conf *conf);
14+
uint8_t get_electricity();
1515

1616
// MX1584.cpp
1717

@@ -48,11 +48,11 @@ void set_IRF520_PWM(uint32_t duty);
4848
// SSD1306.cpp
4949
void setup_SSD1306();
5050
void show_logo();
51-
void show_dashboard(uint8_t mode, uint8_t battery_value);
52-
void show_dashboard_battery_20(uint8_t battery_value);
51+
void show_dashboard(uint8_t mode);
52+
void show_dashboard_battery_20();
5353
void show_dashboard_wifi_20();
54-
void show_dashboard_body(uint8_t mode, uint8_t battery_value);
55-
void show_dashboard_battery(uint8_t battery_value);
54+
void show_dashboard_body(uint8_t mode);
55+
void show_dashboard_battery();
5656

5757
// VL53L0X.cpp
5858
struct VL53L0X_value{

src/components/module/SSD1306.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ void show_logo() {
1919
display.display();
2020
}
2121

22-
void show_dashboard(uint8_t mode, uint8_t battery_value) {
22+
void show_dashboard(uint8_t mode) {
2323
display.clearDisplay();
2424

25-
show_dashboard_battery_20(70);
25+
show_dashboard_battery_20();
2626
display.drawBitmap(6, 22, image_ap_20, 20, 20, WHITE);
2727
show_dashboard_wifi_20();
28-
show_dashboard_body(mode, battery_value);
28+
show_dashboard_body(mode);
2929

3030
display.display();
3131
}
3232

33-
void show_dashboard_battery_20(uint8_t battery_value) {
33+
void show_dashboard_battery_20() {
34+
uint8_t battery_value = get_electricity();
35+
3436
if(battery_value > 60) {
3537
display.drawBitmap(6, 1, image_batter_full_20, 20, 20, WHITE);
3638

@@ -50,7 +52,7 @@ void show_dashboard_wifi_20() {
5052
}
5153
}
5254

53-
void show_dashboard_body(uint8_t mode, uint8_t battery_value) {
55+
void show_dashboard_body(uint8_t mode) {
5456
static int body = 0;
5557
if(body == 0) {
5658
body = 1;
@@ -69,11 +71,13 @@ void show_dashboard_body(uint8_t mode, uint8_t battery_value) {
6971
}
7072
} else if(body == 1) {
7173
body = 0;
72-
show_dashboard_battery(battery_value);
74+
show_dashboard_battery();
7375
}
7476
}
7577

76-
void show_dashboard_battery(uint8_t battery_value) {
78+
void show_dashboard_battery() {
79+
uint8_t battery_value = get_electricity();
80+
7781
display.drawBitmap(55, 7, image_battery_50, 50, 50, WHITE);
7882
display.setTextSize(2);
7983
display.setTextColor(SSD1306_WHITE);

src/main.cpp

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
#include "components/setting.h"
66
#include "components/my_network.h"
77

8-
// TaskHandle_t Task1;
9-
// TaskHandle_t Task2;
8+
TaskHandle_t Task1;
109

11-
// void Task1code(void* parameter);
12-
// void Task2code(void* parameter);
10+
void Task1code(void* parameter);
1311

1412
char *sta_ssid = NULL;
1513
char *sta_passwd = NULL;
@@ -20,7 +18,7 @@ char *mdns = NULL;
2018
Cleaner_module_conf my_cleaner_conf;
2119
String url;
2220

23-
uint8_t cleaner_mode = 1;
21+
uint8_t cleaner_mode = 2;
2422

2523
Network_STA_conf my_sta_conf = {
2624
.ssid = &sta_ssid,
@@ -48,56 +46,29 @@ void setup() {
4846
setup_wifi(&network_conf);
4947
setup_server(&my_cleaner_conf, &network_conf, &cleaner_mode, &url);
5048

51-
show_dashboard(2, 0);
52-
53-
// delay(1000);
54-
// xTaskCreatePinnedToCore(Task1code, "Task1", 1024, NULL, 1, &Task1, 1);
55-
// delay(500);
56-
57-
// xTaskCreatePinnedToCore(Task2code, "Task2", 10000, NULL, 1, &Task2, 1);
58-
// delay(500);
49+
xTaskCreatePinnedToCore(Task1code, "Task1", 1024, NULL, 1, &Task1, 1);
50+
delay(500);
5951
}
6052

6153
void loop() {
6254
loop_server();
63-
if(cleaner_mode == 0) {
55+
if(cleaner_mode == 0) { // OTA mode.
6456
if(!update_loop(url)) {
6557
cleaner_mode = 2;
6658
ESP.restart();
6759
} else {
6860
cleaner_mode = 2;
6961
}
70-
} else if(cleaner_mode == 1) {
71-
72-
} else if(cleaner_mode == 2) {
73-
74-
}
75-
// show_dashboard(2, 0);
76-
// delay(1000);
77-
// show_dashboard(2, 0);
78-
// delay(1000);
79-
80-
// show_dashboard(1, 50);
81-
// delay(1000);
82-
// show_dashboard(1, 50);
83-
// delay(1000);
62+
} else if(cleaner_mode == 1) { // Game mode.
8463

85-
// show_dashboard(0, 100);
86-
// delay(1000);
87-
// show_dashboard(0, 100);
88-
// delay(1000);
64+
} else if(cleaner_mode == 2) { // Auto mode.
8965

66+
}
9067
}
9168

92-
// void Task1code(void* parameter) {
93-
// for(;;) {
94-
// Serial.printf("Hello World!\n");
95-
// vTaskDelay(1000 / portTICK_PERIOD_MS);
96-
// }
97-
// }
98-
99-
// void Task2code(void* parameter) {
100-
// for(;;) {
101-
// delay(1000);
102-
// }
103-
// }
69+
void Task1code(void* parameter) {
70+
for(;;) {
71+
show_dashboard(cleaner_mode);
72+
vTaskDelay(1000 / portTICK_PERIOD_MS);
73+
}
74+
}

0 commit comments

Comments
 (0)