Class: Blink1
- Inherits:
-
Object
- Object
- Blink1
- Defined in:
- lib/blink1.rb,
lib/blink1/version.rb,
ext/blink1/blink1.c
Overview
A Ruby interface for blink(1)[http://blink1.thingm.com/].
Constant Summary collapse
- VERSION =
Version of this module.
"0.0.7"
Instance Attribute Summary collapse
-
#delay_millis ⇒ Object
Delay to next color in millisecond.
-
#millis ⇒ Object
Fade duration in millisecond.
Class Method Summary collapse
-
.cached_count ⇒ Object
:call-seq: cached_count -> integer.
-
.cached_path(i) ⇒ Object
:call-seq: cached_path (index) -> string.
-
.cached_serial(i) ⇒ Object
:call-seq: cached_serial (index) -> string.
-
.degamma(i) ⇒ Object
Return gamma corrected value for a RGB component.
-
.degamma_enabled ⇒ Object
Return degamma enabled.
-
.degamma_enabled=(enabled) ⇒ Object
Set degamma enabled.
-
.enumerate ⇒ Object
Get all devices by default
product_id,vendor_id. -
.enumerate_vid_pid(vid, pid) ⇒ Object
:call-seq: enumerate_by_vid_pid (vid, pid) -> integer.
-
.error_message(code) ⇒ Object
:nodoc:.
-
.list ⇒ Object
Returns array of hash with keys
:id,:serial,:path. -
.open(option = nil, &block) ⇒ Object
:call-seq: open ( Fixnum id ) { |blink1| } open ( Boolean autoopen ) { |blink1| } open ( String serial_id ) { |blink1| } open ( :path => device_path ) { |blink1| } open ( :serial => serial_id ) { |blink1| }.
-
.product_id ⇒ Object
Return product ID.
-
.sleep(delayMillis) ⇒ Object
Sleeps for milliseconds.
-
.sort_paths ⇒ Object
Sort cached device by path.
-
.sort_serials ⇒ Object
Sort cached device by serial id.
-
.vendor_id ⇒ Object
Return vendor ID.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Alias for
read_pattern_line. -
#[]=(index, prop) ⇒ Object
Write pattern line with hash with key
fade_millis,r,g,b. -
#blink(r, g, b, times) ⇒ Object
Blink with RGB value for
times. -
#close ⇒ Object
Closes the device.
-
#eeread(addr) ⇒ Object
:call-seq: eeread (addr) -> integer.
-
#eewrite(addr, val) ⇒ Object
:call-seq: eewrite (addr, val) -> integer.
-
#fade_to_rgb(fadeMillis, r, g, b) ⇒ Object
:call-seq: fade_to_rgb (fade_millis, r, g, b) -> integer.
-
#fade_to_rgbn(fadeMillis, r, g, b, n) ⇒ Object
:call-seq: fade_to_rgbn (fade_millis, r, g, b, n) -> integer.
-
#initialize(option = nil) ⇒ Blink1
constructor
:call-seq: new ( Fixnum id ) new ( Boolean auto_open ) new ( String serial_id ) new ( :path => device_path ) new ( :serial => serial_id ).
-
#off ⇒ Object
Turn LED off.
-
#on ⇒ Object
Turn LED white.
-
#open ⇒ Object
Open device by default
vendor_id,product_id. -
#open_by_id(id) ⇒ Object
Open device by id.
-
#open_by_path(path) ⇒ Object
Open device by device path.
-
#open_by_serial(serial) ⇒ Object
Open device by serial id.
-
#opened? ⇒ Boolean
Return the device is opened.
-
#play(pos) ⇒ Object
:call-seq: play (pos) -> integer.
-
#random(times) ⇒ Object
Flash random color for
times. -
#read_pattern_line(pos) ⇒ Object
:call-seq: read_pattern_line (pos) -> hash.
-
#serialnum ⇒ Object
:nodoc:.
-
#serialnum=(serialnumstr) ⇒ Object
:nodoc:.
-
#serverdown(on, millis) ⇒ Object
:call-seq: serverdown (on, millisecond) -> integer.
-
#set_rgb(r, g, b) ⇒ Object
:call-seq: set_rgb (r, g, b) -> integer.
-
#stop(pos) ⇒ Object
:call-seq: stop (pos) -> integer.
-
#version ⇒ Object
Return blink(1) version.
-
#write_pattern_line(fadeMillis, r, g, b, pos) ⇒ Object
:call-seq: write_pattern_line (fade_millis, r, g, b, pos) -> integer.
Constructor Details
#initialize(option = nil) ⇒ Blink1
:call-seq:
new ( {Fixnum} id )
new ( {Boolean} auto_open )
new ( {String} serial_id )
new ( :path => device_path )
new ( :serial => serial_id )
Returns new instance of Blink1
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/blink1.rb', line 23 def initialize option = nil case option when Fixnum open_by_id(option) when Hash path = option[:path] || option["path"] serial = option[:serial] || option["serial"] if path open_by_path(path) elsif serial open_by_serial(serial) end when String open_by_serial(option) else open if option == true end @millis ||= 300 @delay_millis ||= 500 end |
Instance Attribute Details
#delay_millis ⇒ Object
Delay to next color in millisecond.
12 13 14 |
# File 'lib/blink1.rb', line 12 def delay_millis @delay_millis end |
#millis ⇒ Object
Fade duration in millisecond.
10 11 12 |
# File 'lib/blink1.rb', line 10 def millis @millis end |
Class Method Details
.cached_count ⇒ Object
:call-seq:
cached_count -> integer
Return number of cached devices.
88 89 90 |
# File 'ext/blink1/blink1.c', line 88 static VALUE rb_blink1_getCachedCount(VALUE self) { return INT2NUM(blink1_getCachedCount()); } |
.cached_path(i) ⇒ Object
:call-seq:
cached_path (index) -> string
Return cached device path by index.
65 66 67 |
# File 'ext/blink1/blink1.c', line 65
static VALUE rb_blink1_blink1_getCachedPath(VALUE self, VALUE i) {
return rb_str_new2(blink1_getCachedPath(FIX2INT(i)));
}
|
.cached_serial(i) ⇒ Object
:call-seq:
cached_serial (index) -> string
Return cached device serial id by index.
75 76 77 78 79 80 |
# File 'ext/blink1/blink1.c', line 75
static VALUE rb_blink1_getCachedSerial(VALUE self, VALUE i) {
const wchar_t *ret = blink1_getCachedSerial(FIX2INT(i));
char dest[16] = {"\0"};
int t = wcstombs(dest, ret, sizeof(ret));
return rb_str_new2(dest);
}
|
.degamma(i) ⇒ Object
Return gamma corrected value for a RGB component.
117 118 119 |
# File 'ext/blink1/blink1.c', line 117
static VALUE rb_blink1_degamma(VALUE self, VALUE i) {
return INT2NUM(blink1_degamma(FIX2INT(i)));
}
|
.degamma_enabled ⇒ Object
Return degamma enabled.
100 101 102 |
# File 'ext/blink1/blink1.c', line 100 static VALUE rb_blink1_getDegammaEnabled(VALUE self) { return degamma == 1 ? Qtrue : Qfalse; } |
.degamma_enabled=(enabled) ⇒ Object
Set degamma enabled.
105 106 107 108 109 110 111 112 113 114 |
# File 'ext/blink1/blink1.c', line 105
static VALUE rb_blink1_setDegammaEnabled(VALUE self, VALUE enabled) {
if(RTEST(enabled)) {
degamma = 1;
blink1_enableDegamma();
} else {
degamma = 0;
blink1_disableDegamma();
}
return Qnil;
}
|
.enumerate ⇒ Object
Get all devices by default product_id, vendor_id.
43 44 45 |
# File 'ext/blink1/blink1.c', line 43 static VALUE rb_blink1_enumerate(VALUE self) { return INT2NUM(blink1_enumerate()); } |
.enumerate_vid_pid(vid, pid) ⇒ Object
:call-seq:
enumerate_by_vid_pid (vid, pid) -> integer
Get all matching devices by VID/PID pair.
Return number of devices.
55 56 57 |
# File 'ext/blink1/blink1.c', line 55
static VALUE rb_blink1_enumerateByVidPid(VALUE self, VALUE vid, VALUE pid) {
return INT2NUM(blink1_enumerateByVidPid(FIX2INT(vid), FIX2INT(pid)));
}
|
.error_message(code) ⇒ Object
:nodoc:
94 95 96 97 |
# File 'ext/blink1/blink1.c', line 94
static VALUE rb_blink1_error_msg(VALUE self, VALUE code) {
char *msg = blink1_error_msg(FIX2INT(code));
return msg == NULL ? Qnil : rb_str_new2(msg);
}
|
.list ⇒ Object
Returns array of hash with keys :id, :serial, :path
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/blink1.rb', line 130 def self.list count = enumerate_vid_pid(vendor_id, product_id) i = 0 devs = [] while i < count do devs << { :id => i, :serial => cached_serial(i), :path => cached_path(i) } i += 1 end devs end |
.open(option = nil, &block) ⇒ Object
:call-seq:
open ( {Fixnum} id ) { |blink1| }
open ( {Boolean} autoopen ) { |blink1| }
open ( {String} serial_id ) { |blink1| }
open ( :path => device_path ) { |blink1| }
open ( :serial => serial_id ) { |blink1| }
If block given, yieds new instance of Blink1.
If not, returns new Blink1
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/blink1.rb', line 56 def self.open option = nil, &block b = self.new(option) b.open if option.nil? if block begin b.instance_eval &block ensure b.close end else b end end |
.product_id ⇒ Object
Return product ID
26 27 28 |
# File 'ext/blink1/blink1.c', line 26 static VALUE rb_blink1_pid(VALUE self) { return INT2NUM(blink1_pid()); } |
.sleep(delayMillis) ⇒ Object
Sleeps for milliseconds.
122 123 124 125 |
# File 'ext/blink1/blink1.c', line 122
static VALUE rb_blink1_sleep(VALUE self, VALUE delayMillis) {
blink1_sleep(FIX2UINT(delayMillis));
return Qnil;
}
|
.sort_paths ⇒ Object
Sort cached device by path.
31 32 33 34 |
# File 'ext/blink1/blink1.c', line 31 static VALUE rb_blink1_sortPaths(VALUE self) { blink1_sortPaths(); return Qnil; } |
.sort_serials ⇒ Object
Sort cached device by serial id.
37 38 39 40 |
# File 'ext/blink1/blink1.c', line 37 static VALUE rb_blink1_sortSerials(VALUE self) { blink1_sortSerials(); return Qnil; } |
.vendor_id ⇒ Object
Return vendor ID
21 22 23 |
# File 'ext/blink1/blink1.c', line 21 static VALUE rb_blink1_vid(VALUE self) { return INT2NUM(blink1_vid()); } |
Instance Method Details
#[](index) ⇒ Object
Alias for read_pattern_line.
112 113 114 |
# File 'lib/blink1.rb', line 112 def [] index self.read_pattern_line(index) end |
#[]=(index, prop) ⇒ Object
Write pattern line with hash with key fade_millis, r, g, b.
119 120 121 122 123 124 125 |
# File 'lib/blink1.rb', line 119 def []= index, prop fade_millis = prop[:fade_millis] || prop['fade_millis'] r = prop[:r] || prop['r'] g = prop[:g] || prop['g'] b = prop[:b] || prop['b'] self.write_pattern_line(index, fade_millis, r, g, b) end |
#blink(r, g, b, times) ⇒ Object
Blink with RGB value for times.
73 74 75 76 77 78 79 80 |
# File 'lib/blink1.rb', line 73 def blink r, g, b, times times.times do self.fade_to_rgb(millis, r, g, b) self.class.sleep(delay_millis) self.fade_to_rgb(millis, 0, 0, 0) self.class.sleep(delay_millis) end end |
#close ⇒ Object
Closes the device.
202 203 204 205 206 207 208 209 210 |
# File 'ext/blink1/blink1.c', line 202
static VALUE rb_blink1_close(VALUE self) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
if(ins->opened == 1) {
blink1_close(ins->dev);
ins->opened = 0;
}
return Qnil;
}
|
#eeread(addr) ⇒ Object
:call-seq:
eeread (addr) -> integer
Returns an EEPROM byte.
267 268 269 270 271 272 273 |
# File 'ext/blink1/blink1.c', line 267
static VALUE rb_blink1_eeread(VALUE self, VALUE addr) {
struct Blink1Instance *ins;
uint8_t val = 0;
Data_Get_Struct(self, struct Blink1Instance, ins);
blink1_eeread(ins->dev, FIX2UINT(addr), &val);
return UINT2NUM(val);
}
|
#eewrite(addr, val) ⇒ Object
:call-seq:
eewrite (addr, val) -> integer
Write an EEPROM byte.
Return the actual number of bytes written and -1 on error.
283 284 285 286 287 |
# File 'ext/blink1/blink1.c', line 283
static VALUE rb_blink1_eewrite(VALUE self, VALUE addr, VALUE val) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_eewrite(ins->dev, FIX2UINT(addr), FIX2UINT(val)));
}
|
#fade_to_rgb(fadeMillis, r, g, b) ⇒ Object
:call-seq:
fade_to_rgb (fade_millis, r, g, b) -> integer
Fade LED color to RGB in fade_millis.
Return the actual number of bytes written and -1 on error.
227 228 229 230 231 |
# File 'ext/blink1/blink1.c', line 227
static VALUE rb_blink1_fadeToRGB(VALUE self, VALUE fadeMillis, VALUE r, VALUE g, VALUE b) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_fadeToRGB(ins->dev, FIX2UINT(fadeMillis), FIX2UINT(r), FIX2UINT(g), FIX2UINT(b)));
}
|
#fade_to_rgbn(fadeMillis, r, g, b, n) ⇒ Object
:call-seq:
fade_to_rgbn (fade_millis, r, g, b, n) -> integer
Fade a specific (blink(1) mk2) LED LED color to RGB in fade_millis.
Return the actual number of bytes written and -1 on error.
241 242 243 244 245 |
# File 'ext/blink1/blink1.c', line 241
static VALUE rb_blink1_fadeToRGBN(VALUE self, VALUE fadeMillis, VALUE r, VALUE g, VALUE b, VALUE n) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_fadeToRGBN(ins->dev, FIX2UINT(fadeMillis), FIX2UINT(r), FIX2UINT(g), FIX2UINT(b), FIX2UINT(n)));
}
|
#off ⇒ Object
Turn LED off.
105 106 107 |
# File 'lib/blink1.rb', line 105 def off self.fade_to_rgb(millis, 0, 0, 0) end |
#on ⇒ Object
Turn LED white.
98 99 100 |
# File 'lib/blink1.rb', line 98 def on self.fade_to_rgb(millis, 0xff, 0xff, 0xff) end |
#open ⇒ Object
Open device by default vendor_id, product_id.
154 155 156 157 158 159 160 161 162 163 |
# File 'ext/blink1/blink1.c', line 154
static VALUE rb_blink1_open(VALUE self) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
if(ins->opened == 0) {
ins->dev = blink1_open();
ins->opened = 1;
return Qtrue;
}
return Qfalse;
}
|
#open_by_id(id) ⇒ Object
Open device by id.
190 191 192 193 194 195 196 197 198 199 |
# File 'ext/blink1/blink1.c', line 190
static VALUE rb_blink1_openById(VALUE self, VALUE id) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
if(ins->opened == 0) {
ins->dev = blink1_open();
ins->opened = 1;
return Qtrue;
}
return Qfalse;
}
|
#open_by_path(path) ⇒ Object
Open device by device path.
166 167 168 169 170 171 172 173 174 175 |
# File 'ext/blink1/blink1.c', line 166
static VALUE rb_blink1_openByPath(VALUE self, VALUE path) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
if(ins->opened == 0) {
ins->dev = blink1_open();
ins->opened = 1;
return Qtrue;
}
return Qfalse;
}
|
#open_by_serial(serial) ⇒ Object
Open device by serial id.
178 179 180 181 182 183 184 185 186 187 |
# File 'ext/blink1/blink1.c', line 178
static VALUE rb_blink1_openBySerial(VALUE self, VALUE serial) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
if(ins->opened == 0) {
ins->dev = blink1_open();
ins->opened = 1;
return Qtrue;
}
return Qfalse;
}
|
#opened? ⇒ Boolean
Return the device is opened.
147 148 149 150 151 |
# File 'ext/blink1/blink1.c', line 147
static VALUE rb_blink1_opened(VALUE self) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return ins->opened == 1 ? Qtrue : Qfalse;
}
|
#play(pos) ⇒ Object
:call-seq:
play (pos) -> integer
Start playing color sequence (at pos)
Return the actual number of bytes written and -1 on error.
330 331 332 333 334 |
# File 'ext/blink1/blink1.c', line 330
static VALUE rb_blink1_play(VALUE self, VALUE pos) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_play(ins->dev, 1, FIX2UINT(pos)));
}
|
#random(times) ⇒ Object
Flash random color for times.
85 86 87 88 89 90 91 92 93 |
# File 'lib/blink1.rb', line 85 def random times times.times do r = rand(0xff) g = rand(0xff) b = rand(0xff) self.fade_to_rgb(millis, r, g, b) self.class.sleep(delay_millis) end end |
#read_pattern_line(pos) ⇒ Object
:call-seq:
read_pattern_line (pos) -> hash
Read pattern RGB value at pos
Return hash with value with key "fade_millis", "r", "g", "b"
372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'ext/blink1/blink1.c', line 372
static VALUE rb_blink1_readPatternLine(VALUE self, VALUE pos) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
uint16_t fadeMillis; uint8_t r; uint8_t g; uint8_t b;
blink1_readPatternLine(ins->dev, &fadeMillis, &r, &g, &b, FIX2UINT(pos));
VALUE hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("fade_millis"), UINT2NUM(fadeMillis));
rb_hash_aset(hash, rb_str_new2("r"), UINT2NUM(r));
rb_hash_aset(hash, rb_str_new2("g"), UINT2NUM(g));
rb_hash_aset(hash, rb_str_new2("b"), UINT2NUM(b));
return hash;
}
|
#serialnum ⇒ Object
:nodoc:
290 291 292 293 294 295 296 |
# File 'ext/blink1/blink1.c', line 290
static VALUE rb_blink1_serialnumread(VALUE self) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
uint8_t *serialnum;
blink1_serialnumread(ins->dev, &serialnum);
return UINT2NUM(serialnum);
}
|
#serialnum=(serialnumstr) ⇒ Object
:nodoc:
299 300 301 302 303 304 305 306 |
# File 'ext/blink1/blink1.c', line 299
static VALUE rb_blink1_serialnumwrite(VALUE self, VALUE serialnumstr) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
char *serialnumc = RSTRING_PTR(serialnumstr);
char serialnum[100];
strcpy(serialnumc, serialnum);
return INT2NUM(blink1_serialnumwrite(ins->dev, (uint8_t *)serialnum));
}
|
#serverdown(on, millis) ⇒ Object
:call-seq:
serverdown (on, millisecond) -> integer
Turn on/off servertickle
Return the actual number of bytes written and -1 on error.
316 317 318 319 320 |
# File 'ext/blink1/blink1.c', line 316
static VALUE rb_blink1_serverdown(VALUE self, VALUE on, VALUE millis) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_serverdown(ins->dev, RTEST(on) ? 1 : 0, FIX2UINT(millis)));
}
|
#set_rgb(r, g, b) ⇒ Object
:call-seq:
set_rgb (r, g, b) -> integer
Set LED color to RGB.
Return the actual number of bytes written and -1 on error.
255 256 257 258 259 |
# File 'ext/blink1/blink1.c', line 255
static VALUE rb_blink1_setRGB(VALUE self, VALUE r, VALUE g, VALUE b) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_setRGB(ins->dev, FIX2UINT(r), FIX2UINT(g), FIX2UINT(b)));
}
|
#stop(pos) ⇒ Object
:call-seq:
stop (pos) -> integer
Stop playing color sequence (at pos)
Return the actual number of bytes written and -1 on error.
344 345 346 347 348 |
# File 'ext/blink1/blink1.c', line 344
static VALUE rb_blink1_stop(VALUE self, VALUE pos) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_play(ins->dev, 0, FIX2UINT(pos)));
}
|
#version ⇒ Object
Return blink(1) version.
213 214 215 216 217 |
# File 'ext/blink1/blink1.c', line 213
static VALUE rb_blink1_getVersion(VALUE self) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_getVersion(ins->dev));
}
|
#write_pattern_line(fadeMillis, r, g, b, pos) ⇒ Object
:call-seq:
write_pattern_line (fade_millis, r, g, b, pos) -> integer
Write pattern RGB value at pos
Return the actual number of bytes written and -1 on error.
358 359 360 361 362 |
# File 'ext/blink1/blink1.c', line 358
static VALUE rb_blink1_writePatternLine(VALUE self, VALUE fadeMillis, VALUE r, VALUE g, VALUE b, VALUE pos) {
struct Blink1Instance *ins;
Data_Get_Struct(self, struct Blink1Instance, ins);
return INT2NUM(blink1_writePatternLine(ins->dev, FIX2UINT(fadeMillis), FIX2UINT(r), FIX2UINT(g), FIX2UINT(b), FIX2UINT(pos)));
}
|