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" |
Tomáš Pecka | 9cd349f | 2021-04-20 21:45:42 +0200 | [diff] [blame] | 10 | #include "utils/io.h" |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 11 | #include "utils/log.h" |
| 12 | |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 13 | namespace velia::health { |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 14 | |
| 15 | LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory) |
Tomáš Pecka | 7c6b1d3 | 2020-11-25 14:17:21 +0100 | [diff] [blame] | 16 | : m_log(spdlog::get("health")) |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 17 | , m_brightnessFile(directory / "brightness") |
| 18 | { |
| 19 | // check that brightness file exists |
| 20 | if (!std::filesystem::exists(m_brightnessFile)) { |
| 21 | throw std::invalid_argument("Sysfs dir must contain 'brightness' file."); |
| 22 | } |
| 23 | |
Tomáš Pecka | 9cd349f | 2021-04-20 21:45:42 +0200 | [diff] [blame] | 24 | const auto maxBrightnessFile = directory / "max_brightness"; |
| 25 | if (!std::filesystem::exists(maxBrightnessFile)) { |
| 26 | throw std::invalid_argument("Sysfs dir must contain 'max_brightness' file."); |
| 27 | } |
| 28 | m_maxBrightness = utils::readFileInt64(maxBrightnessFile); |
| 29 | |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 30 | m_log->trace("Initialized LED {}", std::string(directory)); |
| 31 | } |
| 32 | |
| 33 | /** @brief Set the brightness of the LED to @p brightness. |
| 34 | * Caller is responsible for providing correct brightness value. No checks for valid value are performed. |
| 35 | */ |
| 36 | void LedSysfsDriver::set(uint32_t brightness) |
| 37 | { |
| 38 | std::ofstream ofs(m_brightnessFile); |
| 39 | if (!(ofs << brightness)) { |
| 40 | throw std::invalid_argument("Write to '" + std::string(m_brightnessFile) + "' failed."); |
| 41 | } |
| 42 | } |
Tomáš Pecka | 9cd349f | 2021-04-20 21:45:42 +0200 | [diff] [blame] | 43 | uint32_t LedSysfsDriver::maxBrightness() const |
| 44 | { |
| 45 | return m_maxBrightness; |
| 46 | } |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 47 | } |