Class: Aio::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/aio/board.rb

Overview

This is the main class that communicates with the Arduino board.

It implements several mechanisms like ACK/NAK for confirming command execution and redundancy checks to ensure data integrity.

Instance Method Summary collapse

Constructor Details

#initialize(port) {|self| ... } ⇒ Board

Returns a new instance of Board.

Examples:

Modes of initialization

# explicit
board = Aio::Board.new("/dev/ttyUSB1")

# block mode (closes the connection at the end)
Aio::Board.new("/dev/ttyUSB0") do |board|
  # code...
end

Yields:

  • (self)

    an optional block of code to execute.

Raises:

  • (DeviceError)

    when the serial port is unavailable.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aio/board.rb', line 24

def initialize(port)
  @serial = SerialPort.new(port, BAUDRATE, 8, 1, SerialPort::NONE)
rescue Errno::ENOENT
  raise Aio::DeviceError, "Unavailable serial port at address => '#{port}'"
else
  @serial.flow_control = SerialPort::NONE
  @serial.read_timeout = TIMEOUT
  @serial.sync = true
  
  if block_given?
    yield(self)
    close
  end
end

Instance Method Details

#analogRead(pin) ⇒ Integer?

Reads the value from a specified analog pin.

Raises:



99
100
101
102
103
104
105
106
107
108
# File 'lib/aio/board.rb', line 99

def analogRead(pin)
  try(nil) {
    sendCMD [4, pin, empty]
    onACK do
      onData(4) do |bytes|
        bytes[0] + (bytes[1] << 8)
      end
    end
  }
end

#analogReference(type) ⇒ Boolean

Configures the reference voltage used for analog input.

Raises:



130
131
132
133
134
135
# File 'lib/aio/board.rb', line 130

def analogReference(type)
  try(false) {
    sendCMD [6, type, empty]
    onACK {true}
  }
end

#analogWrite(pin, value) ⇒ Boolean

Writes an analog value (PWM wave) to a pin.

Raises:



117
118
119
120
121
122
# File 'lib/aio/board.rb', line 117

def analogWrite(pin, value)
  try(false) {
    sendCMD [5, pin, value]
    onACK {true}
  }
end

#closenil

Closes the connection with the board.



43
44
45
46
# File 'lib/aio/board.rb', line 43

def close
  @serial.close
  nil
end

#digitalRead(pin) ⇒ Integer?

Reads the value from a specified digital pin.

Raises:



68
69
70
71
72
73
74
75
76
77
# File 'lib/aio/board.rb', line 68

def digitalRead(pin)
  try(nil) {
    sendCMD [2, pin, empty]
    onACK do
      onData(4) do |bytes|
        bytes[0]
      end
    end
  }
end

#digitalWrite(pin, state) ⇒ Boolean

Writes a HIGH (1) or a LOW (0) value to a digital pin.

Raises:



86
87
88
89
90
91
# File 'lib/aio/board.rb', line 86

def digitalWrite(pin, state)
  try(false) {
    sendCMD [3, pin, state]
    onACK {true}
  }
end

#millisInteger?

Returns the number of milliseconds since the Arduino board began running the current program.

Raises:



142
143
144
145
146
147
148
149
150
151
# File 'lib/aio/board.rb', line 142

def millis
  try(nil) {
    sendCMD [7, empty, empty]
    onACK do
      onData(4) do |bytes|
        bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24)
      end
    end
  }
end

#pinMode(pin, mode) ⇒ Boolean

Sets the mode of operation for a specific pin.

Raises:



55
56
57
58
59
60
# File 'lib/aio/board.rb', line 55

def pinMode(pin, mode)
  try(false) {
    sendCMD [1, pin, mode]
    onACK {true}
  }
end

#ready?Boolean

Tells if the board is ready.

Raises:



158
159
160
161
162
163
# File 'lib/aio/board.rb', line 158

def ready?
  try(false) {
    sendCMD [8, empty, empty]
    onACK {true}
  }
end