mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-28 15:17:48 +01:00
80 lines
2.3 KiB
C++
80 lines
2.3 KiB
C++
#include "cardboy/sdk/status_bar.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cstdio>
|
|
|
|
namespace cardboy::sdk {
|
|
|
|
StatusBar& StatusBar::instance() {
|
|
static StatusBar bar;
|
|
return bar;
|
|
}
|
|
|
|
void StatusBar::setEnabled(bool value) { enabled_ = value; }
|
|
|
|
void StatusBar::toggle() {
|
|
enabled_ = !enabled_;
|
|
if (services_ && services_->buzzer)
|
|
services_->buzzer->beepMove();
|
|
}
|
|
|
|
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));
|
|
});
|
|
}
|
|
|
|
bool StatusBar::handleToggleInput(const InputState& current, const InputState& previous) {
|
|
const bool comboNow = current.start && current.select && current.up;
|
|
const bool comboPrev = previous.start && previous.select && previous.up;
|
|
if (comboNow && !comboPrev) {
|
|
toggle();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::string StatusBar::prepareLeftText(int displayWidth) const {
|
|
std::string text = appName_.empty() ? std::string("CARDBOY") : appName_;
|
|
int maxWidth = std::max(0, displayWidth - 32);
|
|
while (!text.empty() && font16x8::measureText(text, 1, 1) > maxWidth)
|
|
text.pop_back();
|
|
return text;
|
|
}
|
|
|
|
std::string StatusBar::prepareRightText() const {
|
|
if (!services_)
|
|
return {};
|
|
|
|
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);
|
|
} else {
|
|
std::snprintf(buf, sizeof(buf), "BAT %.2fV", static_cast<double>(services_->battery->voltage()));
|
|
}
|
|
right.assign(buf);
|
|
}
|
|
|
|
if (services_->powerManager && services_->powerManager->isSlowMode()) {
|
|
if (!right.empty())
|
|
right.append(" ");
|
|
right.append("SLOW");
|
|
}
|
|
|
|
if (services_->buzzer && services_->buzzer->isMuted()) {
|
|
if (!right.empty())
|
|
right.append(" ");
|
|
right.append("MUTE");
|
|
}
|
|
|
|
return right;
|
|
}
|
|
|
|
} // namespace cardboy::sdk
|