BatteryInfo
Gets battery info like current volt, kind of akku etc.
Download

BatteryInfo.zip (56 KB)

Functions definition

external "CASL_BatteryInfo";

# Excerpt of the "Palm OS Reference":
#
# warnThresholdP and maxTicksP are the battery-warning
# voltage threshold and time out. If the battery voltage falls below the
# threshold, or the timeout expires, a lowBatteryChr key event is
# put on the queue. Normally, applications call SysHandleEvent
# which calls SysBatteryDialog in response to this event.
#
# criticalThresholdP is the battery voltage threshold. If battery
# voltage falls below this level, the system turns itself off without
# warning and doesn't turn on until battery voltage is above it again.
#

function SysGetBatteryInfo(string Separator) as string;
# Low level function, 
# Returns all battery info in a string:
# CurrentVolt*100<sep>warnThreshold*100<sep>criticalThreshold*100<sep>
# maxTicks<sep>kind<sep>pluggedIn<sep>percent<sep>

end_external;

variables;
  numeric mCurrVolt = 0;
  numeric mWarnThreshold = 1;
  numeric mCriticalThreshold = 2;
  numeric mMaxTicks = 3;
  numeric mKind = 4;
  numeric mPluggedIn = 5;
  numeric mPercent = 6;
  numeric MaxBatteryKind = 6;
  string BatteryKind[MaxBatterykind] = "Alkaline","NiCad","LiIon",
                                       "RechAlk","NiMH","LiIon1400";
end;

#------------------------------------------------------------------
function GetBatteryInfo(numeric Mode) as numeric;
# High level function, 
# Use this if you do not want to make string operations
# Also can be used as a template to make your own functions
# Input:
# Mode = 0 Current Volt
# Mode = 1 WarnThreshold in Volt
# Mode = 2 CriticalThreshold in Volt
# Mode = 3 MaxTicks in ms
# Mode = 6 Percentage 
# Output
# requested value or -1 (error)

variables;
  numeric Pos;
  numeric ScanDone;
  numeric i;
  numeric Max = 7;
  string s[Max];
  string t;
  string sep = ";";
  numeric RVal;
end;

if (Mode >=0 and Mode < Max);
  t = SysGetBatteryInfo(sep);
  # scanning for separator
  ScanDone = false;
  i = 0;
  while not ScanDone;
    Pos = find(sep, t,0);
    if Pos < 0;
      ScanDone = true;
    else;
      s[i] = left(t,Pos);
      Pos = length(t)-Pos-1;
      if Pos > 0;
        t = right(t,Pos);
      else;
        ScanDone = true;
      end_if
      i=i+1;
      if i >= Max;
        ScanDone = true;
      end_if;
    end_if;
  end_while;
  RVal = value(s[Mode]);
  if Mode >= 0 and Mode <= 2;
    RVal = RVal / 100; # transform Volt*100 to Volt
  end_if;
  GetBatteryInfo = RVal;
else;
  GetBatteryInfo = -1;
end_if;
end;

#------------------------------------------------------------------
function GetBatteryKind as string;
variables;
  numeric xkind;
end;
xkind = GetBatteryInfo(mKind);
if xkind >= MaxBatteryKind;
  GetBatteryKind = "unkown";
else;
  GetBatteryKind = BatteryKind[xkind];
end_if;
end;

#------------------------------------------------------------------
function IsDevicePluggedIn as numeric;
# true, if device is charging (in cradle or with travel charger)
# false, if device is not charging
 
IsDevicePluggedIn = GetBatteryInfo(mPluggedIn); 
end;