package VWCDPIC;

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# CAJUN version 4.0, Copyright (C) 2001.
# CAJUN comes with ABSOLUTELY NO WARRANTY; for details see the LICENSE file.
# This is free software, and you are welcome to redistribute it
# under certain conditions; also see the LICENSE file for details.
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#!!! Gui parameters
#@ desc:This module is for use with the VWCDPIC from http://www.k9spud.com.
#@ input:port:Serial port that the VWCDPIC serial output is connected to:string:/dev/ttyS0:nonnull
#@ input:baud:Speed of the serial port:enum::9600:19200
#!!! Gui parameters


use strict;
use POSIX qw(:termios_h);
use lib '/usr/share/cajun/lib';
use InputDevice;
our @ISA=qw(InputDevice);


sub onLoad {
  my ($self)=@_;

  $self->SUPER::onLoad();

  open($self->{portfh},"<$self->{config}{devparam}{port}")
    or die "failed to open serial port: $!";
  my $portfd=fileno($self->{portfh});
  my $baud;
  {
    no strict 'refs';
    $baud=&{"POSIX::B" . $self->{config}{devparam}{baud}};
  }
  my $termios = POSIX::Termios->new();
  $termios->getattr($portfd);
  $termios->setispeed($baud); $termios->setospeed($baud);
  my $c_cflag = $termios->getcflag(); my $c_lflag = $termios->getlflag();
  my $c_oflag = $termios->getoflag(); my $c_iflag = $termios->getiflag();

  # set raw
  #######################################################################
  $c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  $c_oflag &= ~OPOST;
  $c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG);
  $c_cflag &= ~(CSIZE|PARENB|HUPCL);
  $c_cflag |= CS8|CLOCAL;
  #######################################################################

  $termios->setcflag($c_cflag); $termios->setlflag($c_lflag);
  $termios->setoflag($c_oflag); $termios->setiflag($c_iflag);
  $termios->setattr($portfd,TCSANOW);

  binmode $self->{portfh}; select($self->{portfh}); $|=1; select(STDOUT);
}

sub onUnLoad {
  my ($self)=@_;

	  $self->SUPER::onUnLoad();

}

# WVCDPIC logic...  A return "0x0d" is sent after each command.
# We'll read the input and pass on the complete character sets.
# I don't know if it's actually a CR or LR.  Minicom produces the 0x0d.
# We read each byte, and push the character onto an array.  If the character matches a return,
#   Then change the array into a string, remove the whitespace, and return the string.
# In the future, we may filter out cruft that the vwcdpic sends along the way.

sub readMsgFromInputDev {
  my ($self)=@_; 
  my @buffer;
 
 while(1) {
  # read character
  sysread($self->{portfh},$_,1);
  my $char = $_;
  if ( $char eq "\r" || $char eq "\n" ) {
  $self->logmsg("VWCDPIC: matched Return. sending buffer.");
  my $char = "@buffer";
  $char =~ s/\s*//g;
  return $char;
  last;
  } else {
  $self->logmsg("VWCDPIC: Added $char to buffer.");
  push(@buffer, $char);
  } # end else
 } #end while
#
# $bytes=join(" ",map { "0x" . sprintf("%02x",unpack("C",$_)) } split(//,$bytes));
#
}

1;
