added: Vim folding markers to all functions

Tue, 26 Aug 2008 13:18:18 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Tue, 26 Aug 2008 13:18:18 +0100
changeset 1
3e775df109e6
parent 0
745037d69d81
child 2
071056e12321

added: Vim folding markers to all functions
added: Cached LED write (TODO: add to test app)
added: LPFK keys no longer active by default
TODO: build Doxyfile for Doxygen documentation generation

include/liblpfk.h file | annotate | diff | revisions
src/liblpfk.c file | annotate | diff | revisions
     1.1 --- a/include/liblpfk.h	Tue Aug 26 12:45:33 2008 +0100
     1.2 +++ b/include/liblpfk.h	Tue Aug 26 13:18:18 2008 +0100
     1.3 @@ -69,7 +69,8 @@
     1.4  	LPFK_E_PORT_OPEN,			///< Could not open comm port.
     1.5  	LPFK_E_NOT_PRESENT,			///< LPFK not present on specified port.
     1.6  	LPFK_E_COMMS,				///< Communication error.
     1.7 -	LPFK_E_PARAM				///< Invalid function parameter.
     1.8 +	LPFK_E_PARAM,				///< Invalid function parameter.
     1.9 +	LPFK_E_NOT_ENABLED			///< Attempt to read key when LPFK disabled
    1.10  };
    1.11  
    1.12  /**
    1.13 @@ -99,12 +100,40 @@
    1.14  int lpfk_enable(LPFK_CTX *ctx, int val);
    1.15  
    1.16  /**
    1.17 + * @brief	Set or clear an LED in the cached LED mask buffer.
    1.18 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
    1.19 + * @param	num		LED/key number, from 0 to 31.
    1.20 + * @param	state	State, true for on, false for off.
    1.21 + * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
    1.22 + * 			on comms error.
    1.23 + */
    1.24 +int lpfk_set_led_cached(LPFK_CTX *ctx, const int num, const int state);
    1.25 +
    1.26 +/**
    1.27 + * @brief	Set or clear all the LEDs in the shadow register.
    1.28 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
    1.29 + * @param	state	State, true for on, false for off.
    1.30 + * @return	LPFK_E_OK on success.
    1.31 + */
    1.32 +int lpfk_set_leds_cached(LPFK_CTX *ctx, const int state);
    1.33 +
    1.34 +/**
    1.35 + * @brief	Set the LPFK's LED state from the cached LED mask.
    1.36 + * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
    1.37 + * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
    1.38 + * 			on comms error.
    1.39 + */
    1.40 +int lpfk_update_leds(LPFK_CTX *ctx);
    1.41 +
    1.42 +/**
    1.43   * @brief	Set or clear an LED on the LPFK.
    1.44   * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
    1.45   * @param	num		LED/key number, from 0 to 31.
    1.46   * @param	state	State, true for on, false for off.
    1.47   * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
    1.48   * 			on comms error.
    1.49 + * @note	Equivalent to a call to lpfk_set_led_cached() followed by a call
    1.50 + * 			to lpfk_update_leds().
    1.51   */
    1.52  int lpfk_set_led(LPFK_CTX *ctx, const int num, const int state);
    1.53  
    1.54 @@ -114,6 +143,8 @@
    1.55   * @param	state	State, true for on, false for off.
    1.56   * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
    1.57   * 			on comms error.
    1.58 + * @note	Equivalent to a call to lpfk_set_leds_cached() followed by a call
    1.59 + * 			to lpfk_update_leds().
    1.60   */
    1.61  int lpfk_set_leds(LPFK_CTX *ctx, const int state);
    1.62  
     2.1 --- a/src/liblpfk.c	Tue Aug 26 12:45:33 2008 +0100
     2.2 +++ b/src/liblpfk.c	Tue Aug 26 13:18:18 2008 +0100
     2.3 @@ -56,6 +56,7 @@
     2.4  
     2.5  #include "liblpfk.h"
     2.6  
     2.7 +/* lpfk_open {{{ */
     2.8  int lpfk_open(const char *port, LPFK_CTX *ctx)
     2.9  {
    2.10  	struct termios newtio;
    2.11 @@ -156,6 +157,9 @@
    2.12  	}
    2.13  }
    2.14  
    2.15 +/* }}} */
    2.16 +
    2.17 +/* lpfk_close {{{ */
    2.18  int lpfk_close(LPFK_CTX *ctx)
    2.19  {
    2.20  	int status;
    2.21 @@ -178,7 +182,9 @@
    2.22  	// Done!
    2.23  	return LPFK_E_OK;
    2.24  }
    2.25 +/* }}} */
    2.26  
    2.27 +/* lpfk_enable {{{ */
    2.28  int lpfk_enable(LPFK_CTX *ctx, int val)
    2.29  {
    2.30  	if (val) {
    2.31 @@ -199,15 +205,10 @@
    2.32  	return LPFK_E_OK;
    2.33  }
    2.34  
    2.35 -/**
    2.36 - * @brief	Set or clear an LED on the LPFK.
    2.37 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
    2.38 - * @param	num		LED/key number, from 0 to 31.
    2.39 - * @param	state	State, true for on, false for off.
    2.40 - * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
    2.41 - * 			on comms error.
    2.42 - */
    2.43 -int lpfk_set_led(LPFK_CTX *ctx, const int num, const int state)
    2.44 +/* }}} */
    2.45 +
    2.46 +/* lpfk_set_led_cached {{{ */
    2.47 +int lpfk_set_led_cached(LPFK_CTX *ctx, const int num, const int state)
    2.48  {
    2.49  	int i;
    2.50  	time_t tm;
    2.51 @@ -221,23 +222,54 @@
    2.52  	}
    2.53  
    2.54  	// parameters OK, now build the LED mask
    2.55 -	mask = (0x80 >> (num % 8)) << ((num / 8) * 8);
    2.56 -
    2.57 -	leds = ctx->led_mask;
    2.58 +	mask = (0x80 >> (num % 8)) << ((3 - (num / 8)) * 8);
    2.59  
    2.60  	// mask the specified bit
    2.61  	if (state) {
    2.62 -		leds |= mask;
    2.63 +		ctx->led_mask |= mask;
    2.64  	} else {
    2.65 -		leds &= ~mask;
    2.66 +		ctx->led_mask &= ~mask;
    2.67  	}
    2.68  
    2.69 +	return LPFK_E_OK;
    2.70 +}
    2.71 +/* }}} */
    2.72 +
    2.73 +/* lpfk_set_leds_cached {{{ */
    2.74 +int lpfk_set_leds_cached(LPFK_CTX *ctx, const int state)
    2.75 +{
    2.76 +	int i;
    2.77 +	time_t tm;
    2.78 +	unsigned long leds;
    2.79 +	unsigned char buf[5];
    2.80 +	unsigned char status;
    2.81 +
    2.82 +	if (state) {
    2.83 +		// all LEDs on
    2.84 +		ctx->led_mask = 0xFFFFFFFF;
    2.85 +	} else {
    2.86 +		// all LEDs off
    2.87 +		ctx->led_mask = 0x00000000;
    2.88 +	}
    2.89 +
    2.90 +	return LPFK_E_OK;
    2.91 +}
    2.92 +/* }}} */
    2.93 +
    2.94 +/* lpfk_update_leds {{{ */
    2.95 +int lpfk_update_leds(LPFK_CTX *ctx)
    2.96 +{
    2.97 +	int i;
    2.98 +	time_t tm;
    2.99 +	unsigned char buf[5];
   2.100 +	unsigned char status;
   2.101 +
   2.102  	// send new LED mask to the LPFK
   2.103  	buf[0] = 0x94;
   2.104 -	buf[1] = leds & 0xff;
   2.105 -	buf[2] = leds >> 8;
   2.106 -	buf[3] = leds >> 16;
   2.107 -	buf[4] = leds >> 24;
   2.108 +	buf[1] = ctx->led_mask >> 24;
   2.109 +	buf[2] = ctx->led_mask >> 16;
   2.110 +	buf[3] = ctx->led_mask >> 8;
   2.111 +	buf[4] = ctx->led_mask & 0xff;
   2.112  
   2.113  	// make 5 attempts to set the LEDs
   2.114  	for (i=0; i<5; i++) {
   2.115 @@ -274,89 +306,27 @@
   2.116  		}
   2.117  	}
   2.118  
   2.119 -	// update the context
   2.120 -	ctx->led_mask = leds;
   2.121 -
   2.122  	return LPFK_E_OK;
   2.123  }
   2.124 +/* }}} */
   2.125  
   2.126 -/**
   2.127 - * @brief	Set or clear all the LEDs on the LPFK.
   2.128 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   2.129 - * @param	state	State, true for on, false for off.
   2.130 - * @return	LPFK_E_OK on success, LPFK_E_PARAM on bad parameter, LPFK_E_COMMS
   2.131 - * 			on comms error.
   2.132 - */
   2.133 +/* lpfk_set_led {{{ */
   2.134 +int lpfk_set_led(LPFK_CTX *ctx, const int num, const int state)
   2.135 +{
   2.136 +	lpfk_set_led_cached(ctx, num, state);
   2.137 +	return lpfk_update_leds(ctx);
   2.138 +}
   2.139 +/* }}} */
   2.140 +
   2.141 +/* lpfk_set_leds {{{ */
   2.142  int lpfk_set_leds(LPFK_CTX *ctx, const int state)
   2.143  {
   2.144 -	int i;
   2.145 -	time_t tm;
   2.146 -	unsigned long leds;
   2.147 -	unsigned char buf[5];
   2.148 -	unsigned char status;
   2.149 -
   2.150 -	if (state) {
   2.151 -		// all LEDs on
   2.152 -		leds = 0xFFFFFFFF;
   2.153 -	} else {
   2.154 -		// all LEDs off
   2.155 -		leds = 0x00000000;
   2.156 -	}
   2.157 -
   2.158 -	// send new LED mask to the LPFK
   2.159 -	buf[0] = 0x94;
   2.160 -	buf[1] = leds & 0xff;
   2.161 -	buf[2] = leds >> 8;
   2.162 -	buf[3] = leds >> 16;
   2.163 -	buf[4] = leds >> 24;
   2.164 -
   2.165 -	// make 5 attempts to set the LEDs
   2.166 -	for (i=0; i<5; i++) {
   2.167 -		if (write(ctx->fd, &buf, 5) < 5) {
   2.168 -			continue;
   2.169 -		}
   2.170 -
   2.171 -		// check for response -- 0x81 = OK, 0x80 = retransmit
   2.172 -		// save current time (in seconds)
   2.173 -		tm = time(NULL);
   2.174 +	lpfk_set_leds_cached(ctx, state);
   2.175 +	return lpfk_update_leds(ctx);
   2.176 +}
   2.177 +/* }}} */
   2.178  
   2.179 -		// loop until 2 seconds have passed, or LPFK responds
   2.180 -		status = 0x00;
   2.181 -		do {
   2.182 -			// read data, loop if not successful
   2.183 -			if (read(ctx->fd, &status, 1) < 1) {
   2.184 -				continue;
   2.185 -			}
   2.186 -
   2.187 -			// we got some data, what is it?
   2.188 -			if (status == 0x81) {
   2.189 -				// 0x81 -- received successfully
   2.190 -				break;
   2.191 -			}
   2.192 -		} while ((time(NULL) - tm) < 2);
   2.193 -
   2.194 -		// status OK?
   2.195 -		if (status == 0x81) {
   2.196 -			// 0x81: OK
   2.197 -			break;
   2.198 -		} else if (status == 0x80) {
   2.199 -			// 0x80: Retransmit request
   2.200 -			continue;
   2.201 -		}
   2.202 -	}
   2.203 -
   2.204 -	// update the context
   2.205 -	ctx->led_mask = leds;
   2.206 -
   2.207 -	return LPFK_E_OK;
   2.208 -}
   2.209 -
   2.210 -/**
   2.211 - * @brief	Get the status of an LED on the LPFK.
   2.212 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   2.213 - * @param	num		LED/key number, from 0 to 31.
   2.214 - * @return	true if LED is on, false otherwise.
   2.215 - */
   2.216 +/* lpfk_get_led {{{ */
   2.217  int lpfk_get_led(LPFK_CTX *ctx, const int num)
   2.218  {
   2.219  	unsigned long mask;
   2.220 @@ -367,24 +337,26 @@
   2.221  	}
   2.222  
   2.223  	// parameters OK, now build the LED mask
   2.224 -	mask = (0x80 >> (num % 8)) << ((num / 8) * 8);
   2.225 +	mask = (0x80 >> (num % 8)) << ((3 - (num / 8)) * 8);
   2.226  	if (ctx->led_mask & mask) {
   2.227  		return true;
   2.228  	} else {
   2.229  		return false;
   2.230  	}
   2.231  }
   2.232 +/* }}} */
   2.233  
   2.234 -/**
   2.235 - * @brief	Read a key from the LPFK
   2.236 - * @param	ctx		Pointer to an LPFK_CTX struct initialised by lpfk_open().
   2.237 - * @return	-1 if no keys in buffer, 0-31 for key 1-32 down.
   2.238 - */
   2.239 +/* lpfk_read {{{ */
   2.240  int lpfk_read(LPFK_CTX *ctx)
   2.241  {
   2.242  	int nbytes;
   2.243  	unsigned char key;
   2.244  
   2.245 +	// make sure the LPFK is enabled before trying to read a scancode
   2.246 +	if (!ctx->enabled) {
   2.247 +		return LPFK_E_NOT_ENABLED;
   2.248 +	}
   2.249 +
   2.250  	// try and read a byte (keycode) from the LPFK
   2.251  	nbytes = read(ctx->fd, &key, 1);
   2.252  
   2.253 @@ -396,4 +368,5 @@
   2.254  		return key;
   2.255  	}
   2.256  }
   2.257 +/* }}} */
   2.258