class RGBLED::Controller

Public Class Methods

open(path, &block) click to toggle source

Open the USB device for the RGB controller

@param [String] path The path to the USB device

# File lib/rgb_led/controller.rb, line 6
def self.open(path, &block)
  new.open(path, &block)
end

Public Instance Methods

blue(brightness=1.0) click to toggle source

Set the RGB LED to the color blue

@param [#to_f] brightness

# File lib/rgb_led/controller.rb, line 75
def blue(brightness=1.0)
  update(0, 0, brightness)
end
close() click to toggle source

Close the device

# File lib/rgb_led/controller.rb, line 28
def close
  return if @io.nil?

  @io.close
end
green(brightness=1.0) click to toggle source

Set the RGB LED to the color green

@param [#to_f] brightness

# File lib/rgb_led/controller.rb, line 68
def green(brightness=1.0)
  update(0, brightness, 0)
end
off() click to toggle source

Turn off the LED entirely

# File lib/rgb_led/controller.rb, line 54
def off
  update(0, 0, 0)
end
open(path) { |self| ... } click to toggle source

Open the USB device for the RGB controller

@param [String] path The path to the USB device

# File lib/rgb_led/controller.rb, line 13
def open(path, &block)
  @io = File.open(path, 'r+') # TODO: r+ will create the file if it doesn't exist =(

  @io.readbyte # Sync

  if block_given?
    yield(self)

    close
  end

  self
end
red(brightness=1.0) click to toggle source

Set the RGB LED to the color red

@param [#to_f] brightness

# File lib/rgb_led/controller.rb, line 61
def red(brightness=1.0)
  update(brightness, 0, 0)
end
update(red, green, blue) click to toggle source

Update the RGB LED color using floats between 0.0…1.0

@param [#to_f] red @param [#to_f] green @param [#to_f] blue

# File lib/rgb_led/controller.rb, line 39
def update(red, green, blue)
  return false if @io.nil?

  red   = convert_float_to_byte(red)
  green = convert_float_to_byte(green)
  blue  = convert_float_to_byte(blue)

  @io.write(red)
  @io.write(green)
  @io.write(blue)

  return true
end

Protected Instance Methods

convert_float_to_byte(value) click to toggle source
# File lib/rgb_led/controller.rb, line 81
def convert_float_to_byte(value)
  value = value.to_f
  value = 0.0 if value < 0
  value = 1.0 if value > 1
  value = value * 255

  value.to_i.chr
end