23 #define LIRCD_EXACT_GAP_THRESHOLD 10000
25 #include "include/media/lirc.h"
26 #include "lirc/lirc_log.h"
27 #include "lirc/transmit.h"
35 lirc_t _data[WBUF_SIZE];
45 static void send_signals(lirc_t* signals,
int n);
46 static int init_send_or_sim(
struct ir_remote* remote,
struct ir_ncode* code,
int sim,
int repeat_preset);
57 memset(&send_buffer, 0,
sizeof(send_buffer));
60 static void clear_send_buffer(
void)
64 send_buffer.too_long = 0;
65 send_buffer.is_biphase = 0;
66 send_buffer.pendingp = 0;
67 send_buffer.pendings = 0;
71 static void add_send_buffer(lirc_t data)
73 if (send_buffer.wptr < WBUF_SIZE) {
74 LOGPRINTF(3,
"adding to transmit buffer: %u", data);
75 send_buffer.sum += data;
76 send_buffer._data[send_buffer.wptr] = data;
79 send_buffer.too_long = 1;
83 static void send_pulse(lirc_t data)
85 if (send_buffer.pendingp > 0) {
86 send_buffer.pendingp += data;
88 if (send_buffer.pendings > 0) {
89 add_send_buffer(send_buffer.pendings);
90 send_buffer.pendings = 0;
92 send_buffer.pendingp = data;
96 static void send_space(lirc_t data)
98 if (send_buffer.wptr == 0 && send_buffer.pendingp == 0) {
102 if (send_buffer.pendings > 0) {
103 send_buffer.pendings += data;
105 if (send_buffer.pendingp > 0) {
106 add_send_buffer(send_buffer.pendingp);
107 send_buffer.pendingp = 0;
109 send_buffer.pendings = data;
113 static int bad_send_buffer(
void)
115 if (send_buffer.too_long != 0)
117 if (send_buffer.wptr == WBUF_SIZE && send_buffer.pendingp > 0)
122 static int check_send_buffer(
void)
126 if (send_buffer.wptr == 0) {
130 for (i = 0; i < send_buffer.wptr; i++) {
131 if (send_buffer.data[i] == 0) {
144 static void flush_send_buffer(
void)
146 if (send_buffer.pendingp > 0) {
147 add_send_buffer(send_buffer.pendingp);
148 send_buffer.pendingp = 0;
150 if (send_buffer.pendings > 0) {
151 add_send_buffer(send_buffer.pendings);
152 send_buffer.pendings = 0;
156 static void sync_send_buffer(
void)
158 if (send_buffer.pendingp > 0) {
159 add_send_buffer(send_buffer.pendingp);
160 send_buffer.pendingp = 0;
162 if (send_buffer.wptr > 0 && send_buffer.wptr % 2 == 0)
166 static void send_header(
struct ir_remote* remote)
168 if (has_header(remote)) {
169 send_pulse(remote->phead);
170 send_space(remote->
shead);
174 static void send_foot(
struct ir_remote* remote)
176 if (has_foot(remote)) {
177 send_space(remote->
sfoot);
178 send_pulse(remote->pfoot);
182 static void send_lead(
struct ir_remote* remote)
184 if (remote->
plead != 0)
185 send_pulse(remote->
plead);
188 static void send_trail(
struct ir_remote* remote)
191 send_pulse(remote->
ptrail);
194 static void send_data(
struct ir_remote* remote,
ir_code data,
int bits,
int done)
197 int all_bits = bit_count(remote);
201 data = reverse(data, bits);
202 if (is_rcmm(remote)) {
203 mask = 1 << (all_bits - 1 - done);
204 if (bits % 2 || done % 2) {
205 logprintf(LIRC_ERROR,
"invalid bit number.");
208 for (i = 0; i < bits; i += 2, mask >>= 2) {
211 send_pulse(remote->pzero);
212 send_space(remote->
szero);
216 send_pulse(remote->pone);
217 send_space(remote->
sone);
220 send_pulse(remote->ptwo);
221 send_space(remote->
stwo);
224 send_pulse(remote->pthree);
225 send_space(remote->
sthree);
231 }
else if (is_xmp(remote)) {
232 if (bits % 4 || done % 4) {
233 logprintf(LIRC_ERROR,
"invalid bit number.");
236 for (i = 0; i < bits; i += 4) {
239 nibble = reverse(data & 0xf, 4);
240 send_pulse(remote->pzero);
241 send_space(remote->
szero + nibble * remote->
sone);
247 mask = ((
ir_code)1) << (all_bits - 1 - done);
248 for (i = 0; i < bits; i++, mask >>= 1) {
250 if (toggle_bit_mask_bits == 1) {
253 if (remote->toggle_bit_mask_state & mask)
256 if (remote->toggle_bit_mask_state & mask)
260 if (has_toggle_mask(remote) && mask & remote->
toggle_mask && remote->toggle_mask_state % 2)
263 if (is_biphase(remote)) {
265 send_space(2 * remote->
sone);
266 send_pulse(2 * remote->pone);
268 send_space(remote->
sone);
269 send_pulse(remote->pone);
271 }
else if (is_space_first(remote)) {
272 send_space(remote->
sone);
273 send_pulse(remote->pone);
275 send_pulse(remote->pone);
276 send_space(remote->
sone);
280 send_pulse(2 * remote->pzero);
281 send_space(2 * remote->
szero);
282 }
else if (is_space_first(remote)) {
283 send_space(remote->
szero);
284 send_pulse(remote->pzero);
286 send_pulse(remote->pzero);
287 send_space(remote->
szero);
294 static void send_pre(
struct ir_remote* remote)
296 if (has_pre(remote)) {
298 if (remote->pre_p > 0 && remote->
pre_s > 0) {
299 send_pulse(remote->pre_p);
300 send_space(remote->
pre_s);
305 static void send_post(
struct ir_remote* remote)
307 if (has_post(remote)) {
308 if (remote->post_p > 0 && remote->
post_s > 0) {
309 send_pulse(remote->post_p);
310 send_space(remote->
post_s);
316 static void send_repeat(
struct ir_remote* remote)
319 send_pulse(remote->prepeat);
337 send_buffer.sum -= remote->phead + remote->
shead;
340 static void send_signals(lirc_t* signals,
int n)
344 for (i = 0; i < n; i++)
345 add_send_buffer(signals[i]);
350 return init_send_or_sim(remote, code, 0, 0);
359 return init_send_or_sim(remote, code, 1, repeat_preset);
368 return send_buffer.wptr;
374 return send_buffer.data;
379 return send_buffer.sum;
382 static int init_send_or_sim(
struct ir_remote* remote,
struct ir_ncode* code,
int sim,
int repeat_preset)
384 int i, repeat = repeat_preset;
386 if (is_grundig(remote) || is_goldstar(remote) || is_serial(remote) || is_bo(remote)) {
388 logprintf(LIRC_ERROR,
"sorry, can't send this protocol yet");
392 if (strcmp(remote->
name,
"lirc") == 0) {
393 send_buffer.data[send_buffer.wptr] =
LIRC_EOF | 1;
394 send_buffer.wptr += 1;
398 if (is_biphase(remote))
399 send_buffer.is_biphase = 1;
402 remote->repeat_countdown = remote->
min_repeat;
408 if (repeat && has_repeat(remote)) {
413 if (!is_raw(remote)) {
417 next_code = code->
code;
421 if (repeat && has_repeat_mask(remote))
424 send_code(remote, next_code, repeat);
425 if (!sim && has_toggle_mask(remote)) {
426 remote->toggle_mask_state++;
427 if (remote->toggle_mask_state == 4)
428 remote->toggle_mask_state = 2;
430 send_buffer.data = send_buffer._data;
434 logprintf(LIRC_ERROR,
"no signals for raw send");
437 if (send_buffer.wptr > 0) {
440 send_buffer.data = code->
signals;
441 send_buffer.wptr = code->
length;
442 for (i = 0; i < code->
length; i++)
443 send_buffer.sum += code->
signals[i];
448 if (bad_send_buffer()) {
450 logprintf(LIRC_ERROR,
"buffer too small");
456 if (has_repeat_gap(remote) && repeat && has_repeat(remote)) {
459 }
else if (is_const(remote)) {
460 if (min_gap(remote) > send_buffer.sum) {
464 logprintf(LIRC_ERROR,
"too short gap: %u", remote->
gap);
474 if (code->
next != NULL) {
483 if ((remote->repeat_countdown > 0 || code->
transmit_state != NULL)
485 if (send_buffer.data != send_buffer._data) {
489 LOGPRINTF(1,
"unrolling raw signal optimisation");
490 signals = send_buffer.data;
491 n = send_buffer.wptr;
492 send_buffer.data = send_buffer._data;
493 send_buffer.wptr = 0;
495 send_signals(signals, n);
497 LOGPRINTF(1,
"concatenating low gap signals");
499 remote->repeat_countdown--;
510 if (!check_send_buffer()) {
512 logprintf(LIRC_ERROR,
"invalid send buffer");
513 logprintf(LIRC_ERROR,
"this remote configuration cannot be used to transmit");
const lirc_t * send_buffer_data(void)
struct ir_code_node * next
lirc_t send_buffer_sum(void)
void send_buffer_init(void)
int send_buffer_put(struct ir_remote *remote, struct ir_ncode *code)
#define LOGPRINTF(level, fmt, args...)
struct ir_code_node * transmit_state
int send_buffer_length(void)
struct ir_remote * repeat_remote