summaryrefslogtreecommitdiffstats
path: root/set_color/set_color.c
blob: 692355aec0671cb8d256fe4fb71c49985959cb55 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Hidraw Userspace Example
 *
 * Copyright (c) 2010 Alan Ott <alan@signal11.us>
 * Copyright (c) 2010 Signal 11 Software
 *
 * The code may be used by anyone for any purpose,
 * and can serve as a starting point for developing
 * applications using hidraw.
 */

/* Linux */
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>

/* Unix */
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>


typedef struct
{
  uint8_t r;
  uint8_t g;
  uint8_t b;
} C;

#define N_LEDS	9

static unsigned map[N_LEDS]={2,1,0,3,4,5,8,7,6};

C current[N_LEDS];
C last[N_LEDS];


void
sync_leds (int fd)
{
  unsigned i;
  uint8_t buf[8];

  for (i = 0; i < N_LEDS; ++i)
    {
      /* Send a Report to the Device */
      if ((last[i].r != current[i].r)
          || (last[i].g != current[i].g) || (last[i].b != current[i].b))
        {
          buf[0] = 0x05;
          buf[1] = map[i];
          buf[2] = current[i].r;
          buf[3] = current[i].g;
          buf[4] = current[i].b;
          write (fd, buf, 8);
          last[i] = current[i];
        }
    }
}

unsigned f(int v)
{
v-=5;
if (v<0) return 0;
return v;
}


void fade_leds(int fd)
{
unsigned i;
  for (i = 0; i < N_LEDS; ++i)
    {
	current[i].r=f(current[i].r);
	current[i].g=f(current[i].g);
	current[i].b=f(current[i].b);

	}

	sync_leds(fd);

}

int
open_kb (int *fds)
{
  struct hidraw_devinfo info;
  unsigned i;
  int n = 0;
  for (i = 0; i < 128; ++i)
    {
      int fd;
      char device[128];
      char buf[128];
      sprintf (device, "/dev/hidraw%d", i);
      fd = open (device, O_RDWR | O_NONBLOCK);
      if (fd < 0)
        continue;

      memset (buf, 0, sizeof (buf));
      ioctl (fd, HIDIOCGRAWNAME (256), buf);
      buf[sizeof (buf) - 1] = 0;


      if (strcmp (buf, "YMDK YMD09_32A"))
        {
          close (fd);
          fd = -1;
          continue;
        }

      fds[n++] = fd;

    }


  if (n < 2)
    {
      printf ("Devices not found\n");
      return -1;
    }
  return 0;
}

int
main (int argc, char **argv)
{
  int fds[2];
  fd_set rfds;
  ssize_t len;
  char buf[32];
  struct timeval tv;

  unsigned j, k;

  memset (last, 0xff, sizeof (last));

  if (open_kb (fds))
    return -1;


current[4].g=0xff;

  FD_ZERO (&rfds);
  sync_leds (fds[1]);

	tv.tv_sec=0;
tv.tv_usec=0;

  for (;;)
    {
      FD_SET (fds[0], &rfds);

	if (!tv.tv_usec) 
		tv.tv_usec=20000;
	
      switch (select (fds[0] + 1, &rfds, NULL, NULL, &tv))
        {
        case -1:
          break;
        case 1:

          len = read (fds[0], buf, sizeof (buf));
	  
          if (len < 3)
            break;

          for (j = 2; j < len; ++j)
            {
	if (!buf[j]) continue;
              k = buf[j] - 4;


              current[k].r = rand() & 0xff ;
              current[k].g = rand() & 0xff ;
              current[k].b = rand() & 0xff ;
            }

          sync_leds (fds[1]);

          break;
	case 0:
	  fade_leds(fds[1]);
        }

    }

  close (fds[1]);
  close (fds[0]);
  return 0;
}