lib/firmata/board.rb in hybridgroup-firmata-0.4.5 vs lib/firmata/board.rb in hybridgroup-firmata-0.4.6
- old
+ new
@@ -1,7 +1,8 @@
require 'stringio'
require 'event_emitter'
+require 'rubyserial'
module Firmata
class Board
include EventEmitter
@@ -22,13 +23,11 @@
# Public: Initialize a Board
#
# port - a String port or an Object that responds to read and write.
def initialize(port)
if port.is_a?(String)
- require 'serialport'
- @serial_port = SerialPort.new(port, 57600, 8, 1, SerialPort::NONE)
- @serial_port.read_timeout = 2
+ @serial_port = Serial.new(port, 57600, 8)
else
@serial_port = port
end
@serial_port_status = Port::OPEN
@@ -39,12 +38,10 @@
@analog_pins = []
@connected = false
@async_events = []
trap_signals 'INT', 'KILL', 'TERM'
- rescue LoadError
- puts "Please 'gem install hybridgroup-serialport' for serial port support."
end
# Public: Check if a connection to Arduino has been made.
#
# Returns Boolean connected state.
@@ -61,11 +58,11 @@
handle_events!
catch(:initialized) do
loop do
query_report_version #unless @major_version.zero?
- sleep 0.1
+ sleep 1
read_and_process
end
end
end
@@ -240,10 +237,11 @@
ret.push([n].pack("v")[1])
end
ret.push(END_SYSEX)
write(*ret)
end
+
# Public: Set i2c config.
# I2C config
# 0 START_SYSEX (0xF0) (MIDI System Exclusive)
# 1 I2C_CONFIG (0x78)
# 2 Delay in microseconds (LSB)
@@ -287,19 +285,17 @@
#
# write(START_SYSEX, CAPABILITY_QUERY, END_SYSEX)
#
# Returns nothing.
def write(*commands)
- serial_port.write_nonblock(commands.map(&:chr).join)
+ serial_port.write(commands.map(&:chr).join)
end
# Internal: Read data from the underlying serial port.
#
# Returns String data read for serial port.
def read
- return serial_port.read_nonblock(1024)
- rescue EOFError
- rescue Errno::EAGAIN
+ return serial_port.read(1024)
end
# Internal: Process a series of bytes.
#
# data: The String data to process.