src/liblpfk.c

changeset 1
3e775df109e6
parent 0
745037d69d81
child 2
071056e12321
     1.1 --- a/src/liblpfk.c	Tue Aug 26 12:45:33 2008 +0100
     1.2 +++ b/src/liblpfk.c	Tue Aug 26 13:18:18 2008 +0100
     1.3 @@ -56,6 +56,7 @@
     1.4  
     1.5  #include "liblpfk.h"
     1.6  
     1.7 +/* lpfk_open {{{ */
     1.8  int lpfk_open(const char *port, LPFK_CTX *ctx)
     1.9  {
    1.10  	struct termios newtio;
    1.11 @@ -156,6 +157,9 @@
    1.12  	}
    1.13  }
    1.14  
    1.15 +/* }}} */
    1.16 +
    1.17 +/* lpfk_close {{{ */
    1.18  int lpfk_close(LPFK_CTX *ctx)
    1.19  {
    1.20  	int status;
    1.21 @@ -178,7 +182,9 @@
    1.22  	// Done!
    1.23  	return LPFK_E_OK;
    1.24  }
    1.25 +/* }}} */
    1.26  
    1.27 +/* lpfk_enable {{{ */
    1.28  int lpfk_enable(LPFK_CTX *ctx, int val)
    1.29  {
    1.30  	if (val) {
    1.31 @@ -199,15 +205,10 @@
    1.32  	return LPFK_E_OK;
    1.33  }
    1.34  
    1.35 -/**
    1.36 - * @brief	Set or clear an LED on the LPFK.
    1.37 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
    1.38 - * @param	num		LED/key number, from 0 to 31.
    1.39 - * @param	state	State, true for on, false for off.
    1.40 - * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
    1.41 - * 			on comms error.
    1.42 - */
    1.43 -int lpfk_set_led(LPFK_CTX *ctx, const int num, const int state)
    1.44 +/* }}} */
    1.45 +
    1.46 +/* lpfk_set_led_cached {{{ */
    1.47 +int lpfk_set_led_cached(LPFK_CTX *ctx, const int num, const int state)
    1.48  {
    1.49  	int i;
    1.50  	time_t tm;
    1.51 @@ -221,23 +222,54 @@
    1.52  	}
    1.53  
    1.54  	// parameters OK, now build the LED mask
    1.55 -	mask = (0x80 >> (num % 8)) << ((num / 8) * 8);
    1.56 -
    1.57 -	leds = ctx->led_mask;
    1.58 +	mask = (0x80 >> (num % 8)) << ((3 - (num / 8)) * 8);
    1.59  
    1.60  	// mask the specified bit
    1.61  	if (state) {
    1.62 -		leds |= mask;
    1.63 +		ctx->led_mask |= mask;
    1.64  	} else {
    1.65 -		leds &= ~mask;
    1.66 +		ctx->led_mask &= ~mask;
    1.67  	}
    1.68  
    1.69 +	return LPFK_E_OK;
    1.70 +}
    1.71 +/* }}} */
    1.72 +
    1.73 +/* lpfk_set_leds_cached {{{ */
    1.74 +int lpfk_set_leds_cached(LPFK_CTX *ctx, const int state)
    1.75 +{
    1.76 +	int i;
    1.77 +	time_t tm;
    1.78 +	unsigned long leds;
    1.79 +	unsigned char buf[5];
    1.80 +	unsigned char status;
    1.81 +
    1.82 +	if (state) {
    1.83 +		// all LEDs on
    1.84 +		ctx->led_mask = 0xFFFFFFFF;
    1.85 +	} else {
    1.86 +		// all LEDs off
    1.87 +		ctx->led_mask = 0x00000000;
    1.88 +	}
    1.89 +
    1.90 +	return LPFK_E_OK;
    1.91 +}
    1.92 +/* }}} */
    1.93 +
    1.94 +/* lpfk_update_leds {{{ */
    1.95 +int lpfk_update_leds(LPFK_CTX *ctx)
    1.96 +{
    1.97 +	int i;
    1.98 +	time_t tm;
    1.99 +	unsigned char buf[5];
   1.100 +	unsigned char status;
   1.101 +
   1.102  	// send new LED mask to the LPFK
   1.103  	buf[0] = 0x94;
   1.104 -	buf[1] = leds & 0xff;
   1.105 -	buf[2] = leds >> 8;
   1.106 -	buf[3] = leds >> 16;
   1.107 -	buf[4] = leds >> 24;
   1.108 +	buf[1] = ctx->led_mask >> 24;
   1.109 +	buf[2] = ctx->led_mask >> 16;
   1.110 +	buf[3] = ctx->led_mask >> 8;
   1.111 +	buf[4] = ctx->led_mask & 0xff;
   1.112  
   1.113  	// make 5 attempts to set the LEDs
   1.114  	for (i=0; i<5; i++) {
   1.115 @@ -274,89 +306,27 @@
   1.116  		}
   1.117  	}
   1.118  
   1.119 -	// update the context
   1.120 -	ctx->led_mask = leds;
   1.121 -
   1.122  	return LPFK_E_OK;
   1.123  }
   1.124 +/* }}} */
   1.125  
   1.126 -/**
   1.127 - * @brief	Set or clear all the LEDs on the LPFK.
   1.128 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.129 - * @param	state	State, true for on, false for off.
   1.130 - * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
   1.131 - * 			on comms error.
   1.132 - */
   1.133 +/* lpfk_set_led {{{ */
   1.134 +int lpfk_set_led(LPFK_CTX *ctx, const int num, const int state)
   1.135 +{
   1.136 +	lpfk_set_led_cached(ctx, num, state);
   1.137 +	return lpfk_update_leds(ctx);
   1.138 +}
   1.139 +/* }}} */
   1.140 +
   1.141 +/* lpfk_set_leds {{{ */
   1.142  int lpfk_set_leds(LPFK_CTX *ctx, const int state)
   1.143  {
   1.144 -	int i;
   1.145 -	time_t tm;
   1.146 -	unsigned long leds;
   1.147 -	unsigned char buf[5];
   1.148 -	unsigned char status;
   1.149 -
   1.150 -	if (state) {
   1.151 -		// all LEDs on
   1.152 -		leds = 0xFFFFFFFF;
   1.153 -	} else {
   1.154 -		// all LEDs off
   1.155 -		leds = 0x00000000;
   1.156 -	}
   1.157 -
   1.158 -	// send new LED mask to the LPFK
   1.159 -	buf[0] = 0x94;
   1.160 -	buf[1] = leds & 0xff;
   1.161 -	buf[2] = leds >> 8;
   1.162 -	buf[3] = leds >> 16;
   1.163 -	buf[4] = leds >> 24;
   1.164 -
   1.165 -	// make 5 attempts to set the LEDs
   1.166 -	for (i=0; i<5; i++) {
   1.167 -		if (write(ctx->fd, &buf, 5) < 5) {
   1.168 -			continue;
   1.169 -		}
   1.170 -
   1.171 -		// check for response -- 0x81 = OK, 0x80 = retransmit
   1.172 -		// save current time (in seconds)
   1.173 -		tm = time(NULL);
   1.174 +	lpfk_set_leds_cached(ctx, state);
   1.175 +	return lpfk_update_leds(ctx);
   1.176 +}
   1.177 +/* }}} */
   1.178  
   1.179 -		// loop until 2 seconds have passed, or LPFK responds
   1.180 -		status = 0x00;
   1.181 -		do {
   1.182 -			// read data, loop if not successful
   1.183 -			if (read(ctx->fd, &status, 1) < 1) {
   1.184 -				continue;
   1.185 -			}
   1.186 -
   1.187 -			// we got some data, what is it?
   1.188 -			if (status == 0x81) {
   1.189 -				// 0x81 -- received successfully
   1.190 -				break;
   1.191 -			}
   1.192 -		} while ((time(NULL) - tm) < 2);
   1.193 -
   1.194 -		// status OK?
   1.195 -		if (status == 0x81) {
   1.196 -			// 0x81: OK
   1.197 -			break;
   1.198 -		} else if (status == 0x80) {
   1.199 -			// 0x80: Retransmit request
   1.200 -			continue;
   1.201 -		}
   1.202 -	}
   1.203 -
   1.204 -	// update the context
   1.205 -	ctx->led_mask = leds;
   1.206 -
   1.207 -	return LPFK_E_OK;
   1.208 -}
   1.209 -
   1.210 -/**
   1.211 - * @brief	Get the status of an LED on the LPFK.
   1.212 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.213 - * @param	num		LED/key number, from 0 to 31.
   1.214 - * @return	true if LED is on, false otherwise.
   1.215 - */
   1.216 +/* lpfk_get_led {{{ */
   1.217  int lpfk_get_led(LPFK_CTX *ctx, const int num)
   1.218  {
   1.219  	unsigned long mask;
   1.220 @@ -367,24 +337,26 @@
   1.221  	}
   1.222  
   1.223  	// parameters OK, now build the LED mask
   1.224 -	mask = (0x80 >> (num % 8)) << ((num / 8) * 8);
   1.225 +	mask = (0x80 >> (num % 8)) << ((3 - (num / 8)) * 8);
   1.226  	if (ctx->led_mask & mask) {
   1.227  		return true;
   1.228  	} else {
   1.229  		return false;
   1.230  	}
   1.231  }
   1.232 +/* }}} */
   1.233  
   1.234 -/**
   1.235 - * @brief	Read a key from the LPFK
   1.236 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   1.237 - * @return	-1 if no keys in buffer, 0-31 for key 1-32 down.
   1.238 - */
   1.239 +/* lpfk_read {{{ */
   1.240  int lpfk_read(LPFK_CTX *ctx)
   1.241  {
   1.242  	int nbytes;
   1.243  	unsigned char key;
   1.244  
   1.245 +	// make sure the LPFK is enabled before trying to read a scancode
   1.246 +	if (!ctx->enabled) {
   1.247 +		return LPFK_E_NOT_ENABLED;
   1.248 +	}
   1.249 +
   1.250  	// try and read a byte (keycode) from the LPFK
   1.251  	nbytes = read(ctx->fd, &key, 1);
   1.252  
   1.253 @@ -396,4 +368,5 @@
   1.254  		return key;
   1.255  	}
   1.256  }
   1.257 +/* }}} */
   1.258