Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PwmOut.cc
00001 #include <math.h> //For round 00002 #define NUMBER_PWM_STEPS 5000 //5000 steps between 0 and 1. 00003 namespace CPPToPigpio{ 00004 PwmOut::PwmOut(PinName pin) : _pin(pin), _pwmValue(0.0f) 00005 { 00006 CPPToPigpio::_handlePigpioInitialisationIfNeeded(); 00007 //pigpio pwm is based on number of possible steps between fully off and on. Have to convert from %-time on to this step amount 00008 gpioSetPWMrange(static_cast<int>(pin), NUMBER_PWM_STEPS); 00009 PwmOut::write(_pwmValue); 00010 } 00011 00012 PwmOut::PwmOut(PinName pin, float val) : _pin(pin), _pwmValue(val) 00013 { 00014 CPPToPigpio::_handlePigpioInitialisationIfNeeded(); 00015 gpioSetPWMrange(static_cast<int>(pin), NUMBER_PWM_STEPS); 00016 PwmOut::write(_pwmValue); 00017 } 00018 00019 PwmOut::PwmOut(int pin) : _pin(static_cast<PinName>(pin)), _pwmValue(0.0f) 00020 { 00021 //pigpio pwm is based on number of possible steps between fully off and on. Have to convert from %-time on to this step amount 00022 CPPToPigpio::_handlePigpioInitialisationIfNeeded(); 00023 gpioSetPWMrange(pin, NUMBER_PWM_STEPS); 00024 PwmOut::write(_pwmValue); 00025 } 00026 00027 PwmOut::PwmOut(int pin, float val) : _pin(static_cast<PinName>(pin)), _pwmValue(val) 00028 { 00029 CPPToPigpio::_handlePigpioInitialisationIfNeeded(); 00030 gpioSetPWMrange(pin, NUMBER_PWM_STEPS); 00031 PwmOut::write(_pwmValue); 00032 } 00033 00034 00035 void PwmOut::write(float val) 00036 { 00037 unsigned int dutyCycle = round(val*((float) NUMBER_PWM_STEPS)); 00038 gpioPWM(static_cast<int>(_pin), dutyCycle); 00039 } 00040 00041 float PwmOut::read() 00042 { 00043 //Recalculate the pwm duty cycle, since it's possible to get a different result than what was set by the user (if the input period*NUMBER_PWM_STEPS is not an int) 00044 unsigned numSteps = gpioGetPWMdutycycle(static_cast<int>(_pin)); 00045 return (static_cast<float>(numSteps)/static_cast<float>(NUMBER_PWM_STEPS)); 00046 } 00047 00048 float PwmOut::period(float seconds) 00049 { 00050 //Depends on Pi PWM sample rate, default is 5 and for now not changing from that 00051 unsigned int freq = static_cast<int>(1.0f/seconds); 00052 return 1.0f/(static_cast<float>(gpioSetPWMfrequency(_pin, freq))); //Likely WILL NOT match input 00053 } 00054 00055 float PwmOut::period_ms(int ms) 00056 { 00057 float seconds = static_cast<float>(ms)/1000.0f; 00058 unsigned int freq = static_cast<int>(1.0f/seconds); 00059 return 1.0f/(static_cast<float>(gpioSetPWMfrequency(_pin, freq))); 00060 } 00061 00062 float PwmOut::period_us(int us) 00063 { 00064 float seconds = static_cast<float>(us)/1000000.0f; 00065 unsigned int freq = static_cast<int>(1.0f/seconds); 00066 return 1.0f/(static_cast<float>(gpioSetPWMfrequency(_pin, freq))); 00067 } 00068 00069 PwmOut & PwmOut::operator=(float val) 00070 { 00071 PwmOut::write(val); 00072 return *this; 00073 } 00074 00075 PwmOut & PwmOut::operator=(PwmOut & rhs) 00076 { 00077 PwmOut::write(rhs.read()); 00078 return *this; 00079 } 00080 00081 PwmOut::operator float() 00082 { 00083 return PwmOut::read(); 00084 } 00085 00086 PwmOut::~PwmOut() 00087 { 00088 CPPToPigpio::_handlePigpioTermination(); 00089 } 00090 } 00091
Generated on Tue Jul 12 2022 14:39:23 by
1.7.2