Fix possible buffer overflow when at 100% battery capacity
Refactor battery code and add battery state (C = Charging; D = Discharging)