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
6 * Copyright (c) 2013 Gordon Henderson
7 ***********************************************************************
35 static int digits [6] = { 7, 11, 10, 13, 12, 14 } ;
36 static int segments [7] = { 6, 5, 4, 3, 2, 1, 0 } ;
39 static const int segmentDigits [] =
41 // a b c d e f g Segments
42 // 6 5 4 3 2 1 0, // wiringPi pin No.
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
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.
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 *********************************************************************************
80 PI_THREAD (displayDigits)
83 int index, d, segVal ;
89 for (digit = 0 ; digit < 6 ; ++digit)
91 for (segment = 0 ; segment < 7 ; ++segment)
93 d = toupper (display [digit]) ;
94 /**/ if ((d >= '0') && (d <= '9')) // Digit
96 else if ((d >= 'A') && (d <= 'F')) // Hex
97 index = d - 'A' + 10 ;
101 segVal = segmentDigits [index * 7 + segment] ;
103 digitalWrite (segments [segment], segVal) ;
105 digitalWrite (digits [digit], 1) ;
107 digitalWrite (digits [digit], 0) ;
115 * Initialise the hardware and start the thread
116 *********************************************************************************
127 for (i = 0 ; i < 7 ; ++i)
128 { digitalWrite (segments [i], 0) ; pinMode (segments [i], OUTPUT) ; }
132 for (i = 0 ; i < 6 ; ++i)
133 { digitalWrite (digits [i], 0) ; pinMode (digits [i], OUTPUT) ; }
135 strcpy (display, " ") ;
136 piThreadCreate (displayDigits) ;
137 delay (10) ; // Just to make sure it's started
139 // Quick countdown LED test sort of thing
142 for (i = 0 ; i < 10 ; ++i)
144 sprintf (display, "%06d", c) ;
149 strcpy (display, " ") ;
153 sprintf (display, "%s", "123456") ;
163 * No explanation needed. (Nor one given!)
164 *********************************************************************************
169 char *message = " feedbeef babe cafe b00b " ;
172 for (i = 0 ; i < strlen (message) - 4 ; ++i)
174 strncpy (display, &message [i], 6) ;
178 for (i = 0 ; i < 3 ; ++i)
180 strcpy (display, " ") ;
182 strcpy (display, " b00b ") ;
186 strcpy (display, " ") ;
192 *********************************************************************************
195 *********************************************************************************
209 while (time (NULL) == tim)
213 t = localtime (&tim) ;
215 sprintf (display, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec) ;