Class: Frankenpins::LEDTransition

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

Overview

A transition of the brightness of the LED. Transitions encode all the info necessary to change the state

Instance Method Summary collapse

Instance Method Details

#digital_write(value) ⇒ Object



38
39
40
# File 'lib/frankenpins/led_transition.rb', line 38

def digital_write(value)
  pin.write(value)
end

#perform!Object



8
9
10
11
12
13
14
15
16
# File 'lib/frankenpins/led_transition.rb', line 8

def perform!
  if type == :digital
    digital_write(value)
  elsif duration
    transition!
  elsif type == :pwm
    pwm_write(value)
  end
end

#pwm_write(value) ⇒ Object



42
43
44
45
# File 'lib/frankenpins/led_transition.rb', line 42

def pwm_write(value)
  # puts "pwm_write(#{pin.wiring_pin}, #{value.to_i})"
  pin.io.soft_pwm_write(pin.wiring_pin, value.to_i)
end

#transition!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/frankenpins/led_transition.rb', line 18

def transition!
  duration_in_secs = duration
  from_value       = from
  to_value         = to
  increment_time_in_sec = 0.01

  range = to_value - from_value

  increment = increment_time_in_sec.to_f / duration_in_secs.to_f
  steps = (duration_in_secs.to_f / increment_time_in_sec.to_f).to_i

  brightness_value = from_value

  steps.times.each do
    brightness_value = brightness_value + (increment * range)
    pwm_write(brightness_value)
    sleep(increment_time_in_sec)
  end
end