#!/usr/bin/perl
#
#
# Author: Bruce S. Garlock
# Date:   2002-09-11
# Requirements: Device::SerialPort 0.12 (from cpan)
#
# Version: 0.1
#
#
# Description:  This perl script is for logging of data from a serial
# port, to a specified logfile.  The logfile can then be parsed with
# other programs for reporting purposes.
# 
# This program was written for specifically logging Multitech's
# MTASR2-203 T1 Router.  The router outputs text to the command
# port with 57.6k, 8-1-N, and No flow control.
#
#

use Device::SerialPort 0.12;

$LOGDIR    = "/home/frida";              # path to data file
$LOGFILE   = "arduino.log";            # file name to output to
$PORT      = "/dev/usb/tts/1";          # port to watch

my $record=['video','photo','sound','location'];
my $keyword=['house','euro','pencil','tree','heart','book','rabbit','eiffel-tower'];

my $btnstate;

#
#
# Serial Settings
#
#

$ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$ob->baudrate(9600)   || die "failed setting baudrate";
$ob->parity("none")    || die "failed setting parity";
$ob->databits(8)       || die "failed setting databits";
$ob->stopbits(1)       || die "failed setting stopbits";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings    || die "no settings";

#
# Send a string to the port
#
#
#$pass=$ob->write("i\n");
#sleep 1;

#
# open the logfile, and Port
#

open(LOG,">>${LOGDIR}/${LOGFILE}")
    ||die "can't open smdr file $LOGDIR/$LOGFILE for append: $SUB $!\n";

select(LOG), $| = 1;      # set nonbufferd mode

#
# Loop forver, logging data to the log file
#

while (1) {
open(DEV, "<$PORT") 
    || die "Cannot open $PORT: $_";


while($_ = <DEV>){        # print input device to file
    print LOG $_;

  $_=~/s (\d+) (\d+) (\d+)/m;

  my ($sw2,$sw1,$btn) = ($1,$2,$3);

  # onrelease - falling edge
    if ((!$btn) && $btnstate) {
        $btnstate=$btn;
    }
        
          # onpress - rising edge
            if ($btn && (!$btnstate)) {
                $btnstate=$btn;
                    print "Recording ".$record->[$sw1]." with keyword ".$keyword->[$sw2]."!\n";
                    
                        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
                            $year+=1900;
                                $mon+=1;
                                
                                    my $timestamp=sprintf("%04d-%02d-%02d_%02d:%02d:%02d",$year,$mon,$mday,$hour,$min,$sec);
                                    
                                        print "$timestamp\n";
                                           system("./frida-set-led 3 1"); 
                                                system("./frida-record-$record->[$sw1] $timestamp $keyword->[$sw2]");
                                                    sleep(1);
	                                        system("./frida-set-led 3 0");
                                                     sleep(1);
          }
                                                                   
                                                                   


##    print STDOUT $_;
}
close(DEV);
}

undef $ob;
