Class: Aio::Board
- Inherits:
-
Object
- Object
- Aio::Board
- 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
-
#analogRead(pin) ⇒ Integer?
Reads the value from a specified analog pin.
-
#analogReference(type) ⇒ Boolean
Configures the reference voltage used for analog input.
-
#analogWrite(pin, value) ⇒ Boolean
Writes an analog value (PWM wave) to a pin.
-
#close ⇒ nil
Closes the connection with the board.
-
#digitalRead(pin) ⇒ Integer?
Reads the value from a specified digital pin.
- #digitalWrite(pin, state) ⇒ Boolean
-
#initialize(port) {|self| ... } ⇒ Board
constructor
A new instance of Board.
-
#millis ⇒ Integer?
Returns the number of milliseconds since the Arduino board began running the current program.
-
#pinMode(pin, mode) ⇒ Boolean
Sets the mode of operation for a specific pin.
-
#ready? ⇒ Boolean
Tells if the board is ready.
Constructor Details
#initialize(port) {|self| ... } ⇒ Board
Returns a new instance of Board.
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.
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.
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.
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 |
#close ⇒ nil
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.
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
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 |
#millis ⇒ Integer?
Returns the number of milliseconds since the Arduino board began running the current program.
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.
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.
158 159 160 161 162 163 |
# File 'lib/aio/board.rb', line 158 def ready? try(false) { sendCMD [8, empty, empty] onACK {true} } end |