mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-28 23:27:49 +01:00
82 lines
2.4 KiB
C++
82 lines
2.4 KiB
C++
#include "cardboy/sdk/status_bar.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cmath>
|
|
#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 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)) {
|
|
std::snprintf(buf, sizeof(buf), "cur %.2fmA chr %.2fmAh", static_cast<double>(current),
|
|
static_cast<double>(chargeMah));
|
|
} else {
|
|
std::snprintf(buf, sizeof(buf), "vol %.2fV", static_cast<double>(fallbackV));
|
|
}
|
|
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
|