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 | |
| 12 | namespace velia { |
| 13 | |
| 14 | LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory) |
| 15 | : m_log(spdlog::get("output")) |
| 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 | |
| 37 | } |