#!c:/perl/bin/perl.exe

# Do Events from Serial Input
#
# Matt Mankins 
#
# http://www.loremipsumbooks.com/

use strict;

use Win32::SerialPort;
use Time::HiRes qw(sleep);

# unbuffered output
$| = 1;

my %action;

# Set this to whatever port you receive stuff on    
my $port = "COM3";

print "Starting serial port\n";

die unless -e "printer_$port.cfg";

my $port_obj = start Win32::SerialPort ("printer_$port.cfg") || die "Can't open pager_$port.cfg: $^E\n"; 

print "Starting main loop\n";

while (1)
{

    my $gotit = "";
    until ($gotit = $port_obj->input)
    {
	#print "sleepy\n";
	sleep .2;
    }

    &load_actions;

    print "$gotit\n";
    if ($action{$gotit})
    {
	print "doing ".$action{$gotit}."\n";

	# Do the action
        `$action{$gotit}`;
    }

    sleep 1;
}

#  $port_obj->write($to_print);

$port_obj->close();

sub load_actions
{
	# This populates the global %action with a program that should be run when we receive certain input

	open (WOTD, 't:\wotd');
	my $wotd = ;
	close WOTD;

	ACTIONS:
	{
    	 $action{"A"} = 'c:\perl\bin\perl.exe c:\utils\speak '.$wotd;
    	 $action{"C"} = 'c:\perl\bin\perl.exe c:\Inetpub\wwwroot\utils\n.pl';
    	 $action{"DC"} = 'c:\utils\next.pl';
	}
}

1;


