Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 1 | /* |
| 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áš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame^] | 12 | namespace velia::health { |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 13 | |
| 14 | LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory) |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 15 | : m_log(spdlog::get("main")) |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 16 | , 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 | */ |
| 29 | void 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áš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame^] | 37 | } |