/* Fastcharger for Workabout */
/* Example application */
/* (C) Copyright Psion Software PLC 1997 */
#include <p_std.h>
#include <p_file.h>
#include <p_sys.h>
#include <p_keyb.h>
#include <epoc.h>
#include "fcharge.h"
GLREF_D VOID *winHandle;
LOCAL_C VOID State(WORD Val)
    {
    switch (Val)
        {
    case 0:
        p_printf(" Unknown");
        break;
    case 1:
        p_printf(" Not Charging");
        break;
    case 2:
        p_printf(" Fast Charging");
        break;
    case 3:
        p_printf(" Topping Off");
        break;
    default:
        p_printf(" Trickle");
        }
    }
GLDEF_C VOID main(VOID)
    {
    VOID *pcb;
    WORD One,OldOne;
    WORD Two,OldTwo;
    WORD KStat,Disch;
    WORD LogOne[100];
    WORD LogTwo[100];
    WORD P1=0,P2=0,Temp;
    ULONG TTime;
    P_CON_KBREC Key;
    TEXT X1Str[11];
    WORD X1=0;
    ULONG XE=0,DischTime;
    TEXT *X1Ptr;
    INT Item;
    for(Item=0;Item<100;Item++)
        {
        LogOne[Item]=0;
        LogTwo[Item]=0;
        }
    
    p_devdel("FCG",E_LDD);
    p_printf("Loading");
    if(p_loadldd("m:\\sys$fchg.ldd")<0)
    	p_panic(0);
    p_printf("Opening test");
    if(p_open(&pcb,"FCG:A",-1)<0)
    	p_panic(1);
    p_printf("Closing test");
    if(p_close(pcb)<0)
    	p_panic(2);
    p_printf("Opening test part II");
    if(p_open(&pcb,"FCG:A",-1)<0)
    	p_panic(3);
    p_printf("Cant remove test");
    if(p_devdel("FCG:",E_LDD)>=0)
    	p_panic(4);
    p_printf("Setting the charge Mode");
    p_iow(pcb,P_FCHG_READCHARGEMODE,&One);
    if (One!=(P_FCHG_DEFAULTCC | P_FCHG_CTIME1C))
    	p_panic(5);
    p_printf("Test the charge Mode");
    if (p_iow(pcb,P_FCHG_SETCHARGEMODE,&One)!=0)
    	p_panic(6);
    p_printf("Set Rubbish charge Mode");
    One=-1;
    if (p_iow(pcb,P_FCHG_SETCHARGEMODE,&One)==0)
    	p_panic(7);
    p_iow(pcb,P_FCHG_READSTATUS,&One,&Two);
    p_print("Battery one:");
    State(One);
    p_print("Battery two:");
    State(Two);
/*    p_printf("Asynch test1");
     p_iow(pcb,P_FCHG_ASYNCHREAD,&One,&Two);
     p_print("Battery one:");
     State(One);
     p_print("Battery two:");
     State(Two);
     p_printf("Asynch test2");
     p_iow(pcb,P_FCHG_ASYNCHREAD,&One,&Two);
     p_print("Battery one:");
     State(One);
     p_print("Battery two:");
     State(Two);*/
    p_ioc4(winHandle,P_FREAD,&KStat,&Key);
    TTime=p_date();
    Disch=0;
    DischTime=0;
    FOREVER
      {
	   if (KStat!=E_FILE_PENDING) /* Was it the keyboard */
          {
	       p_iowait();             /* not really the way to do this -oh well */
	       if (Key.keycode=='Q' || Key.keycode=='q' || Key.keycode==27)
              p_exit(0);
	       else if (Key.keycode=='D' || Key.keycode=='d')
              {
              p_printf("Start discharge");
              TTime=p_date();
              if (p_iow(pcb,P_FCHG_DISCHARGE))
    	            p_panic(8);
              Disch=1;
              }
	       else if (Key.keycode=='F' || Key.keycode=='f')
              {
              p_printf("Start Fastcharge1");
              TTime=p_date();
              if (p_iow(pcb,P_FCHG_FASTCHARGE1))
    	            p_panic(9);
              }
	       else if (Key.keycode=='G' || Key.keycode=='g')
              {
              p_printf("Start Fastcharge2");
              TTime=p_date();
              if (p_iow(pcb,P_FCHG_FASTCHARGE2))
    	            p_panic(9);
              }
	       else if (Key.keycode=='C' || Key.keycode=='c')
              {
              p_getl("Current: ",&X1Str[0],10);
              X1Ptr=&X1Str[0];
              p_stoi(&X1Ptr,&X1);
              }
	       else if (Key.keycode=='/' || Key.keycode=='?')
              {
              if (Disch==1)
                  p_printf("Discharging: %d",(p_date()-TTime));
              else
                  {
                  p_printf("Discharge Time: %dl",DischTime);
                  if (X1>0 && DischTime>0)
                     {
                     XE=(850*60*60*10)/X1;
                     XE=(DischTime*100*10)/XE;
                     p_printf("Capacity (Cell) %d%%",(UWORD)(XE));
                     }
                  }
              }
	       else if (Key.keycode=='1')
              {
              p_printf("Battery one Log");
              Temp=0;
              while (Temp<P1)
              if (LogOne[Temp++]>0)
                  State(LogOne[Temp-1]);
              }
	       else if (Key.keycode=='2')
              {
              p_printf("Battery two Log");
              Temp=0;
              while (Temp<P2)
                  if (LogTwo[Temp++]>0)
                      State(LogTwo[Temp-1]);
              }
	       else if (Key.keycode=='H' || Key.keycode=='h' || Key.keycode==290 || Key.keycode==291)
              {
              p_printf("");
              p_printf("C...Enter Discharge current");
              p_printf("D...Start Discharge");
              p_printf("F...Fastcharge Battery1");
              p_printf("G...Fastcharge Battery2");
              p_printf("?...Show Discharge time");
              p_printf("1...Show Battery1 Log");
              p_printf("2...Show Battery2 Log");
              p_printf("Q...Quit");
              p_printf("");
              }
          p_ioc4(winHandle,P_FREAD,&KStat,&Key);
          }
      OldOne=One;
      OldTwo=Two;
      p_iow(pcb,P_FCHG_READSTATUS,&One,&Two);
      if (OldOne!=One || OldTwo!=Two)
          {
          if (OldOne==1 && Disch==1 && One!=1)
              {
              Disch=0;
              DischTime=(p_date()-TTime);
              p_printf("Discharge Time: %d",DischTime);
              }  
          p_printf("Time: %d",(p_date()-TTime));
          if (One!=OldOne)
              {  
              if (P1>50)
                  P1=0;
              LogOne[P1++]=One;
              }
          if (Two!=OldTwo)
              {  
              if (P2>50)
                  P2=0;
              LogTwo[P2++]=Two;
              }
          p_print("Battery one:");
          State(One);
          p_print("Battery two:");
          State(Two);
          }         
      }
}
