Class: ArduinoFirmata::Arduino

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/arduino_firmata/arduino.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serialport_name, params) ⇒ Arduino

Returns a new instance of Arduino.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arduino_firmata/arduino.rb', line 8

def initialize(serialport_name, params)
  @serialport_name = serialport_name
  @nonblock_io = !!params[:nonblock_io]
  @eventmachine = !!params[:eventmachine]
  @read_byte_size = eventmachine ? 256 : 9600
  @process_input_interval = eventmachine ? 0.0001 : 0.01
  @status = Status::CLOSE
  @wait_for_data = 0
  @execute_multi_byte_command = 0
  @multi_byte_channel = 0
  @stored_input_data = []
  @parsing_sysex = false
  @sysex_bytes_read = 0

  @digital_output_data = Array.new(16, 0)
  @digital_input_data = Array.new(16, 0)
  @analog_input_data = Array.new(16, 0)

  @version = nil

  @serial = SerialPort.new(@serialport_name, params[:bps], params[:bit], params[:stopbit], params[:parity])
  @serial.read_timeout = 10
  sleep 3 if old_arduino_device?
  @status = Status::OPEN

  at_exit do
    close
  end

  @thread_status = false
  run do
    @thread_status = true
    while status == Status::OPEN do
      process_input
      sleep @process_input_interval
    end
    @thread_status = false
  end

  (0...6).each do |i|
    write(REPORT_ANALOG | i)
    write 1
  end
  (0...2).each do |i|
    write(REPORT_DIGITAL | i)
    write 1
  end

  loop do
    write REPORT_VERSION
    sleep 0.5
    break if @version
  end
  sleep 0.5 if old_arduino_device?
end

Instance Attribute Details

#eventmachineObject (readonly)

Returns the value of attribute eventmachine.



6
7
8
# File 'lib/arduino_firmata/arduino.rb', line 6

def eventmachine
  @eventmachine
end

#nonblock_ioObject (readonly)

Returns the value of attribute nonblock_io.



6
7
8
# File 'lib/arduino_firmata/arduino.rb', line 6

def nonblock_io
  @nonblock_io
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/arduino_firmata/arduino.rb', line 6

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/arduino_firmata/arduino.rb', line 6

def version
  @version
end

Instance Method Details

#analog_read(pin) ⇒ Object



111
112
113
# File 'lib/arduino_firmata/arduino.rb', line 111

def analog_read(pin)
  @analog_input_data[pin]
end

#analog_write(pin, value) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/arduino_firmata/arduino.rb', line 147

def analog_write(pin, value)
  pin_mode pin, PWM
  write(ANALOG_MESSAGE | (pin & 0x0F))
  write(value & 0x7F)
  if write(value >> 7) == 1
    return value
  end
end

#closeObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/arduino_firmata/arduino.rb', line 77

def close
  return if status == Status::CLOSE
  @status = Status::CLOSE
  @serial.close
  loop do
    if @serial.closed? and @thread_status != true
      break
    end
    sleep 0.01
  end
end

#digital_read(pin) ⇒ Object



107
108
109
# File 'lib/arduino_firmata/arduino.rb', line 107

def digital_read(pin)
  (@digital_input_data[pin >> 3] >> (pin & 0x07)) & 0x01 > 0
end

#digital_write(pin, value) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/arduino_firmata/arduino.rb', line 131

def digital_write(pin, value)
  pin_mode pin, OUTPUT
  port_num = (pin >> 3) & 0x0F
  if value == 0 or value == false
    @digital_output_data[port_num] &= ~(1 << (pin & 0x07))
  else
    @digital_output_data[port_num] |= (1 << (pin & 0x07))
  end

  write(DIGITAL_MESSAGE | port_num)
  write(@digital_output_data[port_num] & 0x7F)
  if write(@digital_output_data[port_num] >> 7) == 1
    return value
  end
end

#old_arduino_device?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/arduino_firmata/arduino.rb', line 73

def old_arduino_device?
  File.basename(@serialport_name) !~ /^tty\.usbmodem/
end

#pin_mode(pin, mode) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/arduino_firmata/arduino.rb', line 115

def pin_mode(pin, mode)
  write SET_PIN_MODE
  write pin
  mode = case mode
         when true
           OUTPUT
         when false
           INPUT
         else
           mode
         end
  if write(mode) == 1
    return mode
  end
end

#resetObject



89
90
91
# File 'lib/arduino_firmata/arduino.rb', line 89

def reset
  write SYSTEM_RESET
end

#run(&block) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/arduino_firmata/arduino.rb', line 64

def run(&block)
  return unless block_given?
  if eventmachine
    EM::defer &block
  else
    Thread.new &block
  end
end

#servo_write(pin, angle) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/arduino_firmata/arduino.rb', line 156

def servo_write(pin, angle)
  pin_mode pin, SERVO
  write(ANALOG_MESSAGE | (pin & 0x0F))
  write(angle & 0x7F)
  if write(angle >> 7) == 1
    return angle
  end
end

#sysex(command, data = []) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/arduino_firmata/arduino.rb', line 93

def sysex(command, data=[])
  ## http://firmata.org/wiki/V2.1ProtocolDetails#Sysex_Message_Format
  raise ArgumentError, 'command must be Number' unless command.kind_of? Fixnum
  raise ArgumentError, 'data must be 7bit-Number or Those Array' unless [Fixnum, Array].include? data.class

  write_data = data.kind_of?(Array) ? data : [data]
  write START_SYSEX
  write command
  write_data.each do |d|
    write (d & 0b1111111) # 7bit
  end
  write END_SYSEX
end