test/lpfkbinclock.c

Wed, 28 Jul 2010 15:14:54 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 28 Jul 2010 15:14:54 +0100
changeset 12
f8996e336619
parent 11
bfaaf539bc4d
permissions
-rw-r--r--

Fixed omission: lpfkbinclock wasn't compiled by default.

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