chiark / gitweb /
4c75711f16b8d99e590397080c393c3f4e97e6bd
[wiringPi.git] / examples / test1.c
1 /*
2  * test1.c:
3  *      Simple test program to test the wiringPi functions
4  *      This is a sequencer to make a patter appear on 8 LEDs
5  *      connected to the GPIO pins.
6  *
7  * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
8  ***********************************************************************
9  * This file is part of wiringPi:
10  *      https://projects.drogon.net/raspberry-pi/wiringpi/
11  *
12  *    wiringPi is free software: you can redistribute it and/or modify
13  *    it under the terms of the GNU Lesser General Public License as published by
14  *    the Free Software Foundation, either version 3 of the License, or
15  *    (at your option) any later version.
16  *
17  *    wiringPi is distributed in the hope that it will be useful,
18  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *    GNU Lesser General Public License for more details.
21  *
22  *    You should have received a copy of the GNU Lesser General Public License
23  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
24  ***********************************************************************
25  */
26
27 #include <wiringPi.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32
33
34 // Simple sequencer data
35 //      Triplets of LED, On/Off and delay
36
37 uint8_t data [] =
38 {
39             0, 1, 1,
40             1, 1, 1,
41   0, 0, 0,  2, 1, 1,
42   1, 0, 0,  3, 1, 1,
43   2, 0, 0,  4, 1, 1,
44   3, 0, 0,  5, 1, 1,
45   4, 0, 0,  6, 1, 1,
46   5, 0, 0,  7, 1, 1,
47   6, 0, 1,
48   7, 0, 1,
49
50   0, 0, 1,      // Extra delay
51
52 // Back again
53
54             7, 1, 1,
55             6, 1, 1,
56   7, 0, 0,  5, 1, 1,
57   6, 0, 0,  4, 1, 1,
58   5, 0, 0,  3, 1, 1,
59   4, 0, 0,  2, 1, 1,
60   3, 0, 0,  1, 1, 1,
61   2, 0, 0,  0, 1, 1,
62   1, 0, 1,
63   0, 0, 1,
64
65   0, 0, 1,      // Extra delay
66
67   9, 9, 9,      // End marker
68
69 } ;
70
71
72 int main (void)
73 {
74   int pin ;
75   int dataPtr ;
76   int l, s, d ;
77
78   printf ("Raspberry Pi wiringPi test program\n") ;
79
80   if (wiringPiSetup () == -1)
81     exit (1) ;
82
83   for (pin = 0 ; pin < 8 ; ++pin)
84     pinMode (pin, OUTPUT) ;
85
86   pinMode (8, INPUT) ;  // Pin 8 SDA0 - Has on-board 2k2 pull-up resistor
87
88   dataPtr = 0 ;
89
90   for (;;)
91   {
92     l = data [dataPtr++] ;      // LED
93     s = data [dataPtr++] ;      // State
94     d = data [dataPtr++] ;      // Duration (10ths)
95
96     if ((l + s + d) == 27)
97     {
98       dataPtr = 0 ;
99       continue ;
100     }
101
102     digitalWrite (l, s) ;
103
104     if (digitalRead (8) == 0)   // Pressed as our switch shorts to ground
105       delay (d * 10) ;  // Faster!
106     else
107       delay (d * 100) ;
108   }
109
110   return 0 ;
111 }