module ArduinoLights

Constants

PIXELS
SERIAL_PORT
SERIAL_RATE

Public Class Methods

draw_pixel_map(pixels) click to toggle source
# File lib/arduino-lights.rb, line 71
def self.draw_pixel_map(pixels)
  pixels.each_with_index do |pixel,i|
    set_pixel(PIXELS - i - 1, pixel[0], pixel[1], pixel[2])
  end
end
end_frame() click to toggle source
# File lib/arduino-lights.rb, line 62
def self.end_frame()
  self.serial_port.write(254.chr)
  self.serial_port.flush()
end
radial_pixel_index(value, range) click to toggle source
# File lib/arduino-lights.rb, line 67
def self.radial_pixel_index(value, range)
  (((PIXELS.to_f * value) / range).floor + PIXELS) % PIXELS
end
serial_port(file = SERIAL_PORT) click to toggle source
# File lib/arduino-lights.rb, line 8
def self.serial_port(file = SERIAL_PORT)
  file = ENV.fetch("BLEMU_DEVICE", file)
  @port ||= begin
    res = nil
    if File.chardev?(file)
      res = SerialPort.new(file, SERIAL_RATE, 8, 1, SerialPort::NONE)
    elsif File.pipe?(file)
      res = File.open(file, "w+")
    else
      raise "Unknown device type or device not accessible: #{file}"
    end
    sleep(2)
    res
  end
end
set_pixel(pixel, red, green, blue) click to toggle source
# File lib/arduino-lights.rb, line 39
def self.set_pixel(pixel, red, green, blue)
  # Something about the setup with these LEDs requires a small delay between bytes sent
  # I don't know if this is about the configuration of ruby-serialport, or the pixel
  # processing code on the Arduino itself.
  #
  # With this set to 0.0009, data runs through the device at about 5kb/s. With this set
  # to 0.0008, the data runs through at >5.5kb/s, which causes some of the data to be lost
  sleep(0.0009)

  # first byte is whice led number to switch on
  self.serial_port.write(pixel.chr)     

  # next 3 bytes are red, green and blue values
  # Note: 255 signifies the end of the command, so don't try and set an led value to that
  self.serial_port.write(red.chr)    
  self.serial_port.write(green.chr)    
  self.serial_port.write(blue.chr)

  # then end with a termination character
  self.serial_port.write(255.chr)  
  self.serial_port.flush()
end
set_pixel_xy(x, y, red, green, blue) click to toggle source
# File lib/arduino-lights.rb, line 35
def self.set_pixel_xy(x, y, red, green, blue)
  self.set_pixel(self.xy_to_pixel_number(x, y), red, green, blue)
end
xy_to_pixel_number(x,y) click to toggle source
# File lib/arduino-lights.rb, line 24
def self.xy_to_pixel_number(x,y)
  row_offset = y * 12
  if(y % 2 == 0)
    column_offset = (11 - x)
  else
    column_offset = x
  end

  row_offset + column_offset
end