Loading...
Searching...
No Matches
ir_remote.h
Go to the documentation of this file.
/****************************************************************************
** ir_remote.h *************************************************************
****************************************************************************
*
* ir_remote.h - describes and decodes the signals from IR remotes
*
* Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
* Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
*
*/
#ifndef IR_REMOTE_H
#define IR_REMOTE_H
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "driver.h"
#include "ir_remote_types.h"
#ifdef __cplusplus
extern "C" {
#endif
struct ir_ncode* ncode_dup(struct ir_ncode* ncode);
void ncode_free(struct ir_ncode* ncode);
extern struct ir_remote* last_remote;
extern struct ir_remote* repeat_remote;
extern struct ir_ncode* repeat_code;
static inline ir_code get_ir_code(const struct ir_ncode* ncode,
const struct ir_code_node* node)
{
if (ncode->next && node != NULL)
return node->code;
return ncode->code;
}
static inline struct ir_code_node*
get_next_ir_code_node(const struct ir_ncode* ncode,
const struct ir_code_node* node)
{
if (node == NULL)
return ncode->next;
return node->next;
}
static inline int bit_count(const struct ir_remote* remote)
{
return remote->pre_data_bits + remote->bits + remote->post_data_bits;
}
static inline int bits_set(ir_code data)
{
int ret = 0;
while (data) {
if (data & 1)
ret++;
data >>= 1;
}
return ret;
}
static inline ir_code reverse(ir_code data, int bits)
{
int i;
ir_code c;
c = 0;
for (i = 0; i < bits; i++)
c |= (ir_code)(((data & (((ir_code)1) << i)) ? 1 : 0))
<< (bits - 1 - i);
return c;
}
static inline int is_pulse(lirc_t data)
{
return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_PULSE) ? 1 : 0;
}
static inline int is_space(lirc_t data)
{
return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_SPACE) ? 1 : 0;
}
static inline int is_timeout(lirc_t data)
{
return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_TIMEOUT) ? 1 : 0;
}
static inline int is_overflow(lirc_t data)
{
return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_OVERFLOW) ? 1 : 0;
}
static inline int has_repeat(const struct ir_remote* remote)
{
if (remote->prepeat > 0 && remote->srepeat > 0)
return 1;
else
return 0;
}
static inline void set_protocol(struct ir_remote* remote, int protocol)
{
remote->flags &= ~(IR_PROTOCOL_MASK);
remote->flags |= protocol;
}
static inline int is_raw(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == RAW_CODES)
return 1;
else
return 0;
}
static inline int is_space_enc(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_ENC)
return 1;
else
return 0;
}
static inline int is_space_first(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_FIRST)
return 1;
else
return 0;
}
static inline int is_rc5(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == RC5)
return 1;
else
return 0;
}
static inline int is_rc6(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == RC6 || remote->rc6_mask)
return 1;
else
return 0;
}
static inline int is_biphase(const struct ir_remote* remote)
{
if (is_rc5(remote) || is_rc6(remote))
return 1;
else
return 0;
}
static inline int is_rcmm(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == RCMM)
return 1;
else
return 0;
}
static inline int is_grundig(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
return 1;
else
return 0;
}
static inline int is_bo(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == BO)
return 1;
else
return 0;
}
static inline int is_serial(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
return 1;
else
return 0;
}
static inline int is_xmp(const struct ir_remote* remote)
{
if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
return 1;
else
return 0;
}
static inline int is_const(const struct ir_remote* remote)
{
if (remote->flags & CONST_LENGTH)
return 1;
else
return 0;
}
static inline int has_repeat_gap(const struct ir_remote* remote)
{
if (remote->repeat_gap > 0)
return 1;
else
return 0;
}
static inline int has_pre(const struct ir_remote* remote)
{
if (remote->pre_data_bits > 0)
return 1;
else
return 0;
}
static inline int has_post(const struct ir_remote* remote)
{
if (remote->post_data_bits > 0)
return 1;
else
return 0;
}
static inline int has_header(const struct ir_remote* remote)
{
if (remote->phead > 0 && remote->shead > 0)
return 1;
else
return 0;
}
static inline int has_foot(const struct ir_remote* remote)
{
if (remote->pfoot > 0 && remote->sfoot > 0)
return 1;
else
return 0;
}
static inline int has_toggle_bit_mask(const struct ir_remote* remote)
{
if (remote->toggle_bit_mask > 0)
return 1;
else
return 0;
}
static inline int has_ignore_mask(const struct ir_remote* remote)
{
if (remote->ignore_mask > 0)
return 1;
else
return 0;
}
static inline int has_repeat_mask(struct ir_remote* remote)
{
if (remote->repeat_mask > 0)
return 1;
else
return 0;
}
static inline int has_toggle_mask(const struct ir_remote* remote)
{
if (remote->toggle_mask > 0)
return 1;
else
return 0;
}
static inline lirc_t min_gap(const struct ir_remote* remote)
{
if (remote->gap2 != 0 && remote->gap2 < remote->gap)
return remote->gap2;
else
return remote->gap;
}
static inline lirc_t max_gap(const struct ir_remote* remote)
{
if (remote->gap2 > remote->gap)
return remote->gap2;
else
return remote->gap;
}
static inline unsigned int get_duty_cycle(const struct ir_remote* remote)
{
if (remote->duty_cycle == 0)
return 50;
else if (remote->duty_cycle < 0)
return 1;
else if (remote->duty_cycle > 100)
return 100;
else
return remote->duty_cycle;
}
/* check if delta is inside exdelta +/- exdelta*eps/100 */
static inline int expect(const struct ir_remote* remote,
lirc_t delta,
lirc_t exdelta)
{
int aeps = curr_driver->resolution > remote->aeps ?
curr_driver->resolution : remote->aeps;
if (abs(exdelta - delta) <= exdelta * remote->eps / 100
|| abs(exdelta - delta) <= aeps)
return 1;
return 0;
}
static inline int expect_at_least(const struct ir_remote* remote,
lirc_t delta,
lirc_t exdelta)
{
int aeps = curr_driver->resolution > remote->aeps ?
curr_driver->resolution : remote->aeps;
if (delta + exdelta * remote->eps / 100 >= exdelta
|| delta + aeps >= exdelta)
return 1;
return 0;
}
static inline int expect_at_most(const struct ir_remote* remote,
lirc_t delta,
lirc_t exdelta)
{
int aeps = curr_driver->resolution > remote->aeps ?
curr_driver->resolution : remote->aeps;
if (delta <= exdelta + exdelta * remote->eps / 100
|| delta <= exdelta + aeps)
return 1;
return 0;
}
static inline lirc_t upper_limit(const struct ir_remote* remote, lirc_t val)
{
int aeps = curr_driver->resolution > remote->aeps ?
curr_driver->resolution : remote->aeps;
lirc_t eps_val = val * (100 + remote->eps) / 100;
lirc_t aeps_val = val + aeps;
return eps_val > aeps_val ? eps_val : aeps_val;
}
static inline lirc_t lower_limit(const struct ir_remote* remote, lirc_t val)
{
int aeps = curr_driver->resolution > remote->aeps ?
curr_driver->resolution : remote->aeps;
lirc_t eps_val = val * (100 - remote->eps) / 100;
lirc_t aeps_val = val - aeps;
if (eps_val <= 0)
eps_val = 1;
if (aeps_val <= 0)
aeps_val = 1;
return eps_val < aeps_val ? eps_val : aeps_val;
}
/* only works if last <= current */
static inline unsigned long time_elapsed(const struct timeval* last,
const struct timeval* current)
{
unsigned long secs, diff;
secs = current->tv_sec - last->tv_sec;
diff = 1000000 * secs + current->tv_usec - last->tv_usec;
return diff;
}
static inline ir_code gen_mask(int bits)
{
int i;
ir_code mask;
mask = 0;
for (i = 0; i < bits; i++) {
mask <<= 1;
mask |= 1;
}
return mask;
}
static inline ir_code gen_ir_code(const struct ir_remote* remote,
ir_code pre,
ir_code code,
ir_code post)
{
ir_code all;
all = (pre & gen_mask(remote->pre_data_bits));
all <<= remote->bits;
all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
all <<= remote->post_data_bits;
all |= post & gen_mask(remote->post_data_bits);
return all;
}
const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
const struct ir_remote* remote);
struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
const char* name);
void get_frequency_range(const struct ir_remote* remotes,
unsigned int* min_freq,
unsigned int* max_freq);
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);
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);
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);
struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
const char* name);
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);
char* decode_all(struct ir_remote* remotes);
int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
#ifdef __cplusplus
}
#endif
void ir_remote_init(int use_dyncodes);
const struct ir_remote* get_decoding(void);
#endif
curr_driver
const struct driver *const curr_driver
Read-only access to drv for client code.
Definition driver.c:34
driver.h
Interface to the userspace drivers.
write_message
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)
Formats the arguments into a readable string.
Definition ir_remote.c:709
get_code_by_name
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Return code with given name in remote's list of codes or NULL.
Definition ir_remote.c:393
get_frequency_range
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
Definition ir_remote.c:152
get_filter_parameters
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:189
repeat_code
struct ir_ncode * repeat_code
Global pointer to the code currently repeating.
Definition ir_remote.c:59
ncode_free
void ncode_free(struct ir_ncode *ncode)
Dispose an ir_ncode instance obtained from ncode_dup().
Definition ir_remote.c:100
last_remote
struct ir_remote * last_remote
TODO.
Definition ir_remote.c:55
send_ir_ncode
int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code, int delay)
Transmits the actual code in the second argument by calling the current hardware driver.
Definition ir_remote.c:819
decode_all
char * decode_all(struct ir_remote *remotes)
Tries to decode current signal trying all known remotes.
Definition ir_remote.c:729
repeat_remote
struct ir_remote * repeat_remote
Global pointer to the remote that contains the code currently repeating.
Definition ir_remote.c:57
ir_remote_init
void ir_remote_init(int use_dyncodes)
Initiate: define if dynamic codes should be used.
Definition ir_remote.c:120
get_decoding
const struct ir_remote * get_decoding(void)
Return pointer to currently decoded remote.
Definition ir_remote.c:850
get_ir_remote
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
Return ir_remote with given name in remotes list, or NULL if not found.
Definition ir_remote.c:247
map_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:279
map_gap
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:325
is_in_remotes
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
Test if a given remote is in a list of remotes.
Definition ir_remote.c:235
ncode_dup
struct ir_ncode * ncode_dup(struct ir_ncode *ncode)
Create a malloc'd, deep copy of ncode.
Definition ir_remote.c:65
ir_remote_types.h
Describes and decodes the signals from IR remotes.
RAW_CODES
#define RAW_CODES
for internal use only
Definition ir_remote_types.h:100
XMP
#define XMP
XMP protocol.
Definition ir_remote_types.h:114
RC6
#define RC6
IR data follows RC6 protocol.
Definition ir_remote_types.h:107
GRUNDIG
#define GRUNDIG
encoding found on Grundig remote
Definition ir_remote_types.h:111
BO
#define BO
encoding found on Bang & Olufsen remote
Definition ir_remote_types.h:112
ir_code
uint64_t ir_code
Denotes an internal coded representation for an IR transmission.
Definition ir_remote_types.h:43
SPACE_FIRST
#define SPACE_FIRST
bits are encoded as space+pulse
Definition ir_remote_types.h:110
SPACE_ENC
#define SPACE_ENC
IR data is space encoded.
Definition ir_remote_types.h:109
RC5
#define RC5
IR data follows RC5 protocol.
Definition ir_remote_types.h:101
SERIAL
#define SERIAL
serial protocol
Definition ir_remote_types.h:113
CONST_LENGTH
#define CONST_LENGTH
signal length+gap is always constant
Definition ir_remote_types.h:120
RCMM
#define RCMM
IR data follows RC-MM protocol.
Definition ir_remote_types.h:108
eps
unsigned int eps
Shared list of remotes.
Definition irrecord.c:62
aeps
lirc_t aeps
Error tolerance in per cent.
Definition irrecord.c:63
decode_ctx_t
State describing code, pre, post + gap and repeat state.
Definition ir_remote_types.h:137
driver::resolution
unsigned int resolution
The resolution in microseconds of the recorded durations when reading signals.
Definition driver.h:230
ir_code_node
An ir_code for entering into (singly) linked lists, i.e.
Definition ir_remote_types.h:49
ir_ncode
IR Command, corresponding to one (command defining) line of the configuration file.
Definition ir_remote_types.h:60
ir_ncode::next
struct ir_code_node * next
Linked list of the subsequent ir_code's, after the first one.
Definition ir_remote_types.h:75
ir_ncode::code
ir_code code
The first code of the command.
Definition ir_remote_types.h:65
ir_ncode::name
char * name
Name of command.
Definition ir_remote_types.h:62
ir_remote
One remote as represented in the configuration file.
Definition ir_remote_types.h:150
ir_remote::repeat_gap
uint32_t repeat_gap
time between two repeat codes if different from gap
Definition ir_remote_types.h:188
ir_remote::aeps
unsigned int aeps
detecting very short pulses is difficult with relative tolerance for some remotes,...
Definition ir_remote_types.h:157
ir_remote::gap2
uint32_t gap2
time between signals in usecs
Definition ir_remote_types.h:187
ir_remote::sfoot
lirc_t sfoot
foot
Definition ir_remote_types.h:176
ir_remote::rc6_mask
ir_code rc6_mask
RC-6 doubles signal length of some bits.
Definition ir_remote_types.h:206
ir_remote::duty_cycle
unsigned int duty_cycle
0<duty cycle<=100 default: 50
Definition ir_remote_types.h:204
ir_remote::repeat_mask
ir_code repeat_mask
mask defines which bits are inverted for repeats
Definition ir_remote_types.h:217
ir_remote::srepeat
lirc_t srepeat
indicate repeating
Definition ir_remote_types.h:177
ir_remote::bits
int bits
bits (length of code)
Definition ir_remote_types.h:154
ir_remote::post_data_bits
int post_data_bits
length of post_data
Definition ir_remote_types.h:181
ir_remote::ignore_mask
ir_code ignore_mask
mask defines which bits can be ignored when matching a code
Definition ir_remote_types.h:216
ir_remote::shead
lirc_t shead
header
Definition ir_remote_types.h:169
ir_remote::toggle_mask
ir_code toggle_mask
Sharp (?) error detection scheme.
Definition ir_remote_types.h:205
ir_remote::flags
int flags
flags
Definition ir_remote_types.h:155
ir_remote::gap
uint32_t gap
time between signals in usecs
Definition ir_remote_types.h:186
ir_remote::eps
int eps
eps (relative tolerance)
Definition ir_remote_types.h:156
ir_remote::name
const char * name
name of remote control
Definition ir_remote_types.h:151
ir_remote::toggle_bit_mask
ir_code toggle_bit_mask
previously only one bit called toggle_bit
Definition ir_remote_types.h:190
ir_remote::pre_data_bits
int pre_data_bits
length of pre_data
Definition ir_remote_types.h:179