Class: Firmata::Board

Inherits:
Object
  • Object
show all
Includes:
EventEmitter, MidiMessages, PinLevels, PinModes
Defined in:
lib/firmata/board.rb,
lib/firmata/constants.rb

Defined Under Namespace

Classes: Pin

Constant Summary

Constants included from MidiMessages

MidiMessages::ANALOG_MAPPING_QUERY, MidiMessages::ANALOG_MAPPING_RESPONSE, MidiMessages::ANALOG_MESSAGE, MidiMessages::ANALOG_MESSAGE_RANGE, MidiMessages::CAPABILITY_QUERY, MidiMessages::CAPABILITY_RESPONSE, MidiMessages::DIGITAL_MESSAGE, MidiMessages::DIGITAL_MESSAGE_RANGE, MidiMessages::END_SYSEX, MidiMessages::FIRMWARE_QUERY, MidiMessages::I2C_CONFIG, MidiMessages::I2C_MODE_CONTINUOUS_READ, MidiMessages::I2C_MODE_READ, MidiMessages::I2C_MODE_STOP_READING, MidiMessages::I2C_MODE_WRITE, MidiMessages::I2C_REPLY, MidiMessages::I2C_REQUEST, MidiMessages::PIN_MODE, MidiMessages::PIN_STATE_QUERY, MidiMessages::PIN_STATE_RESPONSE, MidiMessages::REPORT_ANALOG, MidiMessages::REPORT_DIGITAL, MidiMessages::REPORT_VERSION, MidiMessages::START_SYSEX, MidiMessages::SYSTEM_RESET

Constants included from PinLevels

PinLevels::HIGH, PinLevels::LOW

Constants included from PinModes

PinModes::ANALOG, PinModes::INPUT, PinModes::OUTPUT, PinModes::PWM, PinModes::SERVO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Board

Public: Initialize a Board

port - a String port or an Object that responds to read and write.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/firmata/board.rb', line 25

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
  else
    @serial_port = port
  end

  @serial_port_status = Port::OPEN
  @major_version = 0
  @minor_version = 0
  @firmware_name = nil
  @pins = []
  @analog_pins = []
  @connected = false
  @async_events = []

  trap_signals 'INT', 'KILL', 'TERM'
rescue LoadError
  puts "Please 'gem install hybridgroup-serialport' for serial port support."
end

Instance Attribute Details

#analog_pinsObject (readonly)

Public: Returns the Array of analog pins on Arduino.



16
17
18
# File 'lib/firmata/board.rb', line 16

def analog_pins
  @analog_pins
end

#async_eventsObject (readonly)

Public: Returns array of any Events returned from ????



20
21
22
# File 'lib/firmata/board.rb', line 20

def async_events
  @async_events
end

#firmware_nameObject (readonly)

Public: Returns the String firmware name of Arduino.



18
19
20
# File 'lib/firmata/board.rb', line 18

def firmware_name
  @firmware_name
end

#pinsObject (readonly)

Public: Returns the Array of pins on Arduino.



14
15
16
# File 'lib/firmata/board.rb', line 14

def pins
  @pins
end

#serial_portObject (readonly)

Public: Returns the SerialPort port the Arduino is attached to.



12
13
14
# File 'lib/firmata/board.rb', line 12

def serial_port
  @serial_port
end

Instance Method Details

#analog_write(pin, value) ⇒ Object Also known as: servo_write

Public: Write an analog messege.

pin - The Integer pin to write to. value - The Integer value to write to the pin between 0-255.

Returns nothing.



129
130
131
132
# File 'lib/firmata/board.rb', line 129

def analog_write(pin, value)
  @pins[pin].value = value
  write(ANALOG_MESSAGE | pin, value & 0x7F, (value >> 7) & 0x7F)
end

#connectObject

Public: Make connection to Arduino.

Returns Firmata::Board board.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/firmata/board.rb', line 58

def connect
  unless @connected

    handle_events!

    catch(:initialized) do
      loop do
        query_report_version #unless @major_version.zero?
        sleep 0.1
        read_and_process
      end
    end
  end

  self
end

#connected?Boolean

Public: Check if a connection to Arduino has been made.

Returns Boolean connected state.

Returns:

  • (Boolean)


51
52
53
# File 'lib/firmata/board.rb', line 51

def connected?
  @connected
end

#delay(seconds) ⇒ Object

Public: Ask the Arduino to sleep for a number of seconds.

seconds - The Integer seconds to sleep for.

Returns nothing.



147
148
149
# File 'lib/firmata/board.rb', line 147

def delay(seconds)
  sleep(seconds)
end

#digital_write(pin, value) ⇒ Object

Public: Write a value to a digital pin.

pin - The Integer pin to write to. value - The value to write (HIGH or LOW).

Returns nothing.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/firmata/board.rb', line 110

def digital_write(pin, value)
  port = (pin / 8).floor
  port_value = 0

  @pins[pin].value = value

  8.times do |i|
    port_value |= (1 << i) unless @pins[8 * port + i].value.zero?
  end

  write(DIGITAL_MESSAGE | port, port_value & 0x7F, (port_value >> 7) & 0x7F)
end

#i2c_config(*data) ⇒ Object

Public: Set i2c config.

I2C config

0 START_SYSEX (0xF0) (MIDI System Exclusive) 1 I2C_CONFIG (0x78) 2 Delay in microseconds (LSB) 3 Delay in microseconds (MSB) … user defined for special cases, etc n END_SYSEX (0xF7) Returns nothing.



