#!/usr/bin/perl
#-------------------------------------------------------------------------------#
# This Gui will communicate with iocondev8 via rsh (remote shell -  thanks to
# John Haggerty) commands.
# On the iocondev8 end, it talks to programs lv_mon.c, relay_mon.c and coolstat.c
# The c-programs receive [optional]arguments from here, and their ascii output is
# captured here and parsed.                             Jan 2003 Hubert van Hecke
#-------------------------------------------------------------------------------#
use Tk;
my $mainwindow = MainWindow->new;
$mainwindow->title("MVD monitoring and control GUI");
$frame1 = $mainwindow->Frame(-background=>'#999900',
                             -borderwidth=> '5',
                             -label=>"Choose a program",
                             )
          ->pack(-side=>'top', -expand=>'1', -fill=>'x');
$frame1->Button(-text=>"LV",
                    -command=> \&do_lv)
                    ->pack(-side=>'left');
$frame1->Button(-text=>"COOLSTAT",
                    -command=>  \&do_coolstat)
                    ->pack(-side=>'right');
$frame1->Button(-text=>"RELAY",
                    -command=> \&do_relay)
                    ->pack();
$mainwindow->Button(-text=>"exit the MVD GUI",
                    -command=>sub{exit}
                   )->pack(-side=>'bottom',  -anchor=>'sw');
