home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR23 / TOUCH2.ZIP / TOUCH2.C < prev    next >
C/C++ Source or Header  |  1993-08-14  |  12KB  |  481 lines

  1. /*
  2.  * touch2.c - main program for LifeScan One Touch(R) II glucose meter
  3.  * communication interface
  4.  *
  5.  * V. Abell
  6.  */
  7.  
  8. /*
  9.  * Copyright 1993 Victor A. Abell, Lafayette, Indiana  47906.  All rights
  10.  * reserved.
  11.  *
  12.  * Written by Victor A. Abell.
  13.  *
  14.  * Permission is granted to anyone to use this software for any purpose on
  15.  * any computer system, and to alter it and redistribute it freely, subject
  16.  * to the following restrictions:
  17.  *
  18.  * 1. Victor A. Abell is not responsible for any consequences of the use of
  19.  * this software.
  20.  *
  21.  * 2. The origin of this software must not be misrepresented, either by
  22.  *    explicit claim or by omission.  Credit to Victor A. Abell must
  23.  *    appear in documentation and sources.
  24.  *
  25.  * 3. Altered versions must be plainly marked as such, and must not be
  26.  *    misrepresented as being the original software.
  27.  *
  28.  * 4. This notice may not be removed or altered.
  29.  */
  30. #ifndef lint
  31. static char copyright[] =
  32. "@(#) Copyright 1993 Victor A. Abell.\nAll rights reserved.\n";
  33. #endif
  34.  
  35. #include "touch2.h"
  36. #include <stdlib.h>
  37. #include <signal.h>
  38. #include <string.h>
  39. #include "asyncpec.h"
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42.  
  43. char Version[] = "Version 1.11, August 14, 1993";
  44.  
  45. struct menu ZeroFail[] = {
  46.     { 12, 14, "The command to zero the datalog may have failed." },
  47.     {  0,  0, NULL },
  48. };
  49.  
  50. struct menu ZeroMenu[] = {
  51.     {  4, 29, "Zero the meter data log." },
  52.     {  8, 26, "Are you sure you want to do this?" },
  53.     { 12, 23, "Please press Y (upper case) to confirm." },
  54.     { 13, 20, "Press any other key to exit without clearing." },
  55.     {  0,  0, NULL },
  56. };
  57.  
  58. struct menu MainMenu[] = {
  59.     {  2, 13, "LifeScan One Touch(R) II Meter Communications Interface" },
  60.     {  3, 29, Version },
  61. #define    VERSLN    1
  62.     {  9, 18, "C - inspect/change Communications parameters" },
  63.     { 10, 18, "D - Dump memory" },
  64.     { 11, 18, "S - inspect/display meter Status" },
  65.     { 12, 18, "T - read/set date and Time" },
  66.     { 13, 18, "X - eXit" },
  67.     { 14, 18, "Z - Zero the meter datalog" },
  68.     { 20,  8, "Copyright (C) Victor A. Abell, 1992, 1993.  All rights reserved."},
  69.     { 21, 16, "1715 Summit Dr., W. Lafayette, IN (317) 463-6865" },
  70.     {  0,  0, NULL },
  71. };
  72.  
  73. char BarCh[] = { BARCH, '\0'};        /* bar graph character */
  74. short BkClrx =  DEFBCX;            /* background color */
  75. short Ckdump = 0;            /* check dump status */
  76. short Cksum = 1;            /* checksum checking status */
  77. char IntCh[] = { INTCH, '\0' };        /* bar and min/max intersection
  78.                      * character */
  79. char LineCh[] = { LINECH, '\0'};    /* min/max level line character */
  80. short PcDisp = 1;            /* page count display state */
  81. char *Pn;                /* program name */
  82. long PrevBkClr = BLACK;            /* previous background color */
  83. short PrevTxtClr = WHITE;           /* previous text color */
  84. short Qp = 0;                /* QuattroPro output mode */
  85. short TxtClrx = DEFTCX;            /* text color */
  86. short Warn = 1;                /* display warning status */
  87.  
  88. static void InitClr();
  89. static void ParseArgs(int argc, char *argv[], int err);
  90. static void SigHandle();
  91. static void Usage();
  92. static void ZeroLog();
  93.  
  94.  
  95. extern char *optarg;
  96. extern int optind;
  97.  
  98.  
  99. /*
  100.  * main() - main program
  101.  */
  102.  
  103. main(argc, argv )
  104.     int argc;            /* argument count */
  105.     char *argv[];            /* argument pointers */
  106. {
  107.     int err, i;
  108.  
  109. /*
  110.  * Save program name.
  111.  */
  112.     if ((Pn = strrchr(argv[0], '/')) != NULL)
  113.         Pn++;
  114.     else {
  115. #if    !defined(UNIX)
  116.         if ((Pn = strchr(argv[0], '\\')) != NULL)
  117.             Pn++;
  118.         else
  119. #endif
  120.         Pn = argv[0];
  121.     }
  122.  
  123.     InitDump();
  124.     err = ! ReadConf();
  125.     (void) ParseArgs(argc, argv, err);
  126.     if (ExpandTtl()) {
  127.         (void) fprintf(stderr, "%s: graph title error: %s\n",
  128.             Pn, TtlErrMsg);
  129.         Usage();
  130.         exit(1);
  131.     }
  132.     InitMenu();
  133.     InitClr();
  134.     InitStatus();
  135.     (void) signal(SIGINT, SigHandle);
  136.     MainMenu[VERSLN].col = (short)(((Vc.numtextcols-strlen(Version))/2)+1);
  137.     DispMenu(MainMenu, NULL);
  138.     for (;;) {
  139.         if (kbhit()) {
  140.             i = getch();
  141.             switch(i) {
  142.  
  143.             case 'c':
  144.             case 'C':
  145.                 DefCom();
  146.                 break;
  147.             case 'd':
  148.             case 'D':
  149.                 DumpMtr();
  150.                 break;
  151.             case 's':
  152.             case 'S':
  153.                 StatusMtr();
  154.                 break;
  155.             case 't':
  156.             case 'T':
  157.                 DateTime();
  158.                 break;
  159.             case ESC:
  160.             case 'x':
  161.             case 'X':
  162.                 TouchExit(0);
  163.             case 'z':
  164.             case 'Z':
  165.                 ZeroLog();
  166.                 break;
  167.             default:
  168.                 if (i == 0)
  169.                     i = getch();
  170.                 putch(BELL);
  171.             }
  172.             DispMenu(MainMenu, NULL);
  173.         }
  174.         reset_buffer();
  175.     }
  176. }
  177.  
  178.  
  179. /*
  180.  * CloseDump() - close dump files (abnormally)
  181.  */
  182.  
  183. void
  184. CloseDump()
  185. {
  186.     struct stat sbuf;
  187.  
  188.     if (Dumpfs != NULL) {
  189.         (void) fclose(Dumpfs);
  190.         Dumpfs = NULL;
  191.         if (stat(Qp ? DumpfnQ : DumpfnR, &sbuf) == 0
  192.         && sbuf.st_size == 0)
  193.             (void) unlink(Qp ? DumpfnQ : DumpfnR);
  194.     }
  195.     if (Graphfs != NULL) {
  196.         (void) fclose(Graphfs);
  197.         Graphfs = NULL;
  198.         if (stat(Graphfn, &sbuf) == 0 && sbuf.st_size == 0)
  199.             (void) unlink(Graphfn);
  200.     }
  201.     if (Rdumpfs != NULL) {
  202.         (void) fclose(Rdumpfs);
  203.         Rdumpfs = NULL;
  204.     }
  205. }
  206.  
  207.  
  208. /*
  209.  * InitClr() - initialize colors
  210.  */
  211.  
  212. static void
  213. InitClr()
  214. {
  215.     char buf[80];
  216.  
  217.     PrevTxtClr = _gettextcolor();
  218.     _settextcolor(TxtClrx);
  219.     PrevBkClr = _getbkcolor();
  220.     _setbkcolor(BkClrx & 7);
  221.     if (Vc.mode != _TEXTC80 && Warn) {
  222.         _clearscreen(_GCLEARSCREEN);
  223.         PromptMsg("Press ESC to exit; any other key to continue.");
  224.         _settextposition(11, 15);
  225.         _outtext("Warning: the display cannot be put in TEXTC80 mode.");
  226.         _settextposition(13, 15);
  227.         _outtext("This means that display operations, including color");
  228.         _settextposition(14, 15);
  229.         _outtext("selections, may be incorrect.");
  230.         if ((char)WaitAnyKey() == ESC)
  231.             TouchExit(1);
  232.         _clearscreen(_GCLEARSCREEN);
  233.     }
  234.     if (Vc.mode == _TEXTC80 && BkClrx > 7 && Warn) {
  235.         _clearscreen(_GCLEARSCREEN);
  236.         PromptMsg("Press ESC to exit; any other key to continue.");
  237.         _settextposition(11, 15);
  238.         _outtext("Warning: for menus the display is in TEXTC80 mode.");
  239.         _settextposition(13, 15);
  240.         _outtext("Since TEXTC80 is limited to 8 background colors, the");
  241.         _settextposition(14, 15);
  242.         (void) sprintf(buf,
  243.         "background color \"%s\" will be displayed as",
  244.         Colors[BkClrx].nm);
  245.         _outtext(buf);
  246.         _settextposition(15, 15);
  247.         (void) sprintf(buf,
  248.         "\"%s\" on menus; \"%s\" will be used only on",
  249.         Colors[BkClrx & 7].nm, Colors[BkClrx].nm);
  250.         _outtext(buf);
  251.         _settextposition(16, 15);
  252.         _outtext("graph output.");
  253.         if ((char)WaitAnyKey() == ESC)
  254.             TouchExit(1);
  255.         _clearscreen(_GCLEARSCREEN);
  256.     }
  257. }
  258.  
  259.  
  260. /*
  261.  * ParseArgs() - parse command line arguments
  262.  */
  263.  
  264. static void
  265. ParseArgs(argc, argv, err)
  266.     int argc;                /* argument count */
  267.     char *argv[];                /* argument pointers */
  268.     int err;                /* configuration file error
  269.                          * status */
  270. {
  271.     int c;
  272.     int hlp = 0;
  273.  
  274. /*
  275.  * Process command line arguments.
  276.  */
  277.     while ((c = getopt(argc, argv, "a:b:B:cCg:G:hi:l:L:p:Pqrt:v:w"))
  278.     != EOF) {
  279.         switch(c) {
  280.  
  281.         case 'a':    /* -a s -- set after graph printer control */
  282.             if ( ! SetAftGraph(optarg, optarg))
  283.                 err = 1;
  284.             break;
  285.         case 'b':    /* -b char -- define bar graph character */
  286.             if ( ! SetBarChar(optarg, optarg))
  287.                 err = 1;
  288.             break;
  289.         case 'c':    /* -c -- dump check, error and high readings */
  290.             Ckdump = 1;
  291.             break;
  292.         case 'C':    /* -C -- don't check checksums */
  293.             Cksum = 0;
  294.             break;
  295.         case 'B':    /* -B s -- set before graph printer control */
  296.             if ( ! SetBefGraph(optarg, optarg))
  297.                 err = 1;
  298.             break;
  299.         case 'g':    /* -g min -- set graph minimum level value */
  300.             if ( ! SetGmaxLvl(optarg, optarg))
  301.                 err = 1;
  302.             break;
  303.         case 'G':    /* -G max -- set graph maximum level value */
  304.             if ( ! SetGminLvl(optarg, optarg))
  305.                 err = 1;
  306.             break;
  307.         case 'h':    /* -h -- display help */
  308.             hlp = 1;
  309.             break;
  310.         case 'i':    /* -i char -- define bar and max/min
  311.                  *          intersection character */
  312.             if ( ! SetIntChar(optarg, optarg))
  313.                 err = 1;
  314.             break;
  315.         case 'l':    /* -l char -- define max/min line character */
  316.             if ( ! SetLineChar(optarg, optarg))
  317.                 err = 1;
  318.             break;
  319.         case 'L':    /* -L num -- define graph lines per page */
  320.             if ( ! SetLpp(optarg, optarg))
  321.                 err = 1;
  322.             break;
  323.         case 'p':    /* -p name -- define printer */
  324.             if ( ! SetPrinter(optarg, optarg))
  325.                 err = 1;
  326.             break;
  327.         case 'P':    /* -P -- turn off page count display */
  328.             PcDisp = 0;
  329.             break;
  330.         case 'r':    /* -r -- turn off raw dump format */
  331.             Qp = 1;
  332.             break;
  333.         case 't':    /* -t ttl -- set graph title line */
  334.             if ( ! SetGraphTtl(optarg, optarg))
  335.                 err = 1;
  336.             break;
  337.         case 'v':    /* -v n,val -- define title variable n */
  338.             if ( ! SetTtlVar(optarg, optarg))
  339.                 err = 1;
  340.             break;
  341.         case 'w':    /* disable display warnings */
  342.             Warn = 0;
  343.             break;
  344.         case '?':
  345.             err = 1;
  346.         }
  347.     }
  348.     if (AftGraph == NULL) {
  349.         if ( ! EntPrtStr(AFTEPSON, BEFEPSON))
  350.             err = 1;
  351.     }
  352.     if ( ! TestGlev())
  353.         err = 1;
  354.     if (err || hlp) {
  355.         Usage();
  356.         if (err)
  357.             exit(1);
  358.         exit(0);
  359.     }
  360. }
  361.  
  362.  
  363. /*
  364.  * SigHandle() - handle signal
  365.  */
  366.  
  367. static void
  368. SigHandle()
  369. {
  370.     TouchExit(1);
  371. }
  372.  
  373.  
  374. /*
  375.  * TouchExit(xv) - exit
  376.  */
  377.  
  378. void
  379. TouchExit(xv)
  380.     int xv;                    /* exit value */
  381. {
  382.     CloseCom();
  383.     CloseDump();
  384.     _displaycursor(_GCURSORON);
  385.     _settextcursor(Cursor);
  386.     _setbkcolor(PrevBkClr);
  387.     _settextcolor(PrevTxtClr);
  388.     _settextposition(Vc.numtextrows, Vc.numtextcols);
  389.     _outtext("\n");
  390.     if (Mode >= 0)
  391.         _setvideomode(Mode);
  392.     exit(xv);
  393. }
  394.  
  395.  
  396. /*
  397.  * Usage() -- display usage
  398.  */
  399.  
  400. static void
  401. Usage()
  402. {
  403.     (void) fprintf(stderr,
  404.         "%s, v%s\n", Pn, Version+1);
  405.     (void) fprintf(stderr,
  406.         "usage: [-a string] [-b char] [-B string] [-cChPrw] [-g min]");
  407.     (void) fprintf(stderr, " [-G max]\n");
  408.     (void) fprintf(stderr,
  409.      "       [-i char] [-l char] [-L num] [-p name] [-t ttl] [-v n,val]\n");
  410.     (void) fprintf(stderr,
  411.         "\t-a string  set <string> as after graph printer control\n");
  412.     (void) fprintf(stderr,
  413.         "\t-b char    use <char> for bar line\n");
  414.     (void) fprintf(stderr,
  415.         "\t-B string  set <string> as before graph printer control\n");
  416.     (void) fprintf(stderr,
  417.         "\t-c         put check/error/high readings in QuattoPro dump\n");
  418.     (void) fprintf(stderr,
  419.         "\t-C         turn off checksum verification\n");
  420.     (void) fprintf(stderr,
  421.         "\t-g min     set graph minimum level to <min>\n");
  422.     (void) fprintf(stderr,
  423.         "\t           (mg/dl default = %.1f, mmol/l default = %.1f)\n",
  424.         DEFMGMIN, DEFMMMIN);
  425.     (void) fprintf(stderr,
  426.         "\t-G max     set graph maximum level to <max>\n");
  427.     (void) fprintf(stderr,
  428.         "\t           (mg/dl default = %.1f, mmol/l default = %.1f)\n",
  429.         DEFMGMAX, DEFMMMAX);
  430.     (void) fprintf(stderr,
  431.         "\t-h         display help (usage)\n");
  432.     (void) fprintf(stderr,
  433.         "\t-i char    use <char> for bar and max/min intersection\n");
  434.     (void) fprintf(stderr,
  435.         "\t-l char    use <char> for max/min line\n");
  436.     (void) fprintf(stderr,
  437.         "\t-L num     set graph lines/page to <num>");
  438.     (void) fprintf(stderr,
  439.         " (default %d, min %d)\n", LPP, HDRLPP+1);
  440.     (void) fprintf(stderr,
  441.         "\t-p name    define printer as <name> -- EPSON, PCL4 or PPDS\n");
  442.     (void) fprintf(stderr,
  443.         "\t           (default = EPSON)\n");
  444.     (void) fprintf(stderr,
  445.         "\t-P         turn off page count display\n");
  446.     (void) fprintf(stderr,
  447.         "\t-r         change from raw to QuattroPro dump file format\n");
  448.     (void) fprintf(stderr,
  449.         "\t-t ttl     set graph title line to <ttl>\n");
  450.     (void) fprintf(stderr,
  451.         "\t-v n,val   set title variable <n> to <val>\n");
  452.     (void) fprintf(stderr,
  453.         "\t-w         disable warnings about display mode\n");
  454. }
  455.  
  456.  
  457. /*
  458.  * ZeroLog() - clear the meter datalog
  459.  */
  460.  
  461. static void
  462. ZeroLog()
  463. {
  464.     DispMenu(ZeroMenu, NULL);
  465.     if ((char)WaitAnyKey() != 'Y')
  466.         return;
  467.     for (;;) {
  468.         if (WaitRdy() == 0)
  469.             return;
  470.         if (WaitCmd("DMZ", 'Z')) {
  471.             GetDataLn(DumpLine, DUMPLL);
  472.             if (strcmpi(DumpLine, " 005A") == 0)
  473.                 return;
  474.         }
  475.         DispMenu(ZeroFail,
  476.             "Press ESC to exit; any other key to retry.");
  477.         if ((char)WaitAnyKey() == ESC)
  478.             return;
  479.     }
  480. }
  481.