Skip to content

Commit

Permalink
lib: fix battery reading, add battery absence detection
Browse files Browse the repository at this point in the history
keira: add real battery status icon
  • Loading branch information
and3rson committed Mar 10, 2024
1 parent bca883e commit 41101fe
Show file tree
Hide file tree
Showing 9 changed files with 2,090 additions and 508 deletions.
982 changes: 491 additions & 491 deletions firmware/keira/src/apps/icons/battery.h

Large diffs are not rendered by default.

Binary file modified firmware/keira/src/apps/icons/battery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
776 changes: 776 additions & 0 deletions firmware/keira/src/apps/icons/battery_absent.h

Large diffs are not rendered by default.

Binary file added firmware/keira/src/apps/icons/battery_absent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
776 changes: 776 additions & 0 deletions firmware/keira/src/apps/icons/battery_danger.h

Large diffs are not rendered by default.

Binary file added firmware/keira/src/apps/icons/battery_danger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 37 additions & 5 deletions firmware/keira/src/apps/statusbar.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "statusbar.h"
#include "icons/battery.h"
#include "icons/battery_danger.h"
#include "icons/battery_absent.h"
#include "icons/wifi_offline.h"
#include "icons/wifi_connecting.h"
#include "icons/wifi_0.h"
Expand All @@ -19,10 +21,17 @@ void StatusBarApp::run() {
NetworkService* networkService = ServiceManager::getInstance()->getService<NetworkService>();
while (1) {
canvas->fillScreen(lilka::display.color565(0, 0, 0));

int16_t xOffset = 144;

// Print counter for debug purpose
// TODO: Replace with actual time from NTP
canvas->setTextColor(lilka::display.color565(255, 255, 255), lilka::display.color565(0, 0, 0));
canvas->setFont(FONT_9x15);
canvas->setCursor(32, 18);
canvas->print("Time: " + String(counter++));
canvas->setCursor(32, 17);
canvas->print("Час: " + String(counter++));

// Draw WiFi signal strength
if (networkService != NULL) {
if (networkService->getNetworkState() == NETWORK_STATE_OFFLINE) {
canvas->draw16bitRGBBitmapWithTranColor(144, 0, wifi_offline, 0, 24, 24);
Expand All @@ -32,9 +41,32 @@ void StatusBarApp::run() {
canvas->draw16bitRGBBitmapWithTranColor(144, 0, icons[networkService->getSignalStrength()], 0, 24, 24);
}
}
canvas->draw16bitRGBBitmapWithTranColor(144 + 8 + 24, 0, battery, 0, 32, 24);
// canvas->draw16bitRGBBitmapWithTranColor(160, 0, wifi, 24, 24, 0);
// canvas->draw16bitRGBBitmapWithTranColor(160 + 24, 0, battery, 24, 24, 0);
xOffset += 8 + 24;

// Draw battery
int16_t x1 = 6, y1 = 8;
int16_t fullWidth = 22, h = 8;
int level = lilka::battery.readLevel();
if (level == -1) {
canvas->draw16bitRGBBitmapWithTranColor(
xOffset, 0, battery_absent, lilka::display.color565(255, 0, 255), 32, 24
);
} else {
int filledWidth = fullWidth * level / 100;
if (filledWidth < 1) filledWidth = 1;
int16_t color =
level > 50 ? lilka::display.color565(0, 255, 0)
: (level > 20 ? lilka::display.color565(255, 255, 0) : lilka::display.color565(255, 0, 0));
canvas->draw16bitRGBBitmapWithTranColor(
xOffset, 0, level > 10 ? battery : battery_danger, lilka::display.color565(255, 0, 255), 32, 24
);
canvas->fillRect(xOffset + x1 + (fullWidth - filledWidth), y1, filledWidth, h, color);
}
xOffset += 8 + 32;
canvas->setCursor(xOffset, 17);
canvas->print(String(level) + "%");

// Draw everything
queueDraw();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
Expand Down
21 changes: 9 additions & 12 deletions sdk/lib/lilka/src/lilka/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace lilka {

#define fmap(x, in_min, in_max, out_min, out_max) (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
#define fmin(a, b) ((a) < (b) ? (a) : (b))

Battery::Battery() : emptyVoltage(LILKA_DEFAULT_EMPTY_VOLTAGE), fullVoltage(LILKA_DEFAULT_FULL_VOLTAGE) {
}
Expand All @@ -32,21 +33,17 @@ int Battery::readLevel() {
// Напруга акумулятора проходить через дільник напруги (33 КОм і 100 КОм, визначений як LILKA_BATTERY_VOLTAGE_DIVIDER).
// Але при повністю зарядженому акумуляторі (4.2V) напруга на АЦП може бути трохи вищою за максимальне читабельне значення (3.158V замість 3.1V).
// Тому ми сприймаємо таке перевищення як "100%".
float value = ((float)analogRead(LILKA_BATTERY_ADC) / 4095.0); // 0 = 0V, 1 = 3.1V

float fullValue;
if (fullVoltage > LILKA_BATTERY_MAX_MEASURABLE_VOLTAGE) {
fullValue = 1;
} else {
fullValue = fullVoltage / LILKA_BATTERY_MAX_MEASURABLE_VOLTAGE;
float voltage = (float)analogRead(LILKA_BATTERY_ADC) / 4095.0 * LILKA_BATTERY_MAX_MEASURABLE_VOLTAGE;
if (voltage < 0.5) {
return -1;
}

float emptyValue = emptyVoltage / LILKA_BATTERY_MAX_MEASURABLE_VOLTAGE;

float level = fmap(value, emptyValue, fullValue, 0, 100);
level = constrain(level, 0, 100);
// Максимальна напруга акумулятора, яку ми можемо виміряти.
float maxVoltage = fmin(fullVoltage, LILKA_BATTERY_MAX_MEASURABLE_VOLTAGE);

return level * 100;
// Інтерполюємо діапазон [emptyValue;maxVoltage] в діапазон [0;100].
float level = fmap(voltage, emptyVoltage, maxVoltage, 0, 100);
return constrain(level, 0, 100);
#endif
}

Expand Down
1 change: 1 addition & 0 deletions sdk/lib/lilka/src/lilka/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Battery {
/// \warning Цей метод викликається автоматично при виклику `lilka::begin()`.
void begin();
/// Прочитати рівень заряду акумулятора.
/// \return Рівень заряду акумулятора від 0 до 100. Якщо акумулятор відсутній, повертається -1.
int readLevel();

/// Встановити напругу акумулятора, при якій він вважається порожнім.
Expand Down

0 comments on commit 41101fe

Please sign in to comment.