Class: Domotics::Core::Dimmer
- Defined in:
- lib/domotics/core/element/dimmer.rb
Constant Summary collapse
- DEFAULT_LEVEL =
0
- MIN_LEVEL =
0
- MAX_LEVEL =
255
- MAX_STEPS =
128
- STEP_DELAY =
1.0 / MAX_STEPS
- STEP_SIZE =
((MAX_LEVEL + 1) / MAX_STEPS.to_f).round
Instance Attribute Summary
Attributes inherited from Element
Instance Method Summary collapse
-
#bright(value = nil) ⇒ Object
Increase brightness level (value 0-100%).
-
#dim(value = nil) ⇒ Object
Decrease brightness level (value 0-100%).
- #fade_to(value = DEFAULT_LEVEL, speed_divisor = 1) ⇒ Object
-
#initialize(args = {}) ⇒ Dimmer
constructor
A new instance of Dimmer.
- #off ⇒ Object
- #set_state(value = DEFAULT_LEVEL, opt = {}) ⇒ Object
- #state ⇒ Object
Methods inherited from Element
data=, #image, #info, #load_driver, #state_changed, #to_s, #verbose_state
Constructor Details
#initialize(args = {}) ⇒ Dimmer
Returns a new instance of Dimmer.
11 12 13 14 15 16 17 18 |
# File 'lib/domotics/core/element/dimmer.rb', line 11 def initialize(args = {}) @type = args[:type] || :dimmer @fade_lock = Mutex.new @fade_thread = nil args[:driver] = "PWMPin" load_driver args super end |
Instance Method Details
#bright(value = nil) ⇒ Object
Increase brightness level (value 0-100%)
45 46 47 48 49 50 51 |
# File 'lib/domotics/core/element/dimmer.rb', line 45 def bright(value = nil) if value set_state value * MAX_LEVEL / 100 else set_state state + STEP_SIZE end end |
#dim(value = nil) ⇒ Object
Decrease brightness level (value 0-100%)
37 38 39 40 41 42 43 |
# File 'lib/domotics/core/element/dimmer.rb', line 37 def dim(value = nil) if value set_state value * MAX_LEVEL / 100 else set_state state - STEP_SIZE end end |
#fade_to(value = DEFAULT_LEVEL, speed_divisor = 1) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/domotics/core/element/dimmer.rb', line 57 def fade_to(value = DEFAULT_LEVEL, speed_divisor = 1) @fade_lock.synchronize do @fade_thread.kill if @fade_thread and @fade_thread.alive? @fade_thread = Thread.new do op = (value - state) >= 0 ? :+ : :- steps = ((value - state).abs / STEP_SIZE.to_f).round steps.times do set_state(state.public_send(op, STEP_SIZE), kill_fader: :no) sleep speed_divisor * STEP_DELAY end @fade_lock.synchronize { @fade_thread = nil } end end @fade_thread end |
#off ⇒ Object
53 54 55 |
# File 'lib/domotics/core/element/dimmer.rb', line 53 def off set_state MIN_LEVEL unless state == MIN_LEVEL end |
#set_state(value = DEFAULT_LEVEL, opt = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/domotics/core/element/dimmer.rb', line 24 def set_state(value = DEFAULT_LEVEL, opt = {}) unless opt[:kill_fader] == :no @fade_lock.synchronize do @fade_thread.kill if @fade_thread and @fade_thread.alive? end end if value.is_a? Integer value = MIN_LEVEL if value < MIN_LEVEL value = MAX_LEVEL if value > MAX_LEVEL end super value end |
#state ⇒ Object
20 21 22 23 |
# File 'lib/domotics/core/element/dimmer.rb', line 20 def state st = super st.is_a?(Integer) ? st : 0 end |