pwm: Fully Functional.
* C++ example included Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
add_executable (readi2c readi2c.cpp)
|
add_executable (readi2c readi2c.cpp)
|
||||||
add_executable (hellomaa hellomaa.cpp)
|
add_executable (hellomaa hellomaa.cpp)
|
||||||
|
add_executable (cycle-pwm3 cycle-pwm3.cpp)
|
||||||
|
|
||||||
include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include)
|
include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
target_link_libraries (hellomaa maa)
|
target_link_libraries (hellomaa maa)
|
||||||
target_link_libraries (readi2c maa)
|
target_link_libraries (readi2c maa)
|
||||||
|
target_link_libraries (cycle-pwm3 maa)
|
||||||
|
|||||||
24
examples/cycle-pwm3.cpp
Normal file
24
examples/cycle-pwm3.cpp
Normal 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;
|
||||||
|
}
|
||||||
@@ -43,6 +43,7 @@ PWM::PWM(int chipin, int pinin)
|
|||||||
} else {
|
} else {
|
||||||
fprintf(export_f, "%d", pin);
|
fprintf(export_f, "%d", pin);
|
||||||
fclose(export_f);
|
fclose(export_f);
|
||||||
|
setup_duty_fp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +56,8 @@ PWM::write(float percentage)
|
|||||||
float
|
float
|
||||||
PWM::read()
|
PWM::read()
|
||||||
{
|
{
|
||||||
return get_duty() / get_period();
|
float output = get_duty() / (float) get_period();
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -153,9 +155,9 @@ PWM::write_duty(int duty)
|
|||||||
if(duty_fp == NULL) {
|
if(duty_fp == NULL) {
|
||||||
setup_duty_fp();
|
setup_duty_fp();
|
||||||
}
|
}
|
||||||
fseek(duty_fp, SEEK_SET, 0);
|
|
||||||
fprintf(duty_fp, "%d", duty);
|
fprintf(duty_fp, "%d", duty);
|
||||||
fseek(duty_fp, SEEK_SET, 0);
|
rewind(duty_fp);
|
||||||
|
fflush(duty_fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -178,8 +180,8 @@ PWM::get_period()
|
|||||||
FILE *period_f;
|
FILE *period_f;
|
||||||
char bu[64];
|
char bu[64];
|
||||||
char output[16];
|
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) {
|
if((period_f = fopen(bu, "rb")) == NULL) {
|
||||||
fprintf(stderr, "Failed to open period for reading!\n");
|
fprintf(stderr, "Failed to open period for reading!\n");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -197,7 +199,6 @@ PWM::get_duty()
|
|||||||
setup_duty_fp();
|
setup_duty_fp();
|
||||||
}
|
}
|
||||||
char output[16];
|
char output[16];
|
||||||
fseek(duty_fp, SEEK_SET, 0);
|
|
||||||
fgets(output, 16, duty_fp);
|
fgets(output, 16, duty_fp);
|
||||||
fseek(duty_fp, SEEK_SET, 0);
|
fseek(duty_fp, SEEK_SET, 0);
|
||||||
return atoi(output);
|
return atoi(output);
|
||||||
|
|||||||
Reference in New Issue
Block a user