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