Added lpfkbinclock test program (from Ethan Dicks, ethan.dicks usap.gov)

Thu, 25 Sep 2008 15:29:35 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 25 Sep 2008 15:29:35 +0100
changeset 11
bfaaf539bc4d
parent 10
f76c929f65e6
child 12
f8996e336619

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