summaryrefslogtreecommitdiffhomepage
path: root/StatusLED.cpp
blob: 711257d87c15a651d84072142d650f1d62236472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "StatusLED.h"

#include <iostream>

#include <fmt/format.h>
#include <libreichwein/file.h>

namespace fs = std::filesystem;

StatusLED::StatusLED(): m_mode{Mode::OK}, m_bpm{60}
{
}

void StatusLED::add(const LED& led)
{
  if (fs::exists(led.green) && fs::exists(led.red)) {
    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)
{
  m_bpm = bpm;
}

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::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));
}