LIRC libraries
LinuxInfraredRemoteControl
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Modules Pages
ir_remote.h
Go to the documentation of this file.
1 
2 /****************************************************************************
3  ** ir_remote.h *************************************************************
4  ****************************************************************************
5  *
6  * ir_remote.h - describes and decodes the signals from IR remotes
7  *
8  * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
9  * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
10  *
11  */
22 #ifndef IR_REMOTE_H
23 #define IR_REMOTE_H
24 
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <math.h>
30 #include <stdlib.h>
31 
32 #include "driver.h"
33 
34 #include "ir_remote_types.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
43 extern struct ir_remote *last_remote;
44 
45 
49 extern struct ir_remote* repeat_remote;
50 
54 extern struct ir_ncode* repeat_code;
55 
56 
57 static inline ir_code get_ir_code(const struct ir_ncode *ncode,
58  const struct ir_code_node *node)
59 {
60  if (ncode->next && node != NULL)
61  return node->code;
62  return ncode->code;
63 }
64 
65 static inline struct ir_code_node *get_next_ir_code_node(const struct ir_ncode *ncode,
66  const struct ir_code_node *node)
67 {
68  if (node == NULL)
69  return ncode->next;
70  return node->next;
71 }
72 
73 static inline int bit_count(const struct ir_remote *remote)
74 {
75  return remote->pre_data_bits + remote->bits + remote->post_data_bits;
76 }
77 
78 static inline int bits_set(ir_code data)
79 {
80  int ret = 0;
81  while (data) {
82  if (data & 1)
83  ret++;
84  data >>= 1;
85  }
86  return ret;
87 }
88 
89 static inline ir_code reverse(ir_code data, int bits)
90 {
91  int i;
92  ir_code c;
93 
94  c = 0;
95  for (i = 0; i < bits; i++) {
96  c |= (ir_code) (((data & (((ir_code) 1) << i)) ? 1 : 0))
97  << (bits - 1 - i);
98  }
99  return (c);
100 }
101 
102 static inline int is_pulse(lirc_t data)
103 {
104  return (data & PULSE_BIT ? 1 : 0);
105 }
106 
107 static inline int is_space(lirc_t data)
108 {
109  return (!is_pulse(data));
110 }
111 
112 static inline int has_repeat(const struct ir_remote *remote)
113 {
114  if (remote->prepeat > 0 && remote->srepeat > 0)
115  return (1);
116  else
117  return (0);
118 }
119 
120 static inline void set_protocol(struct ir_remote *remote, int protocol)
121 {
122  remote->flags &= ~(IR_PROTOCOL_MASK);
123  remote->flags |= protocol;
124 }
125 
126 static inline int is_raw(const struct ir_remote *remote)
127 {
128  if ((remote->flags & IR_PROTOCOL_MASK) == RAW_CODES)
129  return (1);
130  else
131  return (0);
132 }
133 
134 static inline int is_space_enc(const struct ir_remote *remote)
135 {
136  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_ENC)
137  return (1);
138  else
139  return (0);
140 }
141 
142 static inline int is_space_first(const struct ir_remote *remote)
143 {
144  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_FIRST)
145  return (1);
146  else
147  return (0);
148 }
149 
150 static inline int is_rc5(const struct ir_remote *remote)
151 {
152  if ((remote->flags & IR_PROTOCOL_MASK) == RC5)
153  return (1);
154  else
155  return (0);
156 }
157 
158 static inline int is_rc6(const struct ir_remote *remote)
159 {
160  if ((remote->flags & IR_PROTOCOL_MASK) == RC6 || remote->rc6_mask)
161  return (1);
162  else
163  return (0);
164 }
165 
166 static inline int is_biphase(const struct ir_remote *remote)
167 {
168  if (is_rc5(remote) || is_rc6(remote))
169  return (1);
170  else
171  return (0);
172 }
173 
174 static inline int is_rcmm(const struct ir_remote *remote)
175 {
176  if ((remote->flags & IR_PROTOCOL_MASK) == RCMM)
177  return (1);
178  else
179  return (0);
180 }
181 
182 static inline int is_goldstar(const struct ir_remote *remote)
183 {
184  if ((remote->flags & IR_PROTOCOL_MASK) == GOLDSTAR)
185  return (1);
186  else
187  return (0);
188 }
189 
190 static inline int is_grundig(const struct ir_remote *remote)
191 {
192  if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
193  return (1);
194  else
195  return (0);
196 }
197 
198 static inline int is_bo(const struct ir_remote *remote)
199 {
200  if ((remote->flags & IR_PROTOCOL_MASK) == BO)
201  return (1);
202  else
203  return (0);
204 }
205 
206 static inline int is_serial(const struct ir_remote *remote)
207 {
208  if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
209  return (1);
210  else
211  return (0);
212 }
213 
214 static inline int is_xmp(const struct ir_remote *remote)
215 {
216  if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
217  return (1);
218  else
219  return (0);
220 }
221 
222 static inline int is_const(const struct ir_remote *remote)
223 {
224  if (remote->flags & CONST_LENGTH)
225  return (1);
226  else
227  return (0);
228 }
229 
230 static inline int has_repeat_gap(const struct ir_remote *remote)
231 {
232  if (remote->repeat_gap > 0)
233  return (1);
234  else
235  return (0);
236 }
237 
238 static inline int has_pre(const struct ir_remote *remote)
239 {
240  if (remote->pre_data_bits > 0)
241  return (1);
242  else
243  return (0);
244 }
245 
246 static inline int has_post(const struct ir_remote *remote)
247 {
248  if (remote->post_data_bits > 0)
249  return (1);
250  else
251  return (0);
252 }
253 
254 static inline int has_header(const struct ir_remote *remote)
255 {
256  if (remote->phead > 0 && remote->shead > 0)
257  return (1);
258  else
259  return (0);
260 }
261 
262 static inline int has_foot(const struct ir_remote *remote)
263 {
264  if (remote->pfoot > 0 && remote->sfoot > 0)
265  return (1);
266  else
267  return (0);
268 }
269 
270 static inline int has_toggle_bit_mask(const struct ir_remote *remote)
271 {
272  if (remote->toggle_bit_mask > 0)
273  return (1);
274  else
275  return (0);
276 }
277 
278 static inline int has_ignore_mask(const struct ir_remote *remote)
279 {
280  if (remote->ignore_mask > 0)
281  return (1);
282  else
283  return (0);
284 }
285 
286 static inline int has_repeat_mask(struct ir_remote *remote)
287 {
288  if (remote->repeat_mask > 0)
289  return (1);
290  else
291  return (0);
292 }
293 
294 static inline int has_toggle_mask(const struct ir_remote *remote)
295 {
296  if (remote->toggle_mask > 0)
297  return (1);
298  else
299  return (0);
300 }
301 
302 static inline lirc_t min_gap(const struct ir_remote *remote)
303 {
304  if (remote->gap2 != 0 && remote->gap2 < remote->gap) {
305  return remote->gap2;
306  } else {
307  return remote->gap;
308  }
309 }
310 
311 static inline lirc_t max_gap(const struct ir_remote *remote)
312 {
313  if (remote->gap2 > remote->gap) {
314  return remote->gap2;
315  } else {
316  return remote->gap;
317  }
318 }
319 
320 /* check if delta is inside exdelta +/- exdelta*eps/100 */
321 
322 static inline int expect(const struct ir_remote *remote, lirc_t delta, lirc_t exdelta)
323 {
324  int aeps = curr_driver->resolution > remote->aeps ? curr_driver->resolution : remote->aeps;
325 
326  if (abs(exdelta - delta) <= exdelta * remote->eps / 100 || abs(exdelta - delta) <= aeps)
327  return 1;
328  return 0;
329 }
330 
331 static inline int expect_at_least(const struct ir_remote *remote, lirc_t delta, lirc_t exdelta)
332 {
333  int aeps = curr_driver->resolution > remote->aeps ? curr_driver->resolution : remote->aeps;
334 
335  if (delta + exdelta * remote->eps / 100 >= exdelta || delta + aeps >= exdelta) {
336  return 1;
337  }
338  return 0;
339 }
340 
341 static inline int expect_at_most(const struct ir_remote *remote, lirc_t delta, lirc_t exdelta)
342 {
343  int aeps = curr_driver->resolution > remote->aeps ? curr_driver->resolution : remote->aeps;
344 
345  if (delta <= exdelta + exdelta * remote->eps / 100 || delta <= exdelta + aeps) {
346  return 1;
347  }
348  return 0;
349 }
350 
351 static inline lirc_t upper_limit(const struct ir_remote *remote, lirc_t val)
352 {
353  int aeps = curr_driver->resolution > remote->aeps ? curr_driver->resolution : remote->aeps;
354  lirc_t eps_val = val * (100 + remote->eps) / 100;
355  lirc_t aeps_val = val + aeps;
356  return eps_val > aeps_val ? eps_val : aeps_val;
357 }
358 
359 static inline lirc_t lower_limit(const struct ir_remote *remote, lirc_t val)
360 {
361  int aeps = curr_driver->resolution > remote->aeps ? curr_driver->resolution : remote->aeps;
362  lirc_t eps_val = val * (100 - remote->eps) / 100;
363  lirc_t aeps_val = val - aeps;
364 
365  if (eps_val <= 0)
366  eps_val = 1;
367  if (aeps_val <= 0)
368  aeps_val = 1;
369 
370  return eps_val < aeps_val ? eps_val : aeps_val;
371 }
372 
373 /* only works if last <= current */
374 static inline unsigned long time_elapsed(const struct timeval *last,
375  const struct timeval *current)
376 {
377  unsigned long secs, diff;
378 
379  secs = current->tv_sec - last->tv_sec;
380 
381  diff = 1000000 * secs + current->tv_usec - last->tv_usec;
382 
383  return (diff);
384 }
385 
386 static inline ir_code gen_mask(int bits)
387 {
388  int i;
389  ir_code mask;
390 
391  mask = 0;
392  for (i = 0; i < bits; i++) {
393  mask <<= 1;
394  mask |= 1;
395  }
396  return (mask);
397 }
398 
399 static inline ir_code gen_ir_code(const struct ir_remote *remote,
400  ir_code pre, ir_code code, ir_code post)
401 {
402  ir_code all;
403 
404  all = (pre & gen_mask(remote->pre_data_bits));
405  all <<= remote->bits;
406  all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
407  all <<= remote->post_data_bits;
408  all |= post & gen_mask(remote->post_data_bits);
409 
410  return all;
411 }
412 
420 const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
421  const struct ir_remote* remote);
422 
423 struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
424  const char* name);
425 
426 void get_frequency_range(const struct ir_remote* remotes,
427  unsigned int* min_freq,
428  unsigned int* max_freq);
429 
430 void get_filter_parameters(const struct ir_remote* remotes,
431  lirc_t* max_gap_lengthp,
432  lirc_t* min_pulse_lengthp,
433  lirc_t* min_space_lengthp,
434  lirc_t* max_pulse_lengthp,
435  lirc_t* max_space_lengthp);
436 
437 int map_code(const struct ir_remote* remote,
438  struct decode_ctx_t* ctx,
439  int pre_bits,
440  ir_code pre,
441  int bits,
442  ir_code code,
443  int post_bits,
444  ir_code post);
445 
446 void map_gap(const struct ir_remote* remote,
447  struct decode_ctx_t* ctx,
448  const struct timeval* start,
449  const struct timeval* last,
450  lirc_t signal_length);
451 
452 struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
453  const char* name);
454 
455 int write_message(char* buffer,
456  size_t size,
457  const char* remote_name,
458  const char* button_name,
459  const char* button_suffix,
460  ir_code code, int reps);
471 char* decode_all(struct ir_remote* remotes);
472 
485 int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
486 
487 #ifdef __cplusplus
488 }
489 #endif
490 
497 void ir_remote_init(int use_dyncodes);
498 
501 #endif
struct ir_remote * last_remote
Definition: ir_remote.c:51
struct ir_ncode * repeat_code
Definition: ir_remote.c:55
#define RC6
#define GOLDSTAR
void ir_remote_init(int use_dyncodes)
Definition: ir_remote.c:59
ir_code repeat_mask
void get_filter_parameters(const struct ir_remote *remotes, lirc_t *max_gap_lengthp, lirc_t *min_pulse_lengthp, lirc_t *min_space_lengthp, lirc_t *max_pulse_lengthp, lirc_t *max_space_lengthp)
Definition: ir_remote.c:122
struct ir_code_node * next
const char * name
#define SPACE_ENC
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Definition: ir_remote.c:331
unsigned int resolution
Definition: driver.h:176
Interface to the userspace drivers.
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
Definition: ir_remote.c:185
__u64 ir_code
int map_code(const struct ir_remote *remote, struct decode_ctx_t *ctx, int pre_bits, ir_code pre, int bits, ir_code code, int post_bits, ir_code post)
Definition: ir_remote.c:218
char * decode_all(struct ir_remote *remotes)
Definition: ir_remote.c:617
char * name
#define RCMM
ir_code toggle_mask
Describes and decodes the signals from IR remotes.
int write_message(char *buffer, size_t size, const char *remote_name, const char *button_name, const char *button_suffix, ir_code code, int reps)
Definition: ir_remote.c:601
#define RC5
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
Definition: ir_remote.c:167
#define SPACE_FIRST
__u32 repeat_gap
#define CONST_LENGTH
#define GRUNDIG
lirc_t srepeat
void map_gap(const struct ir_remote *remote, struct decode_ctx_t *ctx, const struct timeval *start, const struct timeval *last, lirc_t signal_length)
Definition: ir_remote.c:263
#define SERIAL
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
Definition: ir_remote.c:87
#define BO
ir_code code
const struct driver const * curr_driver
Definition: driver.c:24
ir_code rc6_mask
struct ir_remote * repeat_remote
Definition: ir_remote.c:53
ir_code toggle_bit_mask
int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code, int delay)
Definition: ir_remote.c:693
#define RAW_CODES
ir_code ignore_mask
#define XMP