Class: LimitlessLed::Bridge

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/limitless_led/bridge.rb

Constant Summary collapse

COMMANDS =
{
  all_off: 65,
  all_on: 66,
  disco: 77,
  disco_slower: 67,
  disco_faster: 68,
  color: 64,
  brightness: 78,
  group_1_on: 69,
  group_1_off: 70,
  group_2_on: 71,
  group_2_off: 72,
  group_3_on: 73,
  group_3_off: 74,
  group_4_on: 75,
  group_4_off: 76
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colors

#color_code_from_color, #color_from_hex

Constructor Details

#initialize(host: 'localhost', port: 8899) ⇒ Bridge

Returns a new instance of Bridge.



25
26
27
28
# File 'lib/limitless_led/bridge.rb', line 25

def initialize(host: 'localhost', port: 8899)
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



23
24
25
# File 'lib/limitless_led/bridge.rb', line 23

def host
  @host
end

#portObject

Returns the value of attribute port.



23
24
25
# File 'lib/limitless_led/bridge.rb', line 23

def port
  @port
end

Instance Method Details

#brightness(amount) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
# File 'lib/limitless_led/bridge.rb', line 62

def brightness(amount)
  raise(ArgumentError.new('Brightness must be within 2 - 27')) unless (2..27).include?(amount)
  command :brightness, amount
end

#color(color) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/limitless_led/bridge.rb', line 50

def color(color)
  color_code = if color.is_a?(Color::RGB)
     color_code_from_color(color)
   elsif color.is_a?(Integer)
     color
   elsif color.is_a?(String)
     color_code_from_color Color::RGB.const_get(color.camelize)
   end

  command :color, color_code
end

#command(command_key, command_param = 0) ⇒ Object



71
72
73
# File 'lib/limitless_led/bridge.rb', line 71

def command(command_key, command_param = 0)
  send_packet COMMANDS[command_key].chr + command_param.chr + 85.chr
end

#go_crazyObject



75
76
77
78
79
# File 'lib/limitless_led/bridge.rb', line 75

def go_crazy
  while true
    color rand(256)
  end
end

#group(number) ⇒ Object



30
31
32
# File 'lib/limitless_led/bridge.rb', line 30

def group(number)
  LimitlessLed::Group.new(number, self)
end

#send_packet(packet) ⇒ Object



67
68
69
# File 'lib/limitless_led/bridge.rb', line 67

def send_packet(packet)
  socket.send packet, 0
end

#smooth_and_fastObject



81
82
83
84
85
86
# File 'lib/limitless_led/bridge.rb', line 81

def smooth_and_fast
  0..255.cycle do |color_code|
    color color_code
    sleep 1/100.0
  end
end

#socketObject



34
35
36
37
38
39
40
# File 'lib/limitless_led/bridge.rb', line 34

def socket
  @socket ||= begin
    UDPSocket.new.tap do |socket|
      socket.connect host, port
    end
  end
end

#whiteObject



46
47
48
# File 'lib/limitless_led/bridge.rb', line 46

def white
  send_packet "\xc2\x00\x55"
end