Register

Fx-9860 battery indicator (C)

Learn how to program. Code snippets for creating sprites etc. Submit your own or use those of others.
Senior Member
Posts: 369
Joined: Tue Jan 03, 2012 11:24 pm
Calculators: Casio Afx 1.0, Casio fx-9860GII SD, Casio Classpad 330, Casio fx-CG20, Casio Classpad fx-CP400

Fx-9860 battery indicator (C)

Postby helder7 » Mon May 07, 2012 11:24 pm

I found this in interesting code in one add-in.

battery indicator
Code: Select all
// battery = 1 = main (do not use other values)
// firstlevel: warning level
// secondlevel: shutdown level
int GetBatteryStatus( int battery, int*firstlevel, int*secondlevel ){
int (*iGBS)( int ) = 0;  // declare an int function pointer
int*battconfig = (int*)0x80000334;
  *firstlevel = 0x17F;
  *secondlevel = 0x16D;
 
     switch ( OSVersionAsInt() ){
       case OS102 : iGBS = (int(*)(int))0x80032E14; break;
       case OS103 : iGBS = (int(*)(int))0x80032E80; break;
       case OS104 :
       case OS105 :
         iGBS = (int(*)(int))0x80032E5C;
         if ( *battconfig==0x43444844 ){
           *firstlevel = 0x1A5;
           *secondlevel = 0x191;
         };
         break;
       case OS1051 :
         iGBS = (int(*)(int))0x80032E44;
         if ( *battconfig==0x43444844 ){
           *firstlevel = 0x1A5;
           *secondlevel = 0x191;
         };
         break;
       case OS110 :
         iGBS = (int(*)(int))0x8003BD2C;
         *firstlevel = 0x2AA;
         *secondlevel = 0x288;
         break;
       case OS111 :
         iGBS = (int(*)(int))0x8003BCF0;
         *firstlevel = 0x2AA;
         *secondlevel = 0x288;
         break;
       case OS200 :
         iGBS = (int(*)(int))0x80039A0C;
         if ( *battconfig==0x43444844 ){
           *firstlevel = 0x1A5;
           *secondlevel = 0x191;
         };
         break;
      case OS200GII :
         iGBS = (int(*)(int))0x8003C484;
         break;
      case OS200slim :
         iGBS = (int(*)(int))0x80047FD4;
         *firstlevel = 0x2AA;
         *secondlevel = 0x288;
         break;
     };
   if (IsSlim()){
      *firstlevel = 0x2AA;
      *secondlevel = 0x288;
   }   
   if (iGBS!=0) return (*iGBS)( battery );
   else return 0;
}

// the warning level lies around 2/3 full scale
int MainBatteryPercentage( void ){
int firstlevel, secondlevel;
int i;
   if ( IsEmulator() ){
      i = 100;
      firstlevel = 70;
      secondlevel = 65;
   } else i = GetBatteryStatus( 1, &firstlevel, &secondlevel );
     
   if (firstlevel > 0 ) return ( 200*i )/(firstlevel*3);
   else return 0;
}


by SimonLothar

