From 5fd637644c7529bfdc5291215f3f8ee1edd304c4 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 20 Jan 2025 20:39:10 +0100 Subject: Fixed status led --- StatusLED.cpp | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'StatusLED.cpp') diff --git a/StatusLED.cpp b/StatusLED.cpp index 3013ddb..711257d 100644 --- a/StatusLED.cpp +++ b/StatusLED.cpp @@ -1,21 +1,33 @@ #include "StatusLED.h" +#include + +#include +#include + namespace fs = std::filesystem; StatusLED::StatusLED(): m_mode{Mode::OK}, m_bpm{60} { } -void StatusLED::addLED(const LED& led) +void StatusLED::add(const LED& led) { if (fs::exists(led.green) && fs::exists(led.red)) { - m_leds.push_back(led); + if (init(led)) { + m_leds.push_back(led); + update(led); + } } } void StatusLED::setMode(Mode mode) { m_mode = mode; + + for (const auto& i: m_leds) { + update(i); + } } void StatusLED::setBPM(int bpm) @@ -23,10 +35,31 @@ void StatusLED::setBPM(int bpm) m_bpm = bpm; } -void StatusLED::initLED(const LED& led) +bool StatusLED::init(const LED& led) { + try { + Reichwein::File::setFile(led.green / "trigger", "none"); + Reichwein::File::setFile(led.red / "trigger", "none"); + return true; + } catch (...) { + return false; + } } -void StatusLED::updateLED(const LED& led) +void StatusLED::update(const LED& led) { + int green{}; + int red{}; + + if (m_mode == Mode::OK) { + green = 255; + } else if (m_mode == Mode::Error) { + red = 255; + } else { + throw std::runtime_error("LED mode unimplemented"); + } + + Reichwein::File::setFile(led.green / "brightness", fmt::format("{}", green)); + Reichwein::File::setFile(led.red / "brightness", fmt::format("{}", red)); } + -- cgit v1.2.3