summaryrefslogtreecommitdiffstats
path: root/video_switch.c
blob: 74d70b57dbadeedb72b3cf89fd43ae6c5795144b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#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