254
255
256
257
258
259
260
261
262
# File 'lib/firmata/board.rb', line 254

def i2c_config(*data)
  ret = [START_SYSEX, I2C_CONFIG]
  data.each do |n|
    ret.push([n].pack("v")[0])
    ret.push([n].pack("v")[1])
  end
  ret.push(END_SYSEX)
  write(*ret)
end

#i2c_read_request(slave_address, num_bytes) ⇒ Object

Public: Make an i2c request.

I2C read/write request

0 START_SYSEX (0xF0) (MIDI System Exclusive) 1 I2C_REQUEST (0x76) 2 slave address (LSB) 3 slave address (MSB) + read/write and address mode bits

{7: always 0} + {6: reserved} + {5: address mode, 1 means 10-bit mode} +
{4-3: read/write, 00 => write, 01 => read once, 10 => read continuously, 11 => stop reading} +
{2-0: slave address MSB in 10-bit mode, not used in 7-bit mode}

4 data 0 (LSB) 5 data 0 (MSB) 6 data 1 (LSB) 7 data 1 (MSB) n END_SYSEX (0xF7) Returns nothing.



230
231
232
233
# File 'lib/firmata/board.rb', line 230

def i2c_read_request(slave_address, num_bytes)
  address = [slave_address].pack("v")
  write(START_SYSEX, I2C_REQUEST, address[0], (I2C_MODE_READ << 3), num_bytes & 0x7F, ((num_bytes >> 7) & 0x7F), END_SYSEX)
end

#i2c_write_request(slave_address, *data) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/firmata/board.rb', line 235

def i2c_write_request(slave_address, *data)
  address = [slave_address].pack("v")
  ret = [START_SYSEX, I2C_REQUEST, address[0], (I2C_MODE_WRITE << 3)] 
  data.each do |n|
    ret.push([n].pack("v")[0])
    ret.push([n].pack("v")[1])
  end
  ret.push(END_SYSEX)
  write(*ret)
end

#query_analog_mappingObject

Public: Ask the Arduino which pins (used with pin mode message) correspond to the analog channels.

Returns nothing.



197
198
199
# File 'lib/firmata/board.rb', line 197

def query_analog_mapping
  write(START_SYSEX, ANALOG_MAPPING_QUERY, END_SYSEX)
end

#query_capabilitiesObject

Public: Ask the Arduino about its capabilities and current state.

Returns nothing.



190
191
192
# File 'lib/firmata/board.rb', line 190

def query_capabilities
  write(START_SYSEX, CAPABILITY_QUERY, END_SYSEX)
end

#query_firmwareObject

Public: Ask the Ardution for its firmware name.

Returns nothing.



169
170
171
# File 'lib/firmata/board.rb', line 169

def query_firmware
  write(START_SYSEX, FIRMWARE_QUERY, END_SYSEX)
end

#query_pin_state(pin) ⇒ Object

Public: Ask the Arduino for the current configuration of any pin.

pin - The Integer pin to query on the board.

Returns nothing.



178
179
180
# File 'lib/firmata/board.rb', line 178

def query_pin_state(pin)
  write(START_SYSEX, PIN_STATE_QUERY, pin.to_i, END_SYSEX)
end

#query_report_versionObject



183
184
185
# File 'lib/firmata/board.rb', line 183

def query_report_version
  write REPORT_VERSION
end

#read_and_processObject

Public: Read the serial port and process the results

Returns nothing.



78
79
80
# File 'lib/firmata/board.rb', line 78

def read_and_process
  process(read)
end

#report_versionObject

Public: Ask the Arduino to report its version.

Returns nothing.



162
163
164
# File 'lib/firmata/board.rb', line 162

def report_version
  write(REPORT_VERSION)
end

#resetObject

Public: Send a SYSTEM_RESET to the Arduino

Returns nothing.



85
86
87
# File 'lib/firmata/board.rb', line 85

def reset
  write(SYSTEM_RESET)
end

#set_pin_mode(pin, mode) ⇒ Object

Public: Set the mode for a pin.

pin - The Integer pin to set. mode - The Fixnum mode (INPUT, OUTPUT, ANALOG, PWM or SERVO)

Examples

set_pin_mode(13, OUTPUT)

Returns nothing.



99
100
101
102
# File 'lib/firmata/board.rb', line 99

def set_pin_mode(pin, mode)
  pins[pin].mode = mode
  write(PIN_MODE, pin, mode)
end

#toggle_pin_reporting(pin, state = HIGH, mode = REPORT_DIGITAL) ⇒ Object

Public: Toggle pin reporting on or off.

pin - The Integer pin to toggle. mode - The Integer mode the pin will report. The valid values are

REPORT_DIGITAL or REPORT_ANALOG (default: REPORT_DIGITAL).

state - The Integer state to toggle the pin. The valid value are

HIGH or LOW (default: HIGH)

Returns nothing.



210
211
212
# File 'lib/firmata/board.rb', line 210

def toggle_pin_reporting(pin, state = HIGH, mode = REPORT_DIGITAL)
  write(mode | pin, state)
end

#versionObject

Public: The major and minor firmware version on the board. Will report as “0.0” if report_version command has not been run.

Returns String the firmware version as “minor.major”.



155
156
157
# File 'lib/firmata/board.rb', line 155

def version
  [@major_version, @minor_version].join('.')
end