chiark / gitweb /
02f0b2274b17697ca9ac84aa3d16b1d6c8b9ae9f
[wiringPi.git] / examples / okLed.c
1 /*
2  * okLed:
3  *      Make the OK LED on the Pi Pulsate...
4  *    Copyright (c) 2012 gordon Henderson, but please Share and Enjoy!
5  *
6  * Originally posted to the Raspberry Pi forums:
7  *  http://www.raspberrypi.org/phpBB3/viewtopic.php?p=162581#p162581
8  *
9  * Compile this and store it somewhere, then kick it off at boot time
10  *    e.g. by putting it in /etc/rc.local and running it in the
11  *    background &
12  *
13  */
14
15 #include <stdio.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <math.h>
21
22 #include <wiringPi.h>
23 #include <softPwm.h>
24
25 #define OK_LED  16
26
27 int main ()
28 {
29   int fd, i ;
30
31   if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0)
32   {
33     fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ;
34     return 1 ;
35   }
36
37   write (fd, "none\n", 5) ;
38   close (fd) ;
39
40   if (wiringPiSetupGpio () < 0)
41   {
42     fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ;
43     return 1 ;
44   }
45
46   softPwmCreate (OK_LED, 0, 100) ;
47
48   for (;;)
49   {
50     for (i = 0 ; i <= 100 ; ++i)
51     {
52       softPwmWrite (OK_LED, i) ;
53       delay (10) ;
54     }
55     delay (50) ;
56
57     for (i = 100 ; i >= 0 ; --i)
58     {
59       softPwmWrite (OK_LED, i) ;
60       delay (10) ;
61     }
62     delay (10) ;
63   }
64
65   return 0 ;
66 }