blob: c3b0f0f9bac40f3337607786650339a2bcafae3a [file] [log] [blame]
Tomáš Peckaefd88432020-06-17 15:31:39 +02001/*
2 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
5 *
6*/
7
8#include <fstream>
9#include "LedSysfsDriver.h"
10#include "utils/log.h"
11
Tomáš Pecka261c8862020-11-05 11:23:08 +010012namespace velia::health {
Tomáš Peckaefd88432020-06-17 15:31:39 +020013
14LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory)
Tomáš Pecka31caebd2020-08-21 11:14:10 +020015 : m_log(spdlog::get("main"))
Tomáš Peckaefd88432020-06-17 15:31:39 +020016 , m_brightnessFile(directory / "brightness")
17{
18 // check that brightness file exists
19 if (!std::filesystem::exists(m_brightnessFile)) {
20 throw std::invalid_argument("Sysfs dir must contain 'brightness' file.");
21 }
22
23 m_log->trace("Initialized LED {}", std::string(directory));
24}
25
26/** @brief Set the brightness of the LED to @p brightness.
27 * Caller is responsible for providing correct brightness value. No checks for valid value are performed.
28 */
29void LedSysfsDriver::set(uint32_t brightness)
30{
31 std::ofstream ofs(m_brightnessFile);
32 if (!(ofs << brightness)) {
33 throw std::invalid_argument("Write to '" + std::string(m_brightnessFile) + "' failed.");
34 }
35}
36
Tomáš Pecka261c8862020-11-05 11:23:08 +010037}