Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / examples / PiGlow / piGlow1.c @ 32:cadb9025f1e0

History | View | Annotate | Download (5.18 KB)

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
#ifndef        TRUE
36
#  define TRUE  (1==1)
37
#  define FALSE (!TRUE)
38
#endif
39

    
40

    
41
/*
42
 * keypressed: clearKeypressed:
43
 *        Simple but effective ways to tell if the enter key has been pressed
44
 *********************************************************************************
45
 */
46

    
47
static int keypressed (void)
48
{
49
  struct pollfd polls ;
50

    
51
  polls.fd     = fileno (stdin) ;
52
  polls.events = POLLIN ;
53

    
54
  return poll (&polls, 1, 0) != 0 ;
55
}
56

    
57
static void clearKeypressed (void)
58
{
59
  while (keypressed ())
60
    (void)getchar () ;
61
}
62

    
63

    
64
/*
65
 * pulseLed:
66
 *        Pulses the LED at position leg, ring from off to a max. value,
67
 *        then off again
68
 *********************************************************************************
69
 */
70

    
71
static void pulseLed (int leg, int ring)
72
{
73
  int i ;
74

    
75
  for (i = 0 ; i < 140 ; ++i)
76
  {
77
    piGlow1 (leg, ring, i) ;
78
    delay (1) ;
79
  }
80
  delay (10) ;
81
  for (i = 140 ; i >= 0 ; --i)
82
  {
83
    piGlow1 (leg, ring, i) ;
84
    delay (1) ;
85
  }
86
}
87

    
88
/*
89
 * pulseLeg:
90
 *        Same as above, but a whole leg at a time
91
 *********************************************************************************
92
 */
93

    
94
static void pulseLeg (int leg)
95
{
96
  int i ;
97

    
98
  for (i = 0 ; i < 140 ; ++i)
99
  {
100
    piGlowLeg (leg, i) ; delay (1) ;
101
  }
102
  delay (10) ;
103
  for (i = 140 ; i >= 0 ; --i)
104
  {
105
    piGlowLeg (leg, i) ; delay (1) ;
106
  }
107
}
108

    
109

    
110
/*
111
 * pulse Ring:
112
 *        Same as above, but a whole ring at a time
113
 *********************************************************************************
114
 */
115

    
116
static void pulseRing (int ring)
117
{
118
  int i ;
119

    
120
  for (i = 0 ; i < 140 ; ++i)
121
  {
122
    piGlowRing (ring, i) ; delay (1) ;
123
  }
124
  delay (10) ;
125
  for (i = 140 ; i >= 0 ; --i)
126
  {
127
    piGlowRing (ring, i) ; delay (1) ;
128
  }
129
}
130

    
131
#define        LEG_STEPS        3
132

    
133
static int legSequence [] =
134
{
135
   4, 12, 99,
136
  99,  4, 12, 
137
  12, 99,  4,
138
} ;
139
  
140

    
141
#define        RING_STEPS        16
142

    
143
static int ringSequence [] =
144
{
145
   0,  0,  0,  0,  0, 64,
146
   0,  0,  0,  0, 64, 64,
147
   0,  0,  0, 64, 64,  0,
148
   0,  0, 64, 64,  0,  0,
149
   0, 64, 64,  0,  0,  0,
150
  64, 64,  0,  0,  0,  0,
151
  64,  0,  0,  0,  0,  0,
152
   0,  0,  0,  0,  0,  0,
153
  64,  0,  0,  0,  0,  0,
154
  64, 64,  0,  0,  0,  0,
155
   0, 64, 64,  0,  0,  0,
156
   0,  0, 64, 64,  0,  0,
157
   0,  0,  0, 64, 64,  0,
158
   0,  0,  0,  0, 64, 64,
159
   0,  0,  0,  0,  0, 64,
160
   0,  0,  0,  0,  0,  0,
161
} ;
162

    
163
/*
164
 * main:
165
 *        Our little demo prgoram
166
 *********************************************************************************
167
 */
168

    
169
int main (void)
170
{
171
  int i ;
172
  int step, ring, leg ;
173

    
174
// Always initialise wiringPi:
175
//        Use the Sys method if you don't need to run as root
176

    
177
  wiringPiSetupSys () ;
178

    
179
// Initialise the piGlow devLib with our chosen pin base
180

    
181
  piGlowSetup (1) ;
182

    
183
// LEDs, one at a time
184

    
185
  printf ("LEDs, one at a time\n") ;
186
  for (; !keypressed () ;)
187
    for (leg = 0 ; leg < 3 ; ++leg)
188
    {
189
      for (ring = 0 ; ring < 6 ; ++ring)
190
      {
191
        pulseLed (leg, ring) ;
192
        if (keypressed ())
193
          break ;
194
      }
195
      if (keypressed ())
196
        break ;
197
    }
198
  clearKeypressed () ;
199

    
200
// Rings, one at a time
201

    
202
  printf ("Rings, one at a time\n") ;
203
  for (; !keypressed () ;)
204
    for (ring = 0 ; ring < 6 ; ++ring)
205
    {
206
      pulseRing (ring) ;
207
      if (keypressed ())
208
        break ;
209
    }
210
  clearKeypressed () ;
211

    
212
// Legs, one at a time
213

    
214
  printf ("Legs, one at a time\n") ;
215
  for (; !keypressed () ;)
216
    for (leg = 0 ; leg < 3 ; ++leg)
217
    {
218
      pulseLeg (leg) ;
219
      if (keypressed ())
220
        break ;
221
    }
222
  clearKeypressed () ;
223

    
224
  delay (1000) ;
225

    
226
// Sequence - alternating rings, legs and random
227

    
228
  printf ("Sequence now\n") ;
229
  for (; !keypressed () ;)
230
  {
231
    for (i = 0 ; i < 20 ; ++i)
232
      for (step = 0 ; step < LEG_STEPS ; ++step)
233
      {
234
        for (leg = 0 ; leg < 3 ; ++leg)
235
          piGlowLeg (leg, legSequence [step * 3 + leg]) ;
236
        delay (80) ;
237
      }
238

    
239
    for (i = 0 ; i < 10 ; ++i)
240
      for (step = 0 ; step < RING_STEPS ; ++step)
241
      {
242
        for (ring = 0 ; ring < 6 ; ++ring)
243
          piGlowRing (ring, ringSequence [step * 6 + ring]) ;
244
        delay (80) ;
245
      }
246

    
247
    for (i = 0 ; i < 1000 ; ++i)
248
    {
249
      leg  = random () % 3 ;
250
      ring = random () % 6 ;
251
      piGlow1 (leg, ring, random () % 256) ;
252
      delay (5) ; 
253
      piGlow1 (leg, ring, 0) ;
254
    }
255
  }
256

    
257
  return 0 ;
258
}