Sha256: f4b0488efba7d5a0788876853a90b900e3174cc85118b113616e420f46ecace6

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'rubyserial'

module L8
  class Smartlight
    CMD_L8_LED_SET = 0x43
    CMD_L8_MATRIX_OFF = 0x45
    CMD_L8_SUPERLED_SET = 0x4b
    CMD_L8_STATUSLEDS_ENABLE = 0x9e
    CMD_L8_SET_LOW_BRIGHTNESS = 0x9a
    CMD_L8_POWEROFF = 0x9d
    CMD_L8_MATRIX_SET = 0x44
    CMD_L8_SET_ORIENTATION = 0x80

    def initialize(serial_port)
      @serial_port = Serial.new(serial_port)

      Kernel.at_exit { @serial_port.close }
    end

    def clear_matrix
      @serial_port.write Util.frame([CMD_L8_MATRIX_OFF])
    end

    def set_led(x, y, r, g, b)
      payload = [CMD_L8_LED_SET, x, y, b, g, r, 0x00]

      @serial_port.write Util.frame(payload)
    end

    def set_superled(r,g,b)
      payload = [CMD_L8_SUPERLED_SET, b, g, r]

      @serial_port.write Util.frame(payload)
    end

    def enable_status_leds
      payload = [CMD_L8_STATUSLEDS_ENABLE, 1]

      @serial_port.write Util.frame(payload)
    end

    def disable_status_leds
      payload = [CMD_L8_STATUSLEDS_ENABLE, 0]

      @serial_port.write Util.frame(payload)
    end

    def set_brightness(level)
      brightness = 0

      if level == :low
        brightness = 2
      elsif level == :medium
        brightness = 1
      end

      payload = [CMD_L8_SET_LOW_BRIGHTNESS, brightness]

      @serial_port.write Util.frame(payload)
    end

    def power_off
      @serial_port.write Util.frame([CMD_L8_POWEROFF])
    end

    def set_matrix(pixels)
      data = Util.pixels_to_two_byte_array(pixels)

      payload = [CMD_L8_MATRIX_SET] + data

      @serial_port.write Util.frame(payload)
    end

    def set_orientation(orientation)
      value = 1 if orientation == :up
      value = 2 if orientation == :down
      value = 5 if orientation == :right
      value = 6 if orientation == :left

      payload = [CMD_L8_SET_ORIENTATION, value]
      @serial_port.write Util.frame(payload)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
l8-0.0.2 lib/l8/smartlight.rb