-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwmcontroller.cpp
More file actions
47 lines (40 loc) · 1.31 KB
/
pwmcontroller.cpp
File metadata and controls
47 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "pwmcontroller.h"
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <sys/stat.h>
PwmController::PwmController(int pwmChipNumber, int channel)
: m_pwmChipNumber(pwmChipNumber), m_channel(channel) {
// 构建 sysfs 路径(如 /sys/class/pwm/pwmchip2/pwm0)
std::ostringstream oss;
oss << "/sys/class/pwm/pwmchip" << m_pwmChipNumber
<< "/pwm" << m_channel;
m_basePath = oss.str();
}
bool PwmController::initPwm() {
// 导出 PWM 通道
std::ostringstream exportPath;
exportPath << "/sys/class/pwm/pwmchip" << m_pwmChipNumber << "/export";
return writeSysFs(exportPath.str(), std::to_string(m_channel));
}
bool PwmController::setPeriod(unsigned int periodNs) {
return writeSysFs(m_basePath + "/period", std::to_string(periodNs));
}
bool PwmController::setDutyCycle(unsigned int dutyNs) {
return writeSysFs(m_basePath + "/duty_cycle", std::to_string(dutyNs));
}
bool PwmController::enable() {
return writeSysFs(m_basePath + "/enable", "1");
}
bool PwmController::disable() {
return writeSysFs(m_basePath + "/enable", "0");
}
bool PwmController::writeSysFs(const std::string& path, const std::string& value) {
std::ofstream file(path);
if (!file.is_open()) {
return false;
}
file << value;
file.close();
return true;
}