class Milight::Bulb

Public Class Methods

new(group: :all, ip: nil, port: 8899) click to toggle source
# File lib/milight/bulb.rb, line 6
def initialize(group: :all, ip: nil, port: 8899)
  self.group = group
  self.ip = ip
  @port = port

  @debugger = lambda {|_| }
end

Public Instance Methods

bright(persent) click to toggle source
# File lib/milight/bulb.rb, line 83
def bright(persent)
  if val = brightness(persent.to_i)
    self.on
    command Command::BRIGHTENESS, val
  else
    debug "invalid persent value '#{persent}'"
  end
end
color_value=(val) click to toggle source
# File lib/milight/bulb.rb, line 74
def color_value=(val)
  if code = color_code(val)
    self.on
    command Command::SET_COLOR, code
  else
    debug "invalid color value '#{val}'"
  end
end
dark() click to toggle source
# File lib/milight/bulb.rb, line 96
def dark
  self.bright 0
end
debug(msg) click to toggle source
# File lib/milight/bulb.rb, line 27
def debug(msg)
  @debugger.call msg
end
debugger=(type) click to toggle source

TODO: move to anywhere…

# File lib/milight/bulb.rb, line 15
def debugger=(type)
  @debugger = case type
              when :puts
                lambda {|msg|
                  puts " - debug :: #{msg}"
                }
              else
                lambda {|_| }
              end
  debug "set debug - #{type}"
end
disco(attr = nil) click to toggle source
# File lib/milight/bulb.rb, line 35
def disco(attr = nil)
  case attr && attr.to_sym
  when nil
    command Command::DISCO_MODE
  when :faster, :fast
    command Command::DISCO_SPEED_FASTER
  when :slower, :slow
    command Command::DISCO_SPEED_SLOWER
  else
    debug "invalid disco attribute: '#{attr}'"
  end
end
full_bright() click to toggle source
# File lib/milight/bulb.rb, line 92
def full_bright
  self.bright 50
end
night() click to toggle source

night-command 100ms followed by ‘off-command’

# File lib/milight/bulb.rb, line 59
def night
  self.off
  command Command::NIGHT[@group]
end
off() click to toggle source
# File lib/milight/bulb.rb, line 48
def off
  command Command::LED_OFF[@group]
end
on() click to toggle source
# File lib/milight/bulb.rb, line 31
def on
  command Command::LED_ON[@group]
end
white() click to toggle source

white-command 100ms followed by ‘on-command’

# File lib/milight/bulb.rb, line 53
def white
  self.on
  command Command::WHITE[@group]
end

Private Instance Methods

brightness(persent) click to toggle source
# File lib/milight/bulb.rb, line 120
def brightness(persent)
  if (0..100).cover? persent
    sprintf(
      '%02d',
      2 + 25 * persent.to_i / 100
    )
  end
end
color_code(val) click to toggle source

parse color code (“00” .. “ff”) valid color-value is 0..255

# File lib/milight/bulb.rb, line 140
def color_code(val)
  if val.is_a? String
    code = val
    val = code.hex
    val = -1 if val == 0 && code != '00'
  elsif val.is_a? Fixnum
    code = val
    val = val.to_i
    code = '%02x' % val
  else
    return nil
  end

  if (0..255).cover? val
    code
  else
    nil
  end
end
command(cmd, value = '00') click to toggle source
# File lib/milight/bulb.rb, line 160
def command(cmd, value = '00')
  debug "cmd : #{cmd} / #{value}"
  msg = message cmd, value

  #TODO: initialize first ?
  sock = UDPSocket.open
  sock.setsockopt(
    Socket::SOL_SOCKET,
    Socket::SO_BROADCAST,
    1
  )
  sock.send(msg, 0, @ipaddr, @port)
end
defined_color(method) click to toggle source
# File lib/milight/bulb.rb, line 129
def defined_color(method)
  name = method.upcase.to_sym
  if Color.constants.include? name
    Color.const_get(name)
  else
    nil
  end
end
group=(group) click to toggle source
# File lib/milight/bulb.rb, line 110
def group=(group)
  if group == :all
    @group = :all
  elsif (1..5).cover? group
    @group = "group#{group}".to_sym
  else
    raise 'invalid group. use :all or 1..5'
  end
end
ip=(ipaddr) click to toggle source
# File lib/milight/bulb.rb, line 102
def ip=(ipaddr)
  unless ipaddr
    raise "ip is not allowed nil"
  end
  IPAddr.new ipaddr   # just validate
  @ipaddr = ipaddr
end
message(cmd, value) click to toggle source
# File lib/milight/bulb.rb, line 174
def message(cmd, value)
  [cmd, value, "55"].map(&:to_s)
    .map(&:hex).pack "C*"
end