chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / Gertboard / 7segments.c
1 /*
2  * 7segments.c:
3  *      Simple test program to see if we can drive a 7-segment LED
4  *      display using the GPIO and little else on the Raspberry Pi
5  *
6  *      Copyright (c) 2013 Gordon Henderson
7  ***********************************************************************
8  */
9
10 #undef  PHOTO_HACK
11
12 #include <wiringPi.h>
13
14 #include <stdio.h>
15 #include <time.h>
16 #include <ctype.h>
17 #include <string.h>
18
19 /*
20  *  Segment mapping
21  *
22  *       --a--
23  *      |     |
24  *      f     b
25  *      |     |
26  *       --g--
27  *      |     |
28  *      e     c
29  *      |     |
30  *       --d--  p
31  */
32
33 // GPIO Pin Mapping
34
35 static int digits   [6] = {  7, 11, 10, 13, 12, 14    } ;
36 static int segments [7] = {  6,  5,  4,  3,  2,  1, 0 } ;
37
38
39 static const int segmentDigits [] =
40 {
41 // a  b  c  d  e  f  g     Segments
42 // 6  5  4  3  2  1  0, // wiringPi pin No.
43
44    1, 1, 1, 1, 1, 1, 0, // 0
45    0, 1, 1, 0, 0, 0, 0, // 1
46    1, 1, 0, 1, 1, 0, 1, // 2
47    1, 1, 1, 1, 0, 0, 1, // 3
48    0, 1, 1, 0, 0, 1, 1, // 4
49    1, 0, 1, 1, 0, 1, 1, // 5
50    1, 0, 1, 1, 1, 1, 1, // 6
51    1, 1, 1, 0, 0, 0, 0, // 7
52    1, 1, 1, 1, 1, 1, 1, // 8
53    1, 1, 1, 1, 0, 1, 1, // 9
54    1, 1, 1, 0, 1, 1, 1, // A
55    0, 0, 1, 1, 1, 1, 1, // b
56    1, 0, 0, 1, 1, 1, 0, // C
57    0, 1, 1, 1, 1, 0, 1, // d
58    1, 0, 0, 1, 1, 1, 1, // E
59    1, 0, 0, 0, 1, 1, 1, // F
60    0, 0, 0, 0, 0, 0, 0, // blank
61 } ;
62  
63
64 // display:
65 //      A global variable which is written to by the main program and
66 //      read from by the thread that updates the display. Only the first
67 //      6 characters are used.
68
69 char display [8] ;
70
71
72 /*
73  * displayDigits:
74  *      This is our thread that's run concurrently with the main program.
75  *      Essentially sit in a loop, parsing and displaying the data held in
76  *      the "display" global.
77  *********************************************************************************
78  */
79
80 PI_THREAD (displayDigits)
81 {
82   int digit, segment ;
83   int index, d, segVal ;
84
85   piHiPri (50) ;
86
87   for (;;)
88   {
89     for (digit = 0 ; digit < 6 ; ++digit)
90     {
91       for (segment = 0 ; segment < 7 ; ++segment)
92       {
93         d = toupper (display [digit]) ;
94         /**/ if ((d >= '0') && (d <= '9'))      // Digit
95           index = d - '0' ;
96         else if ((d >= 'A') && (d <= 'F'))      // Hex
97           index = d - 'A' + 10 ;
98         else
99           index = 16 ;                          // Blank
100
101         segVal = segmentDigits [index * 7 + segment] ;
102
103         digitalWrite (segments [segment], segVal) ;
104       }
105       digitalWrite (digits [digit], 1) ;
106       delay (2) ;
107       digitalWrite (digits [digit], 0) ;
108     }
109   }
110 }
111
112
113 /*
114  * setup:
115  *      Initialise the hardware and start the thread
116  *********************************************************************************
117  */
118
119 void setup (void)
120 {
121   int i, c ;
122
123   wiringPiSetup () ;
124
125 // 7 segments
126
127   for (i = 0 ; i < 7 ; ++i)
128     { digitalWrite (segments [i], 0) ; pinMode (segments [i], OUTPUT) ; }
129
130 // 6 digits
131
132   for (i = 0 ; i < 6 ; ++i)
133     { digitalWrite (digits [i], 0) ;   pinMode (digits [i],   OUTPUT) ; }
134
135   strcpy (display, "      ") ;
136   piThreadCreate (displayDigits) ;
137   delay (10) ; // Just to make sure it's started
138
139 // Quick countdown LED test sort of thing
140
141   c = 999999 ;
142   for (i = 0 ; i < 10 ; ++i)
143   {
144     sprintf (display, "%06d", c) ;
145     delay (400) ;
146     c -= 111111 ;
147   }
148
149   strcpy (display, "      ") ;
150   delay (400) ;
151
152 #ifdef PHOTO_HACK
153   sprintf (display, "%s", "123456") ;
154   for (;;)
155     delay (1000) ;
156 #endif
157
158 }
159
160
161 /*
162  * teenager:
163  *      No explanation needed. (Nor one given!)
164  *********************************************************************************
165  */
166
167 void teenager (void)
168 {
169   char *message = "      feedbeef      babe      cafe      b00b      " ;
170   int i ;
171
172   for (i = 0 ; i < strlen (message) - 4 ; ++i)
173   {
174     strncpy (display, &message [i], 6) ;
175     delay (200) ;
176   }
177   delay (1000) ;
178   for (i = 0 ; i < 3 ; ++i)
179   {
180     strcpy (display, "    ") ;
181     delay (150) ;
182     strcpy (display, " b00b ") ;
183     delay (250) ;
184   }
185   delay (1000) ;
186   strcpy (display, "      ") ;
187   delay (1000) ;
188 }
189
190
191 /*
192  *********************************************************************************
193  * main:
194  *      Let the fun begin
195  *********************************************************************************
196  */
197
198 int main (void)
199 {
200   struct tm *t ;
201   time_t     tim ;
202
203   setup    () ;
204   teenager () ;
205
206   tim = time (NULL) ;
207   for (;;)
208   {
209     while (time (NULL) == tim)
210       delay (5) ;
211
212     tim = time (NULL) ;
213     t   = localtime (&tim) ;
214
215     sprintf (display, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
216
217     delay (500) ;
218   }
219
220   return 0 ;
221 }