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: 68
Joined: Tue May 08, 2012 5:40 pm

Postby happy » Tue May 22, 2012 5:15 pm

Code: Select all
int y = 0;

void F01_1_Handler()
   {
   char s[4];
   memset(s, '\0', 4);
   sprintf(s, "%d", MainBatteryPercentage());
   PrintXY(78, y, (const unsigned char *) "    ", 0);
   PrintXY(78, y, (const unsigned char *) s, 0);
     Bdisp_PutDisp_DD();
   y++;
   }
   
int AddIn_main(int isAppli, unsigned short OptionNum)
   {
   unsigned int key;
   Bdisp_AllClr_DDVRAM();
   SetTimer(ID_USER_TIMER1, 100, (void *) F01_1_Handler);
   while (1)
      {
      F01_1_Handler();
      GetKey(&key);
      }
   return 1;
   }



Changed F01_1_Handler a bit and added a global variable. Doesn't hang when SetTimer is commented out.

Senior Member
User avatar
Posts: 603
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750 G II, Casio fx-9750 G II (SH4), Casio fx-9860G, Casio fx-9860 G SD, Casio fx-9860G Slim, Casio fx-9860 GII SD, Casio fx-9860 GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG 20, Casio Classpad fx-cp400, Casio fx-CG 50

Postby SimonLothar » Tue May 22, 2012 6:03 pm

If F01_1_Handler() (called from out of the main-loop) is interrupted by the timer, F01_1_Handler() would be called while its already running. The outcome of such a situation is difficult to predict. The first thing I would implement is some prevention (busy).
source: Show
Code: Select all
int y = 0;
int busy = 0;

void F01_1_Handler()
   {
   char s[4];
        if ( busy ) return;
        busy = 1;
   memset(s, '\0', 4);
   sprintf(s, "%d", MainBatteryPercentage());
   PrintXY(78, y, (const unsigned char *) "    ", 0);
   PrintXY(78, y, (const unsigned char *) s, 0);
     Bdisp_PutDisp_DD();
   y++;
        busy = 0;
   }
   
int AddIn_main(int isAppli, unsigned short OptionNum)
   {
   unsigned int key;
   Bdisp_AllClr_DDVRAM();
   SetTimer(ID_USER_TIMER1, 100, (void *) F01_1_Handler);
   while (1)
      {
      F01_1_Handler();
      GetKey(&key);
      }
   return 1;
   }
I'll be back!

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

Postby happy » Tue May 22, 2012 6:12 pm

Understood the design paradigm, thanks. Copy/pasted the code you posted, but still no luck. Hangs. Also tried by not calling F01_1_Handler from AddIn_main (but setting it in SetTimer, obviously).

Is it working for you?

Senior Member
User avatar
Posts: 603
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750 G II, Casio fx-9750 G II (SH4), Casio fx-9860G, Casio fx-9860 G SD, Casio fx-9860G Slim, Casio fx-9860 GII SD, Casio fx-9860 GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG 20, Casio Classpad fx-cp400, Casio fx-CG 50

Postby SimonLothar » Tue May 22, 2012 7:38 pm

happy wrote:Is it working for you?
Nope. I had a closer look again. The AD-conversion is interrupt driven, too. If it is called from inside of an interrupt handler, the interrupt system gets confused.

Alternatively you could use PutKey in your interrupt-handler to send a special keycode every now and then, which tells your main program to display the battery level.
The timer-handler could look like this
Code: Select all
//
void TestHandler2( void ){
   PutKey( 0x1234, 0 );
}

// Now you have to react to keycode 0x1234 accordingly.
...
   while (1){
      GetKey(&key);
      if ( key == KEY_CTRL_EXIT ) break;
      if ( key == 0x1234 ) TestHandler();
   }


This worked on my fx-9860GII.
I'll be back!

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

Postby happy » Tue May 22, 2012 8:04 pm

Sorry... I don't get it (:

(1) What is PutKey?

(2) So TestHandler2 is the timer handler (set in SetTimer) whose only job is to generate a key event (via PutKey?) which when caught, I call MainBatteryPercentage()..? How is this different from calling MainBatteryPercentage() from the timer handler directly?

Senior Member
User avatar
Posts: 603
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750 G II, Casio fx-9750 G II (SH4), Casio fx-9860G, Casio fx-9860 G SD, Casio fx-9860G Slim, Casio fx-9860 GII SD, Casio fx-9860 GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG 20, Casio Classpad fx-cp400, Casio fx-CG 50

Postby SimonLothar » Tue May 22, 2012 9:38 pm

happy wrote:(1) What is PutKey?

PutKey is syscall 0x910; it simulates a keypress (and ends the blocking state of GetKey).

happy wrote:(2) So TestHandler2 is the timer handler (set in SetTimer) whose only job is to generate a key event (via PutKey?) which when caught, I call MainBatteryPercentage()..? How is this different from calling MainBatteryPercentage() from the timer handler directly?

If a certain interrupt is handled (in this case the timer-interrupt), other interrupts may be masked off, t. i. they cannot interrupt the current interrupt-service-routine. Obviously the ad-conversion-interrupt cannot interrupt the timer-interrupt, so the battery-voltage-function waits for the ad-conversion-interrupt endlessly.
The difference is, that if you call the battery-voltage-function from the main-loop, the timer -interrupt-handler is not running any more and does not disturb the ad-conversion.
I'll be back!

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

Postby happy » Tue May 22, 2012 9:57 pm

Understood. Thank you!

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

Re:

Postby happy » Sun Sep 23, 2012 5:44 pm

SimonLothar wrote:BTW:
on the Prizm the BatteryVoltage can be read with
syscall 0x1186: int GetMainBatteryVoltage( int one );
The parameter one should be always 1.


In GII-2, MainBatteryPercentage() doesn't work, gives the wrong value. Tried changing return (*iGBS)(battery) to return GetMainBatteryVoltage(1) / 100 in GetBatteryStatus(), but that gives me a memory error. What am I doing wrong?

(Assumption here is that GII-2 architecture is very similar to Prizm and that most syscalls would work. Is that true?)

Senior Member
User avatar
Posts: 603
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750 G II, Casio fx-9750 G II (SH4), Casio fx-9860G, Casio fx-9860 G SD, Casio fx-9860G Slim, Casio fx-9860 GII SD, Casio fx-9860 GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG 20, Casio Classpad fx-cp400, Casio fx-CG 50

Re: Re:

Postby SimonLothar » Sun Sep 23, 2012 6:04 pm

happy wrote:(Assumption here is that GII-2 architecture is very similar to Prizm and that most syscalls would work. Is that true?)
No, alas not. The GII-2 calculators have the same MPU like the Prizm (SH-7305), but they have the fx-9860 syscall-numbers.
I'll be back!

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

Re: Re:

Postby happy » Mon Sep 24, 2012 10:57 am

SimonLothar wrote:No, alas not. The GII-2 calculators have the same MPU like the Prizm (SH-7305), but they have the fx-9860 syscall-numbers.

Thanks!

happy wrote:In GII-2, MainBatteryPercentage() doesn't work, gives the wrong value.

Any way to make this work..?

PreviousNext

Return to Tutorials & Code Snippets

Who is online

Users browsing this forum: No registered users and 2 guests