#!/usr/bin/perl

use strict;
use Time::HiRes qw(usleep nanosleep);
use NetServer::Generic;

my $tty = "/dev/ttyACM0";

my $digits = "1234567890";
#$digits=reverse($digits);

l_scroll($tty,$digits);
sleep(1);
r_scroll($tty,$digits);
sleep(1);
casino_all($tty,$digits);
sleep(1);
casino_one_r($tty,$digits);
sleep(3);
`/bin/echo '            ' > $tty`;
print "FINISHED!\n";

#  print "input: $tmp ".length($tmp)."\n";
#  if (length($tmp)==10) {
#    if (int(rand(10))>=6) {
#      casino_one_r($tty,$tmp);
#    } else {
#      casino_all($tty,$tmp);
#    }
#  } else {
#   if (int(rand(10))>=6) {
#      l_scroll($tty,$tmp);
#    } else {
#      r_scroll($tty,$tmp);
#    }
#  }
#  sleep(5);
#  `/bin/echo '          ' > $tty`;
#  return; 
#};
#}

sub l_scroll { 
  my $tty = shift;
  my $digits = shift;
  my $length=length($digits);
  my @chars=split('',$digits);
  #if ($length==10) {
  #  `/bin/echo $digits > $tty`;
  #  print "$digits\n";
  #}
  #if ($length!=10) {
    my $display="          ";
    `/bin/echo '$display' > $tty`;
    my $i;
    my $displaylen=10+$length;
    while($i<=$displaylen) {
      $display=$display.$chars[$i];
      if ($length<=$i) {
        $display=$display." ";
      }
      $i++;
      $display=substr($display,1);
      usleep(400000);
      `/bin/echo '$display' > $tty`;
      print "$display\n";
    }
  #}
}

sub r_scroll { 
  my $tty = shift;
  my $digits = shift;
  my $length=length($digits);
  my @chars=split('',$digits);
  #if ($length==10) {
  #  `/bin/echo $digits > $tty`;
  #  print "$digits\n";
  #}
  #if ($length!=10) {
    my $display="          ";
    `/bin/echo '$display' > $tty`;
    my $displaylen=10+$length;
    my $i;
    while($displaylen>0) {
      $display=$chars[$i].$display;
      if ($length<=$i) {
        $display=" ".$display;
      }
      $i++;
      $displaylen--;
      $display=substr($display,0,-1);
      usleep(400000);
      `/bin/echo '$display' > $tty`;
    }
    usleep(400000);
    `/bin/echo '$display' > $tty`;
  #}
}

sub casino_all {
  my $tty = shift;
  my $digits = shift;
  $digits= reverse($digits);
  my $length=length($digits);
  print "Starting Casino Random\n";
  if ($length==10) {
    my $i;
    while ($i<50) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      print "$r_digit\n";
      usleep(50000);
      $i++;
    }
    `/bin/echo $digits > $tty`;
  }
}

sub casino_one_l {
  my $tty = shift;
  my $digits = shift;
  my $length=length($digits);
  my @chars=split('',$digits);
  print "Starting Casino Digit Random\n";
  if ($length==10) {
    my $i;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].$chars[3].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].int(rand(10)).int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].$chars[7].int(rand(10)).int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].$chars[7].int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].$chars[7].$chars[8].int(rand(10));
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].$chars[7].$chars[8].int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[0].$chars[1].$chars[2].$chars[3].$chars[4].$chars[5].$chars[6].$chars[7].$chars[8].$chars[9];
    `/bin/echo $digits > $tty`;
  }
}

sub casino_one_r {
  my $tty = shift;
  my $digits = shift;
  my $length=length($digits);
  my @chars=split('',$digits);
  print "Starting Casino Digit Random\n";
  if ($length==10) {
    my $i;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10));
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[3].$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).int(rand(10)).$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).int(rand(10)).$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).int(rand(10)).$chars[7].$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).int(rand(10)).$chars[7].$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=int(rand(10)).$chars[8].$chars[7].$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
    $i=0;
    while ($i<10) {
      my $r_digit=int(rand(10)).$chars[8].$chars[7].$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
      `/bin/echo $r_digit > $tty`;
      usleep(50000);
      $i++;
    }
    $digits=$chars[9].$chars[8].$chars[7].$chars[6].$chars[5].$chars[4].$chars[3].$chars[2].$chars[1].$chars[0];
    `/bin/echo $digits > $tty`;
  }
}