MainLoop;
#==============================================================#
#==============================================================#
sub do_lv{
  &lv_close;
  $lvheader = $mainwindow->Frame(-background=>'#ffff33',
                               -borderwidth=>'5',
                               -label=>"LV commands:")->pack(
                   -expand =>'1',
                   -fill   =>'x',
                   -side=>'top');
 $lvbtnframe = $mainwindow->Frame(-background=>'#009900',
                               -borderwidth=>'5',
                               )->pack(
                   -expand =>'1',
                   -fill   =>'x',
                   -side=>'top');
#--------------------------------------------------------------------#
# These frames hold the 4 quadrants:
  $frameSW = $lvbtnframe->Frame(-background=>'#006666',
				   -borderwidth=>'10')
             ->grid(-row=>0, -column=>0);
  $frameNW = $lvbtnframe->Frame(-background=>'#009999',
				   -borderwidth=>'10')
             ->grid(-row=>0, -column=>1);
  $frameSE = $lvbtnframe->Frame(-background=>'#009999',
				   -borderwidth=>'10')
             ->grid(-row=>1, -column=>0);
  $frameNE = $lvbtnframe->Frame(-background=>'#006666',
				   -borderwidth=>'10')
             ->grid(-row=>1, -column=>1);
  $lvexit =  $lvbtnframe->Button(-text=>'close LV panel', -command=>\&lv_close)
             ->grid(-row=>2, -column=>0, -pady=>10);
  $lvrefresh =  $lvbtnframe->Button(-text=>'refresh readings',
               -command=>[\&lv_sendread, 0, 0])
             ->grid(-row=>2, -column=>1, -pady=>10);
#-----------------------------------------------------------------------------#
  &lv_sendread(0,0);
}     # end do_lv
#==============================================================#
sub lv_frameSE {
  $frameSE->Button(-text=>"SE\nALL", -command=>[\&lv_sendread, 1, 0])
      ->grid(-row=>0, -column=>0, -sticky=>'nsew', -rowspan=>4, -ipadx=>0);
  $frameSE->Button(-text=>'IT'     , -command=>[\&lv_sendread, 1, 1],
                   -background=>"$lvcol[1][1]")
          ->grid(-row=>0, -column=>3, -sticky=>'nsew');
  $frameSE->Button(-text=>'MB'     , -command=>[\&lv_sendread, 1, 8],
                   -background=>"$lvcol[1][8]")
          ->grid(-row=>1, -column=>1, -sticky=>'nsew', -rowspan=>2, -ipadx=>0);
  $frameSE->Button(-text=>'PD'     , -command=>[\&lv_sendread, 1, 7],
                   -background=>"$lvcol[1][7]")
          ->grid(-row=>1, -column=>2, -sticky=>'nsew', -rowspan=>2, -ipadx=>0);
  $frameSE->Button(-text=>'IM'     , -command=>[\&lv_sendread, 1, 2],
                   -background=>"$lvcol[1][2]")
          ->grid(-row=>1, -column=>3, -sticky=>'nsew');
  $frameSE->Button(-text=>'IB'     , -command=>[\&lv_sendread, 1, 3],
                   -background=>"$lvcol[1][3]")
          ->grid(-row=>2, -column=>3, -sticky=>'nsew');
  $frameSE->Button(-text=>'OB'     , -command=>[\&lv_sendread, 1, 4],
                   -background=>"$lvcol[1][4]")
          ->grid(-row=>3, -column=>3, -sticky=>'nsew');
}                   # end lv_frameSE
#-----------------------------------------------------------------------------#
sub lv_frameSW {
  $frameSW->Button(-text=>"SW\nALL", -command=>[\&lv_sendread, 2, 0])
          ->grid(-row=>0, -column=>0, -sticky=>'nsew',-rowspan=>4, -ipadx=>0);
  $frameSW->Button(-text=>'OB'     , -command=>[\&lv_sendread, 2, 4],
                   -background=>"$lvcol[2][4]")
          ->grid(-row=>0, -column=>3, -sticky=>'nsew');
  $frameSW->Button(-text=>'PD'     , -command=>[\&lv_sendread, 2, 7],
                   -background=>"$lvcol[2][7]")
          ->grid(-row=>1, -column=>2, -sticky=>'nsew',-rowspan=>2, -ipadx=>0);
  $frameSW->Button(-text=>'IB'     , -command=>[\&lv_sendread, 2, 3],
                   -background=>"$lvcol[2][3]")
          ->grid(-row=>1, -column=>3, -sticky=>'nsew');
  $frameSW->Button(-text=>'MB'     , -command=>[\&lv_sendread, 2, 8],
                   -background=>"$lvcol[2][8]")
          ->grid(-row=>1, -column=>1, -sticky=>'nsew',-rowspan=>2, -ipadx=>0);
  $frameSW->Button(-text=>'IM'     , -command=>[\&lv_sendread, 2, 2],
                   -background=>"$lvcol[2][2]")
          ->grid(-row=>2, -column=>3, -sticky=>'nsew');
  $frameSW->Button(-text=>'IT'     , -command=>[\&lv_sendread, 2, 1],
                   -background=>"$lvcol[2][1]")
          ->grid(-row=>3, -column=>3, -sticky=>'nsew');
}   # end lv_frameSW
#-----------------------------------------------------------------------------#
sub lv_frameNE {
  $frameNE->Button(-text=>'IT'     , -command=>[\&lv_sendread, 3, 1],
                   -background=>"$lvcol[3][1]")
          ->grid(-row=>0, -column=>0, -sticky=>'nsew');
  $frameNE->Button(-text=>"NW\nALL", -command=>[\&lv_sendread, 3, 0])
          ->grid(-row=>0, -column=>3, -sticky=>'nsew', -rowspan=>4, -ipadx=>0);
  $frameNE->Button(-text=>'IM'     , -command=>[\&lv_sendread, 3, 2],
                   -background=>"$lvcol[3][2]")
          ->grid(-row=>1, -column=>0, -sticky=>'nsew');
  $frameNE->Button(-text=>'PD'     , -command=>[\&lv_sendread, 3, 7],
                   -background=>"$lvcol[3][7]")
          ->grid(-row=>1, -column=>1, -sticky=>'nsew', -rowspan=>2, -ipadx=>0);
  $frameNE->Button(-text=>'MB'     , -command=>[\&lv_sendread, 3, 8],
                   -background=>"$lvcol[3][8]")
          ->grid(-row=>1, -column=>2, -sticky=>'nsew', -rowspan=>2, -ipadx=>0);
  $frameNE->Button(-text=>'IB'     , -command=>[\&lv_sendread, 3, 3],
                   -background=>"$lvcol[3][3]")
          ->grid(-row=>2, -column=>0, -sticky=>'nsew');
  $frameNE->Button(-text=>'OB'     , -command=>[\&lv_sendread, 3, 4],
                   -background=>"$lvcol[3][4]")
          ->grid(-row=>3, -column=>0, -sticky=>'nsew');
} # end lv_frameNE
#-----------------------------------------------------------------------------#
sub lv_frameNW {
  $frameNW->Button(-text=>'OB'     , -command=>[\&lv_sendread, 4, 4],
                   -background=>"$lvcol[4][4]")
          ->grid(-row=>0, -column=>0, -sticky=>'nsew');
  $frameNW->Button(-text=>"NW\nALL", -command=>[\&lv_sendread, 4, 0])
          ->grid(-row=>0, -column=>3, -sticky=>'nsew', -rowspan=>4, -ipadx=>0);
  $frameNW->Button(-text=>'IB'     , -command=>[\&lv_sendread, 4, 3],
                   -background=>"$lvcol[4][3]")
          ->grid(-row=>1, -column=>0, -sticky=>'nsew');
  $frameNW->Button(-text=>'PD'     , -command=>[\&lv_sendread, 4, 7],
                   -background=>"$lvcol[4][7]")
          ->grid(-row=>1, -column=>1, -sticky=>'nsew', -rowspan=>2, -ipadx=>0);
  $frameNW->Button(-text=>'MB'     , -command=>[\&lv_sendread, 4, 8],
                   -background=>"$lvcol[4][8]")
          ->grid(-row=>1, -column=>2, -sticky=>'nsew',
                                   -rowspan=>2, -ipadx=>0);
  $frameNW->Button(-text=>'IM'     , -command=>[\&lv_sendread, 4, 2],
                   -background=>"$lvcol[4][2]")
          ->grid(-row=>2, -column=>0, -sticky=>'nsew');
  $frameNW->Button(-text=>'IT'     , -command=>[\&lv_sendread, 4, 1],
                   -background=>"$lvcol[4][1]")
          ->grid(-row=>3, -column=>0, -sticky=>'nsew');
}    # end lv_frameNW
#=================================================================#
sub lv_close{
# clean up on exit, and disallow multiple copies:
  $frameSE->destroy() if Tk::Exists($frameSE);
  $frameSW->destroy() if Tk::Exists($frameSW);
  $frameNW->destroy() if Tk::Exists($frameNW);
  $frameNE->destroy() if Tk::Exists($frameNE);
  $lvbtnframe->destroy() if Tk::Exists($lvbtnframe);
  $lvheader->destroy() if Tk::Exists($lvheader);
  $text_1->destroy() if Tk::Exists($text_1);
}          # end lv_close
#==============================================================#
sub lv_sendread{
# send commands to iocondev8:
  $imb = $_[0];
  $igrp = $_[1];
  if ("$lvcol[$imb][$igrp]" eq "#00ff00") {
    $ibit=1;
    print (" on now, turn it off\n");
  }
  elsif ("$lvcol[$imb][$igrp]" eq "#ff0000") {
    $ibit=-1;
    print (" off now, turn it on\n");
  }
  else {
      $ibit=0;
  }
  print (" Sending MB=$imb,  Group=$igrp, Bit=$ibit \n");
# open the display window where the readback text is shown:
  #$text_1->destroy() if Tk::Exists($text_1);
  #$text_1 = $mainwindow->Text(-background=>white, -height=>12, -width=>56);
  #$text_1->pack();
  open GRAB, "rsh iocondev8 lv_mon $imb,$igrp,$ibit|";
#  open GRAB, "date|";  # this works!
  $group = -2;
  while (<GRAB>) {
    #$text_1->insert('end',$_);     # copy the line to the readback display window
    print("lines seen: $_");
    $group++;
    if ($group>0 && $group<11) {
      $lvcol[1][$group] = "#ffffff";
      $lvcol[2][$group] = "#ffffff";
      $lvcol[3][$group] = "#ffffff";
      $lvcol[4][$group] = "#ffffff";
      if (substr($_,14,1) == 0) {$lvcol[1][$group] = "#ff0000";}
      else                      {$lvcol[1][$group] = "#00ff00";}
      if (substr($_,19,1) == 0) {$lvcol[2][$group] = "#ff0000";}
      else                      {$lvcol[2][$group] = "#00ff00";}
      if (substr($_,24,1) == 0) {$lvcol[3][$group] = "#ff0000";}
      else                      {$lvcol[3][$group] = "#00ff00";}
      if (substr($_,29,1) == 0) {$lvcol[4][$group] = "#ff0000";}
      else                      {$lvcol[4][$group] = "#00ff00";}
    }
  }
  close GRAB;
  print ("finished with GRAB\n");
  &lv_frameSE;
  &lv_frameSW;
  &lv_frameNE;    # show the 4 quadrants
  &lv_frameNW;
}             # end of lv_sendread
#               end of the LV section
#==============================================================#
#           NOW THE RELAY SECTION:
#==============================================================#
sub do_relay{
  &rl_close;
  $relayheader = $mainwindow->Frame(-background=>'#ffff33',
                               -borderwidth=>'5',
                               -label=>"RELAY commands:")->pack(
                   -expand =>'1',
                   -fill   =>'x',
                   -side=>'top');
 $rlbtnframe = $mainwindow->Frame(-background=>'#00cc00',
                               -borderwidth=>'5',
                               )->pack(
                   -expand =>'1',
                   -fill   =>'x',
                   -side=>'top');
#--------------------------------------------------------------------#
# These frames hold the 2 halves
  $rlexit =  $rlbtnframe->Button(-text=>'close RELAY panel',
                                 -command=>\&rl_close,)
             ->grid(-row=>1, -column=>0);
  $rlrefresh =  $rlbtnframe->Button(-text=>'refresh readings',
               -command=>[\&rl_sendread, 0,999,0])
             ->grid(-row=>1, -column=>1, -pady=>10);
  $frameW = $rlbtnframe->Frame(-background=>'#006666',
				   -borderwidth=>'10')
             ->grid(-row=>0 , -column=>1);
  $frameE = $rlbtnframe->Frame(-background=>'#009999',
				   -borderwidth=>'10')
             ->grid(-row=>0, -column=>0);
#-------------------------------------------------------------------------------#
# Here are the 2 halves filled in:
  &rl_sendread(0,999,0);
}           # end do_relay
#==============================================================#
sub rl_close{
# clean up on exit, and disallow multiple copies:
  $relayheader->destroy() if Tk::Exists($relayheader);
  $rlexit->destroy() if Tk::Exists($rlexit);
  $rlbtnframe->destroy() if Tk::Exists($rlbtnframe);
  $text_1->destroy() if Tk::Exists($text_1);
}
#==============================================================#
sub rl_sendread{
# send commands to iocondev8:
  $iew = 2-$_[0];               # translate 1,2 -> 1,0
  $item_send = $_[1];
  $onoff = $_[2]-1;             # translate 1,2,3, -> 0,1,2
  print (" Sending east/west=$iew,  item=$item_send, onoff=$onoff \n");
# open the display window where the readback text is shown:
  #$text_1->destroy() if Tk::Exists($text_1);
  #$text_1 = $mainwindow->Text(-background=>white, -height=>20, -width=>56);
  #$text_1->pack();
  open GRAB, "rsh iocondev8 relay_mon $iew,$item_send,$onoff|";
  $item = -6;
  while (<GRAB>) {
    #$text_1->insert('end',$_);     # copy the line to the readback display window
    $item++;
    print("$item lines seen: $_");
    if ($item>0 && $item<=5) {     # east
      $rlcol[2][$item][1] = "#cccccc";
      $rlcol[2][$item][2] = "#cccccc";
      $rlcol[2][$item][3] = "#cccccc";
      $tmp =  substr($_,27,4);
      #print(" string: $tmp \n");      # should be on, off, or auto
         if (substr($_,27,3) eq "off" ) {$rlcol[2][$item][1] = "#0000ff";}
      elsif (substr($_,27,2) eq "on"  ) {$rlcol[2][$item][2] = "#0000ff";}
      elsif (substr($_,27,4) eq "auto") {$rlcol[2][$item][3] = "#0000ff";}
    }
    elsif ($item>8 && $item<=13) {     # west
      $rlcol[1][$item-8][1] = "#cccccc";
      $rlcol[1][$item-8][2] = "#cccccc";
      $rlcol[1][$item-8][3] = "#cccccc";
      $tmp =  substr($_,27,4);
      #print(" string: $tmp \n");      # should be on, off, or auto
         if (substr($_,27,3) eq "off" ) {$rlcol[1][$item-8][1] = "#0000ff";}
      elsif (substr($_,27,2) eq "on"  ) {$rlcol[1][$item-8][2] = "#0000ff";}
      elsif (substr($_,27,4) eq "auto") {$rlcol[1][$item-8][3] = "#0000ff";}
    }
  }
  close GRAB;
  print ("finished with GRAB\n");
  &rl_frameW;
  &rl_frameE;
}             # end of rl_sendread
#==============================================================#
sub rl_frameW {
  $frameW->Label(-text=>'WEST')->grid(-row=>0, -column=>1, -columnspan=>3, -sticky=>'nsew');
  $frameW->Label(-text=>'Chil.1')->grid(-row=>1, -column=>0, -sticky=>'nsew');
  $frameW->Button(-text=>"off" , -background=>"$rlcol[1][1][1]",
                                 -command=>[\&rl_sendread, 1 , 1, 1])
         ->grid(-row=>1, -column=>1, -sticky=>'nsew');
  $frameW->Button(-text=>'on'  , -background=>"$rlcol[1][1][2]",
                                 -command=>[\&rl_sendread, 1 , 1, 2])
         ->grid(-row=>1, -column=>2, -sticky=>'nsew');
  $frameW->Button(-text=>'auto', -background=>"$rlcol[1][1][3]",
                                 -command=>[\&rl_sendread, 1 , 1, 3])
         ->grid(-row=>1, -column=>3, -sticky=>'nsew');
  $frameW->Label(-text=>'Chil.2')->grid(-row=>2, -column=>0, -sticky=>'nsew');
  $frameW->Button(-text=>'off'  , -background=>"$rlcol[1][2][1]",
                                 -command=>[\&rl_sendread, 1 , 2, 1])
         ->grid(-row=>2, -column=>1, -sticky=>'nsew');
  $frameW->Button(-text=>'on'   , -background=>"$rlcol[1][2][2]",
                                 -command=>[\&rl_sendread, 1 , 2, 2])
         ->grid(-row=>2, -column=>2, -sticky=>'nsew');
  $frameW->Button(-text=>'auto' , -background=>"$rlcol[1][2][3]",
                                 -command=>[\&rl_sendread, 1 , 2, 3])
         ->grid(-row=>2, -column=>3, -sticky=>'nsew');
  $frameW->Label(-text=>'Fan'  )->grid(-row=>3, -column=>0, -sticky=>'nsew');
  $frameW->Button(-text=>'off' , -background=>"$rlcol[1][3][1]",
                                 -command=>[\&rl_sendread, 1 , 3, 1])
         ->grid(-row=>3, -column=>1, -sticky=>'nsew');
  $frameW->Button(-text=>'on'  , -background=>"$rlcol[1][3][2]",
                                 -command=>[\&rl_sendread, 1 , 3, 2])
         ->grid(-row=>3, -column=>2, -sticky=>'nsew');
  $frameW->Button(-text=>'auto', -background=>"$rlcol[1][3][3]",
                                 -command=>[\&rl_sendread, 1 , 3, 3])
         ->grid(-row=>3, -column=>3, -sticky=>'nsew');
  $frameW->Label(-text=>'Pump' )->grid(-row=>4, -column=>0, -sticky=>'nsew');
  $frameW->Button(-text=>'off' , -background=>"$rlcol[1][4][1]",
                                 -command=>[\&rl_sendread, 1 , 4, 1])
         ->grid(-row=>4, -column=>1, -sticky=>'nsew');
  $frameW->Button(-text=>'on'  , -background=>"$rlcol[1][4][2]",
                                 -command=>[\&rl_sendread, 1 , 4, 2])
         ->grid(-row=>4, -column=>2, -sticky=>'nsew');
  $frameW->Button(-text=>'auto', -background=>"$rlcol[1][4][3]",
                                 -command=>[\&rl_sendread, 1 , 4, 3])
         ->grid(-row=>4, -column=>3, -sticky=>'nsew');
  $frameW->Label(-text=>'LV'   )->grid(-row=>5, -column=>0, -sticky=>'nsew');
  $frameW->Button(-text=>'off' , -background=>"$rlcol[1][5][1]",
                                 -command=>[\&rl_sendread, 1 , 5, 1])
         ->grid(-row=>5, -column=>1, -sticky=>'nsew');
  $frameW->Button(-text=>'on'  , -background=>"$rlcol[1][5][2]",
                                 -command=>[\&rl_sendread, 1 , 5, 2])
         ->grid(-row=>5, -column=>2, -sticky=>'nsew');
  $frameW->Button(-text=>'auto', -background=>"$rlcol[1][5][3]",
                                 -command=>[\&rl_sendread, 1 , 5, 3])
         ->grid(-row=>5, -column=>3, -sticky=>'nsew');
  $frameW->Button(-text=>'west reset', -background=>"#cccccc",
                                 -command=>[\&rl_sendread, 1 , 0, 1])
         ->grid(-row=>6, -column=>1, -columnspan=>3, -sticky=>'nsew');
}  # end rl_frameW
#======================================================================#
sub rl_frameE {
  $frameE->Label(-text=>'EAST')->grid(-row=>0, -column=>1, -columnspan=>3, -sticky=>'nsew');
  $frameE->Label(-text=>'Chil.1')->grid(-row=>1, -column=>0, -sticky=>'nsew');
  $frameE->Button(-text=>"off" , -background=>"$rlcol[2][1][1]",
                                 -command=>[\&rl_sendread, 2 , 1, 1])
         ->grid(-row=>1, -column=>1, -sticky=>'nsew');
  $frameE->Button(-text=>'on'  , -background=>"$rlcol[2][1][2]",
                                 -command=>[\&rl_sendread, 2 , 1, 2])
         ->grid(-row=>1, -column=>2, -sticky=>'nsew');
  $frameE->Button(-text=>'auto', -background=>"$rlcol[2][1][3]",
                                 -command=>[\&rl_sendread, 2 , 1, 3])
         ->grid(-row=>1, -column=>3, -sticky=>'nsew');
  $frameE->Label(-text=>'Chil.2')->grid(-row=>2, -column=>0, -sticky=>'nsew');
  $frameE->Button(-text=>'off'  , -background=>"$rlcol[2][2][1]",
                                 -command=>[\&rl_sendread, 2 , 2, 1])
         ->grid(-row=>2, -column=>1, -sticky=>'nsew');
  $frameE->Button(-text=>'on'   , -background=>"$rlcol[2][2][2]",
                                 -command=>[\&rl_sendread, 2 , 2, 2])
         ->grid(-row=>2, -column=>2, -sticky=>'nsew');
  $frameE->Button(-text=>'auto' , -background=>"$rlcol[2][2][3]",
                                 -command=>[\&rl_sendread, 2 , 2, 3])
         ->grid(-row=>2, -column=>3, -sticky=>'nsew');
  $frameE->Label(-text=>'Fan'  )->grid(-row=>3, -column=>0, -sticky=>'nsew');
  $frameE->Button(-text=>'off' , -background=>"$rlcol[2][3][1]",
                                 -command=>[\&rl_sendread, 2 , 3, 1])
         ->grid(-row=>3, -column=>1, -sticky=>'nsew');
  $frameE->Button(-text=>'on'  , -background=>"$rlcol[2][3][2]",
                                 -command=>[\&rl_sendread, 2 , 3, 2])
         ->grid(-row=>3, -column=>2, -sticky=>'nsew');
  $frameE->Button(-text=>'auto', -background=>"$rlcol[2][3][3]",
                                 -command=>[\&rl_sendread, 2 , 3, 3])
         ->grid(-row=>3, -column=>3, -sticky=>'nsew');
  $frameE->Label(-text=>'Pump' )->grid(-row=>4, -column=>0, -sticky=>'nsew');
  $frameE->Button(-text=>'off' , -background=>"$rlcol[2][4][1]",
                                 -command=>[\&rl_sendread, 2 , 4, 1])
         ->grid(-row=>4, -column=>1, -sticky=>'nsew');
  $frameE->Button(-text=>'on'  , -background=>"$rlcol[2][4][2]",
                                 -command=>[\&rl_sendread, 2 , 4, 2])
         ->grid(-row=>4, -column=>2, -sticky=>'nsew');
  $frameE->Button(-text=>'auto', -background=>"$rlcol[2][4][3]",
                                 -command=>[\&rl_sendread, 2 , 4, 3])
         ->grid(-row=>4, -column=>3, -sticky=>'nsew');
  $frameE->Label(-text=>'LV'   )->grid(-row=>5, -column=>0, -sticky=>'nsew');
  $frameE->Button(-text=>'off' , -background=>"$rlcol[2][5][1]",
                                 -command=>[\&rl_sendread, 2 , 5, 1])
         ->grid(-row=>5, -column=>1, -sticky=>'nsew');
  $frameE->Button(-text=>'on'  , -background=>"$rlcol[2][5][2]",
                                 -command=>[\&rl_sendread, 2 , 5, 2])
         ->grid(-row=>5, -column=>2, -sticky=>'nsew');
  $frameE->Button(-text=>'auto', -background=>"$rlcol[2][5][3]",
                                 -command=>[\&rl_sendread, 2 , 5, 3])
         ->grid(-row=>5, -column=>3, -sticky=>'nsew');
  $frameE->Button(-text=>'east reset', -background=>"#cccccc",
                                 -command=>[\&rl_sendread, 2 , 0, 1])
         ->grid(-row=>6, -column=>1, -columnspan=>3, -sticky=>'nsew');
}  # end rl_frameE
#======================================================================#
#
#                      COOLSTAT NEXT:
#======================================================================#
sub do_coolstat{
  &cl_close;
  $coolheader = $mainwindow->Frame(-background=>'#ffff33',
                               -borderwidth=>'5',
                               -label=>"COOLSTAT:")->pack(
                   -expand =>'1',
                   -fill   =>'x',
                   -side=>'top');
 $clbtnframe = $mainwindow->Frame(-background=>'#00cc00',
                               -borderwidth=>'5',
                               )->pack(
                   -expand =>'1',
                   -fill   =>'x',
                   -side=>'top');
#--------------------------------------------------------------------#
$xwidth  = 250;
$yheight = 200;
$x0 = 10;       # start of 'T1' button
$y0 = 10;       # start of 'T1' button
$dy = 18;       # vertical button spacing
$x1 = $x0+24;   # end of 'T1'
$x2 = $x0+50;   # start of 'air T'
$x3 = $x0+101;  # end of 'air T'
$x4 = $x0+125;  # start of 'East air'
$x5 = $x0+160;  # end of 'East air'
$x6 = $x0+200;  # start of 'east mvd'
# the next 2 buttons and 2 frames go in the 'buttonframe'
  $clexit =  $clbtnframe->Button(-text=>'close COOLSTAT panel',
                                 -command=>\&cl_close,)
             ->grid(-row=>1, -column=>0);
  $clrefresh =  $clbtnframe->Button(-text=>'refresh readings',
               -command=>[\&cl_sendread, 0,999,0])
             ->grid(-row=>1, -column=>1, -pady=>10);
  $frame1 = $clbtnframe->Frame(-background=>'#999900',
          -borderwidth=> '5', -width=>$xwidth+12, -height=>$yheight+12)
          ->grid(-row=>0, -column=>0);
  $frame2 = $clbtnframe->Frame(-background=>'#999900',
          -borderwidth=> '5', -width=>$xwidth+12, -height=>$yheight+12)
          ->grid(-row=>0, -column=>1);
#-------------------------------------------------------------------------------#
# Here are the 2 halves filled in:
  &cl_sendread;     # get the data
}           # end do_coolstat
#--------------------------------------------------------------------#
sub cl_sendread{
# send commands to iocondev8:
# open the display window where the readback text is shown:
  $text_1->destroy() if Tk::Exists($text_1);
  $text_1 = $mainwindow->Text(-background=>white, -height=>16, -width=>72);
  $text_1->pack();
  open GRAB, "rsh iocondev8 coolstat|";  # issue coolstat command, and capture
  $line = -5;                            # output in GRAB.
  $item = 0;
  while (<GRAB>) {
    $text_1->insert('end',$_);     # copy the line to the readback display window
    $line++;
    print("$item lines seen: $_");
    if ($line > 0 && $line <= 10) {    # parse the line for the 2 'OK' occurrences
      $item++;                         # assign button colors 1-20 in order
      $clcol[$item] = "#ff0000";
      if (substr($_,40,2) eq "OK" ) {$clcol[$item] = "#00ff00";}
      $item++;
      $clcol[$item] = "#ff0000";
      if (substr($_,59,2) eq "OK" ) {$clcol[$item] = "#00ff00";}
    }
  }
  close GRAB;
  print ("finished with GRAB\n");
# assign the colors of the 'derived quantities' by hand:
  $clcol[21] = "#ff0000";    # E air T
  if ($clcol[1] eq "#00ff00" || $clcol[3] eq "#00ff00" ||
      $clcol[5] eq "#00ff00") { $clcol[21] = "#00ff00";}
  $clcol[22] = "#ff0000";    # E air F
  if ($clcol[2] eq "#00ff00" || $clcol[4] eq "#00ff00" ||
      $clcol[6] eq "#00ff00") { $clcol[22] = "#00ff00";}
  $clcol[23] = "#ff0000";    # E water T
  if ($clcol[7] eq "#00ff00" || $clcol[9] eq "#00ff00"){ $clcol[23] = "#00ff00";}
  $clcol[24] = "#ff0000";    # E water F
  if ($clcol[8] eq "#00ff00" || $clcol[10] eq "#00ff00"){ $clcol[24] = "#00ff00";}
  $clcol[25] = "#ff0000";    # E air
  if ($clcol[21] eq "#00ff00" && $clcol[22] eq "#00ff00"){ $clcol[25] = "#00ff00";}
  $clcol[26] = "#ff0000";    # E air
  if ($clcol[23] eq "#00ff00" && $clcol[24] eq "#00ff00"){ $clcol[26] = "#00ff00";}
  $clcol[27] = "#ff0000";    # E mvd
  if ($clcol[25] eq "#00ff00" && $clcol[26] eq "#00ff00"){ $clcol[27] = "#00ff00";}
# west:
  $clcol[31] = "#ff0000";    # w air T
  if ($clcol[11] eq "#00ff00" || $clcol[13] eq "#00ff00" ||
      $clcol[15] eq "#00ff00") { $clcol[31] = "#00ff00";}
  $clcol[32] = "#ff0000";    # w air F
  if ($clcol[12] eq "#00ff00" || $clcol[14] eq "#00ff00" ||
      $clcol[16] eq "#00ff00") { $clcol[32] = "#00ff00";}
  $clcol[33] = "#ff0000";    # w water T
  if ($clcol[17] eq "#00ff00" || $clcol[19] eq "#00ff00"){ $clcol[33] = "#00ff00";}
  $clcol[34] = "#ff0000";    # w water F
  if ($clcol[18] eq "#00ff00" || $clcol[20] eq "#00ff00"){ $clcol[34] = "#00ff00";}
  $clcol[35] = "#ff0000";    # w air
  if ($clcol[31] eq "#00ff00" && $clcol[32] eq "#00ff00"){ $clcol[35] = "#00ff00";}
  $clcol[36] = "#ff0000";    # w air
  if ($clcol[33] eq "#00ff00" && $clcol[34] eq "#00ff00"){ $clcol[36] = "#00ff00";}
  $clcol[37] = "#ff0000";    # west mvde
  if ($clcol[35] eq "#00ff00" && $clcol[36] eq "#00ff00"){ $clcol[37] = "#00ff00";}
  &cl_frameW();     # refresh the displays
  &cl_frameE();     #
}                   # end of cl_sendread
#==============================================================#
sub cl_close{
# clean up on exit, and disallow multiple copies:
  $coolheader->destroy() if Tk::Exists($coolheader);
  $clexit->destroy() if Tk::Exists($clexit);
  $clbtnframe->destroy() if Tk::Exists($clbtnframe);
  $text_1->destroy() if Tk::Exists($text_1);
}                   # end of cl_close
#==============================================================#
sub cl_frameW {
# lay a canvas over the whole $frame1, we'll place arrows on this canvas
$canv1 = $frame1->Canvas(-bg=>'#ffffff', -width=>$xwidth, -height=>$yheight)
                    ->place(-x=>0, -y=>0);
$frame1->Label(-text=>"T=temperature", -background=>"white")
                    ->place(-x=>$x4+20, -y=>$y0);
$frame1->Label(-text=>"F=flow       ", -background=>"white")
                    ->place(-x=>$x4+20, -y=>$y0+$dy);
# east air temperature: ---------------------------------------------
$frame1->Label(-text=>"T1", -bd=>0, -width=>3, -background=>"$clcol[1]")
                    ->place(-x=>$x0, -y=>$y0+0*$dy);
$frame1->Label(-text=>"T2", -bd=>0, -width=>3, -background=>"$clcol[3]")
                    ->place(-x=>$x0, -y=>$y0+1*$dy);
$frame1->Label(-text=>"T3", -background=>"$clcol[5]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+2*$dy);
$frame1->Label(-text=>"air T", -background=>"$clcol[21]", -bd=>0, -width=>7)
                    ->place(-x=>$x2, -y=>$y0+1*$dy);
$arrow1 = $canv1->createLine($x1,$y0+0.5*$dy,$x2,$y0+1.5*$dy-1, -arrow=>"last");
$arrow2 = $canv1->createLine($x1,$y0+1.5*$dy,$x2,$y0+1.5*$dy+0                );
$arrow3 = $canv1->createLine($x1,$y0+2.5*$dy,$x2,$y0+1.5*$dy+1, -arrow=>"last");
# east air flow: -----------------------------------------------------
$frame1->Label(-text=>"F1", -background=>"$clcol[2]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+3*$dy);
$frame1->Label(-text=>"F2", -background=>"$clcol[4]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+4*$dy);
$frame1->Label(-text=>"F3", -background=>"$clcol[6]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+5*$dy);
$frame1->Label(-text=>"air F", -background=>"$clcol[22]", -bd=>0, -width=>7)
                    ->place(-x=>$x2, -y=>$y0+4*$dy);
$arrow1 = $canv1->createLine($x1,$y0+3.5*$dy,$x2,$y0+4.5*$dy-2, -arrow=>"last");
$arrow2 = $canv1->createLine($x1,$y0+4.5*$dy,$x2,$y0+4.5*$dy+0                );
$arrow3 = $canv1->createLine($x1,$y0+5.5*$dy,$x2,$y0+4.5*$dy+2, -arrow=>"last");
$frame1->Label(-text=>"East\nair", -background=>"$clcol[25]", -bd=>0, -width=>5)
                    ->place(-x=>$x4, -y=>$y0+2.2*$dy);
$arrow2 = $canv1->createLine($x3,$y0+1.5*$dy,$x4,$y0+3*$dy-1, -arrow=>"last");
$arrow2 = $canv1->createLine($x3,$y0+4.5*$dy,$x4,$y0+3*$dy+1, -arrow=>"last");
# east water temperature: -------------------------------------------
$frame1->Label(-text=>"T4", -background=>"$clcol[7]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+6*$dy);
$frame1->Label(-text=>"T5", -background=>"$clcol[9]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+7*$dy);
$frame1->Label(-text=>"water T", -background=>"$clcol[23]", -bd=>0)
                    ->place(-x=>$x2, -y=>$y0+6.5*$dy);
$arrow1 = $canv1->createLine($x1,$y0+6.5*$dy,$x2,$y0+7*$dy-1, -arrow=>"last");
$arrow2 = $canv1->createLine($x1,$y0+7.5*$dy,$x2,$y0+7*$dy+1, -arrow=>"last");
# east water flow: ------------------------------------------------
$frame1->Label(-text=>"F9", -background=>"$clcol[8]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+8*$dy);
$frame1->Label(-text=>"F10", -background=>"$clcol[10]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+9*$dy);
$frame1->Label(-text=>"water F", -background=>"$clcol[24]", -bd=>0)
                    ->place(-x=>$x2, -y=>$y0+8.5*$dy);
$arrow1 = $canv1->createLine($x1,$y0+8.5*$dy,$x2,$y0+9*$dy-1, -arrow=>"last");
$arrow2 = $canv1->createLine($x1,$y0+9.5*$dy,$x2,$y0+9*$dy+1, -arrow=>"last");
$frame1->Label(-text=>"East\nwater", -background=>"$clcol[26]", -bd=>0)
                    ->place(-x=>$x4, -y=>$y0+7.2*$dy);
$arrow2 = $canv1->createLine($x3,$y0+7*$dy,$x4,$y0+8*$dy, -arrow=>"last");
$arrow2 = $canv1->createLine($x3,$y0+9*$dy,$x4,$y0+8*$dy, -arrow=>"last");
# east MVD : ------------------------------------------------
$frame1->Label(-text=>"East\nMVD", -background=>"$clcol[27]", -bd=>0)
                    ->place(-x=>$x6, -y=>$y0+4.7*$dy);
$arrow2 = $canv1->createLine($x5,$y0+3*$dy,$x6,$y0+5.5*$dy, -arrow=>"last");
$arrow2 = $canv1->createLine($x5,$y0+8*$dy,$x6,$y0+5.5*$dy, -arrow=>"last");
}    # end of cl_frameW
#==========================================================================#
sub cl_frameE{
# lay a canvas over the whole $frame2, we'll place arrows on this canvas
$canv2 = $frame2->Canvas(-bg=>'#ffffcc', -width=>$xwidth, -height=>$yheight)
                    ->place(-x=>0, -y=>0);
$frame2->Label(-text=>"  ", -background=>"#00ff00")
                    ->place(-x=>$x5+25, -y=>$y0+0*$dy);
$frame2->Label(-text=>"  ", -background=>"#ff0000")
                    ->place(-x=>$x5+25, -y=>$y0+1*$dy);
$frame2->Label(-text=>"= OK ", -background=>"#ffffcc")
                    ->place(-x=>$x5+40, -y=>$y0+0*$dy);
$frame2->Label(-text=>"= bad", -background=>"#ffffcc")
                    ->place(-x=>$x5+40, -y=>$y0+1*$dy);
# west air temperature: ---------------------------------------------
$frame2->Label(-text=>"T6", -bd=>0, -width=>3, -background=>"$clcol[11]")
                    ->place(-x=>$x0, -y=>$y0+0*$dy);
$frame2->Label(-text=>"T7", -bd=>0, -width=>3, -background=>"$clcol[13]")
                    ->place(-x=>$x0, -y=>$y0+1*$dy);
$frame2->Label(-text=>"T8", -background=>"$clcol[15]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+2*$dy);
$frame2->Label(-text=>"air T", -background=>"$clcol[31]", -bd=>0, -width=>7)
                    ->place(-x=>$x2, -y=>$y0+1*$dy);
$arrow1 = $canv2->createLine($x1,$y0+0.5*$dy,$x2,$y0+1.5*$dy-1, -arrow=>"last");
$arrow2 = $canv2->createLine($x1,$y0+1.5*$dy,$x2,$y0+1.5*$dy+0                );
$arrow3 = $canv2->createLine($x1,$y0+2.5*$dy,$x2,$y0+1.5*$dy+1, -arrow=>"last");
# west air flow: -----------------------------------------------------
$frame2->Label(-text=>"F6", -background=>"$clcol[12]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+3*$dy);
$frame2->Label(-text=>"F7", -background=>"$clcol[14]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+4*$dy);
$frame2->Label(-text=>"F8", -background=>"$clcol[16]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+5*$dy);
$frame2->Label(-text=>"air F", -background=>"$clcol[32]", -bd=>0, -width=>7)
                    ->place(-x=>$x2, -y=>$y0+4*$dy);
$arrow1 = $canv2->createLine($x1,$y0+3.5*$dy,$x2,$y0+4.5*$dy-2, -arrow=>"last");
$arrow2 = $canv2->createLine($x1,$y0+4.5*$dy,$x2,$y0+4.5*$dy+0                );
$arrow3 = $canv2->createLine($x1,$y0+5.5*$dy,$x2,$y0+4.5*$dy+2, -arrow=>"last");
$frame2->Label(-text=>"West\nair", -background=>"$clcol[35]", -bd=>0, -width=>5)
                    ->place(-x=>$x4, -y=>$y0+2.2*$dy);
$arrow2 = $canv2->createLine($x3,$y0+1.5*$dy,$x4,$y0+3*$dy-1, -arrow=>"last");
$arrow2 = $canv2->createLine($x3,$y0+4.5*$dy,$x4,$y0+3*$dy+1, -arrow=>"last");
# west water temperature: -------------------------------------------
$frame2->Label(-text=>"T9", -background=>"$clcol[17]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+6*$dy);
$frame2->Label(-text=>"T10", -background=>"$clcol[19]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+7*$dy);
$frame2->Label(-text=>"water T", -background=>"$clcol[33]", -bd=>0)
                    ->place(-x=>$x2, -y=>$y0+6.5*$dy);
$arrow1 = $canv2->createLine($x1,$y0+6.5*$dy,$x2,$y0+7*$dy-1, -arrow=>"last");
$arrow2 = $canv2->createLine($x1,$y0+7.5*$dy,$x2,$y0+7*$dy+1, -arrow=>"last");
# west water flow: ------------------------------------------------
$frame2->Label(-text=>"F4", -background=>"$clcol[18]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+8*$dy);
$frame2->Label(-text=>"F5", -background=>"$clcol[20]", -bd=>0, -width=>3)
                    ->place(-x=>$x0, -y=>$y0+9*$dy);
$frame2->Label(-text=>"water F", -background=>"$clcol[34]", -bd=>0)
                    ->place(-x=>$x2, -y=>$y0+8.5*$dy);
$arrow1 = $canv2->createLine($x1,$y0+8.5*$dy,$x2,$y0+9*$dy-1, -arrow=>"last");
$arrow2 = $canv2->createLine($x1,$y0+9.5*$dy,$x2,$y0+9*$dy+1, -arrow=>"last");
$frame2->Label(-text=>"West\nwater", -background=>"$clcol[36]", -bd=>0)
                    ->place(-x=>$x4, -y=>$y0+7.2*$dy);
$arrow2 = $canv2->createLine($x3,$y0+7*$dy,$x4,$y0+8*$dy, -arrow=>"last");
$arrow2 = $canv2->createLine($x3,$y0+9*$dy,$x4,$y0+8*$dy, -arrow=>"last");
# west MVD : ------------------------------------------------
$frame2->Label(-text=>"West\nMVD", -background=>"$clcol[37]", -bd=>0)
                    ->place(-x=>$x6, -y=>$y0+4.7*$dy);
$arrow2 = $canv2->createLine($x5,$y0+3*$dy,$x6,$y0+5.5*$dy, -arrow=>"last");
$arrow2 = $canv2->createLine($x5,$y0+8*$dy,$x6,$y0+5.5*$dy, -arrow=>"last");
}    # end of cl_frameE
#======================================================================#