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
- .end_frame ⇒ Object
- .radial_pixel_index(value, range) ⇒ Object
- .serial_port(file = SERIAL_PORT) ⇒ Object
- .set_pixel(pixel, red, green, blue) ⇒ Object
Class Method Details
.draw_pixel_map(pixels) ⇒ Object
56 57 58 59 60 |
# File 'lib/arduino-lights.rb', line 56 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 ⇒ Object
47 48 49 50 |
# File 'lib/arduino-lights.rb', line 47 def self.end_frame() self.serial_port.write(254.chr) self.serial_port.flush() end |
.radial_pixel_index(value, range) ⇒ Object
52 53 54 |
# File 'lib/arduino-lights.rb', line 52 def self.radial_pixel_index(value, range) (((PIXELS.to_f * value) / range).floor + PIXELS) % PIXELS end |
.serial_port(file = SERIAL_PORT) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/arduino-lights.rb', line 24 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 |