Thu, 25 Sep 2008 15:29:35 +0100
Added lpfkbinclock test program (from Ethan Dicks, ethan.dicks usap.gov)
Makefile | file | annotate | diff | revisions | |
test/lpfkbinclock.c | file | annotate | diff | revisions |
1.1 diff -r f76c929f65e6 -r bfaaf539bc4d Makefile 1.2 --- a/Makefile Wed Sep 03 17:15:47 2008 +0100 1.3 +++ b/Makefile Thu Sep 25 15:29:35 2008 +0100 1.4 @@ -10,7 +10,7 @@ 1.5 doxygen 1.6 1.7 clean: 1.8 - -rm -f lpfktest liblpfk.so* 1.9 + -rm -f lpfktest lpfklife lpfkbinclock liblpfk.so* 1.10 -rm -f src/*.o test/*.o 1.11 -rm -f src/*~ test/*~ *~ 1.12 1.13 @@ -23,6 +23,9 @@ 1.14 lpfklife: test/lpfklife.o 1.15 $(CC) -o $@ $< -L. -llpfk 1.16 1.17 +lpfkbinclock: test/lpfkbinclock.o 1.18 + $(CC) -o $@ $< -L. -llpfk 1.19 + 1.20 src/liblpfk.o: include/liblpfk.h 1.21 test/lpfktest.o: include/liblpfk.h 1.22 test/lpfklife.o: include/liblpfk.h
2.1 diff -r f76c929f65e6 -r bfaaf539bc4d test/lpfkbinclock.c 2.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 +++ b/test/lpfkbinclock.c Thu Sep 25 15:29:35 2008 +0100 2.4 @@ -0,0 +1,123 @@ 2.5 +/* 2.6 + * binclock.c - simple binary clock using an IBM LPFK as a display device 2.7 + * 2.8 + * Written by Ethan Dicks <ethan.dicks@gmail.com> 2.9 + * 2.10 + * Revision History 2.11 + * 2.12 + * 27-Aug-2008 0.1 Ethan Dicks Initial version 2.13 + * 27 Aug-2008 0.2 Ethan Dicks Corrected serial init bits (thanks to 2.14 + * Phil Pemberton!) and proper logic tests 2.15 + * 08-Sep-2008 0.3 Ethan Dicks Converted to work with Phil Pemberton's 2.16 + * lpfklib. 2.17 + * 2.18 + * 2.19 + * 2.20 + */ 2.21 +#include <stdio.h> 2.22 +#include <stdbool.h> 2.23 +#include <time.h> 2.24 +#include "liblpfk.h" 2.25 + 2.26 +//#define TEST 2.27 + 2.28 +int main(int argc, char **argv) 2.29 +{ 2.30 + LPFK_CTX ctx; 2.31 + int i; 2.32 + 2.33 + /* Bits for masking off digits of time */ 2.34 + int bcdmask[] = { 0x01, 0x02, 0x04, 0x08 }; 2.35 + 2.36 + // strftime stuff 2.37 + static const char *timeFormat = "%H%M%S"; 2.38 + time_t thetime; 2.39 + struct tm *rtime; 2.40 + char timestr[40]; 2.41 + 2.42 + /* vars for converting ASCII time to individual bits */ 2.43 + int timedigit, timebit; 2.44 + int maploc; 2.45 + 2.46 + /* Import our serial port name */ 2.47 + if (argc < 2) { 2.48 + printf("Syntax: %s commport\n", argv[0]); 2.49 + return -1; 2.50 + } 2.51 + 2.52 + /* Open the LPFK library and retain the handle in ctx */ 2.53 +#ifndef TEST 2.54 + if ((i = lpfk_open(&ctx, argv[1])) != LPFK_E_OK) { 2.55 + switch(i) { 2.56 + case LPFK_E_PORT_OPEN: 2.57 + printf("Error opening comm port.\n"); 2.58 + break; 2.59 + case LPFK_E_NOT_PRESENT: 2.60 + printf("LPFK not connected to specified port.\n"); 2.61 + break; 2.62 + case LPFK_E_COMMS: 2.63 + printf("LPFK communications error.\n"); 2.64 + break; 2.65 + } 2.66 + return -2; 2.67 + } 2.68 +#endif /* TEST */ 2.69 + 2.70 + /* Turn LEDs off, just in case they were left on by the last application */ 2.71 +#ifndef TEST 2.72 + lpfk_set_leds(&ctx, false); 2.73 +#endif /* TEST */ 2.74 + 2.75 + // 2.76 + // loop forever while collecting, reformatting and sending the time 2.77 + // 2.78 + while(1) { 2.79 + 2.80 + /* get time */ 2.81 + time(&thetime); 2.82 + rtime = localtime(&thetime); 2.83 + 2.84 + /* reformat time from raw time to BCD HHMMSS to make it easy to parse next */ 2.85 + if (strftime(timestr, sizeof(timestr), timeFormat, rtime) == 0) 2.86 + *timestr = '\0'; 2.87 + 2.88 +#ifdef TEST 2.89 + printf("The time is '%s'\n", timestr); 2.90 +#endif /* TEST */ 2.91 + 2.92 + /* start with seconds and work backwards */ 2.93 + for (timedigit = 5; timedigit > -1; timedigit--) { 2.94 + /* count bits from 0001 to 1000 forwards */ 2.95 + for (timebit = 0; timebit < 4; timebit++) { 2.96 + // ugly! 2.97 + maploc = 5 + 6 * (3 - timebit) + timedigit - 1; 2.98 +#ifndef TEST 2.99 + lpfk_set_led_cached(&ctx, maploc, timestr[timedigit] & bcdmask[timebit]); 2.100 +#endif /* TEST */ 2.101 + 2.102 +#ifdef TEST 2.103 + printf("Turning LED %d to %d\n", maploc, timestr[timedigit] & bcdmask[timebit]); 2.104 +#endif /* TEST */ 2.105 + } 2.106 + } 2.107 + 2.108 + /* Update all the LEDs at once */ 2.109 +#ifndef TEST 2.110 + lpfk_update_leds(&ctx); 2.111 +#endif /* TEST */ 2.112 + 2.113 + /* no need to do this hundreds of times per second */ 2.114 + sleep(1); // make it 1 sec for now; screw with nanosleep() later 2.115 + } 2.116 + 2.117 +} 2.118 + 2.119 + 2.120 +#if 0 2.121 + /* Future cleanup code to be called by a SIGHUP trap */ 2.122 + 2.123 + /* Turn LEDs off and close the port */ 2.124 + lpfk_set_leds(&ctx, false); 2.125 + lpfk_close(&ctx); 2.126 +#endif 2.127 +