src/liblpfk.c

changeset 0
745037d69d81
child 1
3e775df109e6
     1.1 diff -r 000000000000 -r 745037d69d81 src/liblpfk.c
     1.2 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 +++ b/src/liblpfk.c	Tue Aug 26 12:45:33 2008 +0100
     1.4 @@ -0,0 +1,399 @@
     1.5 +/****************************************************************************
     1.6 + * Project:		liblpfk
     1.7 + * Purpose:		Driver library for the IBM 6094-020 Lighted Program Function
     1.8 + * 				Keyboard.
     1.9 + * Version:		1.0
    1.10 + * Author:		Philip Pemberton <philpem@philpem.me.uk>
    1.11 + *
    1.12 + * The latest version of this library is available from
    1.13 + * <http://www.philpem.me.uk/code/liblpfk/>.
    1.14 + *
    1.15 + * Copyright (c) 2008, Philip Pemberton
    1.16 + * All rights reserved.
    1.17 + *
    1.18 + * Redistribution and use in source and binary forms, with or without
    1.19 + * modification, are permitted provided that the following conditions
    1.20 + * are met:
    1.21 + *
    1.22 + *  * Redistributions of source code must retain the above copyright
    1.23 + *    notice, this list of conditions and the following disclaimer.
    1.24 + *  * Redistributions in binary form must reproduce the above copyright
    1.25 + *    notice, this list of conditions and the following disclaimer in
    1.26 + *    the documentation and/or other materials provided with the
    1.27 + *    distribution.
    1.28 + *  * Neither the name of the project nor the names of its
    1.29 + *    contributors may be used to endorse or promote products derived
    1.30 + *    from this software without specific prior written permission.
    1.31 + *
    1.32 + *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.33 + *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.34 + *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.35 + *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    1.36 + *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.37 + *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    1.38 + *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
    1.39 + *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    1.40 + *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    1.41 + *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
    1.42 + *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.43 + ****************************************************************************/
    1.44 +
    1.45 +/**
    1.46 + * @file	liblpfk.c
    1.47 + * @brief	liblpfk library, main source
    1.48 + */
    1.49 +
    1.50 +#include <sys/types.h>
    1.51 +#include <sys/stat.h>
    1.52 +#include <fcntl.h>
    1.53 +#include <unistd.h>
    1.54 +#include <termios.h>
    1.55 +#include <sys/ioctl.h>
    1.56 +#include <time.h>
    1.57 +#include <string.h>
    1.58 +#include <stdbool.h>
    1.59 +#include <stdio.h>
    1.60 +
    1.61 +#include "liblpfk.h"
    1.62 +
    1.63 +int lpfk_open(const char *port, LPFK_CTX *ctx)
    1.64 +{
    1.65 +	struct termios newtio;
    1.66 +	int status;
    1.67 +	int fd;
    1.68 +	int i;
    1.69 +
    1.70 +	// open the serial port
    1.71 +	fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
    1.72 +	if (fd < 0) return LPFK_E_PORT_OPEN;
    1.73 +
    1.74 +	// save current port settings
    1.75 +	tcgetattr(fd, &ctx->oldtio);
    1.76 +
    1.77 +	// set up new parameters
    1.78 +	memset(&newtio, 0, sizeof(newtio));
    1.79 +	// 9600 baud, 8 bits, parity enabled, odd parity
    1.80 +	newtio.c_cflag = B9600 | CS8 | PARENB | PARODD | CLOCAL | CREAD;
    1.81 +	newtio.c_iflag = 0;
    1.82 +	newtio.c_oflag = 0;
    1.83 +
    1.84 +	// set input mode -- non canonical, no echo
    1.85 +	newtio.c_lflag = 0;
    1.86 +
    1.87 +	// inter-character timer unused
    1.88 +	newtio.c_cc[VTIME] = 0;
    1.89 +	// read does not block waiting for characters if there are none in the buffer
    1.90 +	newtio.c_cc[VMIN]  = 0;
    1.91 +
    1.92 +	// flush input buffer
    1.93 +	tcflush(fd, TCIFLUSH);
    1.94 +
    1.95 +	// set new port config
    1.96 +	tcsetattr(fd, TCSANOW, &newtio);
    1.97 +
    1.98 +	// set RTS true to pull the LPFK out of reset
    1.99 +	ioctl(fd, TIOCMGET, &status);
   1.100 +	status |= TIOCM_RTS;
   1.101 +	ioctl(fd, TIOCMSET, &status);
   1.102 +
   1.103 +	// wait a few seconds for the LPFK to become ready
   1.104 +	sleep(2);
   1.105 +
   1.106 +	// 0x06: READ CONFIGURATION. LPFK sends 0x03 in response.
   1.107 +	// Try five times to wake it up.
   1.108 +	status = false;
   1.109 +	for (i=0; i<5; i++) {
   1.110 +		unsigned char buf;
   1.111 +		time_t tm;
   1.112 +
   1.113 +		// Send 0x06: READ CONFIGURATION, loop on failure
   1.114 +		if (write(fd, "\x06", 1) < 1) {
   1.115 +			continue;
   1.116 +		}
   1.117 +
   1.118 +		// save current time (in seconds)
   1.119 +		tm = time(NULL);
   1.120 +
   1.121 +		// loop until 2 seconds have passed, or LPFK responds
   1.122 +		status = false;
   1.123 +		do {
   1.124 +			// read data, loop if not successful
   1.125 +			if (read(fd, &buf, 1) < 1) {
   1.126 +				continue;
   1.127 +			}
   1.128 +
   1.129 +			// we got some data, what is it?
   1.130 +			if (buf == 0x03) {
   1.131 +				// 0x03 -- correct response. we're done.
   1.132 +				status = true;
   1.133 +			}
   1.134 +		} while (((time(NULL) - tm) < 2) && (!status));
   1.135 +
   1.136 +		// exit loop if we got the LPFK to talk
   1.137 +		if (status) {
   1.138 +			break;
   1.139 +		}
   1.140 +	}
   1.141 +
   1.142 +	// Did the LPFK respond?
   1.143 +	if (!status) {
   1.144 +		// LPFK isn't talking. Restore serial port state and exit.
   1.145 +		tcsetattr(fd, TCSANOW, &ctx->oldtio);
   1.146 +		close(fd);
   1.147 +		
   1.148 +		return LPFK_E_NOT_PRESENT;
   1.149 +	} else {
   1.150 +		// Initialise LPFK context
   1.151 +		ctx->led_mask = 0;
   1.152 +		ctx->fd = fd;
   1.153 +
   1.154 +		// Enable the LPFK
   1.155 +		write(fd, "\x08", 1);
   1.156 +		ctx->enabled = true;
   1.157 +
   1.158 +		// Return OK status
   1.159 +		return LPFK_E_OK;
   1.160 +	}
   1.161 +}
   1.162 +
   1.163 +int lpfk_close(LPFK_CTX *ctx)
   1.164 +{
   1.165 +	int status;
   1.166 +
   1.167 +	// 0x09: DISABLE. Stop the LPFK responding to keystrokes.
   1.168 +	write(ctx->fd, "\x09", 1);
   1.169 +
   1.170 +	// turn all the LEDs off
   1.171 +	lpfk_set_leds(ctx, false);
   1.172 +
   1.173 +	// set RTS false to put the LPFK into reset
   1.174 +	ioctl(ctx->fd, TIOCMGET, &status);
   1.175 +	status &= ~TIOCM_RTS;
   1.176 +	ioctl(ctx->fd, TIOCMSET, &status);
   1.177 +
   1.178 +	// Restore the port state and close the serial port.
   1.179 +	tcsetattr(ctx->fd, TCSANOW, &ctx->oldtio);
   1.180 +	close(ctx->fd);
   1.181 +
   1.182 +	// Done!
   1.183 +	return LPFK_E_OK;
   1.184 +}
   1.185 +
   1.186 +int lpfk_enable(LPFK_CTX *ctx, int val)
   1.187 +{
   1.188 +	if (val) {
   1.189 +		// val == true, enable the LPFK
   1.190 +		if (write(ctx->fd, "\x08", 1) != 1) {
   1.191 +			ctx->enabled = true;
   1.192 +			return LPFK_E_COMMS;
   1.193 +		}
   1.194 +	} else {
   1.195 +		// val == false, disable the LPFK
   1.196 +		if (write(ctx->fd, "\x09", 1) != 1) {
   1.197 +			return LPFK_E_COMMS;
   1.198 +		}
   1.199 +	}
   1.200 +
   1.201 +	// update the context, return success
   1.202 +	ctx->enabled = val;
   1.203 +	return LPFK_E_OK;
   1.204 +}
   1.205 +
   1.206 +/**
   1.207 + * @brief	Set or clear an LED on the LPFK.
   1.208 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.209 + * @param	num		LED/key number, from 0 to 31.
   1.210 + * @param	state	State, true for on, false for off.
   1.211 + * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
   1.212 + * 			on comms error.
   1.213 + */
   1.214 +int lpfk_set_led(LPFK_CTX *ctx, const int num, const int state)
   1.215 +{
   1.216 +	int i;
   1.217 +	time_t tm;
   1.218 +	unsigned long mask, leds;
   1.219 +	unsigned char buf[5];
   1.220 +	unsigned char status;
   1.221 +
   1.222 +	// check parameters
   1.223 +	if ((num < 0) || (num > 31)) {
   1.224 +		return LPFK_E_PARAM;
   1.225 +	}
   1.226 +
   1.227 +	// parameters OK, now build the LED mask
   1.228 +	mask = (0x80 >> (num % 8)) << ((num / 8) * 8);
   1.229 +
   1.230 +	leds = ctx->led_mask;
   1.231 +
   1.232 +	// mask the specified bit
   1.233 +	if (state) {
   1.234 +		leds |= mask;
   1.235 +	} else {
   1.236 +		leds &= ~mask;
   1.237 +	}
   1.238 +
   1.239 +	// send new LED mask to the LPFK
   1.240 +	buf[0] = 0x94;
   1.241 +	buf[1] = leds & 0xff;
   1.242 +	buf[2] = leds >> 8;
   1.243 +	buf[3] = leds >> 16;
   1.244 +	buf[4] = leds >> 24;
   1.245 +
   1.246 +	// make 5 attempts to set the LEDs
   1.247 +	for (i=0; i<5; i++) {
   1.248 +		if (write(ctx->fd, &buf, 5) < 5) {
   1.249 +			continue;
   1.250 +		}
   1.251 +
   1.252 +		// check for response -- 0x81 = OK, 0x80 = retransmit
   1.253 +		// save current time (in seconds)
   1.254 +		tm = time(NULL);
   1.255 +
   1.256 +		// loop until 2 seconds have passed, or LPFK responds
   1.257 +		status = 0x00;
   1.258 +		do {
   1.259 +			// read data, loop if not successful
   1.260 +			if (read(ctx->fd, &status, 1) < 1) {
   1.261 +				continue;
   1.262 +			}
   1.263 +
   1.264 +			// we got some data, what is it?
   1.265 +			if (status == 0x81) {
   1.266 +				// 0x81 -- received successfully
   1.267 +				break;
   1.268 +			}
   1.269 +		} while ((time(NULL) - tm) < 2);
   1.270 +
   1.271 +		// status OK?
   1.272 +		if (status == 0x81) {
   1.273 +			// 0x81: OK
   1.274 +			break;
   1.275 +		} else if (status == 0x80) {
   1.276 +			// 0x80: Retransmit request
   1.277 +			continue;
   1.278 +		}
   1.279 +	}
   1.280 +
   1.281 +	// update the context
   1.282 +	ctx->led_mask = leds;
   1.283 +
   1.284 +	return LPFK_E_OK;
   1.285 +}
   1.286 +
   1.287 +/**
   1.288 + * @brief	Set or clear all the LEDs on the LPFK.
   1.289 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.290 + * @param	state	State, true for on, false for off.
   1.291 + * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
   1.292 + * 			on comms error.
   1.293 + */
   1.294 +int lpfk_set_leds(LPFK_CTX *ctx, const int state)
   1.295 +{
   1.296 +	int i;
   1.297 +	time_t tm;
   1.298 +	unsigned long leds;
   1.299 +	unsigned char buf[5];
   1.300 +	unsigned char status;
   1.301 +
   1.302 +	if (state) {
   1.303 +		// all LEDs on
   1.304 +		leds = 0xFFFFFFFF;
   1.305 +	} else {
   1.306 +		// all LEDs off
   1.307 +		leds = 0x00000000;
   1.308 +	}
   1.309 +
   1.310 +	// send new LED mask to the LPFK
   1.311 +	buf[0] = 0x94;
   1.312 +	buf[1] = leds & 0xff;
   1.313 +	buf[2] = leds >> 8;
   1.314 +	buf[3] = leds >> 16;
   1.315 +	buf[4] = leds >> 24;
   1.316 +
   1.317 +	// make 5 attempts to set the LEDs
   1.318 +	for (i=0; i<5; i++) {
   1.319 +		if (write(ctx->fd, &buf, 5) < 5) {
   1.320 +			continue;
   1.321 +		}
   1.322 +
   1.323 +		// check for response -- 0x81 = OK, 0x80 = retransmit
   1.324 +		// save current time (in seconds)
   1.325 +		tm = time(NULL);
   1.326 +
   1.327 +		// loop until 2 seconds have passed, or LPFK responds
   1.328 +		status = 0x00;
   1.329 +		do {
   1.330 +			// read data, loop if not successful
   1.331 +			if (read(ctx->fd, &status, 1) < 1) {
   1.332 +				continue;
   1.333 +			}
   1.334 +
   1.335 +			// we got some data, what is it?
   1.336 +			if (status == 0x81) {
   1.337 +				// 0x81 -- received successfully
   1.338 +				break;
   1.339 +			}
   1.340 +		} while ((time(NULL) - tm) < 2);
   1.341 +
   1.342 +		// status OK?
   1.343 +		if (status == 0x81) {
   1.344 +			// 0x81: OK
   1.345 +			break;
   1.346 +		} else if (status == 0x80) {
   1.347 +			// 0x80: Retransmit request
   1.348 +			continue;
   1.349 +		}
   1.350 +	}
   1.351 +
   1.352 +	// update the context
   1.353 +	ctx->led_mask = leds;
   1.354 +
   1.355 +	return LPFK_E_OK;
   1.356 +}
   1.357 +
   1.358 +/**
   1.359 + * @brief	Get the status of an LED on the LPFK.
   1.360 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.361 + * @param	num		LED/key number, from 0 to 31.
   1.362 + * @return	true if LED is on, false otherwise.
   1.363 + */
   1.364 +int lpfk_get_led(LPFK_CTX *ctx, const int num)
   1.365 +{
   1.366 +	unsigned long mask;
   1.367 +
   1.368 +	// check parameters
   1.369 +	if ((num < 0) || (num > 31)) {
   1.370 +		return false;
   1.371 +	}
   1.372 +
   1.373 +	// parameters OK, now build the LED mask
   1.374 +	mask = (0x80 >> (num % 8)) << ((num / 8) * 8);
   1.375 +	if (ctx->led_mask & mask) {
   1.376 +		return true;
   1.377 +	} else {
   1.378 +		return false;
   1.379 +	}
   1.380 +}
   1.381 +
   1.382 +/**
   1.383 + * @brief	Read a key from the LPFK
   1.384 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.385 + * @return	-1 if no keys in buffer, 0-31 for key 1-32 down.
   1.386 + */
   1.387 +int lpfk_read(LPFK_CTX *ctx)
   1.388 +{
   1.389 +	int nbytes;
   1.390 +	unsigned char key;
   1.391 +
   1.392 +	// try and read a byte (keycode) from the LPFK
   1.393 +	nbytes = read(ctx->fd, &key, 1);
   1.394 +
   1.395 +	if ((nbytes < 1) || (key > 31)) {
   1.396 +		// no keys buffered, or keycode invalid.
   1.397 +		return -1;
   1.398 +	} else {
   1.399 +		// key buffered, pass it along.
   1.400 +		return key;
   1.401 +	}
   1.402 +}
   1.403 +