21#define PWM_TOUT_USEC 10000
22#define PWM_EXPORT "export"
23#define PWM_UNEXPORT "unexport"
24#define PWM_PERIOD "period"
25#define PWM_DUTYCYCLE "duty_cycle"
26#define PWM_ENABLE "enable"
32PWM::PWM(uint32_t pwmChipNo, uint32_t pwmPinNo,
bool shouldPrint) :
33 m_shouldPrint(shouldPrint), m_chipNo(pwmChipNo), m_pinNo(pwmPinNo), m_nSecPeriod(
34 0), m_nSecDutyCycle(0), m_chipPath(
SYS_PWM_PATH), m_isSetupComplete(
35 false), m_isPWMRunning(false)
37 m_chipPath +=
"/pwmchip" + to_string(m_chipNo);
40 string error =
"PWM Chip number is invalid: " + to_string(m_chipNo);
41 printf(
"%s\n", error.c_str());
42 throw std::invalid_argument(error);
51bool PWM::setup(uint64_t nSecPeriod, uint64_t nSecDutyCycle)
53 if (nSecDutyCycle > nSecPeriod)
55 throw std::invalid_argument(
56 "Duty cycle time cannot be more than period");
59 m_nSecPeriod = nSecPeriod;
60 m_nSecDutyCycle = nSecDutyCycle;
65 if (m_shouldPrint && didFail)
67 printf(
"stop failed\n");
70 if (m_isSetupComplete && !didFail)
73 if (m_shouldPrint && didFail)
75 printf(
"set duty cycle failed\n");
80 if (m_shouldPrint && didFail)
82 printf(
"set period failed\n");
86 if (!m_isSetupComplete && !didFail)
89 if (m_shouldPrint && didFail)
91 printf(
"export failed\n");
96 didFail = !updatePWMParams(nSecPeriod,
PWM_PERIOD);
97 if (m_shouldPrint && didFail)
99 printf(
"set period failed\n");
105 if (m_shouldPrint && didFail)
107 printf(
"set duty cycle failed\n");
110 m_isSetupComplete = !didFail;
111 if (!m_isSetupComplete)
113 bool didSucceed = UnExport();
114 if (!didSucceed && m_shouldPrint)
116 printf(
"unexport failed\n");
124 bool didFail =
false;
128 m_isPWMRunning = didFail;
130 if (m_isSetupComplete)
132 didFail = !UnExport();
133 m_isSetupComplete = didFail;
140 bool didFail =
false;
141 if (m_isSetupComplete)
144 m_isPWMRunning = !didFail;
151 bool didFail =
false;
152 if (m_isSetupComplete && m_isPWMRunning)
155 m_isPWMRunning = didFail;
162 m_nSecDutyCycle = nSecDutyCycle;
163 m_percentDutyCycle = (m_nSecDutyCycle / m_nSecPeriod) * 100;
169 uint64_t dutyCycle = (m_nSecPeriod * percent) / 100;
170 m_nSecDutyCycle = dutyCycle;
176 return m_percentDutyCycle;
181 return m_nSecDutyCycle;
184std::string PWM::getPWMPinPath()
186 return m_chipPath +
"/pwm" + to_string(m_pinNo);
189bool PWM::updatePWMParams(uint64_t value, std::string params)
191 string pwmPinPath = getPWMPinPath();
196 printf(
"%s doesnt exist\n", pwmPinPath.c_str());
199 "Seems like the pwm(" + to_string(m_pinNo)
200 +
") setup is not done properly.");
201 printf(
"%s\n", log.c_str());
205 char buff[64] = { 0 };
207 fd = open((pwmPinPath +
"/" + params).c_str(), O_WRONLY);
210 printf(
"unable to open %s to set %" PRIu64
"\n", params.c_str(), value);
214 length = snprintf(buff,
sizeof(buff),
"%" PRIu64
"", value);
215 if (write(fd, buff, length) != length)
218 printf(
"unable to write %" PRIu64
" on %s\n", value, params.c_str());
229 char buff[64] = { 0 };
230 string pwmPath = m_chipPath +
"/" +
PWM_EXPORT;
233 printf(
"export path %s\n", pwmPath.c_str());
237 printf(
"%s not found\n", pwmPath.c_str());
240 fd = open(pwmPath.c_str(), O_WRONLY);
243 printf(
"unable to open %s for pwm %" PRIu32
"\n", pwmPath.c_str(),
248 length = snprintf(buff,
sizeof(buff),
"%" PRIu32
"", m_pinNo);
251 printf(
"write on export %s length %d\n", buff, length);
253 if (write(fd, buff, length) != length)
256 printf(
"unable to init pwm %" PRIu32
"\n", m_pinNo);
263 printf(
"export write succeed\n");
267 printf(
"unable to access pwm %" PRIu32
"\n", m_pinNo);
276 char buff[64] = { 0 };
278 fd = open((m_chipPath +
"/" +
PWM_UNEXPORT).c_str(), O_WRONLY);
281 printf(
"unable to open pwm %" PRIu32
" for %s\n", m_pinNo,
286 length = snprintf(buff,
sizeof(buff),
"%" PRIu32
"", m_pinNo);
287 if (write(fd, buff, length) != length)
290 printf(
"unable to %s gpio %" PRIu32
"\n",
PWM_UNEXPORT, m_pinNo);
296 printf(
"unable to close pwm %" PRIu32
"\n", m_pinNo);
bool changeDutyCycle(uint32_t percent)
bool updateDutyCycle(uint64_t nSecDutyCycle)
PWM(uint32_t pwmChipNo, uint32_t pwmPinNo, bool shouldPrint)
uint32_t getDutyCyclePercent()
bool setup(uint64_t nSecPeriod, uint64_t nSecDutyCycle)
uint64_t getDutyCycleInNSec()
static bool fileExists(const std::string &path)
static bool directoryExists(const std::string &path)