As for OSVersionAsInt see: http://www.casiopeia.net/forum/viewtopic.php?f=21&t=1368
There are some constants and other functions, which possibly need to be commented (f. i. IsEmulator()http://www.casiopeia.net/forum/viewtopic.php?f=20&t=1374).
I think the code needs some update, too. I'll see to it as fast as possible.
SiO2 + CaCO3 ----------> CaSiO3 + CO2

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Postby SimonLothar » Tue May 08, 2012 5:46 pm

The following code fetches the pointer to the function GetMainBatteryVoltage (iGBS in the code of the previous post) OS-independently (fx-9860-OSes; verified with versions 1.03, 1.04, 1.05, 1.10, 1.11, 2.00, 2.01 and 2.02):
source: Show
Code: Select all
//
int GetMainBatteryVoltagePointer(){
unsigned int ea;
unsigned int j;
    ea = *(unsigned int*)0x8001007C;
    ea = ea + 0x049C*4;
    ea = *(unsigned int*)( ea );
    while ( *(unsigned short*)( ea ) != 0xE464 ){
      ea = ea + 2;
    };
    ea = ea + 2;
    j = *(unsigned char*)( ea + 1 );
    j = 4*j;
    j = ( ea + j + 4 ) & 0xFFFFFFFC;
    return *(unsigned int*)( j );
}

BTW:
on the Prizm the BatteryVoltage can be read with
syscall 0x1186: int GetMainBatteryVoltage( int one );
The parameter one should be always 1.
I'll be back!

Junior Member
Posts: 3
Joined: Mon May 21, 2012 6:20 pm
Location: Netherlands

Postby tom9358 » Mon May 21, 2012 6:27 pm

cool, could you make an add-in from it or make a tutorial on how to do that? :D

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Postby SimonLothar » Tue May 22, 2012 7:05 am

tom9358 wrote:cool, could you make an add-in from it or make a tutorial on how to do that? :D

Here's the snippet:
(if you are confused by the instant syscalls, put it in a separate file and include it. The advantages of the instant syscalls are its SDK-independence and a quick prototyping. In your final system, they may reside in a library, of course)

You can call F01_1_Handler() from out of your addin_main, f. i..
I do not know, how you want to display the battery status. Hence I implemented a simple printout for now.
source: Show
Code: Select all
// instant syscalls (fx-9860)
#define SCA 0xD201D002
#define SCB 0x422B0009
#define SCE 0x80010070
typedef void(*sc_vpuc)(unsigned char*);
typedef int(*sc_iv)(void);
typedef int(*sc_i2pui)( unsigned int*, unsigned int* );
typedef int(*sc_i4pui)( unsigned int*, unsigned int*, unsigned int*, unsigned int* );
typedef int(*sc_i2puc2pus)( unsigned char*, unsigned char*, unsigned short*, unsigned short* );
typedef int(*sc_iipci)( int, unsigned char*, int );
const unsigned int sc003B[] = { SCA, SCB, SCE, 0x003B };
#define RTC_GetTicks (*(sc_iv)sc003B)
const unsigned int sc0D65[] = { SCA, SCB, SCE, 0x0D65 };
#define InputDateMonth (*(sc_i2pui)sc0D65)
const unsigned int sc0D67[] = { SCA, SCB, SCE, 0x0D67 };
#define InputDateYear (*(sc_i2pui)sc0D67)
const unsigned int sc0D64[] = { SCA, SCB, SCE, 0x0D64 };
#define InputDate (*(sc_i4pui)sc0D64)
const unsigned int sc0015[] = { SCA, SCB, SCE, 0x0015 };
#define GlibGetOSVersionInfo (*(sc_i2puc2pus)sc0015)
const unsigned int sc06C4[] = { SCA, SCB, SCE, 0x06C4 };
#define KBD_PRGM_GetKey (*(sc_vpuc)sc06C4)
const unsigned int sc0160[] = { SCA, SCB, SCE, 0x0160 };
#define LongToAsc (*(sc_iipci)sc0160)
// instant syscalls

// for fx9860 only!
#define PORT_E_DR 0xA4000128

//
char IsSlim(){
   return !( *(char*)PORT_E_DR & 0x08 );
}

//
char IsSD(){
   return ( *(char*)PORT_E_DR & 0x01 );
}

//
int IsEmulator(){
   return !( *(int*)0x8000FFD0 || *(int*)0x8000FFD4 );
}

#define OS102 0x01020000
#define OS103 0x01030000
#define OS104 0x01040000
#define OS105 0x01050000
#define OS1051 0x01051000
#define OS110 0x01100000
#define OS111 0x01110000
#define OS200 0x02000300
#define OS200slim 0x02000100
#define OS200GII 0x02000200

//
int OSVersionAsInt( void ){
unsigned char mainversion;
unsigned char minorversion;
unsigned short release;
unsigned short build;
  GlibGetOSVersionInfo( &mainversion, &minorversion, &release, &build );
  return ( ( mainversion << 24 ) & 0xFF000000 ) | ( ( minorversion << 16 ) & 0x00FF0000 ) | ( release & 0x0000FFFF );
}

//
int GetMainBatteryVoltagePointer(){
unsigned int ea;
unsigned int j;
    ea = *(unsigned int*)0x8001007C;
    ea = ea + 0x049C*4;
    ea = *(unsigned int*)( ea );
    while ( *(unsigned short*)( ea ) != 0xE464 ){
      ea = ea + 2;
    };
    ea = ea + 2;
    j = *(unsigned char*)( ea + 1 );
    j = 4*j;
    j = ( ea + j + 4 ) & 0xFFFFFFFC;
    return *(unsigned int*)( j );
}

// battery = 1 = main (do not use other values)
// firstlevel: warning level
// secondlevel: shutdown level
int GetBatteryStatus( int battery, int*firstlevel, int*secondlevel ){
int (*iGBS)( int ) = 0;  // declare an int function pointer
int*battconfig = (int*)0x80000334;
   *firstlevel = 0x17F;
   *secondlevel = 0x16D;

   iGBS = (int(*)(int))GetMainBatteryVoltagePointer();
 
     switch ( OSVersionAsInt() ){
       case OS102 : break;
       case OS103 : break;
       case OS104 :
       case OS105 :
         if ( *battconfig==0x43444844 ){
           *firstlevel = 0x1A5;
           *secondlevel = 0x191;
         };
         break;
       case OS1051 :
         if ( *battconfig==0x43444844 ){
           *firstlevel = 0x1A5;
           *secondlevel = 0x191;
         };
         break;
       case OS110 :
         *firstlevel = 0x2AA;
         *secondlevel = 0x288;
         break;
       case OS111 :
         *firstlevel = 0x2AA;
         *secondlevel = 0x288;
         break;
       case OS200 :
         if ( *battconfig==0x43444844 ){
           *firstlevel = 0x1A5;
           *secondlevel = 0x191;
         };
         break;
      case OS200GII :
         break;
      case OS200slim :
         *firstlevel = 0x2AA;
         *secondlevel = 0x288;
         break;
     };
   if (IsSlim()){
      *firstlevel = 0x2AA;
      *secondlevel = 0x288;
   }   
   if (iGBS!=0) return (*iGBS)( battery );
   else return 0;
}

// the warning level lies around 2/3 full scale
int MainBatteryPercentage( void ){
int firstlevel, secondlevel;
int i;
   if ( IsEmulator() ){
      i = 100;
      firstlevel = 70;
      secondlevel = 65;
   } else i = GetBatteryStatus( 1, &firstlevel, &secondlevel );
     
   if (firstlevel > 0 ) return ( 200*i )/(firstlevel*3);
   else return 0;
}

//
int F01_1_Handler(){
unsigned int key;
unsigned char x[21];
int currentlevel = 0;
   memset( x, 0, sizeof( x ) );
   while (1){
      currentlevel = MainBatteryPercentage();
      locate(5,3); 
      LongToAsc(currentlevel, (unsigned char*)x, 5 ); 
      Print( (unsigned char*)x );
      Print( "%" );
      GetKey( &key );
      if ( key == KEY_CTRL_EXIT ) break;
   }
   return 1;
}
I'll be back!

Junior Member
Posts: 3
Joined: Mon May 21, 2012 6:20 pm
Location: Netherlands

Postby tom9358 » Tue May 22, 2012 9:09 am

I know about that add-in, it doesn't work, instead it instantly comes up with a system error :(

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Postby SimonLothar » Tue May 22, 2012 9:41 am

tom9358 wrote:I know about that add-in, it doesn't work, instead it instantly comes up with a system error :(
Oh. I did not try it myself, yet. But I will now and have a look to its internals.

Edit:
I tried BATTERY.G1A on a fx-9860G (OS 1.03) and on a fx-9860GII SD (OS 2.01).
I encounter an immediate crash on both machines, too. I think it should be removed from the download area.

BTW: I hope you get along with the snippet I dropped.
I'll be back!

Senior Member
Posts: 369
Joined: Tue Jan 03, 2012 11:24 pm
Calculators: Casio Afx 1.0, Casio fx-9860GII SD, Casio Classpad 330, Casio fx-CG20, Casio Classpad fx-CP400

Postby helder7 » Tue May 22, 2012 1:04 pm

Battery.g1a works in my fx9860gII sd with os 2.X (i dont know now the os version, i have my calc at home).
SiO2 + CaCO3 ----------> CaSiO3 + CO2

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Postby SimonLothar » Tue May 22, 2012 1:51 pm

helder7 wrote:Battery.g1a works in my fx9860gII sd with os 2.X (i dont know now the os version, i have my calc at home).
Yep. I just downgraded my fx-9860GII SD to OS 2.00 and Battery.G1A works as expected.

Hence I had a closer look. In fact, BATTERY.G1A uses a single jump-address 0x8003C484 to get the battery-voltage. This address is valid on fx-9860GII OS 2.00, only! And leads to a crash on any different OS.
I'll be back!

Senior Member
Posts: 68
Joined: Tue May 08, 2012 5:40 pm

Postby happy » Tue May 22, 2012 3:44 pm

Thanks Simon. It works, but hung (had to reset my 9860 - OS 2.00) when I used F01_1_Handler in a timer.

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Postby SimonLothar » Tue May 22, 2012 4:55 pm

happy wrote:...but hung (had to reset my 9860 - OS 2.00) when I used F01_1_Handler in a timer.

What parts of "F01_1_Handler" do you exactly use in your timer-handler?
I'll be back!

Next

Return to Tutorials & Code Snippets

Who is online

Users browsing this forum: No registered users and 16 guests