-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm.cpp
138 lines (112 loc) · 3.34 KB
/
pwm.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "pwm.h"
namespace pwm {
LinuxPwmOutput::LinuxPwmOutput(const std::string& basePath, unsigned int pwmchip, unsigned int pwm) :
basePath(basePath),
pwmchip(pwmchip),
pwm(pwm) {
exportPwm();
setProperty("enable", "0");
setProperty("period", "0");
setProperty("duty_cycle", "0");
}
void LinuxPwmOutput::enable(bool en) {
if (en)
setProperty("enable", "1");
else
setProperty("enable", "0");
}
void LinuxPwmOutput::setPeriodNs(unsigned long int value) {
setProperty("period", std::to_string(value));
}
void LinuxPwmOutput::setDutyCycleNs(unsigned long int value) {
setProperty("duty_cycle", std::to_string(value));
}
void LinuxPwmOutput::exportPwm() {
auto exportPath = basePath + std::string("/pwmchip") +
std::to_string(pwmchip) + std::string("/export");
int fd = open(exportPath.c_str(), O_WRONLY);
if (fd == -1) {
printf("ERROR while exporting %s.\n", exportPath.c_str());
return;
}
auto pwmString = (std::to_string(pwm));
size_t bytesWritten = write(fd, pwmString.c_str(), pwmString.size());
if (bytesWritten != pwmString.size()) {
printf("ERROR while exporting %s.\n", pwmString.c_str());
close(fd);
return;
}
close(fd);
}
void LinuxPwmOutput::setProperty(const std::string& prop, const std::string& value) {
auto propertyPath = basePath + std::string("/pwmchip") +
std::to_string(pwmchip) + std::string("/pwm") +
std::to_string(pwm) + std::string("/") + prop;
int fd = open(propertyPath.c_str(), O_WRONLY);
if (fd == -1) {
printf("ERROR while opening property %s.\n", propertyPath.c_str());
return;
}
size_t bytesWritten = write(fd, value.c_str(), value.size());
if (bytesWritten != value.size()) {
printf("ERROR while opening property %s.\n", propertyPath.c_str());
close(fd);
return;
}
close(fd);
}
PwmBeeper::PwmBeeper(PwmOutput& pwmOutput, SleepFunction sleepFunction) :
pwmOutput(pwmOutput),
sleepFunction(sleepFunction) {
pwmOutput.setPeriodNs(0);
pwmOutput.setDutyCycleNs(0);
pwmOutput.enable(false);
}
void PwmBeeper::setVolume(uint8_t volume) {
if (volume <= 100)
this->volume = volume;
else
this->volume = 100;
}
uint8_t PwmBeeper::getVolume() {
return volume;
}
void PwmBeeper::playTone(const common::BeeperTone& tone) {
auto period = tone.frequency_hz ? common::GIGA / tone.frequency_hz : 0;
pwmOutput.setPeriodNs(period);
pwmOutput.setDutyCycleNs(period * volume / 100);
pwmOutput.enable(true);
sleepFunction(tone.duration_ms);
pwmOutput.enable(false);
}
PwmRgbLed::PwmRgbLed(PwmOutput& pwmRed, PwmOutput& pwmGreen, PwmOutput& pwmBlue) :
pwmRed(pwmRed),
pwmGreen(pwmGreen),
pwmBlue(pwmBlue) {
setChannelDefaults(pwmRed);
setChannelDefaults(pwmGreen);
setChannelDefaults(pwmBlue);
}
unsigned long PwmRgbLed::getDefaultPeriodNs() {
return 70000UL; // 14 kHz
}
void PwmRgbLed::set(common::LightSetting value) {
bufferedSetting = value;
setChannel(pwmRed, value.r);
setChannel(pwmGreen, value.g);
setChannel(pwmBlue, value.b);
}
common::LightSetting PwmRgbLed::get() {
return bufferedSetting;
}
void PwmRgbLed::setChannel(PwmOutput& pwmOutput, unsigned long value) {
pwmOutput.setPeriodNs(getDefaultPeriodNs());
pwmOutput.setDutyCycleNs(getDefaultPeriodNs() * DUTY_CYCLE_MAX_RATIO * value / 255);
pwmOutput.enable(true);
}
void PwmRgbLed::setChannelDefaults(PwmOutput& pwmOutput) {
pwmOutput.setPeriodNs(getDefaultPeriodNs());
pwmOutput.setDutyCycleNs(0);
pwmOutput.enable(false);
}
}