Advertisement
fooflington

morse.pl for RaspberryPi

Jul 7th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.13 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use Device::BCM2835;
  4. use strict;
  5.  
  6. # call set_debug(1) to do a non-destructive test on non-RPi hardware
  7. #Device::BCM2835::set_debug(1);
  8. Device::BCM2835::init()
  9.  || die "Could not init library";
  10.  
  11. # Blink pin 11:
  12. # Set RPi pin 11 to be an output
  13. Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_GPIO_P1_11,
  14.                             &Device::BCM2835::BCM2835_GPIO_FSEL_OUTP);
  15.  
  16. my %code = (
  17.         a => [\&dot, \&dash],
  18.         b => [\&dash,\&dot,\&dot,\&dot,],
  19.         c => [\&dash,\&dot,\&dash,\&dot,],
  20.         d => [\&dash,\&dot,\&dot,],
  21.         e => [\&dot,],
  22.         f => [\&dot,\&dot,\&dash,\&dot,],
  23.         g => [\&dash,\&dash,\&dot,],
  24.         h => [\&dot,\&dot,\&dot,\&dot,],
  25.         i => [\&dot,\&dot,],
  26.         j => [\&dot,\&dash,\&dash,\&dash,],
  27.         k => [\&dash,\&dot,\&dash,],
  28.         l => [\&dot,\&dash,\&dot,\&dot,],
  29.         m => [\&dash,\&dash,],
  30.         n => [\&dash,\&dot,],
  31.         o => [\&dash,\&dash,\&dash,],
  32.         p => [\&dot,\&dash,\&dash,\&dot,],
  33.         q => [\&dash,\&dash,\&dot,\&dash,],
  34.         r => [\&dot,\&dash,\&dot,],
  35.         s => [\&dot,\&dot,\&dot,],
  36.         t => [\&dash,],
  37.         u => [\&dot,\&dot,\&dash,],
  38.         v => [\&dot,\&dot,\&dot,\&dash,],
  39.         w => [\&dot,\&dash,\&dash,],
  40.  
  41.         # Turn it on
  42.         Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, 1);
  43.         Device::BCM2835::delay($on); # Milliseconds
  44.         # Turn it off
  45.         Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, 0);
  46.         Device::BCM2835::delay($off); # Milliseconds
  47. }
  48.  
  49. sub dot() {
  50.         flash(200);
  51. }
  52.  
  53. sub dash() {
  54.         flash(400);
  55. }
  56.  
  57. sub space() {
  58.         Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, 0);
  59.         Device::BCM2835::delay(500); # Milliseconds;
  60. }
  61.  
  62. sub eom() {
  63. # ...-.-
  64.  
  65. }
  66.  
  67. while(my $line = <>) {
  68.         my @letters = split( //, $line );
  69.         foreach my $letter (@letters) {
  70.                 $letter = lc ($letter);
  71.                 next unless defined $code{$letter};
  72.  
  73.                 foreach my $fn ( @{ $code{$letter} } ) {
  74.                         &$fn;
  75.                 }
  76.         }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement