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 #pragma once
#include "input_state.hpp"
#include "cardboy/sdk/framebuffer_hooks.hpp" #include "cardboy/sdk/framebuffer_hooks.hpp"
#include "input_state.hpp"
#include <concepts> #include <concepts>
#include <cstdint> #include <cstdint>

View File

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

View File

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