Module: ArduinoLights
- Defined in:
- lib/arduino-lights.rb
Constant Summary collapse
- SERIAL_PORT =
"/dev/ttyUSB0"- SERIAL_RATE =
115200- PIXELS =
24
Class Method Summary collapse
- .draw_pixel_map(pixels) ⇒ Object
- .radial_pixel_index(value, range) ⇒ Object
- .serial_port ⇒ Object
- .set_pixel(pixel, red, green, blue) ⇒ Object
Class Method Details
.draw_pixel_map(pixels) ⇒ Object
42 43 44 45 46 |
# File 'lib/arduino-lights.rb', line 42 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 |
.radial_pixel_index(value, range) ⇒ Object
38 39 40 |
# File 'lib/arduino-lights.rb', line 38 def self.radial_pixel_index(value, range) (((PIXELS.to_f * value) / range).floor + PIXELS) % PIXELS end |
.serial_port ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/arduino-lights.rb', line 8 def self.serial_port @port ||= begin res = SerialPort.new(SERIAL_PORT, baud: SERIAL_RATE, flow_control: SerialPort::SOFT) sleep(2) res end end |
.set_pixel(pixel, red, green, blue) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/arduino-lights.rb', line 16 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) end |