chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / softPwm.c
1 /*
2  * softPwm.c:
3  *      Test of the software PWM driver. Needs 8 LEDs connected
4  *      to the Pi - e.g. Ladder board.
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          8
35
36 int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7 } ;
37
38 int values [NUM_LEDS] = { 0, 25, 50, 75, 100, 75, 50, 25 } ;
39
40 int main ()
41 {
42   int i, j ;
43   char buf [80] ;
44
45   wiringPiSetup ()  ;
46
47   for (i = 0 ; i < NUM_LEDS ; ++i)
48   {
49     softPwmCreate (ledMap [i], 0, RANGE) ;
50     printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ;
51   }
52
53   fgets (buf, 80, stdin) ;
54
55 // Bring all up one by one:
56
57   for (i = 0 ; i < NUM_LEDS ; ++i)
58     for (j = 0 ; j <= 100 ; ++j)
59     {
60       softPwmWrite (ledMap [i], j) ;
61       delay (10) ;
62     }
63
64   fgets (buf, 80, stdin) ;
65
66 // All Down
67
68   for (i = 100 ; i > 0 ; --i)
69   {
70     for (j = 0 ; j < NUM_LEDS ; ++j)
71       softPwmWrite (ledMap [j], i) ;
72     delay (10) ;
73   }
74
75   fgets (buf, 80, stdin) ;
76
77   for (;;)
78   {
79     for (i = 0 ; i < NUM_LEDS ; ++i)
80       softPwmWrite (ledMap [i], values [i]) ;
81
82     delay (50) ;
83
84     i = values [0] ;
85     for (j = 0 ; j < NUM_LEDS - 1 ; ++j)
86       values [j] = values [j + 1] ;
87     values [NUM_LEDS - 1] = i ;
88   }
89 }