test/lpfkbinclock.c

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