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