/*
    Big console Multimeter and bargraph for METEX M-3850M
    Copyright (C) 2003  Wiesner Thomas^M
^M
    This program is free software; you can redistribute it and/or modify^M
    it under the terms of the GNU General Public License as published by^M
    the Free Software Foundation; either version 2 of the License, or^M
    (at your option) any later version.^M
^M
    This program is distributed in the hope that it will be useful,^M
    but WITHOUT ANY WARRANTY; without even the implied warranty of^M
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the^M
    GNU General Public License for more details.^M
^M
    You should have received a copy of the GNU General Public License^M
    along with this program; if not, write to the Free Software^M
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
    FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN^M
    OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES^M
    PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED^M
    OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF^M
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS^M
    TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE^M
    PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,^M
    REPAIR OR CORRECTION.
*/

#include <stdio.h>
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include "bigdmm.h"

extern struct termios oldser;
extern int oldline;
extern int sfd;

int main(int argc, char *argv[])
{
   int del, i, j;
   char buf[56];
   float vals[79], max, val;
   char sbuf[7];
   unsigned char curval;

   if(argc != 3) {
      printf("%s device delay (seconds)\n"
            "if delay is 0 seconds, the refresh speed is limited by the DMM\n"
            "Please note, that the time isn't too accurate, because the dmm\n"
            "needs some time to convert and send the value.\n", argv[0]);
      return -1;
   }

   printf(  "BIGDMM version %s\n"
            "Console multimeter link for METEX M-3850M\n"
            "Special characters and colours for Linux terminal\n"
            "Initializing...", VERSION);
   fflush(stdout);
   if((sfd = init_dmm(argv[1])) < 0) {
      printf("Error on opening device\n");
      return -2;
   }
   del = atoi(argv[2]);

   if(signal(SIGINT, abrt) == SIG_ERR) {
      printf("Unable to install signal handler for SIGINT\n");
      return -3;
   }

   for(i = 0; i < 79; i++) // Initialize array
      vals[i] = 0;
   curval = 0;

   highlight(1);
   setcolor(GREEN);
   for(;;) {
      clrscr();
      if(read_dmm(sfd, buf) >= 0) {
         strncpy(sbuf, buf+3, 6);
         sbuf[7] = '\0';
         val = atof(sbuf);
         if(val < 0) // Make absolute value
            val = -val;

         // find the correct prefixes (n, u, m, , k, M)
         i = 9;
         while(i < 13 && buf[i] == ' ')   // Read till the first unit char
            i++;
         switch(buf[i]) {
            case 'n':   val /= 1000000000;
               break;
            case 'u':   val /= 1000000;
               break;
            case 'm':   val /= 1000;
               break;
            case 'k':   val *= 1000;
               break;
            case 'M':   val *= 1000000;
               break;
         }

         vals[curval] = val;

         g1(1);   // Special charset on
         bigdig(buf, 3, 8, 1);   // Print value
         putchar('\n');
         bigdig(buf, 9, 14, 0);  // Print units
         g1(0);
         setcolor(YELLOW);
         for(i = 0; i < 56; i++) {
            if(buf[i] != '\r')   // DMM sends \r as seperators ...
               putchar(buf[i]);
            else
               putchar('\t');    // ... map them to \t to fit in one line
         }
         putchar('\n');
         putchar('\n');

         // Find maximum value
         max = 0.01;
         for(i = 0; i < 79; i++) {
            if(vals[i] > max)
               max = vals[i];
         }

         // Draw the graph
         for(j = 0; j < 10; j++) {  // 10 lines
            for(i = 0; i < 80; i++) {  // 80 columns
               if(i == 0) {
                  setcolor(WHITE);
                  putchar('|');
               } else {
                  if(round(vals[i-1]*100/max) >= (9-j)*10 ) {
                     if(i-1 == curval)
                        setcolor(CYAN);
                     else
                        setcolor(BLUE);
                     g1(1);
                     putchar('a');
                     g1(0);
                  } else
                     putchar(' ');
               }
            }
            putchar('\n');
         }
         setcolor(WHITE);
         printf("Max: %.3f;   Time: %d;   Note: Graph values are absolute, CNTRL-C=Exit", max, curval);

         setcolor(GREEN);

         curval++;
         if(curval > 78)
            curval = 0;
      }
      fflush(stdout);
      if(del != 0)
         sleep(del);
   }
   return 0;
}