1.1 diff -r f76c929f65e6 -r bfaaf539bc4d test/lpfkbinclock.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/test/lpfkbinclock.c Thu Sep 25 15:29:35 2008 +0100 1.4 @@ -0,0 +1,123 @@ 1.5 +/* 1.6 + * binclock.c - simple binary clock using an IBM LPFK as a display device 1.7 + * 1.8 + * Written by Ethan Dicks <ethan.dicks@gmail.com> 1.9 + * 1.10 + * Revision History 1.11 + * 1.12 + * 27-Aug-2008 0.1 Ethan Dicks Initial version 1.13 + * 27 Aug-2008 0.2 Ethan Dicks Corrected serial init bits (thanks to 1.14 + * Phil Pemberton!) and proper logic tests 1.15 + * 08-Sep-2008 0.3 Ethan Dicks Converted to work with Phil Pemberton's 1.16 + * lpfklib. 1.17 + * 1.18 + * 1.19 + * 1.20 + */ 1.21 +#include <stdio.h> 1.22 +#include <stdbool.h> 1.23 +#include <time.h> 1.24 +#include "liblpfk.h" 1.25 + 1.26 +//#define TEST 1.27 + 1.28 +int main(int argc, char **argv) 1.29 +{ 1.30 + LPFK_CTX ctx; 1.31 + int i; 1.32 + 1.33 + /* Bits for masking off digits of time */ 1.34 + int bcdmask[] = { 0x01, 0x02, 0x04, 0x08 }; 1.35 + 1.36 + // strftime stuff 1.37 + static const char *timeFormat = "%H%M%S"; 1.38 + time_t thetime; 1.39 + struct tm *rtime; 1.40 + char timestr[40]; 1.41 + 1.42 + /* vars for converting ASCII time to individual bits */ 1.43 + int timedigit, timebit; 1.44 + int maploc; 1.45 + 1.46 + /* Import our serial port name */ 1.47 + if (argc < 2) { 1.48 + printf("Syntax: %s commport\n", argv[0]); 1.49 + return -1; 1.50 + } 1.51 + 1.52 + /* Open the LPFK library and retain the handle in ctx */ 1.53 +#ifndef TEST 1.54 + if ((i = lpfk_open(&ctx, argv[1])) != LPFK_E_OK) { 1.55 + switch(i) { 1.56 + case LPFK_E_PORT_OPEN: 1.57 + printf("Error opening comm port.\n"); 1.58 + break; 1.59 + case LPFK_E_NOT_PRESENT: 1.60 + printf("LPFK not connected to specified port.\n"); 1.61 + break; 1.62 + case LPFK_E_COMMS: 1.63 + printf("LPFK communications error.\n"); 1.64 + break; 1.65 + } 1.66 + return -2; 1.67 + } 1.68 +#endif /* TEST */ 1.69 + 1.70 + /* Turn LEDs off, just in case they were left on by the last application */ 1.71 +#ifndef TEST 1.72 + lpfk_set_leds(&ctx, false); 1.73 +#endif /* TEST */ 1.74 + 1.75 + // 1.76 + // loop forever while collecting, reformatting and sending the time 1.77 + // 1.78 + while(1) { 1.79 + 1.80 + /* get time */ 1.81 + time(&thetime); 1.82 + rtime = localtime(&thetime); 1.83 + 1.84 + /* reformat time from raw time to BCD HHMMSS to make it easy to parse next */ 1.85 + if (strftime(timestr, sizeof(timestr), timeFormat, rtime) == 0) 1.86 + *timestr = '\0'; 1.87 + 1.88 +#ifdef TEST 1.89 + printf("The time is '%s'\n", timestr); 1.90 +#endif /* TEST */ 1.91 + 1.92 + /* start with seconds and work backwards */ 1.93 + for (timedigit = 5; timedigit > -1; timedigit--) { 1.94 + /* count bits from 0001 to 1000 forwards */ 1.95 + for (timebit = 0; timebit < 4; timebit++) { 1.96 + // ugly! 1.97 + maploc = 5 + 6 * (3 - timebit) + timedigit - 1; 1.98 +#ifndef TEST 1.99 + lpfk_set_led_cached(&ctx, maploc, timestr[timedigit] & bcdmask[timebit]); 1.100 +#endif /* TEST */ 1.101 + 1.102 +#ifdef TEST 1.103 + printf("Turning LED %d to %d\n", maploc, timestr[timedigit] & bcdmask[timebit]); 1.104 +#endif /* TEST */ 1.105 + } 1.106 + } 1.107 + 1.108 + /* Update all the LEDs at once */ 1.109 +#ifndef TEST 1.110 + lpfk_update_leds(&ctx); 1.111 +#endif /* TEST */ 1.112 + 1.113 + /* no need to do this hundreds of times per second */ 1.114 + sleep(1); // make it 1 sec for now; screw with nanosleep() later 1.115 + } 1.116 + 1.117 +} 1.118 + 1.119 + 1.120 +#if 0 1.121 + /* Future cleanup code to be called by a SIGHUP trap */ 1.122 + 1.123 + /* Turn LEDs off and close the port */ 1.124 + lpfk_set_leds(&ctx, false); 1.125 + lpfk_close(&ctx); 1.126 +#endif 1.127 +