home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / oplexamp / BATTERY.OPL < prev    next >
Text File  |  1994-03-02  |  7KB  |  217 lines

  1. /*
  2.         Author:         DP
  3.         Started:        17/2/94
  4.         Machine:        S3a only
  5.  
  6. Procedure to return the current values of the batteries from the 
  7. E_SUPPLY_INFO structure below.
  8.  
  9. typedef struct
  10.     {
  11.     unsigned char mainBatteryLevel;
  12.     unsigned char mainBatteryStatus;
  13.     unsigned char backupBatteryLevel;
  14.     unsigned char dcLevel;
  15.     unsigned short int warningFlags;
  16.     unsigned long int insertionDate;
  17.     unsigned long int ticksInUseBatter
  18.     unsigned long int ticksInUseDc;
  19.     unsigned long int maTicks;
  20.     } E_SUPPLY_INFO;
  21.  
  22. */
  23. #define HwManager       $008E   
  24. #define HwSupplyInfo    $2200
  25.  
  26. #define E_MBAT_ZERO     0       /* Main battery levels & status */
  27. #define E_MBAT_VERY_LOW 1
  28. #define E_MBAT_LOW      2
  29. #define E_MBAT_GOOD     3
  30.  
  31. #define E_SUPPLY_SOUND_WARNING 0x0001           /* Warning flags */
  32. #define E_SUPPLY_FLASH_WARNING 0x0002
  33. #define E_SUPPLY_SYSTEM_TIME_CHANGED 0x0004
  34.  
  35.  
  36. #define TRUE    1
  37. #define FALSE   0
  38.  
  39. proc battery:
  40.  
  41.         LOCAL ax%,bx%,cx%,dx%,si%,di%   /* OS registers*/
  42.         LOCAL yr%, mo%, dy%, hr%, mn%, sc%, yrday%      /* SECSTODATE vars */
  43.         LOCAL Info$(22)         /* buffer for the struct to be returned in to */
  44.         LOCAL pbuff%            /* pointer to the buffer */
  45.         LOCAL flags%            /* the return flags */
  46.  
  47. /* return values into the following variables from the struct */
  48.  
  49.         LOCAL MainBL%   /* main battery level */
  50.         LOCAL MainBS%   /* main battery status */
  51.         LOCAL BackBL%   /* backup battery level */
  52.         LOCAL DCLevel%  /* mains supply status */
  53.         LOCAL WarnFlg%  /* warning flags */
  54.         LOCAL InsDate&  /* insertion date */
  55.         LOCAL UseBat&   /* time on batteries */
  56.         LOCAL UseDC&    /* time on mains */
  57.         LOCAL mA&       /* total battery used */
  58.  
  59. /* variables used for conversions */
  60.  
  61.         LOCAL BatHour%
  62.         LOCAL BatMin%
  63.         LOCAL DCHour%
  64.         LOCAL DCMin% 
  65.         LOCAL mA%
  66.  
  67.         LOCAL MainBL$(14)
  68.         LOCAL MainBS$(14)
  69.         LOCAL BackBL$(9)
  70.         LOCAL Mains$(7)
  71.         LOCAL Date$(10)
  72.  
  73.         LOCAL BatTime$(10) 
  74.         LOCAL DCTime$(10)
  75.         LOCAL TotCurr$(8)
  76.  
  77.         
  78.         pbuff% = ADDR(Info$) + 1        /* set up a pointer to the buffer */
  79.  
  80. /* fill in the OS registers with the correct information */
  81.  
  82.         ax% = HwSupplyInfo              
  83.         bx% = pbuff%
  84.         cx% = 0
  85.         dx% = 0
  86.         si% = 0
  87.         di% = 0
  88.  
  89.         flags% = OS(HwManager, ADDR(ax%))       /* call the system service */
  90.  
  91.         IF flags% AND 1                 /* if there is an error report it */
  92.         PRINT err$(flags%)
  93.                 GET
  94.                 STOP
  95.  
  96.         ENDIF
  97.  
  98.         pokeb ADDR(info$),22    /* poke the lbc of the buffer */
  99.  
  100. /* read the values in the buffer into the correct variables */
  101.  
  102.         MainBL%  = peekb(ADDR(info$) + 1)
  103.         MainBS%  = peekb(ADDR(info$) + 2)
  104.         BackBL%  = peekb(ADDR(info$) + 3)
  105.         DCLevel% = peekb(ADDR(info$) + 4)
  106.         WarnFlg% = peekw(ADDR(info$) + 5)
  107.         InsDate& = peekl(ADDR(info$) + 7)
  108.         UseBat&  = peekl(ADDR(info$) + 11)
  109.         UseDC&   = peekl(ADDR(info$) + 15)
  110.         mA&      = peekl(ADDR(info$) + 19)
  111.  
  112. /* 
  113. Read the main batery level and status and backup battery level 
  114. and mains level and convert them into verbose English for use with 
  115. dTEXT
  116. */
  117.  
  118.         IF MainBl% = E_MBAT_ZERO    
  119.                 MainBL$ = "Not present !"
  120.         ELSEIF MainBl% = E_MBAT_VERY_LOW
  121.                 MainBL$ = "Replace !"
  122.         ELSEIF MainBL% = E_MBAT_LOW     
  123.                 MainBL$ = "Low"
  124.         ELSEIF MainBl% = E_MBAT_GOOD    
  125.                 MainBL$ = "Good"
  126.         ENDIF
  127.  
  128.  
  129.         IF MainBS% = E_MBAT_ZERO    
  130.                 MainBS$ = "Not present !"
  131.         ELSEIF MainBS% = E_MBAT_VERY_LOW
  132.                 MainBS$ = "Replace !"
  133.         ELSEIF MainBS% = E_MBAT_LOW     
  134.                 MainBS$ = "Low"
  135.         ELSEIF MainBS% = E_MBAT_GOOD    
  136.                 MainBS$ = "Good"
  137.         ENDIF
  138.  
  139.         IF BackBL% = TRUE
  140.                 BackBL$ = "Good"
  141.         ELSE
  142.                 BackBL$ = "Replace !"
  143.         ENDIF
  144.  
  145.         IF DCLevel% = TRUE
  146.                 Mains$ = "Present"
  147.         ELSE
  148.                 Mains$ = "No"
  149.         ENDIF
  150.  
  151. /* 
  152.    convert the insertion date from system time into a numeric 
  153.    representation
  154. */
  155.         SECSTODATE InsDate&, yr%, mo%, dy%, hr%, mn%, sc%, yrday%
  156.  
  157. /* convert the integer variables into a string for use with dTEXT */
  158.  
  159.         date$  = FIX$(dy%,0,-2)+"/"+FIX$(mo%,0,-2)+"/"+FIX$(yr%,0,4)
  160.  
  161.  
  162.         BatHour% = (UseBat&/32)/3600    /* get the number of hours the main battery has been in */
  163.         BatMin%  = mod%:(UseBat&)       /* get the number of minutes the main battery has been in */
  164.         DCHour%  = (UseDC&/32)/3600     /* get the number of hours the mains  has been in */
  165.         DCMin%   = mod%:(UseDC&)        /* get the number of minutes the mains has been in */
  166.         mA%      = (mA&/32)/3600        /* get the total time in mAh */
  167.  
  168.  
  169. /* convert the integer variables into strings that can be displayed by dTEXT */
  170.  
  171.         BatTime$ = FIX$(BatHour%,0,4)+"h "+FIX$(BatMin%,0,4)+"m"
  172.         DCTime$  = FIX$(DCHour%,0,4)+"h "+FIX$(DCMin%,0,4)+"m"
  173.         TotCurr$  = FIX$(mA%,0,4)+" mAh"
  174.  
  175.  
  176. /* print the results, leaving out some of the less verbose */
  177.  
  178.  
  179.         dINIT "Usage Monitor (live readings)"
  180.         dTEXT "Main batteries",                 MainBL$
  181. rem     dTEXT "Main battery Status",            MainBS$  
  182.         dTEXT "Backup battery",                 BackBL$  
  183.         dTEXT "External power",                 Mains$  ,$200
  184. rem     PRINT "Warning Flags",                  WarnFlg%
  185.         dTEXT "Main batteries inserted on",     date$
  186.         dTEXT "Time on external power",         DCTime$  
  187.         dTEXT "Time on batteries",              BatTime$ 
  188.         dTEXT "Total battery used",             TotCurr$  
  189.         DIALOG
  190.  
  191. endp
  192.  
  193.  
  194. proc mod%:(a&)
  195.  
  196. /*
  197.         routine to return the number of minutes as an integer
  198.         from a value passed to it as time in ticks (1/32 of second)
  199. */
  200.  
  201.         LOCAL Mins%
  202.         LOCAL Time&
  203.         LOCAL Time
  204.  
  205.         Time& = a&
  206.  
  207.         Time = FLT(Time&/32)  /* divide by 32 to get number of seconds */
  208.  
  209. /* find the remainder, ie number of minutes, after removing the number
  210.    of hours from the total number of seconds */
  211.  
  212.         Mins% = INT((Time-(INT(Time/3600.0)*3600))/60)  
  213.  
  214.         RETURN Mins%    /* return the number of minutes */
  215.  
  216. endp
  217.