Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

LED blinking Morse code from Raspberry Pi

by rje (Deacon)
on Aug 30, 2014 at 15:23 UTC ( [id://1099072]=CUFP: print w/replies, xml ) Need Help??

With the handy Device::BCM2835 package, I tossed together this little package so I could send "morse code" out on an LED I connected to GPIO Pins #5 and #7.

(FYI: the Raspberry Pi can run a number of Linux systems, all with Perl. I wrote a tiny HTTP server on it which lets me create and serve Commodore disk images, also allowing me to extract and inject files, in Perl of course. It runs behind my firewall...)

package GPIO; use strict; use Device::BCM2835; Device::BCM2835::init() || die "Error initializing BCM2835 interface.\ +n"; sub new { bless {}, shift } my @pins = ( &Device::BCM2835::RPI_V2_GPIO_P1_07, &Device::BCM2835::RPI_V2_GPIO_P1_11, &Device::BCM2835::RPI_V2_GPIO_P1_12, &Device::BCM2835::RPI_V2_GPIO_P1_13, &Device::BCM2835::RPI_V2_GPIO_P1_15, &Device::BCM2835::RPI_V2_GPIO_P1_16, &Device::BCM2835::RPI_V2_GPIO_P1_18, &Device::BCM2835::RPI_V2_GPIO_P1_22 ); # Set pins to be outputs. foreach my $pin ( @pins ) { Device::BCM2835::gpio_fsel( $pin, &Device::BCM2835::BCM2835_GPIO_FS +EL_OUTP ); } sub dit { my $gpio4 = $pins[0]; Device::BCM2835::gpio_write( $gpio4, 1 ); Device::BCM2835::delay( 80 ); Device::BCM2835::gpio_write( $gpio4, 0 ); Device::BCM2835::delay( 50 ); } sub dah { my $gpio4 = $pins[0]; Device::BCM2835::gpio_write( $gpio4, 1 ); Device::BCM2835::delay( 240 ); Device::BCM2835::gpio_write( $gpio4, 0 ); Device::BCM2835::delay( 50 ); } sub key { foreach (split '', shift) { dit() if /\./; dah() if /-/; Device::BCM2835::delay( 150 ) if /\s/; } Device::BCM2835::delay( 400 ); } sub word { foreach ( split '', shift ) # letters { print "$_\n"; key( '.-' ) if /a/i; key( '-...' ) if /b/i; key( '-.-.' ) if /c/i; key( '-..' ) if /d/i; key( '.' ) if /e/i; key( '..-.' ) if /f/i; key( '--.' ) if /g/i; key( '....' ) if /h/i; key( '..' ) if /i/i; key( '.---' ) if /j/i; key( '-.-' ) if /k/i; key( '.-..' ) if /l/i; key( '--' ) if /m/i; key( '-.' ) if /n/i; key( '---' ) if /o/i; key( '.--.' ) if /p/i; key( '--.-' ) if /q/i; key( '.-.' ) if /r/i; key( '...' ) if /s/i; key( '-' ) if /t/i; key( '..-' ) if /u/i; key( '...-' ) if /v/i; key( '.--' ) if /w/i; key( '-..-' ) if /x/i; key( '-.--' ) if /y/i; key( '--..' ) if /z/i; key( ' ' ) if /\s/i; } } 1;

Replies are listed 'Best First'.
Re: LED blinking Morse code from Raspberry Pi
by choroba (Cardinal) on Aug 30, 2014 at 19:28 UTC
    I would probably use a more declarative approach to the Morse code:
    my %morse = ( a => '.-', b => '-...', c => '-.-.', d => '-..' , e => '.', f => '..-.', g => '--.', h => '....', i => '..' , j => '.---', k => '-.-', l => '.-..', m => '--', n => '-.', o => '---', p => '.--.', q => '--.-', r => '.-.', s => '...', t => '-', u => '..-', v => '...-', w => '.--', x => '-..-', y => '-.--', z => '--..', ' ' => ' ' , ); key $morse{$_} for split //, lc shift;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: LED blinking Morse code from Raspberry Pi
by zentara (Archbishop) on Aug 31, 2014 at 08:52 UTC
    I know it's a sin, but I envy you and your project.... its so cool from a geek standpoint. Does this mean that you need some sort of socket i/o connection to the Pi, but the Perl script runs on your computer? Or can a Pi run the Perl?

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      A raspi usually runs a Linux distribution, Debian or something derived from that. (Other systems like the BSDs are also possible.) So, running Perl on the raspi should be no problem, and according to the module name Device::BCM2835, I guess the code runs on the raspi. The BCM2835 is the SoC, around which the raspi was designed. 700 MHz ARM CPU and 512 MByte RAM should be sufficient for Perl and lots of other things. Some years ago, a common PC or Mac had less RAM and a slower CPU clock, and even then, running Perl on Linux or *BSD was no problem. Update: According to the Wikipedia Article, the raspi CPU performance is roughly equal to a Pentium III 300 MHz.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        The (I would assume) most widely used Pi-disto is called Raspian and is indeed based on Debian.

        You can without problems either use the perl that comes with it or install your own by hand or via perlbrew (compilation however is a bit slow of course).

        Also installing cpan-modules is no problem.

      As mentioned already: yes, the RPi can run one of several Linux distributions. I've run Arch Linux before; currently a version of Debian is on my RPi. The OS is stored on a SD card, which can also act as storage.

      The standard "Raspbian" install comes with Perl, and I've used CPAN to install modules (such as the package I mentioned).

      I posted this code because I've just discovered how to use the I/O pins to read or write data to/from the outside world, with Perl, on my Raspberry Pi, and I'm excited!

        I'm excited

        Yeah, me too. I guess now the next step would be to get some opto-isolation relays, are be able to send messages out via the LEDS. Then, see if and ports give a varying output, say 128 or 256 voltage levels, then you could use it to provide realtime feedback for controls. After watching the various youtube videos on the newer anti-gravity experiments, it may be that the only way to get one, is to build one yourself.... and I'm dreamin of using the Raspian to do the Hall effect sensor and stabilizer functions. I need to get back to my home world. :-) Get me my Imhotep's flying platform!


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: LED blinking Morse code from Raspberry Pi
by rje (Deacon) on Sep 10, 2014 at 01:27 UTC
    I've got an update. Today I got a set of little patch cables in the mail and hooked up an old single-digit LED display to seven of the GPIO ports. I then added these subroutines to my library:
    sub bar { my @bar = @_; for my $pin (@pins) { Device::BCM2835::gpio_write( $pin, 0 ); } Device::BCM2835::delay( 40 ); for my $pin (@pins) { my $val = shift @bar; Device::BCM2835::gpio_write( $pin, 1 ) if $val == 1; } Device::BCM2835::delay( 240 ); } sub bilanidin { while ($_ = lc shift) { print "$_\n"; bar( qw/1 0 1 0 1 0 1 1/ ) if /a/; bar( qw/0 0 0 0 0 1 0 1/ ) if /b/; bar( qw/1 0 1 1 0 1 1 1/ ) if /ch/; bar( qw/1 0 0 0 1 1 0 0/ ) if /d/; bar( qw/1 0 0 1 0 1 1 0/ ) if /e/; bar( qw/1 0 0 0 1 0 0 1/ ) if /g/; bar( qw/0 0 0 1 0 1 1 0/ ) if /[ijy]/; bar( qw/1 0 0 1 1 0 1 0/ ) if /[kq]/; bar( qw/0 0 1 1 1 1 0 0/ ) if /l/; bar( qw/0 0 1 1 0 0 1/ ) if /m/; bar( qw/0 0 1 1 1 0 0 1/ ) if /n/; bar( qw/1 0 0 0 1 1 1 1/ ) if /o/; bar( qw/0 0 1 1 0 1 1 0/ ) if /p/; bar( qw/0 0 1 0 0 0 1 1/ ) if /r/; bar( qw/0 0 1 1 1 0 1 0/ ) if /s/; bar( qw/0 0 1 0 1 0 1 1/ ) if /t/; bar( qw/0 0 1 0 0 0 1 0/ ) if /[huvw]/; bar( qw/1 0 1 1 1 0 1 0/ ) if /x/; bar( qw/0 0 0 1 0 0 1 1/ ) if /z/; } Device::BCM2835::delay( 400 ); bar( qw/0 0 0 0 0 0 0 0/ ); }
    "Bilanidin" is a font designed by Marc Miller for the Traveller pencil-and-paper RPG. I adapted it to the display as closely as I could.

    Now my little server can talk to me in bilanidin letters as well as Morse code. This is a lot of fun.

Re: LED blinking Morse code from Raspberry Pi
by rje (Deacon) on May 03, 2021 at 14:47 UTC

    Since this post, of course, I've gotten (only slightly) fancier. I've got a set of eight LEDs, driven directly from eight GPIO lines (plus ground :) ). Fun only. And Perl driven.

    But my goal now is to hook up photodiodes so I can read my old paper tape reels I've had sitting in my house for 40 years...

Re: LED blinking Morse code from Raspberry Pi
by wjw (Priest) on Dec 06, 2014 at 01:36 UTC
    I too have just begun to work with the RPi and Arduino. My end project is to replace the controls on the grain dryer on my farm which currently uses all relay/timer logic: aka no feedback/tracking/analysis of any kind.

    The plan is to use the Arduino for control logic/data acquisition and Apache/Perl on the RPI to monitor status and display analysis. I was very pleased to find Perl ready to rock-n-roll on my Debian install on the RPi.

    If all goes well, by harvest 2015, I will have a machine which I can start/stop and monitor the entire process from some simple web forms all driven by Perl and delivered via wireless access.

    Good to hear someone else here playing around with Perl on the RPi! Thanks for you post and your code!

    ...the majority is always wrong, and always the last to know about it...

    A solution is nothing more than a clearly stated problem...

      Good to hear someone else here playing around with Perl on the RPi!

      I've also been playing around with Raspberry Pi.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1099072]
Approved by stefbv
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-18 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found