Gp2x Voltage Tester


Squidge

Certified Guru
Joined
Nov 16, 2003
Messages
8,493
Location
UK
Website
Visit site
I wanted to put a battery monitor into my SNES emu to warn you to save when the batteries start getting low rather than just crashing, so I wrote a simple program that checks the voltage of the batteries. It can be used to check that your adaptor is functioning correctly too - ie, outputing a voltage within specification, so could be useful to some people.

You can get the executable here: http://www.squidgey.plus.com/voltage.gpe

The voltage is constantly checked until you press the START button to exit.

For rechargable batteries, 2.4V and below is considered "empty", 2.5v - 2.6v is considered "medium", and 2.7v+ is considered "full" (same as the battery status in the settings menu).
 
Useful program, could you put up a source .tar.gz or .zip or whatever? It'd be handy just to have a little function to call to check the battery, and would mean more battery checks and stuff in apps, so we wouldn't end up with playing then funny lines ARGH MY GP2X CRASHED.
 
What's "midium"? <_< The correct spelling is medium....
 
Hey Squidge,

First of all GREAT WORK!

To be very honest my one and only reason that i was to buy my GP2X was being able to play Snes roms on the go.
but I love the GP2X for all that it's worth ( No need to explain ).
You've mentioned that you have programmed the voltage meter to be able to see the current status of the batteries / ac addapter, so that you will be able to save before the batteries are drained.

that's get's to to my actual question: are there any options in your current SNES version to save the games? savestates would be great but i am satisfies with the in game safegames as well.
It's a bit hard to play without them.
or am i doing something wrong?
and maybe it would be advisable to have a look at the visuals ( GFX problems ( watersurfaces, rain in zelda and that sort of stuff.

Thanks,

Regards,

Wes
 
Last edited by a moderator:
What's "midium"? <_< The correct spelling is medium....

I think he was taking the piss out of Gamepark B)

Except Gamepark uses 'Midum' actually.

:D LOL, you mean I actually mispelled "midum"? Ha! But yes, if you ever saw the battery meter for the original GPH skin, it is actually mispelled.


squide is true that next version of snes has music???? thankxxxxxx
 
Last edited by a moderator:
What's "midium"? <_< The correct spelling is medium....

I think he was taking the piss out of Gamepark B)

Except Gamepark uses 'Midum' actually.

:D LOL, you mean I actually mispelled "midum"? Ha! But yes, if you ever saw the battery meter for the original GPH skin, it is actually mispelled.


squide is true that next version of snes has music???? thankxxxxxx
Apparently it does.
 
Last edited by a moderator:
Here's the source code to the voltage tester. It's written with Rlyeh's minimal SDK, but should be quite portable to other SDKs.

