rb-blink1 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,296 @@
1
+ #include <stdlib.h>
2
+ #include "ruby.h"
3
+ #include "blink1-lib.h"
4
+ #include <stdio.h>
5
+
6
+ struct Blink1Instance {
7
+ hid_device *dev;
8
+ int opened;
9
+ };
10
+
11
+ static int degamma = 1;
12
+
13
+ #pragma mark - Static methods
14
+
15
+ static VALUE rb_blink1_vid(VALUE self) {
16
+ return INT2NUM(blink1_vid());
17
+ }
18
+
19
+ static VALUE rb_blink1_pid(VALUE self) {
20
+ return INT2NUM(blink1_pid());
21
+ }
22
+
23
+ static VALUE rb_blink1_sortPaths(VALUE self) {
24
+ blink1_sortPaths();
25
+ return Qnil;
26
+ }
27
+
28
+ static VALUE rb_blink1_sortSerials(VALUE self) {
29
+ blink1_sortSerials();
30
+ return Qnil;
31
+ }
32
+
33
+ static VALUE rb_blink1_enumerate(VALUE self) {
34
+ return INT2NUM(blink1_enumerate());
35
+ }
36
+
37
+ static VALUE rb_blink1_enumerateByVidPid(VALUE self, VALUE vid, VALUE pid) {
38
+ return INT2NUM(blink1_enumerateByVidPid(FIX2INT(vid), FIX2INT(pid)));
39
+ }
40
+
41
+ static VALUE rb_blink1_blink1_getCachedPath(VALUE self, VALUE i) {
42
+ return rb_str_new2(blink1_getCachedPath(FIX2INT(i)));
43
+ }
44
+
45
+ static VALUE rb_blink1_getCachedSerial(VALUE self, VALUE i) {
46
+ const wchar_t *ret = blink1_getCachedSerial(FIX2INT(i));
47
+ char *dest;
48
+ wcstombs(dest, ret, sizeof(ret));
49
+ return rb_str_new2(dest);
50
+ }
51
+
52
+ static VALUE rb_blink1_getCachedCount(VALUE self) {
53
+ return INT2NUM(blink1_getCachedCount());
54
+ }
55
+
56
+ static VALUE rb_blink1_error_msg(VALUE self, VALUE code) {
57
+ char *msg = blink1_error_msg(FIX2INT(code));
58
+ return msg == NULL ? Qnil : rb_str_new2(msg);
59
+ }
60
+
61
+ static VALUE rb_blink1_getDegammaEnabled(VALUE self) {
62
+ return degamma == 1 ? Qtrue : Qfalse;
63
+ }
64
+
65
+ static VALUE rb_blink1_setDegammaEnabled(VALUE self, VALUE enabled) {
66
+ if(RTEST(enabled)) {
67
+ degamma = 1;
68
+ blink1_enableDegamma();
69
+ } else {
70
+ degamma = 0;
71
+ blink1_disableDegamma();
72
+ }
73
+ return Qnil;
74
+ }
75
+
76
+ static VALUE rb_blink1_degamma(VALUE self, VALUE i) {
77
+ return INT2NUM(blink1_degamma(FIX2INT(i)));
78
+ }
79
+
80
+ static VALUE rb_blink1_sleep(VALUE self, VALUE delayMillis) {
81
+ blink1_sleep(FIX2UINT(delayMillis));
82
+ return Qnil;
83
+ }
84
+
85
+ #pragma mark - Instance methods
86
+
87
+ void rb_blink1_free(struct Blink1Instance *ins) {
88
+ if(ins->opened == 1) {
89
+ blink1_close(ins->dev);
90
+ ins->opened = 0;
91
+ }
92
+ // free(ins);
93
+ ruby_xfree(ins);
94
+ }
95
+
96
+ static VALUE rb_blink1_allocate(VALUE self) {
97
+ struct Blink1Instance *ins = malloc(sizeof(struct Blink1Instance));
98
+ ins->opened = 0;
99
+ return Data_Wrap_Struct(self, 0, rb_blink1_free, ins);
100
+ }
101
+
102
+ static VALUE rb_blink1_opened(VALUE self) {
103
+ struct Blink1Instance *ins;
104
+ Data_Get_Struct(self, struct Blink1Instance, ins);
105
+ return ins->opened == 1 ? Qtrue : Qfalse;
106
+ }
107
+
108
+ static VALUE rb_blink1_open(VALUE self) {
109
+ struct Blink1Instance *ins;
110
+ Data_Get_Struct(self, struct Blink1Instance, ins);
111
+ if(ins->opened == 0) {
112
+ ins->dev = blink1_open();
113
+ ins->opened = 1;
114
+ return Qtrue;
115
+ }
116
+ return Qfalse;
117
+ }
118
+
119
+ static VALUE rb_blink1_openByPath(VALUE self, VALUE path) {
120
+ struct Blink1Instance *ins;
121
+ Data_Get_Struct(self, struct Blink1Instance, ins);
122
+ if(ins->opened == 0) {
123
+ ins->dev = blink1_open();
124
+ ins->opened = 1;
125
+ return Qtrue;
126
+ }
127
+ return Qfalse;
128
+ }
129
+
130
+ static VALUE rb_blink1_openBySerial(VALUE self, VALUE serial) {
131
+ struct Blink1Instance *ins;
132
+ Data_Get_Struct(self, struct Blink1Instance, ins);
133
+ if(ins->opened == 0) {
134
+ ins->dev = blink1_open();
135
+ ins->opened = 1;
136
+ return Qtrue;
137
+ }
138
+ return Qfalse;
139
+ }
140
+
141
+ static VALUE rb_blink1_openById(VALUE self, VALUE id) {
142
+ struct Blink1Instance *ins;
143
+ Data_Get_Struct(self, struct Blink1Instance, ins);
144
+ if(ins->opened == 0) {
145
+ ins->dev = blink1_open();
146
+ ins->opened = 1;
147
+ return Qtrue;
148
+ }
149
+ return Qfalse;
150
+ }
151
+
152
+ static VALUE rb_blink1_close(VALUE self) {
153
+ struct Blink1Instance *ins;
154
+ Data_Get_Struct(self, struct Blink1Instance, ins);
155
+ if(ins->opened == 1) {
156
+ blink1_close(ins->dev);
157
+ ins->opened = 0;
158
+ }
159
+ return Qnil;
160
+ }
161
+
162
+ static VALUE rb_blink1_getVersion(VALUE self) {
163
+ struct Blink1Instance *ins;
164
+ Data_Get_Struct(self, struct Blink1Instance, ins);
165
+ return INT2NUM(blink1_getVersion(ins->dev));
166
+ }
167
+
168
+ static VALUE rb_blink1_fadeToRGB(VALUE self, VALUE fadeMillis, VALUE r, VALUE g, VALUE b) {
169
+ struct Blink1Instance *ins;
170
+ Data_Get_Struct(self, struct Blink1Instance, ins);
171
+ return INT2NUM(blink1_fadeToRGB(ins->dev, FIX2UINT(fadeMillis), FIX2UINT(r), FIX2UINT(g), FIX2UINT(b)));
172
+ }
173
+
174
+ static VALUE rb_blink1_setRGB(VALUE self, VALUE r, VALUE g, VALUE b) {
175
+ struct Blink1Instance *ins;
176
+ Data_Get_Struct(self, struct Blink1Instance, ins);
177
+ return INT2NUM(blink1_setRGB(ins->dev, FIX2UINT(r), FIX2UINT(g), FIX2UINT(b)));
178
+ }
179
+
180
+ static VALUE rb_blink1_eeread(VALUE self, VALUE addr) {
181
+ struct Blink1Instance *ins;
182
+ uint8_t val = 0;
183
+ Data_Get_Struct(self, struct Blink1Instance, ins);
184
+ blink1_eeread(ins->dev, FIX2UINT(addr), &val);
185
+ return UINT2NUM(val);
186
+ }
187
+
188
+ static VALUE rb_blink1_eewrite(VALUE self, VALUE addr, VALUE val) {
189
+ struct Blink1Instance *ins;
190
+ Data_Get_Struct(self, struct Blink1Instance, ins);
191
+ return INT2NUM(blink1_eewrite(ins->dev, FIX2UINT(addr), FIX2UINT(val)));
192
+ }
193
+
194
+ static VALUE rb_blink1_serialnumread(VALUE self) {
195
+ struct Blink1Instance *ins;
196
+ Data_Get_Struct(self, struct Blink1Instance, ins);
197
+ uint8_t *serialnum;
198
+ blink1_serialnumread(ins->dev, &serialnum);
199
+ return UINT2NUM(serialnum);
200
+ }
201
+
202
+ static VALUE rb_blink1_serialnumwrite(VALUE self, VALUE serialnumstr) {
203
+ struct Blink1Instance *ins;
204
+ Data_Get_Struct(self, struct Blink1Instance, ins);
205
+ char *serialnumc = RSTRING_PTR(serialnumstr);
206
+ char serialnum[100];
207
+ strcpy(serialnumc, serialnum);
208
+ return INT2NUM(blink1_serialnumwrite(ins->dev, (uint8_t *)serialnum));
209
+ }
210
+
211
+ static VALUE rb_blink1_serverdown(VALUE self, VALUE on, VALUE millis) {
212
+ struct Blink1Instance *ins;
213
+ Data_Get_Struct(self, struct Blink1Instance, ins);
214
+ return INT2NUM(blink1_serverdown(ins->dev, RTEST(on) ? 1 : 0, FIX2UINT(millis)));
215
+ }
216
+
217
+ static VALUE rb_blink1_play(VALUE self, VALUE pos) {
218
+ struct Blink1Instance *ins;
219
+ Data_Get_Struct(self, struct Blink1Instance, ins);
220
+ return INT2NUM(blink1_play(ins->dev, 1, FIX2UINT(pos)));
221
+ }
222
+
223
+ static VALUE rb_blink1_stop(VALUE self, VALUE pos) {
224
+ struct Blink1Instance *ins;
225
+ Data_Get_Struct(self, struct Blink1Instance, ins);
226
+ return INT2NUM(blink1_play(ins->dev, 0, FIX2UINT(pos)));
227
+ }
228
+
229
+ static VALUE rb_blink1_writePatternLine(VALUE self, VALUE fadeMillis, VALUE r, VALUE g, VALUE b, VALUE pos) {
230
+ struct Blink1Instance *ins;
231
+ Data_Get_Struct(self, struct Blink1Instance, ins);
232
+ return INT2NUM(blink1_writePatternLine(ins->dev, FIX2UINT(fadeMillis), FIX2UINT(r), FIX2UINT(g), FIX2UINT(b), FIX2UINT(pos)));
233
+ }
234
+
235
+ static VALUE rb_blink1_readPatternLine(VALUE self, VALUE pos) {
236
+ struct Blink1Instance *ins;
237
+ Data_Get_Struct(self, struct Blink1Instance, ins);
238
+ uint16_t fadeMillis; uint8_t r; uint8_t g; uint8_t b;
239
+ blink1_readPatternLine(ins->dev, &fadeMillis, &r, &g, &b, FIX2UINT(pos));
240
+ VALUE hash = rb_hash_new();
241
+ rb_hash_aset(hash, rb_str_new2("fade_millis"), UINT2NUM(fadeMillis));
242
+ rb_hash_aset(hash, rb_str_new2("r"), UINT2NUM(r));
243
+ rb_hash_aset(hash, rb_str_new2("g"), UINT2NUM(g));
244
+ rb_hash_aset(hash, rb_str_new2("b"), UINT2NUM(b));
245
+ return hash;
246
+ }
247
+
248
+ /* UNSPPORT
249
+ int blink1_write( hid_device* dev, void* buf, int len);
250
+ int blink1_read( hid_device* dev, void* buf, int len);
251
+ int blink1_getSerialNumber(hid_device *dev, char* buf);
252
+ //int blink1_playPattern(hid_device *dev,,);
253
+ */
254
+
255
+
256
+ void Init_blink1() {
257
+ VALUE module;
258
+ VALUE klass = rb_define_class("Blink1", rb_cObject);
259
+
260
+ rb_define_singleton_method(klass, "vendor_id", rb_blink1_vid, 0);
261
+ rb_define_singleton_method(klass, "product_id", rb_blink1_pid, 0);
262
+ rb_define_singleton_method(klass, "sort_paths", rb_blink1_sortPaths, 0);
263
+ rb_define_singleton_method(klass, "sort_serials", rb_blink1_sortSerials, 0);
264
+ rb_define_singleton_method(klass, "enumerate", rb_blink1_enumerate, 0);
265
+ rb_define_singleton_method(klass, "enumerate_vid_pid", rb_blink1_enumerateByVidPid, 2);
266
+ rb_define_singleton_method(klass, "cached_path", rb_blink1_blink1_getCachedPath, 1);
267
+ rb_define_singleton_method(klass, "cached_serial", rb_blink1_getCachedSerial, 1);
268
+ rb_define_singleton_method(klass, "cached_count", rb_blink1_getCachedCount, 0);
269
+ rb_define_singleton_method(klass, "error_message", rb_blink1_error_msg, 1); // Not implemented in the library
270
+ rb_define_singleton_method(klass, "degamma_enabled", rb_blink1_getDegammaEnabled, 0);
271
+ rb_define_singleton_method(klass, "degamma_enabled=", rb_blink1_setDegammaEnabled, 1);
272
+ rb_define_singleton_method(klass, "degamma", rb_blink1_degamma, 1);
273
+ rb_define_singleton_method(klass, "sleep", rb_blink1_sleep, 1);
274
+
275
+ rb_define_alloc_func(klass, rb_blink1_allocate);
276
+
277
+ rb_define_method(klass, "opened?", rb_blink1_opened, 0);
278
+ rb_define_method(klass, "open", rb_blink1_open, 0);
279
+ rb_define_method(klass, "open_by_path", rb_blink1_openByPath, 1);
280
+ rb_define_method(klass, "open_by_serial", rb_blink1_openBySerial, 1);
281
+ rb_define_method(klass, "open_by_id", rb_blink1_openById, 1);
282
+ rb_define_method(klass, "close", rb_blink1_close, 0);
283
+ rb_define_method(klass, "version", rb_blink1_getVersion, 0);
284
+ rb_define_method(klass, "fade_to_rgb", rb_blink1_fadeToRGB, 4);
285
+ rb_define_method(klass, "set_rgb", rb_blink1_setRGB, 3);
286
+ rb_define_method(klass, "eeread", rb_blink1_eeread, 1);
287
+ rb_define_method(klass, "eewrite", rb_blink1_eewrite, 2);
288
+ rb_define_method(klass, "serialnum=", rb_blink1_serialnumwrite, 1);
289
+ rb_define_method(klass, "serialnum", rb_blink1_serialnumread, 0);
290
+ rb_define_method(klass, "serverdown", rb_blink1_serverdown, 2);
291
+ rb_define_method(klass, "play", rb_blink1_play, 1);
292
+ rb_define_method(klass, "stop", rb_blink1_stop, 1);
293
+ rb_define_method(klass, "read_pattern_line", rb_blink1_readPatternLine, 1);
294
+ rb_define_method(klass, "write_pattern_line", rb_blink1_writePatternLine, 5);
295
+ }
296
+
@@ -0,0 +1,56 @@
1
+ require 'mkmf'
2
+
3
+ uname = `uname -s`.chop.split
4
+
5
+ dir_config 'blink1'
6
+
7
+ case uname[0]
8
+ when 'Darwin'
9
+
10
+ $CFLAGS <<
11
+ ' -arch x86_64 -pthread '
12
+ $LIBS <<
13
+ ' -framework IOKit -framework CoreFoundation '
14
+
15
+ # RbConfig::MAKEFILE_CONFIG['CC'] = 'gcc'
16
+
17
+ $HID_C = "#{$srcdir}/hid.c.mac"
18
+
19
+ when 'Windows_NT'
20
+
21
+ $CFLAGS <<
22
+ ' -arch i386 -arch x86_64 ' <<
23
+ ' -pthread '
24
+
25
+ $LIBS <<
26
+ ' -lsetupapi -Wl,--enable-auto-import -static-libgcc -static-libstdc++ '
27
+
28
+ $HID_C = "#{$srcdir}/hid.c.windows"
29
+
30
+ when 'Linux'
31
+
32
+ $CFLAGS <<
33
+ `pkg-config libusb-1.0 --cflags`
34
+
35
+ $LIBS <<
36
+ `pkg-config libusb-1.0 --libs` <<
37
+ ' -lrt -lpthread -ldl -static '
38
+
39
+ $HID_C = "#{$srcdir}/hid.c.libusb"
40
+
41
+ when 'FreeBSD'
42
+
43
+ $LIBS <<
44
+ ' -L/usr/local/lib -lusb -lrt -lpthread -liconv -static '
45
+
46
+ $HID_C = "#{$srcdir}/hid.c.libusb"
47
+
48
+ end
49
+
50
+ FileUtils.copy $HID_C, "#{$srcdir}/hid.c"
51
+
52
+ $CFLAGS <<
53
+ ' -std=gnu99' <<
54
+ " -I #{$srcdir}/include"
55
+
56
+ create_makefile('blink1')