blob: 73e460d5d4c1341dbe6ea2f360ef64f84f4128e2 [file] [log] [blame]
/*
* Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
*
* Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
*
*/
#include <fstream>
#include "LedSysfsDriver.h"
#include "utils/log.h"
namespace velia::health {
LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory)
: m_log(spdlog::get("health"))
, m_brightnessFile(directory / "brightness")
{
// check that brightness file exists
if (!std::filesystem::exists(m_brightnessFile)) {
throw std::invalid_argument("Sysfs dir must contain 'brightness' file.");
}
m_log->trace("Initialized LED {}", std::string(directory));
}
/** @brief Set the brightness of the LED to @p brightness.
* Caller is responsible for providing correct brightness value. No checks for valid value are performed.
*/
void LedSysfsDriver::set(uint32_t brightness)
{
std::ofstream ofs(m_brightnessFile);
if (!(ofs << brightness)) {
throw std::invalid_argument("Write to '" + std::string(m_brightnessFile) + "' failed.");
}
}
}