[ There may be a few too many #includes, I just copy and paste :) ]

Code:
#include <math.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdarg.h>
#include <stropts.h>
#include <time.h>

extern "C"
{
#include "minimal.h"
};

extern "C"
{
	void           gp2x_sound_frame (void *, void *, int)
	{
	}
};

//---------------------------------------------------------------------------

int main(int argc, char **argv)
{
	int devbatt;
	int needexit = 0;
	
	gp2x_init (1000, 16, 44100, 16, 1, 60, 1);
	
	devbatt = open ("/dev/batt", O_RDONLY);
	if (devbatt == -1)
	{
  int i;
  
  gp2x_printf(NULL, 0, 0, "Error occured getting voltage status");
  gp2x_video_RGB_flip(0);  
  
  i = gp2x_timer_read() + 5000;
  
  while (i > gp2x_timer_read());
  return 0;
	}
  
	gp2x_printf(NULL, 0, 0, "Wait...");
	gp2x_printf(NULL, 0, 30, "Press START to exit.");
	gp2x_video_RGB_flip(0);  
	gp2x_printf(NULL, 0, 0, "Wait...");
	gp2x_printf(NULL, 0, 30, "Press START to exit.");
	while (1)
	{
  int i;
  int battval;
  unsigned short cbv;
  int v;
  
  battval = 0;  
  for (i = 0; i < 1000; i ++)
  {
  	if (read (devbatt, &cbv, 2) == 2)
    battval += cbv;
  	if (gp2x_joystick_read() & GP2X_START)
  	{
    needexit = 1;
    break;
  	}
  }
  if (needexit) break;
  
  battval /= 1000;

  // do a rough translation
  if (battval > 1016) v = 37;
  else if (battval > 974) v = 33;
  else if (battval > 943) v = 32;
  else if (battval > 915) v = 31;
  else if (battval > 896) v = 30;
  else if (battval > 837) v = 29;
  else if (battval > 815) v = 28;
  else if (battval > 788) v = 27;
  else if (battval > 745) v = 26;
  else if (battval > 708) v = 25;
  else if (battval > 678) v = 24;
  else if (battval > 649) v = 23;
  else if (battval > 605) v = 22;
  else if (battval > 573) v = 21;
  else if (battval > 534) v = 20;
  else if (battval > 496) v = 19;
  else if (battval > 448) v = 18;
  else v = 17;

  gp2x_printf (NULL, 0, 0, "Voltage: ~%d.%dV (%s)", v/10, v%10, v>26?"Battery Full" : v>24?"Battery Medium" : "Battery Empty");  
  gp2x_video_RGB_flip(0);  	
	}
	
	close (devbatt);
	
	return 0;
}

Have fun :)
 
Grar, just getting used to all this low-level stuff, now I have to put this in the game loop...

Thanks Squidge, you have made my life easier.
 
Wow!!! :ph34r: Great!!! Exactly what I needed...!!!
I've just reported GP2X's misspelled 'midum' thing to GPH...
 
Awesome code, Squidge. o.o With this, we have rivalled yet another of the PSP's one-ups! Telling you the batteries are going to die! I know I'll be using it, too =3. With credit to you, of course.

And personally I LIKED "Please Waiting.". It's just THAT awesome.
 
For rechargable batteries, 2.4V and below is considered "empty", 2.5v - 2.6v is considered "medium", and 2.7v+ is considered "full" (same as the battery status in the settings menu).

I am no electronics wizard but don't rechargeable (I used google to spell that :rolleyes: ) only supply 2.4v max? So wouldn't they read as empty all the time? Or does the GP2X, have idk the name of it transformer, that ups the volts? (the whole changing the amps into volts whatnot)

Please inlighten me.
 
Last edited by a moderator:
Thanks a lot Squidge! I was going to figure out what exactly the output from /dev/batt meant, but you've saved me the trouble!

Edit: Actually, I still don't quite get it.

You can read out an unsigned 16-bit integer which represents the delta in voltage since the last read-out (or some time interval)? Wouldn't an absolute value have been much easier?

And then it needs translation from some random scale?

How did you ever find this out?
 
Edit: Actually, I still don't quite get it.

The way I read his code, this it was it does:

open /dev/batt to read the kernel's information on the battery. If this fails,
print an appropriate message, then wait for 5000 timer ticks and exit. (5 seconds seems a resonable time to me, so probably the 1000 in the init call refers to 1000 ticks per second (never looked at minimal lib)) Squidge, what's wrong with sleep(5)?

Then read a thousand (where thousand is just a convenient 'big number', why not use 1024?) samples of the value given by the linux kernel, then averages by dividing by 1000. This is to smooth things a little, I'd expect the value to drift a little, depending on the AD conversion used.. (As an aside, you could use this as an analog input, just modulate the battery voltage... Would be great if when running on ac adapter, /dev/batt would still show whatever is the voltage on the battery pins. I'll try it out, one of these days, altough it probably won't...)

You can interrupt this by pressing a button and it will break off what it's doing and exit.

The conversion is probably based on a calibration done with a lab-voltage supply. It might differ per device; you'd expect a nice constant factor, like 32 or 24 to convert the kernel supplied value to a number in V..

P.
 
Last edited by a moderator:
Back
Top