-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_PWM.cpp
90 lines (65 loc) · 1.49 KB
/
test_PWM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Usage example of the JETGPIO library
* Compile with: g++ -Wall -o test_PWM test_PWM.cpp -ljetgpio
* Execute with: sudo ./test_PWM
*/
#include <iostream>
#include <unistd.h>
#include <signal.h> /* for catching exceptions e.g. control-C*/
#include <jetgpio.h>
void inthandler(int signum)
{
usleep(1500);
gpioTerminate();
printf("Caught signal %d, coming out ...\n", signum);
exit(1);
}
int main(int argc, char *argv[])
{
int Init;
int status;
signal(SIGINT, inthandler);
signal(SIGHUP, inthandler);
signal(SIGABRT, inthandler);
signal(SIGILL, inthandler);
signal(SIGSEGV, inthandler);
signal(SIGTERM, inthandler);
Init = gpioInitialise();
if (Init < 0)
{
/* jetgpio initialisation failed */
printf("Jetgpio initialisation failed. Error code: %d\n", Init);
exit(Init);
}
else
{
/* jetgpio initialised okay*/
printf("Jetgpio initialisation OK. Return code: %d\n", Init);
}
gpioSetPWMfrequency(33, 1000);
gpioPWM(33, 0);
int x = 0;
int level = 0;
// Increase brightness
while (x<=256) {
usleep(100000);
level = x;
printf("level: %d - ",level);
status = gpioPWM(33, x);
printf("status: %d\n",status);
usleep(100000);
x=x+1;
}
x = 256;
// Decrease brightness
while (x>=0) {
usleep(100000);
level = x;
printf("level: %d - ",level);
status = gpioPWM(33, x);
printf("status: %d\n",status);
usleep(100000);
x=x-1;
}
gpioTerminate();
exit(0);
}