Sha256: 383923ef5c5df087e61b5ab46155e3b99bfdd84b52ed30bfd2865429493204d0

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'milight/colour'

module Milight
  class RgbwGroup

    attr_reader :commander

    GROUP_ON = [0x45, 0x47, 0x49, 0x4B]
    GROUP_OFF = [0x46, 0x48, 0x4A, 0x4C]
    GROUP_WHITE = [0xC5, 0xC7, 0xC9, 0xCB]

    BRIGHTNESS = 0x4E
    COLOUR = 0x40

    def initialize(commander, group, colour_helper: Milight::Colour)
      raise invalid_group_error unless valid_group? group
      @index = group - 1
      @commander = commander
      @colour_helper = colour_helper
    end

    def on
      @commander.send_command GROUP_ON[@index]
      self
    end

    def off
      @commander.send_command GROUP_OFF[@index]
      self
    end

    def white
      send_white_cmd
      self
    end

    def hue(hue)
      colour = @colour_helper.new(hue)
      send_colour_cmd colour
      self
    end

    def brightness(value)
      brightness = Milight::Brightness.new(value)
      send_brightness_cmd brightness
      self
    end

    def colour(colour)
      colour_value = @colour_helper.new(colour)
      colour_value.greyscale? ? send_white_cmd : send_colour_cmd(colour_value)
      send_brightness_cmd colour_value
      self
    end

    private

    def send_white_cmd
      @commander.send_command GROUP_WHITE[@index]
    end

    def send_colour_cmd colour
      select
      @commander.send_command COLOUR, colour.to_milight_colour
    end

    def send_brightness_cmd colour
      select
      @commander.send_command BRIGHTNESS, colour.to_milight_brightness
    end

    def select
      on
    end

    def invalid_group_error
      ArgumentError.new('Group must be between 1 and 4')
    end

    def valid_group?(value)
      value.between?(1, 4)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
milight-easybulb-1.0.0 lib/milight/rgbw_group.rb