diff --git a/app/include/fan/PwmControl.h b/app/include/fan/PwmControl.h new file mode 100644 index 0000000..9faeae5 --- /dev/null +++ b/app/include/fan/PwmControl.h @@ -0,0 +1,31 @@ +#ifndef PWMCONTROL_H_ +#define PWMCONTROL_H_ + +#include + +enum class PWM_CONTROL_PROPERTY { CONTROL, ENABLE, MODE }; +enum class PWM_ENABLE { FULL_SPEED = 0, MANUAL_CONTROL }; +enum class PWM_MODE { DC = 0, PWM }; + +class PwmControl { +public: + PwmControl(std::string controlPath); + + void pwm(int percent); + int pwm(); + + void enableManualControl(); + void reset(); + +private: + int readValue(std::string path); + + std::string mControlPath; + std::string mEnablePath; + std::string mModePath; + + std::string mInitialEnable; + std::string mInitialMode; +}; + +#endif // PWMCONTROL_H_ diff --git a/app/src/fan/PwmControl.cxx b/app/src/fan/PwmControl.cxx new file mode 100644 index 0000000..d972939 --- /dev/null +++ b/app/src/fan/PwmControl.cxx @@ -0,0 +1,60 @@ +#include +#include + +#include + +#define PWM_POSTFIX_ENABLE "_enable" +#define PWM_POSTFIX_MODE "_mode" + +#define PWM_MAX_VALUE 255 + +using namespace std; +namespace fs = filesystem; + +PwmControl::PwmControl(string controlPath) : mControlPath(controlPath) { + fs::path pathEnable(mControlPath + PWM_POSTFIX_ENABLE); + fs::path pathMode(mControlPath + PWM_POSTFIX_MODE); + + mEnablePath = pathEnable; + mModePath = pathMode; + + ifstream istrm; + + istrm.open(mEnablePath); + istrm >> mInitialEnable; + istrm.close(); + + istrm.open(mInitialMode); + istrm >> mInitialMode; + istrm.close(); +} + +void PwmControl::pwm(int percent) { + int pwmValue = PWM_MAX_VALUE * percent / 100; + + ofstream ostrm(mControlPath, ios::trunc); + ostrm << pwmValue; + ostrm.close(); +} + +int PwmControl::pwm() { + int value; + ifstream istrm; + + istrm.open(mControlPath); + istrm >> value; + + return value; +} + +void PwmControl::enableManualControl() { + ofstream ostrm(mEnablePath, ios::trunc); + ostrm << static_cast(PWM_ENABLE::MANUAL_CONTROL); + ostrm.close(); +} + +void PwmControl::reset() { + ofstream ostrm(mEnablePath, ios::trunc); + ostrm << mInitialEnable; + ostrm.close(); +}