pwm: Completed functions:
Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
This commit is contained in:
20
api/pwm.h
20
api/pwm.h
@@ -66,6 +66,8 @@ private:
|
|||||||
void write_period(int period);
|
void write_period(int period);
|
||||||
void write_duty(int duty);
|
void write_duty(int duty);
|
||||||
int setup_duty_fp();
|
int setup_duty_fp();
|
||||||
|
int get_period();
|
||||||
|
int get_duty();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -98,28 +100,28 @@ public:
|
|||||||
*/
|
*/
|
||||||
void period(float seconds);
|
void period(float seconds);
|
||||||
|
|
||||||
/** Set period. Microseconds.
|
/** Set period. milli-oseconds.
|
||||||
* @param ms microseconds for period.
|
* @param ms milli-seconds for period.
|
||||||
*/
|
*/
|
||||||
void period_ms(int ms);
|
void period_ms(int ms);
|
||||||
|
|
||||||
/** Set period. Nanoseconds
|
/** Set period. microseconds
|
||||||
* @param ns nanoseconds as period.
|
* @param ns microseconds as period.
|
||||||
*/
|
*/
|
||||||
void perod_us(int us);
|
void period_us(int us);
|
||||||
|
|
||||||
/** Set pulsewidth, As represnted by seconds in a (float).
|
/** Set pulsewidth, As represnted by seconds in a (float).
|
||||||
* @param seconds The duration of a pulse
|
* @param seconds The duration of a pulse
|
||||||
*/
|
*/
|
||||||
void pulsewidth(float seconds);
|
void pulsewidth(float seconds);
|
||||||
|
|
||||||
/** Set pulsewidth. Microseconds
|
/** Set pulsewidth. Milliseconds
|
||||||
* @param ms microseconds for pulsewidth.
|
* @param ms milliseconds for pulsewidth.
|
||||||
*/
|
*/
|
||||||
void pulsewidth_ms(int ms);
|
void pulsewidth_ms(int ms);
|
||||||
|
|
||||||
/** Set pulsewidth, nanoseconds.
|
/** Set pulsewidth, microseconds.
|
||||||
* @param us nanoseconds for pulsewidth.
|
* @param us microseconds for pulsewidth.
|
||||||
*/
|
*/
|
||||||
void pulsewidth_us(int us);
|
void pulsewidth_us(int us);
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "pwm.h"
|
#include "pwm.h"
|
||||||
|
|
||||||
using namespace maa;
|
using namespace maa;
|
||||||
@@ -47,67 +49,102 @@ PWM::PWM(int chipin, int pinin)
|
|||||||
void
|
void
|
||||||
PWM::write(float percentage)
|
PWM::write(float percentage)
|
||||||
{
|
{
|
||||||
//DO some writting
|
write_duty(percentage*get_period());
|
||||||
}
|
}
|
||||||
|
|
||||||
float
|
float
|
||||||
PWM::read()
|
PWM::read()
|
||||||
{
|
{
|
||||||
//Do Something
|
return get_duty() / get_period();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::period(float seconds)
|
PWM::period(float seconds)
|
||||||
{
|
{
|
||||||
//Do Something
|
period_ms(seconds*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::period_ms(int ms)
|
PWM::period_ms(int ms)
|
||||||
{
|
{
|
||||||
//Do Something
|
period_us(ms*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::perod_us(int us)
|
PWM::period_us(int us)
|
||||||
{
|
{
|
||||||
//Do Something
|
write_period(us*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::pulsewidth(float seconds)
|
PWM::pulsewidth(float seconds)
|
||||||
{
|
{
|
||||||
//Do Something
|
pulsewidth_ms(seconds*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::pulsewidth_ms(int ms)
|
PWM::pulsewidth_ms(int ms)
|
||||||
{
|
{
|
||||||
//Do Something
|
pulsewidth_us(ms*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::pulsewidth_us(int us)
|
PWM::pulsewidth_us(int us)
|
||||||
{
|
{
|
||||||
//Do Something
|
write_duty(us*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::enable(int enable)
|
PWM::enable(int enable)
|
||||||
{
|
{
|
||||||
|
int status;
|
||||||
|
if(enable != 0) {
|
||||||
|
status = 1;
|
||||||
|
} else {
|
||||||
|
status = enable;
|
||||||
|
}
|
||||||
|
FILE *enable_f;
|
||||||
|
char bu[64];
|
||||||
|
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/enable", chipid, pin);
|
||||||
|
|
||||||
|
if((enable_f = fopen(bu, "w")) == NULL) {
|
||||||
|
fprintf(stderr, "Failed to open export for writing!\n");
|
||||||
|
} else {
|
||||||
|
fprintf(enable_f, "%d", status);
|
||||||
|
fclose(enable_f);
|
||||||
|
}
|
||||||
//Do Something
|
//Do Something
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::close()
|
PWM::close()
|
||||||
{
|
{
|
||||||
//Do Something
|
enable(0);
|
||||||
|
FILE *unexport_f;
|
||||||
|
char buffer[64];
|
||||||
|
snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/unexport", chipid);
|
||||||
|
|
||||||
|
if((unexport_f = fopen(buffer, "w")) == NULL) {
|
||||||
|
fprintf(stderr, "Failed to open unexport for writing!\n");
|
||||||
|
} else {
|
||||||
|
fprintf(unexport_f, "%d", pin);
|
||||||
|
fclose(unexport_f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PWM::write_period(int period)
|
PWM::write_period(int period)
|
||||||
{
|
{
|
||||||
//Impossible
|
FILE *period_f;
|
||||||
|
char bu[64];
|
||||||
|
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", chipid, pin);
|
||||||
|
|
||||||
|
if((period_f = fopen(bu, "r+b")) == NULL) {
|
||||||
|
fprintf(stderr, "Failed to open period for writing!\n");
|
||||||
|
} else {
|
||||||
|
fprintf(period_f, "%d", period);
|
||||||
|
fclose(period_f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -134,3 +171,34 @@ PWM::setup_duty_fp()
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
PWM::get_period()
|
||||||
|
{
|
||||||
|
FILE *period_f;
|
||||||
|
char bu[64];
|
||||||
|
char output[16];
|
||||||
|
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", chipid, pin);
|
||||||
|
|
||||||
|
if((period_f = fopen(bu, "rb")) == NULL) {
|
||||||
|
fprintf(stderr, "Failed to open period for reading!\n");
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
fgets(output, 16, period_f);
|
||||||
|
fclose(period_f);
|
||||||
|
return atoi(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
PWM::get_duty()
|
||||||
|
{
|
||||||
|
if(duty_fp == NULL) {
|
||||||
|
setup_duty_fp();
|
||||||
|
}
|
||||||
|
char output[16];
|
||||||
|
fseek(duty_fp, SEEK_SET, 0);
|
||||||
|
fgets(output, 16, duty_fp);
|
||||||
|
fseek(duty_fp, SEEK_SET, 0);
|
||||||
|
return atoi(output);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user