bat stats

This commit is contained in:
2025-10-06 09:14:25 +02:00
parent c439aecd03
commit 3f8d90c18a

View File

@@ -10,19 +10,22 @@
#include "freertos/task.h"
#include "sdkconfig.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <driver/gpio.h>
#include <power_helper.hpp>
#include <random>
#include <shutdowner.hpp>
#include <spi_global.hpp>
#include <vector>
#include "display.hpp"
#include "driver/i2c_master.h"
#include "driver/spi_master.h"
#include "i2c_global.hpp"
#include <algorithm>
#include <array>
#include <random>
#include <vector>
// Battery monitor header (conditionally included)
#include <bat_mon.hpp>
namespace cfg {
constexpr int BoardW = 10;
@@ -269,14 +272,31 @@ struct Font5x7 {
return 20;
case 'A':
return 21;
case 'B':
return 22;
case 'U':
return 23;
case 'M':
return 24;
case 'H':
return 25;
case '.':
return 26;
case ':':
return 27;
case '-':
return 28;
case 'G':
return 29; // newly added glyph
// space handled specially in drawText
default:
return 255;
}
}
// columns × 7 rows (bit0 = top, bit6 = bottom)
static const uint8_t data[22][5];
static const uint8_t data[30][5];
};
const uint8_t Font5x7::data[22][5] = {
const uint8_t Font5x7::data[30][5] = {
/*0*/ {0b0111110, 0b1000001, 0b1000001, 0b1000001, 0b0111110},
/*1*/ {0b0000000, 0b1000010, 0b1111111, 0b1000000, 0b0000000},
/*2*/ {0b11100010, 0b10010001, 0b10001001, 0b10001001, 0b10000110},
@@ -299,6 +319,14 @@ const uint8_t Font5x7::data[22][5] = {
/*T*/ {0b00000001, 0b00000001, 0b11111111, 0b00000001, 0b00000001},
/*V*/ {0b00000111, 0b00111000, 0b11000000, 0b00111000, 0b00000111},
/*A*/ {0b11111110, 0b00010001, 0b00010001, 0b00010001, 0b11111110},
/*B*/ {0b11111111, 0b10010001, 0b10010001, 0b10010001, 0b01101110},
/*U*/ {0b01111111, 0b10000000, 0b10000000, 0b10000000, 0b01111111},
/*M*/ {0b11111111, 0b00001100, 0b00011000, 0b00001100, 0b11111111},
/*H*/ {0b11111111, 0b00010000, 0b00010000, 0b00010000, 0b11111111},
/*.*/ {0b00000000, 0b00000000, 0b01100000, 0b01100000, 0b00000000}, // wider centered dot (2 cols)
/*:*/ {0b00000000, 0b00000000, 0b00110110, 0b00110110, 0b00000000}, // beefier colon (two stacked 2x2 dots)
/*-*/ {0b00010000, 0b00010000, 0b00010000, 0b00010000, 0b00010000},
/*G*/ {0b01111110, 0b10000001, 0b10001001, 0b10001001, 0b01111010}, // refined G with inner bar & notch
};
// ─────────────────────────────────────────────────────────────────────────────
@@ -315,6 +343,7 @@ public:
void render(const Board& b, int px, int py, int prot, int pidx, int score, int level, int lines, int nextIdx,
bool ghost) {
fb.clear(false); // white background
drawBatteryOverlay();
// Outer playfield frame
rect(ox - 2, oy - 2, bw + 4, bh + 4, true);
@@ -621,6 +650,45 @@ private:
for (int i = 0; i < len; ++i)
drawChar5x7(x + i * 6, y, buf[i], true);
}
void drawText(int x, int y, const char* s) { // new generic text (supports space)
for (int i = 0; s[i]; ++i) {
if (s[i] == ' ') {
x += 6;
continue;
}
drawChar5x7(x, y, s[i], true);
x += 6;
}
}
void drawBatteryOverlay() {
float cur = BatMon::get().get_current(); // mA, may be negative (charging)
float volt = BatMon::get().get_voltage(); // V
float ch = BatMon::get().get_charge(); // mAh (accumulated)
uint8_t btnMask = Buttons::get().get_pressed();
if (cur != cur)
cur = 0.f;
if (volt != volt)
volt = 0.f;
if (ch != ch)
ch = 0.f;
char line1[64];
char line2[64];
char line3[64];
// NOTE: Font only has uppercase letters; labels rendered uppercase.
// Format values one decimal. Current may be negative.
snprintf(line1, sizeof(line1), "CURRENT: %5.1fMA %3.1fV", cur, volt);
snprintf(line2, sizeof(line2), "CHARGE: %5.1fMAH", ch);
strcpy(line3, "BTN: ");
size_t base = strlen(line3);
for (int i = 0; i < 8; ++i)
line3[base + i] = (btnMask & (1 << (7 - i))) ? '1' : '0';
line3[base + 8] = '\0';
int x = 5; // shifted one pixel right from previous (was 4)
int y = 4;
drawText(x, y, line1);
drawText(x, y + 10, line2);
drawText(x, y + 20, line3);
}
};
// ─────────────────────────────────────────────────────────────────────────────