Class: RGBLED::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/rgb_led/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(path, &block) ⇒ Object

Open the USB device for the RGB controller

Parameters:

  • path (String)

    The path to the USB device



6
7
8
# File 'lib/rgb_led/controller.rb', line 6

def self.open(path, &block)
  new.open(path, &block)
end

Instance Method Details

#blue(brightness = 1.0) ⇒ Object

Set the RGB LED to the color blue

Parameters:

  • brightness (#to_f) (defaults to: 1.0)


75
76
77
# File 'lib/rgb_led/controller.rb', line 75

def blue(brightness=1.0)
  update(0, 0, brightness)
end

#closeObject

Close the device



28
29
30
31
32
# File 'lib/rgb_led/controller.rb', line 28

def close
  return if @io.nil?

  @io.close
end

#green(brightness = 1.0) ⇒ Object

Set the RGB LED to the color green

Parameters:

  • brightness (#to_f) (defaults to: 1.0)


68
69
70
# File 'lib/rgb_led/controller.rb', line 68

def green(brightness=1.0)
  update(0, brightness, 0)
end

#offObject

Turn off the LED entirely



54
55
56
# File 'lib/rgb_led/controller.rb', line 54

def off
  update(0, 0, 0)
end

#open(path, &block) ⇒ Object

Open the USB device for the RGB controller

Parameters:

  • path (String)

    The path to the USB device



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object

Set the RGB LED to the color red

Parameters:

  • brightness (#to_f) (defaults to: 1.0)


61
62
63
# File 'lib/rgb_led/controller.rb', line 61

def red(brightness=1.0)
  update(brightness, 0, 0)
end

#update(red, green, blue) ⇒ Object

Update the RGB LED color using floats between 0.0…1.0

Parameters:

  • red (#to_f)
  • green (#to_f)
  • blue (#to_f)


39
40
41
42
43
44
45
46
47
48
49
50
51
# 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