/*
---------------------------------------------------------
*
| Intel Edison C++ Example
Code |
*
| PWM EXAMPLE
|
*
---------------------------------------------------------
* This example tries to control 4 PWM signals
with Intel Edison
* Makefile: g++ -lmraa –o PWM_EDISON
PWM_EDISON.cpp
* ./PWM_EDISON 0.5
*/
#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa.h>
#include <mraa/pwm.h>
using namespace std;
sig_atomic_t volatile isrunning = 1;
void sig_handler(int signum);
int main(int argc, char *argv[])
{
signal(SIGINT, &sig_handler);
float
duty=1; /* Will hold the duty cycle */
mraa_init();
mraa_pwm_context
PWM_3_pin;
mraa_pwm_context
PWM_5_pin;
mraa_pwm_context
PWM_6_pin;
mraa_pwm_context
PWM_9_pin;
PWM_3_pin
= mraa_pwm_init(3);
if (PWM_3_pin == NULL) {
cout
<< "Error in
PWM_3_pin...\n";
}
PWM_5_pin
= mraa_pwm_init(5);
if (PWM_5_pin == NULL) {
cout
<< "Error in
PWM_5_pin...\n";
}
PWM_6_pin
= mraa_pwm_init(6);
if (PWM_6_pin == NULL) {
cout
<< "Error in
PWM_6_pin...\n";
}
PWM_9_pin
= mraa_pwm_init(9);
if (PWM_9_pin == NULL)
{
cout
<< "Error in
PWM_9_pin...\n";
}
mraa_pwm_period_ms(PWM_3_pin,
1);//period 1ms
mraa_pwm_period_ms(PWM_5_pin,
1);
mraa_pwm_period_ms(PWM_6_pin,
1);
mraa_pwm_period_ms(PWM_9_pin,
1);
duty
= atof(argv[1]);//tries
to convert the input to a floating number
duty
= duty <= 1 ? duty : 1;//if the number is higher than 1 then duty=1;
cout
<< "The duty selected is:
" <<duty << endl;
mraa_pwm_write(PWM_3_pin,
duty);
mraa_pwm_write(PWM_5_pin,
duty);
mraa_pwm_write(PWM_6_pin,
duty);
mraa_pwm_write(PWM_9_pin,
duty);
cout
<< "Press any key and then
Enter to exit from the aplication..."<<endl;
mraa_pwm_enable(PWM_3_pin,
1);
mraa_pwm_enable(PWM_5_pin,
1);
mraa_pwm_enable(PWM_6_pin,
1);
mraa_pwm_enable(PWM_9_pin,
1);
while (isrunning)
{
if (cin.get()) break;
}
cout
<< "Closing
application..." <<
endl;
mraa_pwm_enable(PWM_3_pin,
0);
mraa_pwm_enable(PWM_5_pin,
0);
mraa_pwm_enable(PWM_6_pin,
0);
mraa_pwm_enable(PWM_9_pin,
0);
return MRAA_SUCCESS;
}
void sig_handler(int signum)
{
if
(signum == SIGINT) isrunning = 0;
}
No comments:
Post a Comment