How is the battery level calculated in the magic mouse? I wonder how the battery level in the magic mouse is calculated.

This question about battery problems raised another one, I would like to know how the electronics in the magic mouse work and evaluate the remaining energy left as it seems quite inaccurate with some of my batteries.

If you don't know how the magic mouse does its calculation maybe you know how other electronic devices calculate the level?

I am no electrician and I have no skills in e- engineering. But maybe this image from iFixit might give a clue to a someone with the right knowledge (click the image for the original high-res version):



High res pictures available here if you need more details.
Generally the amount of energy left in a cell is related to its voltage at a given current draw, and the chemistry.  For instance, AA Alkaline cells are about 1.62V new, and drop to 0.9V near the end of their life as they are depleted.  Rechargeable cells often output 1.29V fully charged and will discharge to 0.9V.

Further, the voltage drops more with larger current draws, and this drop increases for cells that are near the end of their life.  So if you turn off the mouse and test the voltage, then turn the mouse on and test for voltage, the second test will have a lower voltage.  If that difference is small, the cell is near the beginning of its life.  If the difference is large, the cell is near the end of its life.

There are ways to determine the battery chemistry with some level of confidence by performing a few tests, but generally there's no reason to.  The majority of users merely need to have a little advance notice that their favorite iDevice is about to cease functioning so they can prepare.

So most such devices simply wait for the battery to drop below a specified voltage, before alerting the user.  Since most batteries can be considered dead at 0.9 volts, and few batteries go lower than 1.2 volts when new, the simplest solution is to alert the user to a low battery condition at 1.0 or 1.1 volts.

When a user expects a battery bar that gives a relative measurement (3 boxes for full, 0 boxes for nearly dead) then it will generally show full until it gets below 1.2 volts.

So an alkaline battery will often show full until very close to the end of its life, then appear to die quickly.  A rechargeable battery will show 3/4 full for most of its life, then die rapidly (due to the discharge curve being more flat than an alkaline, and being closer to the 1.0v cutoff).

The batteries you will have the most problem with are "heavy duty" which is code for "cheap, non-alkaline" batteries.  They have their uses, but not in electronic devices.  If you use the cheapest of the cheap AA batteries from the drugstore, or the "40 batteries for $6.99" pack from woot, amazon, or [insert favorite membership club here] then you're very likely getting non-alkaline, "heavy duty" batteries.  These cells have a very different, and much faster, discharge curve when used in most electronics, and you will see flakiness long before the battery is actually dead, and perhaps even before the mouse itself recognizes it's dead.  The issue is that they can't supply the sudden bursts of power the radio in the mouse requires, so some transmissions are lost.  When it's not transmitting, the voltage appears to be fine, but the difference between no load and an RF load causes the battery voltage to drop significantly simply because it wasn't designed to support those power draws.

This is why almost all electronic devices insist on "name brand alkaline" batteries.  Rechargeable batteries work well for this type of use as well.

This article, "Does the battery fuel gauge lie?" is relevant to this discussion, though it mostly focuses on larger rechargeable batteries used in the iPad and similar devices.  It discuses how smart battery electronics monitor and manage the charge and discharge of batteries in advanced electronic devices.