Skip to content

Commit

Permalink
Add battery monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
andreock committed Aug 23, 2024
1 parent 2029cc6 commit 1878f83
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 13 deletions.
22 changes: 22 additions & 0 deletions include/battery_level.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define MIN_ANALOG_VALUE 0 // Low analog value, read using analogRead()
#define MAX_ANALOG_VALUE 3300 // High analog value, read using analogRead()
#define MIN_LEVEL 0 // Minimium battery level in percentage
#define MAX_LEVEL 100 // Maximium battery level in percentage
#define TASK_POLLING_RATE 10000 // Level updater timeout in milliseconds(it will update every x ms)
5 changes: 5 additions & 0 deletions include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#define PN532_SDA 8
#define PN532_SCL 9

#define BATTERY_MONITOR 2

#elif ARDUINO_NANO_ESP32

// Define buttons pins
Expand Down Expand Up @@ -76,4 +78,7 @@
// PN532(NFC)
#define PN532_SCL A6
#define PN532_SDA A7

#define BATTERY_MONITOR A0

#endif
31 changes: 22 additions & 9 deletions lib/UI/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@
#include "i18n.hpp"
#include "i18n/main_page_keys.h"
#include "style.h"
#include "./ui_tasks/battery_monitor/battery_monitor_task.hpp"

void Gui::init_gui() {
#ifdef CONFIG_IDF_TARGET_ESP32S2
main_page = new MainPage(5, 0, 4, screen, this);
#else
main_page = new MainPage(6, 0, 4, screen, this);
#endif
main_page->display();
delete current_page;
current_page = nullptr;
Serial.println("Init"); // For some really strange reason this is needed to avoid UI glitches
if(main_page == nullptr) {
#ifdef CONFIG_IDF_TARGET_ESP32S2
main_page = new MainPage(5, 0, 4, screen, this);
#else
main_page = new MainPage(6, 0, 4, screen, this);
#endif
main_page->display();
battery_monitor_task_params.page = main_page;
xTaskCreate(battery_monitor_task, "battery_monitor_ui", 2000, &battery_monitor_task_params, tskIDLE_PRIORITY, NULL);
}else {
main_page->display();
delete current_page;
current_page = nullptr;
}
battery_monitor_task_params.visible = true;
}

void Gui::click_element() {
Expand Down Expand Up @@ -72,8 +81,12 @@ void Gui::right() {
}

void Gui::set_current_page(Page *page, bool display) {
while(battery_monitor_task_params.lock) {
NOP(); // Wait
}
battery_monitor_task_params.visible = false;
delete current_page;
current_page = page;
if(display)
if(display)
page->display();
}
2 changes: 2 additions & 0 deletions lib/UI/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "GFXForms.hpp"
#include "widgets/RectText.hpp"
#include "pages/main_page/MainPage.hpp"
#include "./ui_tasks/battery_monitor/battery_monitor_task_types.h"

#ifndef GUI_H
#define GUI_H
Expand All @@ -29,6 +30,7 @@ class Gui {
Page *current_page = nullptr;
MainPage *main_page = nullptr; // Never delete main_page since it's always necessary
// Reduce memory fragmentation
BatteryMonitorTaskParams battery_monitor_task_params;
public:
Gui(GFXForms *_screen) { screen = _screen; };
~Gui(){};
Expand Down
9 changes: 7 additions & 2 deletions lib/UI/pages/main_page/MainPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "gui.hpp"
#include "i18n/main_page_keys.h"
#include "style.h"
#include "battery_monitor.hpp"

MainPage::~MainPage() {}

Expand Down Expand Up @@ -79,14 +80,18 @@ void MainPage::display() {
grid->set_space_between(10);
grid->set_padding(0, 20);
Text text = Text(screen, HOME_INFO_COLOR,
String(english_words->at(VERSION_KEY)) + String(VERSION) +
String(" - ") + String(DEVICE));
String(english_words->at(VERSION_KEY)) + String(VERSION));
text.set_size(2);
text.set_wrap(true);
text.set_pos(5, 200);
battery_level = new Text(screen, HOME_INFO_COLOR, "Battery: 0%");
int level = read_battery_level();
set_battery_level(level);
battery_level->set_pos(5, 220);
grid->display();
grid->set_selected(lower_limit, true);
text.display();
battery_level->display();
}

void MainPage::right() {
Expand Down
5 changes: 4 additions & 1 deletion lib/UI/pages/main_page/MainPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MainPage : public Page {
RectText *IR;
RectText *net_attacks;
RectText *settings;

Text *battery_level;
public:
MainPage(uint8_t _position_limit, uint8_t _lower_limit,
uint8_t _position_increment, GFXForms *screen, Gui *_gui)
Expand All @@ -42,6 +42,9 @@ class MainPage : public Page {
void display();
void left();
void right();
void set_battery_level(int level) {
battery_level->set_text("Battery: " + String(level) + "%");
};
};

#endif
35 changes: 35 additions & 0 deletions lib/UI/ui_tasks/battery_monitor/battery_monitor_task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "battery_monitor.hpp"
#include "../../../../include/battery_level.h"
#include <Arduino.h>
#include "battery_monitor_task_types.h"

void battery_monitor_task(void *pv) {
BatteryMonitorTaskParams *params = static_cast<BatteryMonitorTaskParams *>(pv);

while (true) {
if(params->visible) {
params->lock = true;
int level = read_battery_level();
params->page->set_battery_level(level);
params->lock = false;
}
delay(TASK_POLLING_RATE);
}
}
18 changes: 18 additions & 0 deletions lib/UI/ui_tasks/battery_monitor/battery_monitor_task.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

void battery_monitor_task(void *pv);
13 changes: 13 additions & 0 deletions lib/UI/ui_tasks/battery_monitor/battery_monitor_task_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "../../pages/main_page/MainPage.hpp"
#include <mutex>

#ifndef BATTERY_MONITOR_TASK_TYPES_H
#define BATTERY_MONITOR_TASK_TYPES_H

typedef struct {
bool visible = false;
bool lock = false;
MainPage *page;
} BatteryMonitorTaskParams;

#endif
26 changes: 26 additions & 0 deletions lib/battery_monitor/battery_monitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <Arduino.h>
#include "../../include/battery_level.h"
#include "../../include/pins.h"

int read_battery_level() {
int battery_level = analogRead(BATTERY_MONITOR);
battery_level = map(battery_level, MIN_ANALOG_VALUE, MAX_ANALOG_VALUE, MIN_LEVEL, MAX_LEVEL);
return battery_level;
}
18 changes: 18 additions & 0 deletions lib/battery_monitor/battery_monitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or
* https://capibarazero.github.io/). Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

int read_battery_level();
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "posixsd.hpp"
#include "style.h"
#include "display_config.h"
#include "battery_monitor.hpp"

/* TODO: To lower this, we can may switch to heap for wifi_networks */
#define TASK_STACK_SIZE 16000
Expand Down Expand Up @@ -103,7 +104,7 @@ void setup() {
init_sd();
#endif
init_english_dict();

pinMode(BATTERY_MONITOR, INPUT);
display = new Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
screen = new GFXForms(DISPLAY_WIDTH, DISPLAY_HEIGHT, display);
screen->set_rotation(1);
Expand Down

0 comments on commit 1878f83

Please sign in to comment.