Skip to content

Commit

Permalink
✨ feat: Add battery level display to SSD1306 OLED
Browse files Browse the repository at this point in the history
Add battery level display to SSD1306 OLED screen for ESP32.

Closes #4
  • Loading branch information
Hsun1031 committed Mar 18, 2023
1 parent f612c12 commit 164dd5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 56 deletions.
10 changes: 5 additions & 5 deletions include/components/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Battery_conf {
};

void setup_battery(Battery_conf *conf);
uint8_t get_electricity(Battery_conf *conf);
uint8_t get_electricity();

// MX1584.cpp

Expand Down Expand Up @@ -48,11 +48,11 @@ void set_IRF520_PWM(uint32_t duty);
// SSD1306.cpp
void setup_SSD1306();
void show_logo();
void show_dashboard(uint8_t mode, uint8_t battery_value);
void show_dashboard_battery_20(uint8_t battery_value);
void show_dashboard(uint8_t mode);
void show_dashboard_battery_20();
void show_dashboard_wifi_20();
void show_dashboard_body(uint8_t mode, uint8_t battery_value);
void show_dashboard_battery(uint8_t battery_value);
void show_dashboard_body(uint8_t mode);
void show_dashboard_battery();

// VL53L0X.cpp
struct VL53L0X_value{
Expand Down
18 changes: 11 additions & 7 deletions src/components/module/SSD1306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ void show_logo() {
display.display();
}

void show_dashboard(uint8_t mode, uint8_t battery_value) {
void show_dashboard(uint8_t mode) {
display.clearDisplay();

show_dashboard_battery_20(70);
show_dashboard_battery_20();
display.drawBitmap(6, 22, image_ap_20, 20, 20, WHITE);
show_dashboard_wifi_20();
show_dashboard_body(mode, battery_value);
show_dashboard_body(mode);

display.display();
}

void show_dashboard_battery_20(uint8_t battery_value) {
void show_dashboard_battery_20() {
uint8_t battery_value = get_electricity();

if(battery_value > 60) {
display.drawBitmap(6, 1, image_batter_full_20, 20, 20, WHITE);

Expand All @@ -50,7 +52,7 @@ void show_dashboard_wifi_20() {
}
}

void show_dashboard_body(uint8_t mode, uint8_t battery_value) {
void show_dashboard_body(uint8_t mode) {
static int body = 0;
if(body == 0) {
body = 1;
Expand All @@ -69,11 +71,13 @@ void show_dashboard_body(uint8_t mode, uint8_t battery_value) {
}
} else if(body == 1) {
body = 0;
show_dashboard_battery(battery_value);
show_dashboard_battery();
}
}

void show_dashboard_battery(uint8_t battery_value) {
void show_dashboard_battery() {
uint8_t battery_value = get_electricity();

display.drawBitmap(55, 7, image_battery_50, 50, 50, WHITE);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
Expand Down
59 changes: 15 additions & 44 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
#include "components/setting.h"
#include "components/my_network.h"

// TaskHandle_t Task1;
// TaskHandle_t Task2;
TaskHandle_t Task1;

// void Task1code(void* parameter);
// void Task2code(void* parameter);
void Task1code(void* parameter);

char *sta_ssid = NULL;
char *sta_passwd = NULL;
Expand All @@ -20,7 +18,7 @@ char *mdns = NULL;
Cleaner_module_conf my_cleaner_conf;
String url;

uint8_t cleaner_mode = 1;
uint8_t cleaner_mode = 2;

Network_STA_conf my_sta_conf = {
.ssid = &sta_ssid,
Expand Down Expand Up @@ -48,56 +46,29 @@ void setup() {
setup_wifi(&network_conf);
setup_server(&my_cleaner_conf, &network_conf, &cleaner_mode, &url);

show_dashboard(2, 0);

// delay(1000);
// xTaskCreatePinnedToCore(Task1code, "Task1", 1024, NULL, 1, &Task1, 1);
// delay(500);

// xTaskCreatePinnedToCore(Task2code, "Task2", 10000, NULL, 1, &Task2, 1);
// delay(500);
xTaskCreatePinnedToCore(Task1code, "Task1", 1024, NULL, 1, &Task1, 1);
delay(500);
}

void loop() {
loop_server();
if(cleaner_mode == 0) {
if(cleaner_mode == 0) { // OTA mode.
if(!update_loop(url)) {
cleaner_mode = 2;
ESP.restart();
} else {
cleaner_mode = 2;
}
} else if(cleaner_mode == 1) {

} else if(cleaner_mode == 2) {

}
// show_dashboard(2, 0);
// delay(1000);
// show_dashboard(2, 0);
// delay(1000);

// show_dashboard(1, 50);
// delay(1000);
// show_dashboard(1, 50);
// delay(1000);
} else if(cleaner_mode == 1) { // Game mode.

// show_dashboard(0, 100);
// delay(1000);
// show_dashboard(0, 100);
// delay(1000);
} else if(cleaner_mode == 2) { // Auto mode.

}
}

// void Task1code(void* parameter) {
// for(;;) {
// Serial.printf("Hello World!\n");
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// }
// }

// void Task2code(void* parameter) {
// for(;;) {
// delay(1000);
// }
// }
void Task1code(void* parameter) {
for(;;) {
show_dashboard(cleaner_mode);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

0 comments on commit 164dd5a

Please sign in to comment.