platform for developing on SQFMI's Watchy
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.
 
 

75 lines
1.6 KiB

#include "watchos.h"
#include "Module_UI.h"
#define TASK_BATTERY_UPDATE_INTERVAL_MS 30000
class Task_Battery : public IRunnable, public IDrawable, public Task
{
Module_UI* ui = nullptr;
kernel_handle_t window_handle;
int voltage = 0;
unsigned long last_update = 0;
int getBatteryVoltage()
{
float raw_value = (analogRead(HW_BATTERY_ADC_PIN) / 4096.0 * 7.23);
return (int)round(raw_value * 100);
}
public:
void start()
{
ui = static_cast<Module_UI*>(watchos::module(WATCHOS_MODULE_UI));
window_handle = ui->createWindow(this, ui->getRoot());
ui->setAbsolutePosition(window_handle, DISPLAY_WIDTH - 32, 2, 22, 10);
}
void tick()
{
if (millis() - last_update > TASK_BATTERY_UPDATE_INTERVAL_MS)
{
int new_voltage = getBatteryVoltage();
if (new_voltage != voltage)
{
ui->setDirty(window_handle);
voltage = new_voltage;
}
last_update = millis();
}
}
void draw(kernel_handle_t handle)
{
voltage = getBatteryVoltage();
// Main body
ui->fillRectangle(0, 0, 20, 10);
// Pin
ui->fillRectangle(20, 2, 2, 6);
if (voltage >= 350)
{
int bars = 0;
if (voltage > 410)
bars = 3;
else if (voltage > 395)
bars = 2;
else if (voltage >= 380)
bars = 1;
else
bars = 0;
int bar_width = (20 - 8) / 3;
int x = 2;
for (int i = 0; i < bars; i++)
{
ui->fillRectangle(x, 2, bar_width, ui->getHeight() - 4, COLOUR_SECONDARY);
x += bar_width + 2;
}
}
else
{
ui->line(0, ui->getHeight(), ui->getWidth(), 0, COLOUR_SECONDARY);
}
}
};