Class: Frankenpins::LED

Inherits:
Object
  • Object
show all
Defined in:
lib/frankenpins/led.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LED

Returns a new instance of LED.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/frankenpins/led.rb', line 14

def initialize(options={})
  options[:direction] = :out
  @pin = Frankenpins::Pin.new(options)

  @using_pwm = false
  @pwm_max   = 100

  @is_on = false
  @brightness = 0

  # TODO: Should be 0?
  @default_duration = nil

  @queue = TransitionQueue.new
  @queue.start!
end

Instance Attribute Details

#default_durationObject

Returns the value of attribute default_duration.



11
12
13
# File 'lib/frankenpins/led.rb', line 11

def default_duration
  @default_duration
end

Instance Method Details

#brightness(value, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/frankenpins/led.rb', line 41

def brightness(value, opts={})

  duration = opts[:duration] || @default_duration

  if value != 100 || value != 0 || duration
    use_pwm!
  end

  props = { :pin => @pin }

  if duration
    props[:type]     = :pwm
    props[:from]     = @brightness
    props[:to]       = value
    props[:duration] = duration
  elsif @using_pwm
    props[:type]  = :pwm
    props[:value] = value
  else
    props[:type]  = :digital
    props[:value] = false if @brightness == 0
    props[:value] = true  if @brightness == 100
  end

  @queue.push(LEDTransition.new(props))
  @brightness = value
end

#off(opts = {}) ⇒ Object



36
37
38
39
# File 'lib/frankenpins/led.rb', line 36

def off(opts={})
  brightness(0, opts)
  @is_on = false
end

#on(opts = {}) ⇒ Object



31
32
33
34
# File 'lib/frankenpins/led.rb', line 31

def on(opts={})
  brightness(100, opts)
  @is_on = true
end