test/lpfktest.c

Sun, 08 Apr 2012 22:27:40 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Sun, 08 Apr 2012 22:27:40 +0100
changeset 13
23a3d358dad3
parent 8
fd84ec9e11df
permissions
-rw-r--r--

BUGFIX: lpfktest didn't enable the LPFK before reading keystrokes

Reported-By: Duncan Domingue

     1 #include <stdio.h>
     2 #include <stdbool.h>
     3 #include <time.h>
     4 #include "liblpfk.h"
     6 int main(int argc, char **argv) 
     7 {
     8 	LPFK_CTX	ctx;
     9 	int			i;
    10 	time_t		tm;
    12 	if (argc < 2) {
    13 		printf("Syntax: %s commport\n", argv[0]);
    14 		return -1;
    15 	}
    17 	if ((i = lpfk_open(&ctx, argv[1])) != LPFK_E_OK) {
    18 		switch(i) {
    19 			case LPFK_E_PORT_OPEN:
    20 				printf("Error opening comm port.\n");
    21 				break;
    22 			case LPFK_E_NOT_PRESENT:
    23 				printf("LPFK not connected to specified port.\n");
    24 				break;
    25 			case LPFK_E_COMMS:
    26 				printf("LPFK communications error.\n");
    27 				break;
    28 		}
    29 		return -2;
    30 	}
    32 //	printf("disable: %d\n", lpfk_enable(&ctx, false));
    33 //	printf("enable : %d\n", lpfk_enable(&ctx, true));
    35 	printf("Scanning LEDs, 1-32...\n");
    37 	for (i=0; i<32; i++) {
    38 		if (i>0) {
    39 			lpfk_set_led(&ctx, i-1, false);
    40 		}
    41 		lpfk_set_led(&ctx, i, true);
    42 		usleep(100000);
    43 	}
    45 	// Turn LEDs off and enable LPFK keystroke input
    46 	lpfk_set_leds(&ctx, false);
    47 	lpfk_enable(&ctx, true);
    49 	printf("Now press the keys on the LPFK...\n");
    51 	// scan keys for 5 seconds
    52 	tm = time(NULL);
    53 	do {
    54 		// read keys
    55 		if ((i = lpfk_read(&ctx)) >= 0) {
    56 			// key buffered, toggle the LED
    57 			printf("Key down: #%d\n", i);
    58 			lpfk_set_led(&ctx, i, !lpfk_get_led(&ctx, i));
    59 		}
    60 	} while ((time(NULL) - tm) < 30);
    62 	lpfk_close(&ctx);
    63 }