Class: Lightpack

Inherits:
Object
  • Object
show all
Defined in:
lib/lightpack.rb,
lib/lightpack/version.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 3636, api_key = nil) ⇒ Lightpack

Returns a new instance of Lightpack.



37
38
39
40
41
# File 'lib/lightpack.rb', line 37

def initialize(host = "127.0.0.1", port = 3636, api_key = nil)
  @host     = host
  @port     = port
  @api_key  = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



35
36
37
# File 'lib/lightpack.rb', line 35

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



34
35
36
# File 'lib/lightpack.rb', line 34

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



34
35
36
# File 'lib/lightpack.rb', line 34

def port
  @port
end

Class Method Details

.open(*args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/lightpack.rb', line 23

def self.open(*args)
  pack = new(*args)

  begin
    pack.connect
    yield pack
  ensure
    pack.disconnect
  end
end

Instance Method Details

#add_profile(name) ⇒ Object



212
213
214
215
216
# File 'lib/lightpack.rb', line 212

def add_profile(name)
  with_lock do
    command("newprofile:#{name}")
  end
end

#api_statusObject



123
124
125
# File 'lib/lightpack.rb', line 123

def api_status
  command("getstatusapi").gsub(" ", "_").to_sym
end

#authenticate(api_key = @api_key) ⇒ Object



78
79
80
81
82
# File 'lib/lightpack.rb', line 78

def authenticate(api_key = @api_key)
  return true unless api_key

  command("apikey:#{api_key}") == true
end

#colorsObject



148
149
150
151
152
# File 'lib/lightpack.rb', line 148

def colors
  command("getcolors").split(";").map do |info|
    info.split("-", 2)[1].split(",").map(&:to_i)
  end
end

#connectObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lightpack.rb', line 47

def connect
  return false if connected?

  @socket = TCPSocket.new(@host, @port)
  @connected = true

  @socket.gets # Read welcome message

  unless authenticate
    disconnect
    raise Errors::AuthenticationFailed
  end

  true
rescue
  false
end

#connected?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/lightpack.rb', line 43

def connected?
  @connected
end

#delete_profile(name) ⇒ Object



218
219
220
221
222
# File 'lib/lightpack.rb', line 218

def delete_profile(name)
  with_lock do
    command("deleteprofile:#{name}")
  end
end

#disconnectObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lightpack.rb', line 65

def disconnect
  return false unless connected?

  unlock

  @connected = false

  @socket.close if @socket && !@socket.closed?
  @socket = nil

  true
end

#fpsObject



154
155
156
# File 'lib/lightpack.rb', line 154

def fps
  command("getfps").to_f
end

#led_areasObject



139
140
141
142
143
144
145
146
# File 'lib/lightpack.rb', line 139

def led_areas
  command("getleds").split(";").map do |info|
    keys = [:x, :y, :width, :height]
    dimensions = info.split("-", 2)[1].split(",").map(&:to_i)

    Hash[keys.zip(dimensions)]
  end
end

#led_countObject



135
136
137
# File 'lib/lightpack.rb', line 135

def led_count
  command("getcountleds").to_i
end

#lockObject



88
89
90
91
92
93
# File 'lib/lightpack.rb', line 88

def lock
  return true if locked?

  result = command("lock")
  @locked = result == :success
end

#locked?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/lightpack.rb', line 84

def locked?
  @locked
end

#modeObject



162
163
164
# File 'lib/lightpack.rb', line 162

def mode
  command("getmode").to_sym
end

#on?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/lightpack.rb', line 119

def on?
  status == :on
end

#profileObject



131
132
133
# File 'lib/lightpack.rb', line 131

def profile
  command("getprofile")
end

#profilesObject



127
128
129
# File 'lib/lightpack.rb', line 127

def profiles
  command("getprofiles").split(";")
end

#screen_sizeObject



158
159
160
# File 'lib/lightpack.rb', line 158

def screen_size
  command("getscreensize").split(",").map(&:to_i)
end

#set_all_colors(r, g, b) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/lightpack.rb', line 193

def set_all_colors(r, g, b)
  with_lock do
    colors = [r, g, b].join(",")

    message = "setcolor:"
    led_count.times do |i|
      message << "#{led_number_at_index(i)}-#{colors};"
    end
    command(message)
  end
end

#set_color(n, r, g, b) ⇒ Object



187
188
189
190
191
# File 'lib/lightpack.rb', line 187

def set_color(n, r, g, b)
  with_lock do
    command("setcolor:#{led_number_at_index(n)}-#{[r, g, b].join(",")};")
  end
end

#set_led_areas(n, dimensions) ⇒ Object



205
206
207
208
209
210
# File 'lib/lightpack.rb', line 205

def set_led_areas(n, dimensions)
  with_lock do
    dimensions = [:x, :y, :width, :height].map { |key| dimensions[key] }.join(",")
    command("setleds:#{led_number_at_index(n)}-#{dimensions};")
  end
end

#statusObject

Getters



115
116
117
# File 'lib/lightpack.rb', line 115

def status
  command("getstatus").gsub(" ", "_").to_sym
end

#turn_offObject



173
174
175
176
177
# File 'lib/lightpack.rb', line 173

def turn_off
  with_lock do
    command("setstatus:off")
  end
end

#turn_onObject

Setters



167
168
169
170
171
# File 'lib/lightpack.rb', line 167

def turn_on
  with_lock do
    command("setstatus:on")
  end
end

#unlockObject



95
96
97
98
99
100
101
102
# File 'lib/lightpack.rb', line 95

def unlock
  return false unless locked?

  result = command("unlock")
  @locked = false

  result == :success
end

#with_lock(&block) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/lightpack.rb', line 104

def with_lock(&block)
  already_locked = locked?

  lock unless already_locked

  yield self
ensure
  unlock unless already_locked
end