Wed, 28 Jul 2010 15:14:54 +0100
Fixed omission: lpfkbinclock wasn't compiled by default.
1 /*
2 * binclock.c - simple binary clock using an IBM LPFK as a display device
3 *
4 * Written by Ethan Dicks <ethan.dicks@gmail.com>
5 *
6 * Revision History
7 *
8 * 27-Aug-2008 0.1 Ethan Dicks Initial version
9 * 27 Aug-2008 0.2 Ethan Dicks Corrected serial init bits (thanks to
10 * Phil Pemberton!) and proper logic tests
11 * 08-Sep-2008 0.3 Ethan Dicks Converted to work with Phil Pemberton's
12 * lpfklib.
13 *
14 *
15 *
16 */
17 #include <stdio.h>
18 #include <stdbool.h>
19 #include <time.h>
20 #include "liblpfk.h"
22 //#define TEST
24 int main(int argc, char **argv)
25 {
26 LPFK_CTX ctx;
27 int i;
29 /* Bits for masking off digits of time */
30 int bcdmask[] = { 0x01, 0x02, 0x04, 0x08 };
32 // strftime stuff
33 static const char *timeFormat = "%H%M%S";
34 time_t thetime;
35 struct tm *rtime;
36 char timestr[40];
38 /* vars for converting ASCII time to individual bits */
39 int timedigit, timebit;
40 int maploc;
42 /* Import our serial port name */
43 if (argc < 2) {
44 printf("Syntax: %s commport\n", argv[0]);
45 return -1;
46 }
48 /* Open the LPFK library and retain the handle in ctx */
49 #ifndef TEST
50 if ((i = lpfk_open(&ctx, argv[1])) != LPFK_E_OK) {
51 switch(i) {
52 case LPFK_E_PORT_OPEN:
53 printf("Error opening comm port.\n");
54 break;
55 case LPFK_E_NOT_PRESENT:
56 printf("LPFK not connected to specified port.\n");
57 break;
58 case LPFK_E_COMMS:
59 printf("LPFK communications error.\n");
60 break;
61 }
62 return -2;
63 }
64 #endif /* TEST */
66 /* Turn LEDs off, just in case they were left on by the last application */
67 #ifndef TEST
68 lpfk_set_leds(&ctx, false);
69 #endif /* TEST */
71 //
72 // loop forever while collecting, reformatting and sending the time
73 //
74 while(1) {
76 /* get time */
77 time(&thetime);
78 rtime = localtime(&thetime);
80 /* reformat time from raw time to BCD HHMMSS to make it easy to parse next */
81 if (strftime(timestr, sizeof(timestr), timeFormat, rtime) == 0)
82 *timestr = '\0';
84 #ifdef TEST
85 printf("The time is '%s'\n", timestr);
86 #endif /* TEST */
88 /* start with seconds and work backwards */
89 for (timedigit = 5; timedigit > -1; timedigit--) {
90 /* count bits from 0001 to 1000 forwards */
91 for (timebit = 0; timebit < 4; timebit++) {
92 // ugly!
93 maploc = 5 + 6 * (3 - timebit) + timedigit - 1;
94 #ifndef TEST
95 lpfk_set_led_cached(&ctx, maploc, timestr[timedigit] & bcdmask[timebit]);
96 #endif /* TEST */
98 #ifdef TEST
99 printf("Turning LED %d to %d\n", maploc, timestr[timedigit] & bcdmask[timebit]);
100 #endif /* TEST */
101 }
102 }
104 /* Update all the LEDs at once */
105 #ifndef TEST
106 lpfk_update_leds(&ctx);
107 #endif /* TEST */
109 /* no need to do this hundreds of times per second */
110 sleep(1); // make it 1 sec for now; screw with nanosleep() later
111 }
113 }
116 #if 0
117 /* Future cleanup code to be called by a SIGHUP trap */
119 /* Turn LEDs off and close the port */
120 lpfk_set_leds(&ctx, false);
121 lpfk_close(&ctx);
122 #endif