class Frankenpins::RGBLED
Attributes
default_duration[RW]
Public Class Methods
new(options={})
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 7 def initialize(options={}) @is_on = false @pin_nums = options.delete(:pins) @red = LED.new( config_for_pin(@pin_nums[:red], options) ) @green = LED.new( config_for_pin(@pin_nums[:green], options) ) @blue = LED.new( config_for_pin(@pin_nums[:blue], options) ) @default_duration = nil end
Public Instance Methods
off(opts={})
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 24 def off(opts={}) [@red, @green, @blue].map { |led| led.off(opts) } @is_off = true end
on(opts={})
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 17 def on(opts={}) [@red, @green, @blue].map { |led| led.on(opts) } rgb(opts[:rgb]) if opts[:rgb] percentage(opts[:percent]) if opts[:percent] @is_on = true end
percentage(rgb, opts={})
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 33 def percentage(rgb, opts={}) write_colours(*rgb, opts) end
rgb(rgb, opts={})
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 29 def rgb(rgb, opts={}) write_colours(*rgb.map { |val| rgb_val_to_brightness(val) }, opts) end
Private Instance Methods
config_for_pin(num, options)
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 42 def config_for_pin(num, options) opts = options.clone opts[:pin] = num opts end
rgb_val_to_brightness(val)
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 38 def rgb_val_to_brightness(val) scale(val, 255, 100).to_i end
scale(val, domain, range)
click to toggle source
e.g. 30/255 -> ?/100
= scale(30, 255, 100) = 11.764705882
# File lib/frankenpins/rgb_led.rb, line 58 def scale(val, domain, range) (val.to_f/domain.to_f) * range.to_f end
write_colours(r, g, b, opts={})
click to toggle source
# File lib/frankenpins/rgb_led.rb, line 48 def write_colours(r, g, b, opts={}) duration = opts[:duration] || @default_duration @red.brightness(r, :duration => duration) @green.brightness(g, :duration => duration) @blue.brightness(b, :duration => duration) end