A Ruby interface for blink(1).

Constants

VERSION = "0.0.7"  

Version of this module.

Attributes

delay_millis [RW]

Delay to next color in millisecond.

millis [RW]

Fade duration in millisecond.

Public Class methods

cached_count -> integer

Return number of cached devices.

static VALUE rb_blink1_getCachedCount(VALUE self) {
  return INT2NUM(blink1_getCachedCount());
}

cached_path (index) -> string

Return cached device path by index.

static VALUE rb_blink1_blink1_getCachedPath(VALUE self, VALUE i) {
  return rb_str_new2(blink1_getCachedPath(FIX2INT(i)));
}

cached_serial (index) -> string

Return cached device serial id by index.

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 (p1)

Return gamma corrected value for a RGB component.

static VALUE rb_blink1_degamma(VALUE self, VALUE i) {
  return INT2NUM(blink1_degamma(FIX2INT(i)));
}

degamma_enabled ()

Return degamma enabled.

static VALUE rb_blink1_getDegammaEnabled(VALUE self) {
  return degamma == 1 ? Qtrue : Qfalse;
}

degamma_enabled= (p1)

Set degamma enabled.

static VALUE rb_blink1_setDegammaEnabled(VALUE self, VALUE enabled) {
  if(RTEST(enabled)) {
    degamma = 1;
    blink1_enableDegamma();
  } else {
    degamma = 0;
    blink1_disableDegamma();
  }
  return Qnil;
}

enumerate ()

Get all devices by default product_id, vendor_id.

static VALUE rb_blink1_enumerate(VALUE self) {
  return INT2NUM(blink1_enumerate());
}

enumerate_by_vid_pid (vid, pid) -> integer

Get all matching devices by VID/PID pair.

Return number of devices.

static VALUE rb_blink1_enumerateByVidPid(VALUE self, VALUE vid, VALUE pid) {
  return INT2NUM(blink1_enumerateByVidPid(FIX2INT(vid), FIX2INT(pid)));
}

list ()

Returns array of hash with keys :id, :serial, :path

130
131
132
133
134
135
136
137
138
139
140
141
142
143
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

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
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

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
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 ()

Return product ID

static VALUE rb_blink1_pid(VALUE self) {
  return INT2NUM(blink1_pid());
}

sleep (p1)

Sleeps for milliseconds.

static VALUE rb_blink1_sleep(VALUE self, VALUE delayMillis) {
  blink1_sleep(FIX2UINT(delayMillis));
  return Qnil;
}

sort_paths ()

Sort cached device by path.

static VALUE rb_blink1_sortPaths(VALUE self) {
  blink1_sortPaths();
  return Qnil;
}

sort_serials ()

Sort cached device by serial id.

static VALUE rb_blink1_sortSerials(VALUE self) {
  blink1_sortSerials();
  return Qnil;
}

vendor_id ()

Return vendor ID

static VALUE rb_blink1_vid(VALUE self) {
  return INT2NUM(blink1_vid());
}

Public Instance methods

[] (index)

Alias for read_pattern_line.

112
113
114
def [] index
  self.read_pattern_line(index)
end

[]= (index, prop)

Write pattern line with hash with key fade_millis, r, g, b.

119
120
121
122
123
124
125
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

close ()

Closes the device.

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) -> integer

Returns an EEPROM byte.

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) -> integer

Write an EEPROM byte.

Return the actual number of bytes written and -1 on error.

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 (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.

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 (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.

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 ()

Turn LED off.

105
106
107
def off
  self.fade_to_rgb(millis, 0, 0, 0)
end

on ()

Turn LED white.

98
99
100
def on
  self.fade_to_rgb(millis, 0xff, 0xff, 0xff)
end

open ()

Open device by default vendor_id, product_id.

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 (p1)

Open device by id.

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 (p1)

Open device by device path.

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 (p1)

Open device by serial id.

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? ()

Return the device is opened.

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) -> integer

Start playing color sequence (at pos)

Return the actual number of bytes written and -1 on error.

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)

Flash random color for times.

85
86
87
88
89
90
91
92
93
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) -> hash

Read pattern RGB value at pos

Return hash with value with key +“fade_millis”+, +“r”+, +“g”+, +“b”+

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;
}

serverdown (on, millisecond) -> integer

Turn on/off servertickle

Return the actual number of bytes written and -1 on error.

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) -> integer

Set LED color to RGB.

Return the actual number of bytes written and -1 on error.

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) -> integer

Stop playing color sequence (at pos)

Return the actual number of bytes written and -1 on error.

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 ()

Return blink(1) version.

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 (fade_millis, r, g, b, pos) -> integer

Write pattern RGB value at pos

Return the actual number of bytes written and -1 on error.

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)));
}