Private
Public Access
2
0

pwm: Fully Functional.

* C++ example included

Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
This commit is contained in:
Thomas Ingleby
2014-04-14 16:55:19 +01:00
parent 5c2235dc0c
commit fbd863ca39
3 changed files with 32 additions and 5 deletions

View File

@@ -1,7 +1,9 @@
add_executable (readi2c readi2c.cpp)
add_executable (hellomaa hellomaa.cpp)
add_executable (cycle-pwm3 cycle-pwm3.cpp)
include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include)
target_link_libraries (hellomaa maa)
target_link_libraries (readi2c maa)
target_link_libraries (cycle-pwm3 maa)

24
examples/cycle-pwm3.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <unistd.h>
#include "maa.h"
int
main ()
{
maa::PWM pwm(0, 3);
pwm.period_us(200);
pwm.enable(1);
float value = 0.0f;
while(1) {
value = value + 0.01f;
pwm.write(value);
usleep(50000);
if (value >= 1.0f) {
value = 0.0f;
}
float output = pwm.read();
}
return 0;
}

View File

@@ -43,6 +43,7 @@ PWM::PWM(int chipin, int pinin)
} else {
fprintf(export_f, "%d", pin);
fclose(export_f);
setup_duty_fp();
}
}
@@ -55,7 +56,8 @@ PWM::write(float percentage)
float
PWM::read()
{
return get_duty() / get_period();
float output = get_duty() / (float) get_period();
return output;
}
void
@@ -153,9 +155,9 @@ PWM::write_duty(int duty)
if(duty_fp == NULL) {
setup_duty_fp();
}
fseek(duty_fp, SEEK_SET, 0);
fprintf(duty_fp, "%d", duty);
fseek(duty_fp, SEEK_SET, 0);
rewind(duty_fp);
fflush(duty_fp);
}
int
@@ -178,8 +180,8 @@ PWM::get_period()
FILE *period_f;
char bu[64];
char output[16];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", chipid, pin);
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;
@@ -197,7 +199,6 @@ PWM::get_duty()
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);