Module: Aio

Defined in:
lib/aio.rb,
lib/aio/board.rb,
lib/aio/errors.rb,
lib/aio/sketch.rb,
lib/aio/helpers.rb,
lib/aio/constants.rb

Overview

Aio (Arduino Input/Output) is a robust library with built-in redundancy check for data integrity and command confirmation for remotely controlling Arduino boards using Ruby code and the serial port.

It implements lots of functions found in the Arduino Reference and they even share the same names and parameters for an easier experience.

  • Currently, these boards are fully supported (and many others should work):

    • UNO, Duemilanove, Diecimila, Pro

    • Leonardo, Yun, Micro

    • Mega, Mega 2560, Mega ADK

    • Nano, Mini, Fio, Pro Mini

  • Supported functions implemented in the Aio::Board class:

    • pinMode

    • digitalRead, digitalWrite

    • analogRead, analogWrite, analogReference

    • millis

  • Supported constants:

    • LOW, HIGH

    • INPUT, INPUT_PULLUP, OUTPUT

    • DEFAULT, INTERNAL, EXTERNAL, INTERNAL1V1, INTERNAL2V56

  • Other supported functions (see Aio::Helpers module):

    • lowByte, highByte, bit, bitClear, bitSet, bitRead, bitWrite

    • min, max, abs, pow, sqrt, map, constrain

    • sin, cos, tan

    • random, randomSeed

Example

Blinking a LED:

require "aio"

Aio::Board.new("/dev/ttyUSB0") do |board|
  sleep 0.1 until board.ready?

  board.pinMode(13, Aio::OUTPUT)

  5.times do
    board.digitalWrite(13, Aio::HIGH)
    sleep 0.5
    board.digitalWrite(13, Aio::LOW)
    sleep 0.5
  end
end

Check more examples at the Github page.

Installation

  1. Install the Aio gem:

    $ gem install aio
    
  2. Print the sketch required by the library:

    $ aiosketch > sketch.ino
    

    or

    $ ruby -raio -e "Aio.print_sketch" > sketch.ino
    
  3. Compile and burn the sketch using the Arduino IDE.

Limitations

  • As this library uses the Arduino serial port, digital pins 0 and 1 must not be used.

  • This library code isn’t executed in realtime as Arduino does, and the communication protocol is relatively slow, so trying to make code that requires exact timing is tricky.

  • Also take into account that this library does not check if PIN X is available in BOARD Z, or if the value supplied is valid or not for a specific command, so check your board’s documentation first.

  • This library is NOT thread safe.

Issues

Post any issues at the Github issue tracker.

Defined Under Namespace

Modules: Helpers Classes: Board, CommandError, DeviceError, Error

Constant Summary collapse

BAUDRATE =

Baud rate (shared with the sketch).

Changing it requires printing and burning the sketch again.

57600
TIMEOUT =

Read timeout in milliseconds (shared with the sketch).

Changing it requires printing and burning the sketch again.

100
ACK =
246
NAK =
144
SYNCBYTE =
255
LOW =
0
HIGH =
1
INPUT =
0
OUTPUT =
1
INPUT_PULLUP =
2
EXTERNAL =
0
DEFAULT =
1
INTERNAL =
3
INTERNAL1V1 =
2
INTERNAL2V56 =
3

Class Method Summary collapse

Class Method Details

Prints the Arduino sketch to STDOUT.

Returns:

  • (nil)


103
104
105
106
# File 'lib/aio.rb', line 103

def self.print_sketch
	STDOUT.puts SKETCH.gsub(/\t/,'')
	nil
end