Refactoring, housekeeping, documentation
Add a class diagram. Still needs some details. Refactor to better respect SOLID principles. Housekeeping, move and rename classes/files.
This commit is contained in:
82
app/src/pwm/PWMControl.cxx
Normal file
82
app/src/pwm/PWMControl.cxx
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <boost/json/object.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <pwm/PWMControl.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
PWMControl::~PWMControl() {
|
||||
cout << "Cleanup" << endl;
|
||||
Reset();
|
||||
}
|
||||
|
||||
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<int>(PWM_ENABLE::MANUAL_CONTROL);
|
||||
ostrm.close();
|
||||
}
|
||||
|
||||
void PWMControl::Reset() {
|
||||
ofstream ostrm(mEnablePath, ios::trunc);
|
||||
|
||||
ostrm << mInitialEnable;
|
||||
ostrm.close();
|
||||
|
||||
ostrm.open(mModePath, ios::trunc);
|
||||
ostrm << mInitialMode;
|
||||
|
||||
ostrm.close();
|
||||
}
|
||||
|
||||
const string PWMControl::toString() const {
|
||||
return fs::path(mControlPath).filename();
|
||||
}
|
||||
|
||||
json PWMControl::toJson() const {
|
||||
json obj = {"PWMControl", mControlPath};
|
||||
return obj;
|
||||
}
|
||||
37
app/src/pwm/PWMControlFacade.cxx
Normal file
37
app/src/pwm/PWMControlFacade.cxx
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
|
||||
#include <pwm/PWMControlFacade.h>
|
||||
|
||||
using namespace std;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
vector<shared_ptr<PWMControl>> PWMControlFacade::PWMControls() {
|
||||
vector<shared_ptr<PWMControl>> controls;
|
||||
|
||||
const regex re_ctrl_enable("pwm[0-9]_enable");
|
||||
const regex re_ctrl_mode("pwm[0-9]_mode");
|
||||
const regex re_ctrl("pwm[0-9]");
|
||||
|
||||
if (!fs::exists(HWMON_BASE_PATH)) {
|
||||
cout << HWMON_BASE_PATH << " doesn't exist" << 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(make_shared<PWMControl>(controlPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return controls;
|
||||
}
|
||||
Reference in New Issue
Block a user