chiark / gitweb /
Added in PiGlow devLib and a couple of examples for the PiGlow
[wiringPi.git] / examples / PiGlow / piGlow1.c
1 /*
2  * piGlow1.c:
3  *      Very simple demonstration of the PiGlow board.
4  *      This uses the piGlow devLib.
5  *
6  * Copyright (c) 2013 Gordon Henderson.
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 <stdlib.h>
28 #include <poll.h>
29
30 #include <wiringPi.h>
31 #include <piGlow.h>
32
33 #define PIGLOW_BASE     533
34
35
36 /*
37  * keypressed: clearKeypressed:
38  *      Simple but effective ways to tell if the enter key has been pressed
39  *********************************************************************************
40  */
41
42 static int keypressed (void)
43 {
44   struct pollfd polls ;
45
46   polls.fd     = fileno (stdin) ;
47   polls.events = POLLIN ;
48
49   return poll (&polls, 1, 0) != 0 ;
50 }
51
52 static void clearKeypressed (void)
53 {
54   while (keypressed ())
55     (void)getchar () ;
56 }
57
58
59 /*
60  * pulseLed:
61  *      Pulses the LED at position leg, ring from off to a max. value,
62  *      then off again
63  *********************************************************************************
64  */
65
66 static void pulseLed (int leg, int ring)
67 {
68   int i ;
69
70   for (i = 0 ; i < 140 ; ++i)
71   {
72     piGlow1 (leg, ring, i) ;
73     delay (1) ;
74   }
75   delay (10) ;
76   for (i = 140 ; i >= 0 ; --i)
77   {
78     piGlow1 (leg, ring, i) ;
79     delay (1) ;
80   }
81 }
82
83 /*
84  * pulseLeg:
85  *      Same as above, but a whole leg at a time
86  *********************************************************************************
87  */
88
89 static void pulseLeg (int leg)
90 {
91   int i ;
92
93   for (i = 0 ; i < 140 ; ++i)
94   {
95     piGlowLeg (leg, i) ; delay (1) ;
96   }
97   delay (10) ;
98   for (i = 140 ; i >= 0 ; --i)
99   {
100     piGlowLeg (leg, i) ; delay (1) ;
101   }
102 }
103
104
105 /*
106  * pulse Ring:
107  *      Same as above, but a whole ring at a time
108  *********************************************************************************
109  */
110
111 static void pulseRing (int ring)
112 {
113   int i ;
114
115   for (i = 0 ; i < 140 ; ++i)
116   {
117     piGlowRing (ring, i) ; delay (1) ;
118   }
119   delay (10) ;
120   for (i = 140 ; i >= 0 ; --i)
121   {
122     piGlowRing (ring, i) ; delay (1) ;
123   }
124 }
125
126
127 /*
128  * main:
129  *      Our little demo prgoram
130  *********************************************************************************
131  */
132
133 int main (void)
134 {
135   int i ;
136   int ring, leg ;
137
138 // Always initialise wiringPi:
139 //      Use the Sys method if you don't need to run as root
140
141   wiringPiSetupSys () ;
142
143 // Initialise the piGlow devLib with our chosen pin base
144
145   piGlowSetup (PIGLOW_BASE) ;
146
147 // LEDs, one at a time
148
149   printf ("LEDs, one at a time\n") ;
150   for (; !keypressed () ;)
151     for (leg = 0 ; leg < 3 ; ++leg)
152     {
153       for (ring = 0 ; ring < 6 ; ++ring)
154       {
155         pulseLed (leg, ring) ;
156         if (keypressed ())
157           break ;
158       }
159       if (keypressed ())
160         break ;
161     }
162   clearKeypressed () ;
163
164 // Rings, one at a time
165
166   printf ("Rings, one at a time\n") ;
167   for (; !keypressed () ;)
168     for (ring = 0 ; ring < 6 ; ++ring)
169     {
170       pulseRing (ring) ;
171       if (keypressed ())
172         break ;
173     }
174   clearKeypressed () ;
175
176 // Legs, one at a time
177
178   printf ("Legs, one at a time\n") ;
179   for (; !keypressed () ;)
180     for (leg = 0 ; leg < 3 ; ++leg)
181     {
182       pulseLeg (leg) ;
183       if (keypressed ())
184         break ;
185     }
186   clearKeypressed () ;
187
188   delay (1000) ;
189
190 // Sequence - alternating rings, legs and random
191
192   printf ("Sequence now\n") ;
193
194   for (; !keypressed () ;)
195   {
196     for (i = 0 ; i < 20 ; ++i)
197       for (leg = 2 ; leg >= 0 ; --leg)
198       {
199         piGlowLeg (leg, 100) ;
200         delay (100) ;
201         piGlowLeg (leg, 0) ;
202       }
203
204     for (i = 0 ; i < 20 ; ++i)
205       for (ring = 5 ; ring >= 0 ; --ring)
206       {
207         piGlowRing (ring, 100) ;
208         delay (50) ;
209         piGlowRing (ring,   0) ;
210       }
211
212     for (i = 0 ; i < 1000 ; ++i)
213     {
214       leg = random () % 3 ;
215       ring = random () % 6 ;
216       piGlow1 (leg, ring, random () % 256) ;
217       delay (5) ; 
218       piGlow1 (leg, ring, 0) ;
219     }
220   }
221
222   return 0 ;
223 }