A simple operating system for the ESP32-based M5StickC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
2.8 KiB

#include <M5StickC.h>
#include "Kernel.h"
#include "UI.h"
#include "Util.h"
int voltage_to_percent(float voltage);
void Battery_draw(int hwnd, int x, int y, int width, int height);
#define BATTERY_BARS 4
#define BATTERY_TERMINAL_HEIGHT_PCNT 20
#define BATTERY_TERMINAL_WIDTH_PCNT 5
#define BATTERY_PADDING_PCNT 5
#define BATTERY_COLOUR 0x999999
#define BATTERY_LOW_COLOUR 0xAA0000
#define BATTERY_BAR_COLOUR 0xAAAAAA
#define BATTERY_BAR_LOW_COLOUR 0xFF0000
int battery_bars = -1;
int Battery(int pid, unsigned int signal)
{
if (signal & SIGNAL_START)
{
Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK);
UI_create_window(&Battery_draw, 1);
}
if (signal & SIGNAL_STOP)
{
return 255;
}
if (signal & SIGNAL_TICK)
{
float vbat = M5.Axp.GetVbatData() * 1.1 / 1000;
int percent = voltage_to_percent(vbat);
int bars = floor((percent / (100.0 / BATTERY_BARS)) + 0.5);
if (battery_bars != bars)
{
battery_bars = bars;
Kernel_signal(SIGNAL_REDRAW);
}
}
return 0;
}
void Battery_draw(int hwnd, int wnd_x, int wnd_y, int wnd_width, int wnd_height)
{
// Draw terminal
int terminal_width = wnd_width * (BATTERY_TERMINAL_WIDTH_PCNT / 100.0);
int terminal_height = wnd_height * (BATTERY_TERMINAL_HEIGHT_PCNT / 100.0);
M5.Lcd.fillRect(
wnd_x,
wnd_y + (wnd_height / 2) - (terminal_height / 2),
terminal_width,
terminal_height,
battery_bars == 0 ? rgb_to_colour(BATTERY_LOW_COLOUR) : rgb_to_colour(BATTERY_COLOUR)
);
// Draw battery shell
M5.Lcd.fillRect(
wnd_x + terminal_width,
wnd_y,
wnd_width - terminal_width,
wnd_height,
battery_bars == 0 ? rgb_to_colour(BATTERY_LOW_COLOUR) : rgb_to_colour(BATTERY_COLOUR)
);
// Do some math
int padding_horizontal = wnd_width * (BATTERY_PADDING_PCNT / 100.0);
int padding_vertical = wnd_height * (BATTERY_PADDING_PCNT / 100.0);
int bar_width = (wnd_width - terminal_width - 2 - (padding_horizontal * (BATTERY_BARS + 1))) / BATTERY_BARS;
int bar_height = wnd_height - (padding_vertical * 2) - 2;
int bar_x = wnd_x + wnd_width - 1 - padding_horizontal - bar_width;
int bar_y = wnd_y + 1 + padding_vertical;
// Draw battery bars
int bars = battery_bars;
Serial.printf("Drawing %d bars\n", bars);
while (bars > 0)
{
M5.Lcd.fillRect(
bar_x,
bar_y,
bar_width,
bar_height,
battery_bars == 0 ? rgb_to_colour(BATTERY_BAR_LOW_COLOUR) : rgb_to_colour(BATTERY_BAR_COLOUR)
);
bar_x -= (bar_width + padding_horizontal);
bars--;
}
}
int voltage_to_percent(float voltage)
{
/*
4.2 volts 100%
4.1 about 90%
4.0 about 80%
3.9 about 60%
3.8 about 40%
3.7 about 20%
3.6 empty for practical purposes
*/
return ((voltage - 3.6) / (4.2-3.6)) * 100;
}