#!/usr/bin/env ruby # encoding: utf-8 # ruby: 1.9 require 'serialport' atmolight = SerialPort.open("/dev/ttyUSB0",{ baud: 19200, databits: 8, parity: SerialPort::NONE, stop_bit: 1, flow_control: SerialPort::NONE}) # reset arduino atmolight.dtr = 1 sleep 0.5 atmolight.dtr = 0 sleep 0.5 atmolight.flush_output atmolight.flush_input atmolight.baud = 19200 puts "waiting for welcome message" puts atmolight.gets puts "sending test commands" commands = [ "\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # start bootloader "\x00\x87\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # start application "\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x00", # sync sequence "\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x1b\x00", # too long sync sequence "\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" # unhandled command ] commands.each do |command| sleep 0.5 atmolight.write command atmolight.flush_output end # test all LEDs with red puts "cycle LEDs" 50.times do |i| atmolight.write [i,0x01,0x00,0x00,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00].pack("C*") # set LED to red atmolight.flush_output atmolight.write [0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00].pack("C*") # flush atmolight.flush_output sleep 0.25 atmolight.write [i,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00].pack("C*") # switch off LED atmolight.flush_output end # increase white puts "white fading" atmolight.write "\x00\x87\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" atmolight.flush_output sleep 0.5 white = 0 while true do start = Time.now 51.times do |i| atmolight.write [i,0x01,0x00,0x00,white,white,white,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00].pack("C*") # set LED to red atmolight.flush_output end white = (white+10)%256 puts "set white #{white} in #{Time.now-start} s" end