# Arduino Control from Perl

## Controlling an Arduino with Perl

## Outline

- Quick intros on Arduino
- Quick intros on Firmata
- Quick intros on Demo Perl Library
- Practical Example - Specifications Parsing

## What's an Arduino?

- Hobbyist's Microcontroller Development Platform
- Both Hardware and Software

## Arduino's Hardware (One example!):

One part uC...

## Arduino's Software:

Another part IDE...

## Quick Arduino Example (Blink):

    void setup() {
      pinMode(13, OUTPUT);  // LED is connected to pin 13
    }

    void loop() {
      digitalWrite(13, HIGH);   // set the LED on
      delay(1000);                  // wait for a second
      digitalWrite(13, LOW);    // set the LED off
      delay(1000);                  // wait for a second
    }

## Why's Arduino Cool?

- Easy
- Mindshare
- Support and Accessibility

## Firmata:

http://www.firmata.org
"Firmata is a generic protocol for communicating with microcontrollers from software on a host computer. It is intended to work with any host computer software package."

## In pragmatic terms...

System to control Arduino I/O pins from a computer.

## Control Example

Firmata GUI Clients

## How does it work?

- Arduino runs host code
- Computer runs client code
- Serial communication

## Serial Communication Protocol

- MIDI usually used for electronic instruments
- Redefine MIDI messages for I/O

## MIDI for Firmata

- 8-bit protocol
- Top bit indicates data or command

  c/d | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0

- Variable packet length
- Has interruptible data-stream mode

## Example Firmata Protocol

- Digital Pin 11 On

  HEX | c | CMD | PORT
  0x91 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1

  0x04 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0

  0x00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0

- v2.1 Protocol

## Perl Based Interface

Work in progress: Device::Firmata

- Spartan interface
- Use Device::SerialPort

## Example: Blink

    use Device::Firmata;
    use Device::Firmata::Constants qw/:all/;

    my $device = Device::Firmata->open('/dev/ttyUSB0');

    $device->pin_mode(13=>PIN_OUTPUT);
    while (1) {
        $device->digital_write(13=>1);
        sleep(1);
        $device->digital_write(13=>0);
        sleep(1);
    }

## Non-demonstrated Features

- Digital Input - $device->digital_read($pin)
- Analog Input - $device->analog_read($pin)
- PWM! - $device->analog_write($pin)

## Practical Uses?

- I/O for desktop applications (Very Poor Man's Labjack)
- Education/Study

## Example: Interface Exploration

Sure Electronics: Red LED Matrix Board
- 32x8 matrix
- Driver IC: HT1632
- Specifications...

## HT1632 Specifications

Ugh.

## Prototyping the Interface

- Avoid save, compile, upload, reset, disappointment cycle!

SO:

- Use Firmata to link Arduino and Perl
- Write signal implementation in Perl

## Connection Diagram

## Logic Timings

## Game Plan

- 1. Strobe CS pin
- 2. Clock data in
- ???
- 4. Profit! Display cool things!

## Pin Strobe Code

    # $pin = pin connected CS
    # $PULSE_LEN = 0.001s

    $device->digital_write($pin=>1);
    select undef, undef, undef, $PULSE_LEN;
    $device->digital_write($pin=>0);
    select undef, undef, undef, $PULSE_LEN;

## Data Writing

    # $data_pin = connected DATA line
    # $wr_pin = connected to WR line
    # $PULSE_LEN = 0.001s

    $device->digital_write($data_pin=>$v);
    $device->digital_write($wr_pin=>1);
    select undef, undef, undef, $PULSE_LEN;
    $device->digital_write($wr_pin=>0);
    select undef, undef, undef, $PULSE_LEN;

## Applied Example

- Full detail of code not discussed.
- Can draw lines!
- Can draw graphs!
- Can draw words!

## Prototyping!!!

- Bandwidth limitations
- MIDI interpreter limitations
- Computer controlled microcontroller?!

## Summary

- Arduino = Cool Prototyping Platform
- Firmata = Cool Protocol to control Arduino
- Perl = RAD tool to script interactions with the real world

## Presentation Tool

Used jQuery plugin by Trevor Davis:

- jquery-presentation

http://bako.ca/pm/firmata

??