Saturday, March 13, 2010

Pinguino and perl

You can use perl to communicate with Pinguino via USB.

To do it you have to install libusb library and Device::USB module for perl.

After that you can upload this program to Pinguino:

testusb.pde:


// USB Test
// Marin Purgar marin.purgar@gmail.com
// Use with led_on.pl and led_off.pl companion programs

uchar cmd;

void setup(void) {
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH);
}

void loop(void) {
  if (USB.available()) {
    cmd = USB.read();
  }
  if (cmd == 'N') {
    digitalWrite(0, HIGH);
  }
  if (cmd == 'F') {
    digitalWrite(0, LOW);
  }
}

// EOF


And you can create two perl programs to turn the LED on (sending the character 'N' to Pinguino) and off (sending the character 'F' to Pinguino):

led_on.pl:


#!/usr/bin/perl -w

use strict;
use warnings;

use Device::USB;

my $usb = Device::USB->new() || die "Unable to initialize libusb!";
$usb->debug_mode(2);
my $dev = $usb->find_device(0x04d8, 0xfeaa) || die "Unable to find USB device!";
$dev->open() || die "Unable to open USB device!";
$dev->set_configuration(3);
$dev->claim_interface(0);
$dev->bulk_write(0x01, "N", 100);

# EOF

led_off.pl:


#!/usr/bin/perl -w

use strict;
use warnings;

use Device::USB;

my $usb = Device::USB->new() || die "Unable to initialize libusb!";
$usb->debug_mode(2);
my $dev = $usb->find_device(0x04d8, 0xfeaa) || die "Unable to find USB device!";
$dev->open() || die "Unable to open USB device!";
$dev->set_configuration(3);
$dev->claim_interface(0);
$dev->bulk_write(0x01, "F", 100);

# EOF

By running led_on.pl or led_off.pl you can turn the LED on/off via USB using perl.

Next, I am going to put some sensors (TCRT5000) on my protoboard and try to get sensor status from it via USB and perl.

Stay tuned ...
I'm reading: Pinguino and perl Tweet this!

0 comment(s):

Post a Comment