chiark / gitweb /
c1fc3317c79912eefab0498dc935fd95c2975e92
[wiringPi.git] / examples / pwm.c
1 /*
2  * pwm.c:
3  *      Test of the software PWM driver. Needs 12 LEDs connected
4  *      to the Pi.
5  *
6  * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
7  ***********************************************************************
8  * This file is part of wiringPi:
9  *      https://projects.drogon.net/raspberry-pi/wiringpi/
10  *
11  *    wiringPi is free software: you can redistribute it and/or modify
12  *    it under the terms of the GNU Lesser General Public License as published by
13  *    the Free Software Foundation, either version 3 of the License, or
14  *    (at your option) any later version.
15  *
16  *    wiringPi is distributed in the hope that it will be useful,
17  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *    GNU Lesser General Public License for more details.
20  *
21  *    You should have received a copy of the GNU Lesser General Public License
22  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
23  ***********************************************************************
24  */
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29
30 #include <wiringPi.h>
31 #include <softPwm.h>
32
33 #define RANGE           100
34 #define NUM_LEDS         12
35
36 int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ;
37
38 int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ;
39
40 int main ()
41 {
42   int i, j ;
43   char buf [80] ;
44
45   if (wiringPiSetup () == -1)
46   {
47     fprintf (stdout, "oops: %s\n", strerror (errno)) ;
48     return 1 ;
49   }
50
51   for (i = 0 ; i < NUM_LEDS ; ++i)
52   {
53     softPwmCreate (ledMap [i], 0, RANGE) ;
54     printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ;
55   }
56
57   fgets (buf, 80, stdin) ;
58
59 // Bring all up one by one:
60
61   for (i = 0 ; i < NUM_LEDS ; ++i)
62     for (j = 0 ; j <= 100 ; ++j)
63     {
64       softPwmWrite (ledMap [i], j) ;
65       delay (10) ;
66     }
67
68   fgets (buf, 80, stdin) ;
69
70 // Down fast
71
72   for (i = 100 ; i > 0 ; --i)
73   {
74     for (j = 0 ; j < NUM_LEDS ; ++j)
75       softPwmWrite (ledMap [j], i) ;
76     delay (10) ;
77   }
78
79   fgets (buf, 80, stdin) ;
80
81   for (;;)
82   {
83     for (i = 0 ; i < NUM_LEDS ; ++i)
84       softPwmWrite (ledMap [i], values [i]) ;
85
86     delay (50) ;
87
88     i = values [0] ;
89     for (j = 0 ; j < NUM_LEDS - 1 ; ++j)
90       values [j] = values [j + 1] ;
91     values [NUM_LEDS - 1] = i ;
92   }
93 }