#include "project.h" #include "lirc.h" #define NOTPULSE 0 #define PULSE 0 #define ONE(a) ((a) | PULSE) #define ZERO(a) ((a) | NOTPULSE) typedef struct { int fd; int a; int b; unsigned mask; } MS; struct MM_struct { MS *left, *right, *top; }; static uint8_t read_byte (int **bits, int *n) { uint8_t c = 0; int i = 8; while (i-- && (*n)--) { c <<= 1; c |= ! !(*((*bits)++)); } return c; } static void dump_bits (int *bits, int n) { uint8_t b; int i; for (i = 0; i < n; ++i) { printf ("%d", bits[i]); if ((i % 8) == 7) printf (" "); } printf ("\n"); while (n > 0) { b = read_byte (&bits, &n); printf ("%02x ", b); } printf ("\n"); } static void bit (int b) { static int bits[1024]; static int bc; if (b == 2) { bc = 0; return; } if (b == 3) { if (bc) dump_bits (bits, bc); bc = 0; return; } // printf(" > bit[%3d]=%d\n",bc,b); bits[bc++] = b; } static void ana (lirc_t * buf, int n, int mode) { int s = 0; while (n--) { lirc_t v = *(buf++); if (mode == 2) s = (v & 0xff000000U) ? 1 : 0; else s = !s; v &= 0xffffff; // printf ("%5d %d\n", v, s); if (s) continue; if ((v < 5000) && (v > 4000)) { bit (2); } else if ((v < 750) && (v > 450)) { bit (0); } else if ((v < 1850) && (v > 1500)) { bit (1); } else { bit (3); } } } static void listen (int fd) { for (;;) { lirc_t v; read (fd, &v, sizeof (v)); ana (&v, 1, 2); } } static int send_command (int fd, unsigned mask, uint8_t * buf, int n) { lirc_t cbuf[128], *ptr = cbuf; uint8_t c; int b = 0; printf ("Sending %x %x %x %x to fd %d mask %x\n", buf[0], buf[1], buf[2], buf[3], fd, mask); *(ptr++) = ONE (9300); *(ptr++) = ZERO (4500); b += 2; while (n--) { for (c = 128; c; c >>= 1) { if ((*buf) & c) { *(ptr++) = ONE (550); *(ptr++) = ZERO (1650); } else { *(ptr++) = ONE (550); *(ptr++) = ZERO (600); } b += 2; } buf++; } *(ptr++) = ONE (550); b++; #if 0 ana (cbuf, b, 0); bit (3); #endif ioctl (fd, LIRC_SET_TRANSMITTER_MASK, &mask); #if 0 if (write (fd, cbuf, sizeof (lirc_t) * b) != (b * sizeof (lirc_t))) return -1; usleep (20000); #endif if (write (fd, cbuf, sizeof (lirc_t) * b) != (b * sizeof (lirc_t))) return -1; usleep (100000); return 0; } static int send_command4 (int fd, unsigned m, uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4) { uint8_t c[] = { c1, c2, c3, c4 }; return send_command (fd, m, c, sizeof (c)); } static int s42 (int fd, int ab, unsigned mask, int d) { uint8_t c3[2][5] = { {0xb0, 0x98, 0xa8, 0x0a, 0x52}, {0x40, 0x18, 0x8a, 0xca, 0xa2} }; uint8_t c4[2][5] = { {0x4f, 0x67, 0x57, 0xf5, 0xad}, {0xbf, 0xe7, 0x75, 0x35, 0x5d} }; if (ab < 0) return -1; if (ab > 1) return -1; if (d < 0) return -1; if (d > 4) return -1; return send_command4 (fd, mask, 0x00, 0xff, c3[ab][d], c4[ab][d]); } static int s51 (int fd, unsigned mask, int d) { uint8_t c3[6] = { 0x00, 0xa0, 0x10, 0x90, 0x50, 0xb0 }; uint8_t c4[6] = { 0x00, 0x5f, 0xef, 0x6f, 0xaf, 0x4f }; if (d < 1) return -1; if (d > 5) return -1; return send_command4 (fd, mask, 0x00, 0xfd, c3[d], c4[d]); } static int MS_set (MS * m, int a, int b) { if (!m) return -1; if ((a != -1) && (m->a != a)) { if (s42 (m->fd, 0, m->mask, a)) return -1; m->a = a; } if ((b != -1) && (m->b != b)) { if (s42 (m->fd, 1, m->mask, b)) return -1; m->b = b; } return 0; } static MS * MS_open (int fd, unsigned mask) { MS *ret = malloc (sizeof (MS)); ret->fd = fd; ret->a = -1; ret->b = -1; ret->mask = mask; return ret; } MM * MM_open (void) { int fd; MM *ret = malloc (sizeof (MM)); unsigned u; fd = open ("/dev/lirc0", O_RDWR); u = 40000; ioctl (fd, LIRC_SET_SEND_CARRIER, &u); //s51(fd,1,2); //listen(fd); ret->left = MS_open (fd, 1); ret->right = NULL; //MS_open (fd, 2); fd = open ("/dev/lirc1", O_RDWR); u = 40000; ioctl (fd, LIRC_SET_SEND_CARRIER, &u); ret->top = NULL; // MS_open (fd, 1); return ret; } int MM_set (MM * m, int a, int b) { return MS_set (m->left, a, b); } int VS_set (MM * m, int v) { return s51 (m->left->fd, 2, v); } #if 0 main (int argc, char *argv[]) { MM *mm; mm = MM_open (); // MM_set (mm,1, 2); //MM_set (mm,-1, 1); // MM_set (mm,1, -1); } #endif