Add feature to set PWM values

This commit is contained in:
2022-07-29 00:22:23 +02:00
parent 6763dd9e3c
commit d6f0d9650c
3 changed files with 42 additions and 2 deletions

View File

@@ -3,6 +3,7 @@
#include <string>
#include <unordered_map>
#include <vector>
typedef struct {
std::string control;
@@ -18,6 +19,8 @@ public:
PWM();
void dumpValues();
std::vector<PWM_CONTROL> getControls();
void setEnable(PWM_CONTROL control, PWM_ENABLE value);
void setMode(PWM_CONTROL control, PWM_MODE mode);
void setValuePwm(PWM_CONTROL control, int pwm);

View File

@@ -47,8 +47,12 @@ int main() {
auto temp = nv.get_gpu_temperature();
std::cout << "\nGPU Temp: " << temp << std::endl;
PWM pwm;
class PWM pwm;
pwm.dumpValues();
auto controls = pwm.getControls();
pwm.setEnable(controls[1], AUTOMATIC);
return 0;
}

View File

@@ -1,8 +1,11 @@
#include <cstdio>
#include <pwm.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
namespace fs = std::filesystem;
using namespace std;
@@ -12,6 +15,8 @@ using namespace std;
#define PWM_POSTFIX_ENABLE "_enable"
#define PWM_POSTFIX_MODE "_mode"
#define PWM_MAX_VALUE 255
PWM::PWM() {
const regex re_ctrl_enable("pwm[0-9]_enable");
const regex re_ctrl_mode("pwm[0-9]_mode");
@@ -22,7 +27,6 @@ PWM::PWM() {
} else {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
cout << hwmon_device << endl;
for (const fs::directory_entry &control :
fs::directory_iterator{hwmon_device}) {
@@ -54,3 +58,32 @@ void PWM::dumpValues() {
<< control.second.mode << endl;
}
}
std::vector<PWM_CONTROL> PWM::getControls() {
std::vector<PWM_CONTROL> vec;
for (auto elem : mPwmControls) {
vec.push_back(elem.second);
}
return vec;
}
void PWM::setEnable(PWM_CONTROL control, PWM_ENABLE value) {
cout << control.control << endl;
std::ofstream ostrm(control.enable, std::ios::trunc);
ostrm << value;
ostrm.close();
}
void PWM::setValuePwm(PWM_CONTROL control, int pwm) {
if (pwm < 0 || pwm > 255)
return;
std::ofstream ostrm(control.control, std::ios::trunc);
ostrm << pwm;
ostrm.close();
}
void PWM::setValuePercent(PWM_CONTROL control, int percentage) {
setValuePwm(control, PWM_MAX_VALUE * percentage / 100);
}