pikeyd165 – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /**** config.c *****************************/ |
2 | /* M. Moller 2013-01-16 */ |
||
3 | /* Universal RPi GPIO keyboard daemon */ |
||
4 | /*******************************************/ |
||
5 | |||
6 | /* |
||
7 | Copyright (C) 2013 Michael Moller. |
||
8 | This file is part of the Universal Raspberry Pi GPIO keyboard daemon. |
||
9 | |||
10 | This is free software; you can redistribute it and/or |
||
11 | modify it under the terms of the GNU Lesser General Public |
||
12 | License as published by the Free Software Foundation; either |
||
13 | version 2.1 of the License, or (at your option) any later version. |
||
14 | |||
15 | The software is distributed in the hope that it will be useful, |
||
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
18 | Lesser General Public License for more details. |
||
19 | |||
20 | You should have received a copy of the GNU Lesser General Public |
||
21 | License along with the GNU C Library; if not, write to the Free |
||
22 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
||
23 | 02111-1307 USA. |
||
24 | */ |
||
25 | |||
26 | #include <stdio.h> |
||
27 | #include <stdlib.h> |
||
28 | #include <string.h> |
||
29 | #include "config.h" |
||
30 | |||
31 | #define NUM_GPIO 32 |
||
32 | #define MAX_LN 128 |
||
33 | |||
34 | |||
35 | extern key_names_s key_names[]; |
||
36 | |||
37 | |||
38 | key_special_s KEY_SPECIAL[20]; |
||
39 | |||
40 | |||
41 | |||
42 | |||
43 | |||
44 | typedef struct _gpio_key{ |
||
45 | int gpio; |
||
46 | int idx; |
||
47 | int key; |
||
48 | struct _gpio_key *next; |
||
49 | }gpio_key_s; |
||
50 | |||
51 | |||
52 | static int find_key(const char *name); |
||
53 | static void add_event(gpio_key_s **ev, int gpio, int key); |
||
54 | static gpio_key_s *get_event(gpio_key_s *ev, int idx); |
||
55 | |||
56 | static gpio_key_s *gpio_key[NUM_GPIO]; |
||
57 | static gpio_key_s *last_gpio_key = NULL; |
||
58 | static int gpios[NUM_GPIO]; |
||
59 | static int num_gpios_used=0; |
||
60 | |||
61 | static int SP; |
||
62 | |||
63 | #define SET_BIT(var,pos) ((var) | (1<<(pos))) |
||
64 | |||
65 | int init_config(void) |
||
66 | { |
||
67 | int i,k,n; |
||
68 | FILE *fp; |
||
69 | char ln[MAX_LN]; |
||
70 | char name[32]; |
||
71 | char conffile[80]; |
||
72 | int gpio; |
||
73 | int idx; |
||
74 | int a,b,c,d,e; |
||
75 | |||
76 | for(i=0;i<NUM_GPIO;i++){ |
||
77 | gpio_key[i] = NULL; |
||
78 | } |
||
79 | |||
80 | /* search for the conf file in ~/.piked.conf and /etc/pikeyd165.conf */ |
||
81 | sprintf(conffile, "%s/.pikeyd165.conf", getenv("HOME")); |
||
82 | fp = fopen(conffile, "r"); |
||
83 | if(!fp){ |
||
84 | fp = fopen("/etc/pikeyd165.conf", "r"); |
||
85 | if(!fp){ |
||
86 | perror(conffile); |
||
87 | perror("/etc/pikeyd165.conf"); |
||
88 | } |
||
89 | sprintf(conffile, "/etc/pikeyd165.conf"); |
||
90 | } |
||
91 | if(fp){ |
||
92 | printf("Config file is %s\n", conffile); |
||
93 | } |
||
94 | |||
95 | idx=0; |
||
96 | while(fp && !feof(fp)){ |
||
97 | if(fgets(ln, MAX_LN, fp)){ |
||
98 | if(strlen(ln) > 1){ |
||
99 | if(ln[0]=='#')continue; |
||
100 | a=b=c=d=e=-1; |
||
101 | n=sscanf(ln, "%s %d %d %d %d %d", name, &gpio, &b, &c, &d, &e); |
||
102 | if(n>1){ |
||
103 | k = find_key(name); |
||
104 | if(k){ |
||
105 | if(n==2) |
||
106 | { |
||
107 | //printf("[%s] = %d (%d)\n",name, gpio, key_names[k].code); |
||
108 | SP=0; |
||
109 | add_event(&gpio_key[gpio], gpio, key_names[k].code); |
||
110 | } |
||
111 | else |
||
112 | { |
||
113 | a=gpio; |
||
114 | //printf("%i %i %i %i %i\n",a,b,c,d,e); |
||
115 | KEY_SPECIAL[idx].pressed=0; |
||
116 | KEY_SPECIAL[idx].key=k; |
||
117 | if(a>0) KEY_SPECIAL[idx].bits |= 1 << a; |
||
118 | if(b>0) KEY_SPECIAL[idx].bits |= 1 << b; |
||
119 | if(c>0) KEY_SPECIAL[idx].bits |= 1 << c; |
||
120 | if(d>0) KEY_SPECIAL[idx].bits |= 1 << d; |
||
121 | if(e>0) KEY_SPECIAL[idx].bits |= 1 << e; |
||
122 | |||
123 | idx++; |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | KEY_SPECIAL[idx].key=0; |
||
132 | KEY_SPECIAL[idx].bits=0; |
||
133 | |||
134 | |||
135 | for(idx=0; KEY_SPECIAL[idx].key!=0 && KEY_SPECIAL[idx].bits!=0 ; idx++) |
||
136 | { |
||
137 | printf("%i %08x\n", KEY_SPECIAL[idx].key,KEY_SPECIAL[idx].bits); |
||
138 | } |
||
139 | |||
140 | if(fp){ |
||
141 | fclose(fp); |
||
142 | } |
||
143 | |||
144 | n=0; |
||
145 | for(i=0; i<NUM_GPIO; i++){ |
||
146 | if(gpio_key[i]){ |
||
147 | gpios[n] = gpio_key[i]->gpio; |
||
148 | n++; |
||
149 | } |
||
150 | } |
||
151 | num_gpios_used = n; |
||
152 | |||
153 | return 0; |
||
154 | } |
||
155 | |||
156 | static int find_key(const char *name) |
||
157 | { |
||
158 | int i=0; |
||
159 | while(key_names[i].code >= 0){ |
||
160 | if(!strncmp(key_names[i].name, name, 32))break; |
||
161 | i++; |
||
162 | } |
||
163 | return i; |
||
164 | } |
||
165 | |||
166 | static void add_event(gpio_key_s **ev, int gpio, int key) |
||
167 | { |
||
168 | if(*ev){ |
||
169 | SP++; |
||
170 | /* Recursive call to add the next link in the list */ |
||
171 | add_event(&(*ev)->next, gpio, key); |
||
172 | } |
||
173 | else{ |
||
174 | *ev = malloc(sizeof(gpio_key_s)); |
||
175 | if(*ev==NULL){ |
||
176 | perror("malloc"); |
||
177 | } |
||
178 | else{ |
||
179 | (*ev)->gpio = gpio; |
||
180 | (*ev)->idx = SP; |
||
181 | (*ev)->key = key; |
||
182 | (*ev)->next = NULL; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | static gpio_key_s *get_event(gpio_key_s *ev, int idx) |
||
188 | { |
||
189 | if(ev->idx == idx){ |
||
190 | return ev; |
||
191 | } |
||
192 | else if(!ev->next){ |
||
193 | return NULL; |
||
194 | } |
||
195 | else{ |
||
196 | return get_event(ev->next, idx); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | int get_event_key(int gpio, int idx) |
||
201 | { |
||
202 | gpio_key_s *ev; |
||
203 | ev = get_event(gpio_key[gpio], idx); |
||
204 | if(ev){ |
||
205 | return ev->key; |
||
206 | } |
||
207 | else{ |
||
208 | return 0; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | int got_more_keys(int gpio) |
||
213 | { |
||
214 | if( last_gpio_key == NULL ){ |
||
215 | return (gpio_key[gpio] != NULL); |
||
216 | } |
||
217 | else if( last_gpio_key->next == NULL){ |
||
218 | return 0; |
||
219 | } |
||
220 | else{ |
||
221 | return 1; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | void restart_keys(void) |
||
226 | { |
||
227 | last_gpio_key = NULL; |
||
228 | } |
||
229 | |||
230 | int get_next_key(int gpio) |
||
231 | { |
||
232 | static int lastgpio=-1; |
||
233 | static int idx = 0; |
||
234 | gpio_key_s *ev; |
||
235 | int k; |
||
236 | |||
237 | ev = last_gpio_key; |
||
238 | if( (ev == NULL) || (gpio != lastgpio) ){ |
||
239 | /* restart at the beginning after reaching the end, or reading a new gpio */ |
||
240 | ev = gpio_key[gpio]; |
||
241 | lastgpio = gpio; |
||
242 | } |
||
243 | else{ |
||
244 | /* get successive events while retrieving the same gpio */ |
||
245 | ev = last_gpio_key->next; |
||
246 | } |
||
247 | last_gpio_key = ev; |
||
248 | |||
249 | if(ev){ |
||
250 | k = ev->key; |
||
251 | } |
||
252 | else{ |
||
253 | k = 0; |
||
254 | } |
||
255 | return k; |
||
256 | } |
||
257 | |||
258 | void test_config(void) |
||
259 | { |
||
260 | int e, i, k; |
||
261 | e=21; |
||
262 | |||
263 | for(i=0; i<NUM_GPIO; i++){ |
||
264 | if(gpio_key[i]){ |
||
265 | printf(" %d\n", i); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | while( got_more_keys(e) ){ |
||
270 | k = get_next_key(e); |
||
271 | printf("%d: EV %d = %d\n", i++, e, k); |
||
272 | if(i>8)break; |
||
273 | } |
||
274 | i=0; |
||
275 | restart_keys(); |
||
276 | while( got_more_keys(e) ){ |
||
277 | k = get_next_key(e); |
||
278 | printf("%d: EV %d = %d\n", i++, e, k); |
||
279 | if(i>8)break; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | int gpios_used(void) |
||
284 | { |
||
285 | return num_gpios_used; |
||
286 | } |
||
287 | |||
288 | int gpio_pin(int n) |
||
289 | { |
||
290 | return gpios[n % NUM_GPIO]; |
||
291 | } |