/* Flow rate reader */
/* Programmed by Stephen Penney 8-3-95 6:45pm */
/* With additional material by John Finney and Simon Graves */

#include <disp.h>
#include <stdio.h>
#include <msmouse.h>

main()
{    
   printf ("Please wait, initialising.");
   FILE *fp;
   fp=fopen("DATA.DAT","w");
   
   disp_open();
   disp_move(0,0);
   disp_eeop();
   disp_box(1,2,0,0,24,79);
   disp_box(0,2,1,1,6,24);
   disp_move(2,2);
   disp_printf("YOUNG PROJECT ENGINEER");
   disp_move(3,2);
   disp_printf("----------------------");
   disp_move(4,2);
   disp_printf("  Flow rate measuring ");
   disp_move(5,2);
   disp_printf("     Software Pt1");
   disp_box(0,2,7,1,10,24);
   disp_move(8,2);
   disp_printf("(c) Stephen Penney");
   disp_move(9,2);
   disp_printf("    John Finney  '95");   
   disp_box(0,2,11,20,14,66);

    disp_move(12,23);
    disp_printf("            FLOW RATE MEASURER");
   disp_move(13,23);
    disp_printf("Press 'GO' on Flow sensor to begin timing");   
   
   if(msm_init() == -1)
   {
     int timer;
     int ena=0;
     int count; 
     int status;
     unsigned ad;
     int loop;
     unsigned x,y;
     while (1)
     {
      status = msm_getstatus(&x, &y);
      if (status & LEFT_BUTTON)
      {
       disp_box(0,2,16,20,19,66);
       disp_move(17,21);
        disp_printf("Measuring and collating data into file......\n");
       disp_move(18,21);
        disp_printf("Press 'STOP' on Flow sensor to stop measuring"); 
       while (1)
       {
        msm_init();
        count = 0;
        loop = 240;
        ad = 240;
        for (timer=1; timer <=2000; timer++)
        { 
        msm_hidecursor();
        loop = ad;
        status = msm_getstatus(&x, &ad);
        count = count + (loop - ad);
        if (ad > 478)
           {
           msm_init();
           loop = 240;
           ad = 240;
           }       
        }
        fprintf(fp, "%d\n", count);  
        status = msm_getstatus(&x, &y);
        if (status & RIGHT_BUTTON)
          ena=1;
        if (ena>0)
          break;
       }
       if (ena > 0)
        break;
      }
     if (ena > 0)
      break;
     }
      fclose(fp);
      disp_move(15,1);
      disp_printf("STOP was pressed.\n");
      disp_printf("\nOK, saved to A:\DATA.DAT.");
      msm_term();
      disp_close();
   }
   else 
       printf("Mouse initialization failed\n");
}  

