status bar 2

This commit is contained in:
2025-10-12 15:18:42 +02:00
parent 5ab8662332
commit c64f03a09f
6 changed files with 46 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include "input_state.hpp"
#include "cardboy/sdk/framebuffer_hooks.hpp"
#include "input_state.hpp"
#include <concepts>
#include <cstdint>

View File

@@ -43,23 +43,32 @@ private:
if (width <= 0)
return;
const int barHeight = font16x8::kGlyphHeight + 2;
const int fillHeight = std::min(barHeight, fb.height());
if (fillHeight <= 0)
return;
const std::string leftText = prepareLeftText(width);
const std::string rightText = prepareRightText();
for (int y = 0; y < fillHeight; ++y) {
for (int x = 0; x < width; ++x)
fb.drawPixel(x, 0, true);
fb.drawPixel(x, y, true);
}
for (int x = 0; x < width; ++x)
fb.drawPixel(x, 0, false);
const int textY = 1;
const int barHeight = font16x8::kGlyphHeight + 2;
const int bottomSeparatorY = textY + font16x8::kGlyphHeight + 1;
if (bottomSeparatorY < barHeight && bottomSeparatorY < fb.height()) {
if (bottomSeparatorY < fillHeight) {
for (int x = 0; x < width; ++x)
fb.drawPixel(x, bottomSeparatorY, (x % 2) == 0);
fb.drawPixel(x, bottomSeparatorY, (x % 2) != 0);
}
const int leftX = 2;
if (!leftText.empty())
font16x8::drawText(fb, leftX, textY, leftText, 1, true, 1);
font16x8::drawText(fb, leftX, textY, leftText, 1, false, 1);
if (!rightText.empty()) {
int rightWidth = font16x8::measureText(rightText, 1, 1);
@@ -68,7 +77,7 @@ private:
if (rightX < minRightX)
rightX = std::max(minRightX, width / 2);
if (rightX < width)
font16x8::drawText(fb, rightX, textY, rightText, 1, true, 1);
font16x8::drawText(fb, rightX, textY, rightText, 1, false, 1);
}
}

View File

@@ -252,4 +252,3 @@ bool AppSystem::handlePendingSwitchRequest() {
}
} // namespace cardboy::sdk

View File

@@ -2,6 +2,7 @@
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
namespace cardboy::sdk {
@@ -21,9 +22,8 @@ void StatusBar::toggle() {
void StatusBar::setCurrentAppName(std::string_view name) {
appName_.assign(name.begin(), name.end());
std::transform(appName_.begin(), appName_.end(), appName_.begin(), [](unsigned char ch) {
return static_cast<char>(std::toupper(ch));
});
std::transform(appName_.begin(), appName_.end(), appName_.begin(),
[](unsigned char ch) { return static_cast<char>(std::toupper(ch)); });
}
bool StatusBar::handleToggleInput(const InputState& current, const InputState& previous) {
@@ -50,13 +50,16 @@ std::string StatusBar::prepareRightText() const {
std::string right;
if (services_->battery && services_->battery->hasData()) {
const float charge = services_->battery->charge();
char buf[32];
if (charge > 0.0f && charge <= 1.5f) {
const int pct = std::clamp(static_cast<int>(charge * 100.0f + 0.5f), 0, 100);
std::snprintf(buf, sizeof(buf), "BAT %d%%", pct);
const float current = services_->battery->current();
const float chargeMah = services_->battery->charge();
const float fallbackV = services_->battery->voltage();
char buf[64];
if (std::isfinite(current) && std::isfinite(chargeMah)) {
const long currentRounded = std::lround(static_cast<double>(current));
const long chargeRounded = std::lround(static_cast<double>(chargeMah));
std::snprintf(buf, sizeof(buf), "CUR %+ldMA CAP %ldMAH", currentRounded, std::max(0L, chargeRounded));
} else {
std::snprintf(buf, sizeof(buf), "BAT %.2fV", static_cast<double>(services_->battery->voltage()));
std::snprintf(buf, sizeof(buf), "BAT %.2fV", static_cast<double>(fallbackV));
}
right.assign(buf);
}