79 lines
2.7 KiB
C++
79 lines
2.7 KiB
C++
#include "ui_common.h"
|
|
|
|
#include <Arduino.h>
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <GxEPD2_4G.h> // nur für die Farbwerte GxEPD_WHITE … GxEPD_BLACK
|
|
|
|
const char* const DAY_NAMES[7] = {"So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"};
|
|
|
|
// nach Sakamoto
|
|
int weekday(const char* date) {
|
|
static const int offsets[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
|
|
int y = atoi(date), m = atoi(date + 5), d = atoi(date + 8);
|
|
if (m < 3) y -= 1;
|
|
return (y + y / 4 - y / 100 + y / 400 + offsets[m - 1] + d) % 7;
|
|
}
|
|
|
|
void useFont(U8G2_FOR_ADAFRUIT_GFX& fonts, const uint8_t* font) {
|
|
fonts.setFont(font);
|
|
fonts.setFontMode(1);
|
|
}
|
|
|
|
void drawCentered(U8G2_FOR_ADAFRUIT_GFX& fonts, const char* text, int centerX, int baseline) {
|
|
fonts.drawUTF8(centerX - fonts.getUTF8Width(text) / 2, baseline, text);
|
|
}
|
|
|
|
// Ladezustand einer LiPo-Zelle aus der Spannung, grob nach typischer Entladekurve
|
|
int batteryPercent(float voltage) {
|
|
static const float VOLTS[] = {3.30f, 3.60f, 3.70f, 3.75f, 3.80f, 3.85f, 3.90f, 4.00f, 4.10f, 4.20f};
|
|
static const int PERCENT[] = {0, 5, 15, 25, 40, 55, 65, 80, 90, 100};
|
|
const int count = sizeof(VOLTS) / sizeof(VOLTS[0]);
|
|
if (voltage <= VOLTS[0]) return 0;
|
|
for (int i = 1; i < count; i++) {
|
|
if (voltage < VOLTS[i]) {
|
|
float t = (voltage - VOLTS[i - 1]) / (VOLTS[i] - VOLTS[i - 1]);
|
|
return lroundf(PERCENT[i - 1] + t * (PERCENT[i] - PERCENT[i - 1]));
|
|
}
|
|
}
|
|
return 100;
|
|
}
|
|
|
|
namespace {
|
|
|
|
void batteryIcon(Adafruit_GFX& g, int x, int y, int percent) {
|
|
const int w = 16, h = 8;
|
|
g.drawRect(x, y, w, h, GxEPD_BLACK);
|
|
g.fillRect(x + w, y + 2, 2, h - 4, GxEPD_BLACK);
|
|
g.fillRect(x + 2, y + 2, (w - 4) * percent / 100, h - 4, percent <= 15 ? GxEPD_BLACK : GxEPD_DARKGREY);
|
|
}
|
|
|
|
void appDots(Adafruit_GFX& g, int centerX, int centerY, int index, int count) {
|
|
const int spacing = 9;
|
|
int x = centerX - (count - 1) * spacing / 2;
|
|
for (int i = 0; i < count; i++, x += spacing) {
|
|
if (i == index) g.fillCircle(x, centerY, 3, GxEPD_BLACK);
|
|
else g.drawCircle(x, centerY, 3, GxEPD_DARKGREY);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void drawStatusBar(Adafruit_GFX& gfx, U8G2_FOR_ADAFRUIT_GFX& fonts, const char* leftText, const StatusInfo& status) {
|
|
gfx.drawFastHLine(0, STATUS_LINE_Y, gfx.width(), GxEPD_LIGHTGREY);
|
|
fonts.setForegroundColor(GxEPD_DARKGREY);
|
|
useFont(fonts, u8g2_font_helvR08_tf);
|
|
fonts.drawUTF8(6, 124, leftText);
|
|
|
|
if (status.appCount > 1) appDots(gfx, gfx.width() / 2, 120, status.appIndex, status.appCount);
|
|
|
|
if (!isnan(status.batteryVoltage)) {
|
|
char text[8];
|
|
int percent = batteryPercent(status.batteryVoltage);
|
|
snprintf(text, sizeof(text), "%d %%", percent);
|
|
int textX = gfx.width() - 6 - fonts.getUTF8Width(text);
|
|
fonts.drawUTF8(textX, 124, text);
|
|
batteryIcon(gfx, textX - 24, 116, percent);
|
|
}
|
|
}
|