I am using PWM library by Derek Molloy with few modifications in the file path.
They are as follows
PWM.h
`
#ifndef PWM_H_
#define PWM_H_
#include
using std::string;
#define PWM_PATH “/sys/devices/platform/ocp/”
#define PWM_PERIOD “period”
#define PWM_DUTY “duty_cycle”
#define PWM_POLARITY “polarity”
#define PWM_RUN “enable”
namespace exploringBB {
class PWM {
public:
enum POLARITY{ ACTIVE_LOW=0, ACTIVE_HIGH=1 };
private:
string name, path;
float analogFrequency; //defaults to 100,000 Hz
float analogMax; //defaults to 3.3V
public:
PWM(string pinName);
virtual int setPeriod(unsigned int period_ns);
virtual unsigned int getPeriod();
virtual int setFrequency(float frequency_hz);
virtual float getFrequency();
virtual int setDutyCycle(unsigned int duration_ns);
virtual int setDutyCycle(float percentage);
virtual unsigned int getDutyCycle();
virtual float getDutyCyclePercent();
virtual int setPolarity(PWM::POLARITY);
virtual void invertPolarity();
virtual PWM::POLARITY getPolarity();
virtual void setAnalogFrequency(float frequency_hz) { this->analogFrequency = frequency_hz; }
virtual int calibrateAnalogMax(float analogMax); //must be between 3.2 and 3.4
virtual int analogWrite(float voltage);
virtual int run();
virtual bool isRunning();
virtual int stop();
virtual ~PWM();
private:
float period_nsToFrequency(unsigned int);
unsigned int frequencyToPeriod_ns(float);
};
} /* namespace exploringBB */
#endif /* PWM_H_ */
`
PWM.cpp
`
#include “PWM.h”
#include “util.h”
#include
namespace exploringBB {
PWM::PWM(string pinName) {
this->name = pinName;
this->path = PWM_PATH + this->name + “/”;
this->analogFrequency = 100000;
this->analogMax = 3.3;
}
int PWM::setPeriod(unsigned int period_ns){
return write(this->path, PWM_PERIOD, period_ns);
}
unsigned int PWM::getPeriod(){
return atoi(read(this->path, PWM_PERIOD).c_str());
}
float PWM::period_nsToFrequency(unsigned int period_ns){
float period_s = (float)period_ns/1000000000;
return 1.0f/period_s;
}
unsigned int PWM::frequencyToPeriod_ns(float frequency_hz){
float period_s = 1.0f/frequency_hz;
return (unsigned int)(period_s*1000000000);
}
int PWM::setFrequency(float frequency_hz){
return this->setPeriod(this->frequencyToPeriod_ns(frequency_hz));
}
float PWM::getFrequency(){
return this->period_nsToFrequency(this->getPeriod());
}
int PWM::setDutyCycle(unsigned int duty_ns){
return write(this->path, PWM_DUTY, duty_ns);
}
int PWM::setDutyCycle(float percentage){
if ((percentage>100.0f)||(percentage<0.0f)) return -1;
unsigned int period_ns = this->getPeriod();
float duty_ns = period_ns * (percentage/100.0f);
this->setDutyCycle((unsigned int) duty_ns );
return 0;
}
unsigned int PWM::getDutyCycle(){
return atoi(read(this->path, PWM_DUTY).c_str());
}
float PWM::getDutyCyclePercent(){
unsigned int period_ns = this->getPeriod();
unsigned int duty_ns = this->getDutyCycle();
return 100.0f * (float)duty_ns/(float)period_ns;
}
int PWM::setPolarity(PWM::POLARITY polarity){
return write(this->path, PWM_POLARITY, polarity);
}
void PWM::invertPolarity(){
if (this->getPolarity()==PWM::ACTIVE_LOW) this->setPolarity(PWM::ACTIVE_HIGH);
else this->setPolarity(PWM::ACTIVE_LOW);
}
PWM::POLARITY PWM::getPolarity(){
if (atoi(read(this->path, PWM_POLARITY).c_str())==0) return PWM::ACTIVE_LOW;
else return PWM::ACTIVE_HIGH;
}
int PWM::calibrateAnalogMax(float analogMax){ //must be between 3.2 and 3.4
if((analogMax<3.2f) || (analogMax>3.4f)) return -1;
else this->analogMax = analogMax;
return 0;
}
int PWM::analogWrite(float voltage){
if ((voltage<0.0f)||(voltage>3.3f)) return -1;
this->setFrequency(this->analogFrequency);
this->setPolarity(PWM::ACTIVE_LOW);
this->setDutyCycle((100.0f*voltage)/this->analogMax);
return this->run();
}
int PWM::run(){
return write(this->path, PWM_RUN, 1);
}
bool PWM::isRunning(){
string running = read(this->path, PWM_RUN);
return (running==“1”);
}
int PWM::stop(){
return write(this->path, PWM_RUN, 0);
}
PWM::~PWM() {}
} /* namespace exploringBB */
`
util.cpp
`
#include “util.h”
#include
#include
#include
using namespace std;
namespace exploringBB {
/**
- Helper write function that writes a single string value to a file in the path provided
-
@param path The sysfs path of the file to be modified
-
@param filename The file to be written to in that path
-
@param value The value to be written to the file
-
@return
/
int write(string path, string filename, string value){
ofstream fs;
fs.open((path + filename).c_str());
if (!fs.is_open()){
perror("GPIO: write failed to open file ");
return -1;
}
fs << value;
fs.close();
return 0;
}
/*
- Helper read function that reads a single string value to a file from the path provided
-
@param path The sysfs path of the file to be read
-
@param filename Filename The file to be written to in that path
-
@return
*/
string read(string path, string filename){
ifstream fs;
fs.open((path + filename).c_str());
if (!fs.is_open()){
perror("GPIO: read failed to open file ");
}
string input;
getline(fs,input);
fs.close();
return input;
}
/**
- Private write method that writes a single int value to a file in the path provided
-
@param path The sysfs path of the file to be modified
-
@param filename The file to be written to in that path
-
@param value The int value to be written to the file
-
@return
*/
int write(string path, string filename, int value){
stringstream s;
s << value;
return write(path,filename,s.str());
}
} /* namespace exploringBB */
`