From 1da0844ae89248e3118f2618aeab1ada8a377cf3 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 2 Aug 2022 22:29:14 +0200 Subject: [PATCH] Add function to read PWM control values --- app/include/CorrelatePwmRpm.h | 16 ++++++++++++++++ app/include/pwm.h | 14 ++++++++++---- app/src/main.cxx | 5 +++-- app/src/pwm.cxx | 36 +++++++++++++++++++++++++++-------- 4 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 app/include/CorrelatePwmRpm.h diff --git a/app/include/CorrelatePwmRpm.h b/app/include/CorrelatePwmRpm.h new file mode 100644 index 0000000..4d2aa1a --- /dev/null +++ b/app/include/CorrelatePwmRpm.h @@ -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_ diff --git a/app/include/pwm.h b/app/include/pwm.h index 7805ff2..46adc3e 100644 --- a/app/include/pwm.h +++ b/app/include/pwm.h @@ -5,14 +5,19 @@ #include #include -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 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); diff --git a/app/src/main.cxx b/app/src/main.cxx index d2245ed..e28de21 100644 --- a/app/src/main.cxx +++ b/app/src/main.cxx @@ -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; } diff --git a/app/src/pwm.cxx b/app/src/pwm.cxx index dca58a9..ce002b6 100644 --- a/app/src/pwm.cxx +++ b/app/src/pwm.cxx @@ -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::getControls() { - std::vector vec; +vector PWM::getControls() { + vector vec; for (auto elem : mPwmControls) { vec.push_back(elem.second); @@ -71,8 +68,8 @@ std::vector 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(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); }