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.



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

def initialize(port)
  if port.is_a?(String)
    @serial_port = Serial.new(port, 57600, 8)
  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'
end

Instance Attribute Details

#analog_pinsObject (readonly)

Public: Returns the Array of analog pins on Arduino.



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

def analog_pins
  @analog_pins
end

#async_eventsObject (readonly)

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



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

def async_events
  @async_events
end

#firmware_nameObject (readonly)

Public: Returns the String firmware name of Arduino.



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

def firmware_name
  @firmware_name
end

#pinsObject (readonly)

Public: Returns the Array of pins on Arduino.



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

def pins
  @pins
end

#serial_portObject (readonly)

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



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

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.



126
127
128
129
# File 'lib/firmata/board.rb', line 126

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.



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

def connect
  unless @connected

    handle_events!

    catch(:initialized) do
      loop do
        query_report_version #unless @major_version.zero?
        sleep 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)


48
49
50
# File 'lib/firmata/board.rb', line 48

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.



144
145
146
# File 'lib/firmata/board.rb', line 144

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.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/firmata/board.rb', line 107

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.



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

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.



227
228
229
230
# File 'lib/firmata/board.rb', line 227

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



232
233
234
235
236
237
238
239
240
241
# File 'lib/firmata/board.rb', line 232

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.



194
195
196
# File 'lib/firmata/board.rb', line 194

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.



187
188
189
# File 'lib/firmata/board.rb', line 187

def query_capabilities
  write(START_SYSEX, CAPABILITY_QUERY, END_SYSEX)
end

#query_firmwareObject

Public: Ask the Ardution for its firmware name.

Returns nothing.



166
167
168
# File 'lib/firmata/board.rb', line 166

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.



175
176
177
# File 'lib/firmata/board.rb', line 175

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

#query_report_versionObject



180
181
182
# File 'lib/firmata/board.rb', line 180

def query_report_version
  write REPORT_VERSION
end

#read_and_processObject

Public: Read the serial port and process the results

Returns nothing.



75
76
77
# File 'lib/firmata/board.rb', line 75

def read_and_process
  process(read)
end

#report_versionObject

Public: Ask the Arduino to report its version.

Returns nothing.



159
160
161
# File 'lib/firmata/board.rb', line 159

def report_version
  write(REPORT_VERSION)
end

#resetObject

Public: Send a SYSTEM_RESET to the Arduino

Returns nothing.



82
83
84
# File 'lib/firmata/board.rb', line 82

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.



96
97
98
99
# File 'lib/firmata/board.rb', line 96

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.



207
208
209
# File 'lib/firmata/board.rb', line 207

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”.



152
153
154
# File 'lib/firmata/board.rb', line 152

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