Wednesday, May 16, 2012

First steps with Perl on the BeagleBone

Today I received a BeagleBone A5, which is a very small board containing a 720 MHz ARM CPU and 256 MB of RAM, among other things.

The first step was to start flashing the built-in LEDs via a Perl script..
I found a script on another site (http://www.impulseair.com.au/site/?p=215) but it's not "modern" Perl, and was working in an inefficient way. (Opening and closing the LED filehandles continually).
Here's my updated version:


#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Fcntl qw(:DEFAULT);

unless (-w "/sys/class/leds/beaglebone::usr0/brightness") {
    die "I think you need to run this as root..\n";
}

my @leds = map {
    my $tmp;
    sysopen($tmp, "/sys/class/leds/beaglebone::usr$_/brightness", O_WRONLY);
    $tmp;
} (0..3);

print "Hit Ctrl-C to exit..\n";
while (1) {
  
  led(0,0,0,1);
  led(0,0,1,0);
  led(0,1,0,0);
  led(1,0,0,0);
  led(0,1,0,0);
  led(0,0,1,0);
  
}

sub led {
    my @vals = @_;
    for my $i (0..3) {
        syswrite($leds[$i], "$vals[$i]", 1);
    }
    select(undef, undef, undef, 0.05);
}


5 comments:

  1. Did you compile Perl yourself for or on it?
    I've tried to crosscompile Perl for my Synology NAS but failed because cross compilation isn't really maintained.
    The Perl that ships with DSM 4.0 is 5.8.6 ;(

    ReplyDelete
  2. I didn't compile Perl for it..
    I'm running Ubuntu 12.04 (Precise Pangolin) on it, so it already had Perl 5.14.2 available, and plenty of modules.

    ReplyDelete
  3. I would love a PERL tutorial or example of reading and writing GPIO's and reading analog inputs too! :-) Love that I can program this thing in PERL!

    ReplyDelete
  4. Actually, after some googling, I found these two links:

    http://www.gigamegablog.com/2012/01/05/beaglebone-coding-101-blinking-an-led/
    http://www.nathandumont.com/node/250

    If you combine those two, you can pretty quickly make a perl script to read or write gpio38 (port 8, pin 3). The read was a bit tricky, since you need to set some higher order bits in the mux byte to set the direction. See the second link above.

    ReplyDelete
  5. Hello Unknown,
    I guess you must have missed my entire Perl library that makes this stuff quite easy?
    I thought I blogged about it -- see the repo at https://github.com/TJC/BeagleBone

    It's not what I would call finished, but it does provide support GPIO in both directions as well as support for a LCD controller.

    ReplyDelete