Add function to read PWM control values

This commit is contained in:
2022-08-02 22:29:14 +02:00
parent d6f0d9650c
commit 1da0844ae8
4 changed files with 57 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
#ifndef CORRELATEPWMRPM_H_
#define CORRELATEPWMRPM_H_
/*
** What I need:
** - All system RPM sensors
** - All system PWM controls
**
** ToDo: Find which PWM controller affects which RPM sensor
**
*/
class CorrelatePwmRpm {
public:
};
#endif // CORRELATEPWMRPM_H_

View File

@@ -5,14 +5,19 @@
#include <unordered_map>
#include <vector>
typedef struct {
enum class PWM_CONTROL_PROPERTY { CONTROL, ENABLE, MODE };
struct PWM_CONTROL {
std::string control;
std::string enable;
std::string mode;
} PWM_CONTROL;
typedef enum { FULL_SPEED = 0, MANUAL_CONTROL, AUTOMATIC } PWM_ENABLE;
typedef enum { DC = 0, PWM } PWM_MODE;
std::string initialEnable;
std::string initialMode;
};
enum class PWM_ENABLE { FULL_SPEED = 0, MANUAL_CONTROL };
enum class PWM_MODE { DC = 0, PWM };
class PWM {
public:
@@ -20,6 +25,7 @@ public:
void dumpValues();
std::vector<PWM_CONTROL> getControls();
int readValue(PWM_CONTROL control, PWM_CONTROL_PROPERTY property);
void setEnable(PWM_CONTROL control, PWM_ENABLE value);
void setMode(PWM_CONTROL control, PWM_MODE mode);

View File

@@ -48,11 +48,12 @@ int main() {
std::cout << "\nGPU Temp: " << temp << std::endl;
class PWM pwm;
std::cout << '\n';
pwm.dumpValues();
auto controls = pwm.getControls();
pwm.setEnable(controls[1], AUTOMATIC);
std::cout << pwm.readValue(controls[0], PWM_CONTROL_PROPERTY::ENABLE)
<< std::endl;
return 0;
}

View File

@@ -40,9 +40,6 @@ PWM::PWM() {
fs::path path_ctrl_mode(
string(controlPath + string(PWM_POSTFIX_MODE)));
if (fs::exists(path_ctrl_enable) && fs::exists(path_ctrl_mode))
cout << control << endl;
mPwmControls.insert(
{controlPath, PWM_CONTROL{controlPath, path_ctrl_enable.string(),
path_ctrl_mode.string()}});
@@ -59,8 +56,8 @@ void PWM::dumpValues() {
}
}
std::vector<PWM_CONTROL> PWM::getControls() {
std::vector<PWM_CONTROL> vec;
vector<PWM_CONTROL> PWM::getControls() {
vector<PWM_CONTROL> vec;
for (auto elem : mPwmControls) {
vec.push_back(elem.second);
@@ -71,8 +68,8 @@ std::vector<PWM_CONTROL> PWM::getControls() {
void PWM::setEnable(PWM_CONTROL control, PWM_ENABLE value) {
cout << control.control << endl;
std::ofstream ostrm(control.enable, std::ios::trunc);
ostrm << value;
ofstream ostrm(control.enable, ios::trunc);
ostrm << static_cast<int>(value);
ostrm.close();
}
@@ -80,10 +77,33 @@ void PWM::setValuePwm(PWM_CONTROL control, int pwm) {
if (pwm < 0 || pwm > 255)
return;
std::ofstream ostrm(control.control, std::ios::trunc);
ofstream ostrm(control.control, ios::trunc);
ostrm << pwm;
ostrm.close();
}
int PWM::readValue(PWM_CONTROL control, PWM_CONTROL_PROPERTY property) {
int result;
ifstream istrm;
switch (property) {
case PWM_CONTROL_PROPERTY::CONTROL:
istrm.open(control.control, ios::in);
istrm >> result;
break;
case PWM_CONTROL_PROPERTY::ENABLE:
istrm.open(control.enable, ios::in);
istrm >> result;
break;
case PWM_CONTROL_PROPERTY::MODE:
istrm.open(control.mode, ios::in);
istrm >> result;
break;
}
return result;
}
void PWM::setValuePercent(PWM_CONTROL control, int percentage) {
setValuePwm(control, PWM_MAX_VALUE * percentage / 100);
}