Karthik Chikmagalur
By: a guest | Jun 6th, 2009 | Syntax:
Perl | Size: 4.54 KB | Hits: 273 | Expires: Never
#!/usr/bin/perl -w
###################################################################
#morseblink.pl --- blinks a text file in morse on keyboard leds
#
#Quick and dirty hack to read text files in morse by flashing a
#keyboard LED. (Default is the scroll-lock LED) Inspired by the novel
#*Cryptonomicon* by Neal Stephenson.
#
#Copyright (C) Karthik Chikmagalur (2007-09)
#<karthik[dot]chikmagalur@gmail.com>
#
#Version: 0.0.1
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2, or (at your option)
#any later version.
#
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#General Public License for more details.
#
###################################################################
#USAGE: perl morseblink.pl [options] [File1 File2 ... | STDIN]
#options: -do[t] n -da[sh] n -g[ap] n -v[erbose] -e[ncode]
# -l[ed] [1-32]
#
# dot: dot duration (float, seconds)
# dash: dash duration (float, seconds)
# gap: gap between letters (float, seconds)
# verbose: report bits being flashed at STDOUT
# encode: encode and print, do not flash LED
# led: led number from 1-32. Scrollock is 3.
#
#REQUIREMENTS: perl v. 5.8 or higher, Any *NIX with Xorg will do.
###################################################################
use Getopt::Long;
use Time
::HiRes qw( usleep
);
my ($dot, $dash, $gap, $wordsep, $led) = (0.4, 1, 0.6, 1, 3);
my ($pointer, $verbose, $encode, $cipher);
our $linesep = 1;
GetOptions(
'dot=f'=>\$dot,
'dash=f'=>\$dash,
'gap=f'=>\$gap,
'wordsep=i'=> \$wordsep,
'linesep=i'=> \$linesep,
'verbose+'=> \$verbose,
'led=i' => \$led,
'encode' => \$encode);
$wordsep = ' ' x $wordsep;
$linesep = ' ' x $linesep;
our %morse = (
'\"' => '.-..-.',
'$' => '...-..-',
'(' => '-.--.',
')' => '-.--.-',
'+' => '.-.-.',
',' => '--..--',
'-' => '-....-',
'.' => '.-.-.-',
'/' => '-..-.',
'0' => '-----',
'1' => '.----',
'2' => '..---',
'3' => '...--',
'4' => '....-',
'5' => '.....',
'6' => '-....',
'7' => '--...',
'8' => '---..',
'9' => '----.',
':' => '---...',
';' => '-.-.-.',
'=' => '-...-',
'?' => '..--..',
'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' => '--..',
'_' => '..--.-',
'\'' => '.----.',
' ' => $wordsep);
sub encode {
#encodes a string to morse given a file object
our (%morse, $linesep);
my ($line, @letters, $result);
while (<$file>) {
foreach (@letters) {
$_ = '0' if (not exists $morse{$_});
$result = $result . "$morse{$_} ";
}
$result = $result . $linesep;
}
}
sub flash {
#Flash LED given a morse string (. and -)
my ($dot, $dash, $gap, $led, $verbose) = @_;
my $i;
my @bits = split "", $message;
foreach (@bits) {
$i++;
print "bit $i: \"$_\"\n" if ($verbose);
m/\./ and do { system("xset", "led", $led);
usleep $dot*1e6;
system("xset", "-led", $led);};
m/-/ and do { system("xset", "led", $led);
usleep $dash*1e6;
system("xset", "-led", $led);};
m/ / and do { usleep $gap*1e6; };
usleep $gap*1e6;
}
}
$cipher = main::encode STDIN if not @ARGV;
while (@ARGV) {
if (not -e $file) {
print "File \"$file\" does not exist. Skipping. \n";
next;}
$cipher = $cipher . encode $pointer;
}
else {main::flash $cipher, $dot, $dash, $gap, $led, $verbose;}