Add documentation, cleanup

This commit is contained in:
2022-09-17 15:45:48 +02:00
parent bbdd612209
commit 828964bf8f
17 changed files with 215 additions and 121 deletions

View File

@@ -1,23 +1,24 @@
#include <exception>
#include <filesystem>
#include <iostream>
#include <memory>
#include <regex>
#include <vector>
#include <sensors/sensors.h>
#include <fan/Pwm.h>
#include <sensor/HwmonTemperatureSensor.h>
#include <sensor/NvidiaTemperatureSensor.h>
#include <fan/PwmControl.h>
#include <sensor/HwmonSensor.h>
#include <sensor/NvidiaSensor.h>
#define CONFIG_FILE "/etc/conf.d/sensors"
#define HWMON_BASE_PATH "/sys/class/hwmon"
std::vector<std::shared_ptr<TemperatureSensor>> sensors() {
std::vector<std::shared_ptr<TemperatureSensor>> sensors;
namespace fs = std::filesystem;
auto config = fopen(CONFIG_FILE, "r");
if (sensors_init(config) != 0) {
throw std::runtime_error("Config file doesn't exist");
}
template <sensors_subfeature_type T>
std::vector<std::shared_ptr<Sensor>> sensors() {
std::vector<std::shared_ptr<Sensor>> sensors;
int c = 0;
for (const sensors_chip_name *chipName;
@@ -26,32 +27,64 @@ std::vector<std::shared_ptr<TemperatureSensor>> sensors() {
int d = 0;
for (const sensors_feature *feature;
(feature = sensors_get_features(chipName, &d)) != NULL;) {
auto tempFeature = sensors_get_subfeature(chipName, feature,
SENSORS_SUBFEATURE_TEMP_INPUT);
if (tempFeature)
sensors.push_back(std::make_shared<HwmonTemperatureSensor>(
chipName, feature, tempFeature));
auto subFeature = sensors_get_subfeature(chipName, feature, T);
if (subFeature) {
sensors.push_back(
std::make_shared<HwmonSensor>(chipName, feature, subFeature));
}
}
}
sensors.push_back(std::make_shared<NvidiaTemperatureSensor>());
return sensors;
}
std::vector<std::shared_ptr<PwmControl>> pwmControls() {
std::vector<std::shared_ptr<PwmControl>> controls;
const std::regex re_ctrl_enable("pwm[0-9]_enable");
const std::regex re_ctrl_mode("pwm[0-9]_mode");
const std::regex re_ctrl("pwm[0-9]");
if (!fs::exists(HWMON_BASE_PATH)) {
std::cout << HWMON_BASE_PATH << " doesn't exist" << std::endl;
} else {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
for (const fs::directory_entry &control :
fs::directory_iterator{hwmon_device}) {
auto filename = control.path().filename().string();
if (regex_match(filename, re_ctrl)) {
auto controlPath = control.path().string();
controls.push_back(std::make_shared<PwmControl>(controlPath));
}
}
}
}
return controls;
}
int main() {
class PWM pwm;
std::cout << '\n';
pwm.dumpValues();
auto config = fopen(CONFIG_FILE, "r");
if (sensors_init(config) != 0) {
throw std::runtime_error("Config file doesn't exist");
}
auto controls = pwm.getControls();
std::cout << pwm.readValue(controls[0], PWM_CONTROL_PROPERTY::ENABLE)
<< std::endl;
auto tempSensors = sensors();
auto tempSensors = sensors<SENSORS_SUBFEATURE_TEMP_INPUT>();
tempSensors.push_back(std::make_shared<NvidiaSensor>());
for (auto s : tempSensors) {
std::cout << s->name() << ": " << s->temperature() << std::endl;
std::cout << s->name() << ": " << s->value() << std::endl;
}
std::cout << "pwm" << std::endl;
auto pwmSensors = sensors<SENSORS_SUBFEATURE_FAN_INPUT>();
for (auto s : pwmSensors) {
std::cout << s->name() << ": " << s->value() << std::endl;
}
return 0;