Class: Domotics::Core::RgbStrip

Inherits:
Element show all
Defined in:
lib/domotics/core/element/rgb_strip.rb

Instance Attribute Summary

Attributes inherited from Element

#device, #name, #room, #type

Instance Method Summary collapse

Methods inherited from Element

data=, #image, #info, #load_driver, #set_state, #state, #state_changed, #to_s, #verbose_state

Constructor Details

#initialize(args = {}) ⇒ RgbStrip

Returns a new instance of RgbStrip.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/domotics/core/element/rgb_strip.rb', line 3

def initialize(args = {})
  @type = args[:type] || :rgb_strip
  @strips = Hash.new
  @crazy_lock = Mutex.new
  @crazy_thread = nil
  super
  sub_args = args.dup
  %w(r g b).each do |x|
    sub_args[:name] = (args[:name].to_s+"_#{x}_strip").to_sym
    sub_args[:pin] = args[x.to_sym]
    @strips[x.to_sym] = Dimmer.new(sub_args)
  end
end

Instance Method Details

#blueObject



23
24
25
# File 'lib/domotics/core/element/rgb_strip.rb', line 23

def blue
  @strips[:b]
end

#colorObject



35
36
37
# File 'lib/domotics/core/element/rgb_strip.rb', line 35

def color
  @strips.values.map { |strip| strip.state }
end

#crazyObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/domotics/core/element/rgb_strip.rb', line 73

def crazy
  @crazy_lock.synchronize do
    @crazy_thread.kill if @crazy_thread
    @crazy_thread = Thread.new do
      loop do
        @fade_threads = @strips.values.map { |strip| strip.fade_to(rand(Dimmer::MAX_LEVEL), 1) }
        @fade_threads.each { |thread| thread.join }
      end
    end
  end
  set_state :on
end

#greenObject



20
21
22
# File 'lib/domotics/core/element/rgb_strip.rb', line 20

def green
  @strips[:g]
end

#kill_crazyObject



86
87
88
89
90
91
92
93
# File 'lib/domotics/core/element/rgb_strip.rb', line 86

def kill_crazy
  @crazy_lock.synchronize do
    if @crazy_thread
      @crazy_thread.kill
      @crazy_thread = nil
    end
  end
end

#offObject



27
28
29
30
31
32
33
# File 'lib/domotics/core/element/rgb_strip.rb', line 27

def off
  if on?
    kill_crazy
    @strips.values.each { |strip| strip.off }
    set_state :off
  end
end

#onObject



43
44
45
# File 'lib/domotics/core/element/rgb_strip.rb', line 43

def on
  set_color 255, 255, 255
end

#on?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/domotics/core/element/rgb_strip.rb', line 39

def on?
  color.reduce(:+) != 0
end

#randomObject



69
70
71
# File 'lib/domotics/core/element/rgb_strip.rb', line 69

def random
  set_color 3.times.map { rand Dimmer::MAX_LEVEL }
end

#redObject



17
18
19
# File 'lib/domotics/core/element/rgb_strip.rb', line 17

def red
  @strips[:r]
end

#set_color(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/domotics/core/element/rgb_strip.rb', line 47

def set_color(*args)
  kill_crazy
  args=args[0] if args.size == 1 and args[0].is_a? Array
  if args.size == 3
    @strips[:r].fade_to args[0]
    @strips[:g].fade_to args[1]
    @strips[:b].fade_to args[2]
    set_state args.reduce(:+) == 0 ? :off : :on
  end
end

#set_power(value = 50) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/domotics/core/element/rgb_strip.rb', line 58

def set_power(value=50)
  return unless value.is_a? Integer
  value=100 if value>100
  value=0 if value<0
  if state == :on
    set_color color.map { |c| c * Dimmer::MAX_LEVEL * value / color.max / 100 }
  else
    set_color 3.times.map { Dimmer::MAX_LEVEL * value / 100 }
  end
end