Root/
| 1 | #include "battery.h" |
| 2 | |
| 3 | #include "surfacecollection.h" |
| 4 | |
| 5 | #include <SDL.h> |
| 6 | #include <cstdio> |
| 7 | #include <sstream> |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * Reads the current battery state and returns a number representing its level |
| 12 | * of charge. |
| 13 | * @return A number representing battery charge: 0 means fully discharged, |
| 14 | * 5 means fully charged, 6 represents running on external power. |
| 15 | */ |
| 16 | static unsigned short getBatteryLevel() |
| 17 | { |
| 18 | FILE *batteryHandle = NULL, *usbHandle = NULL; |
| 19 | |
| 20 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) |
| 21 | usbHandle = fopen("/sys/class/power_supply/usb/online", "r"); |
| 22 | #endif |
| 23 | if (usbHandle) { |
| 24 | int usbval = 0; |
| 25 | fscanf(usbHandle, "%d", &usbval); |
| 26 | fclose(usbHandle); |
| 27 | if (usbval == 1) |
| 28 | return 6; |
| 29 | } |
| 30 | |
| 31 | #if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) |
| 32 | batteryHandle = fopen("/sys/class/power_supply/battery/capacity", "r"); |
| 33 | #endif |
| 34 | if (batteryHandle) { |
| 35 | int battval = 0; |
| 36 | fscanf(batteryHandle, "%d", &battval); |
| 37 | fclose(batteryHandle); |
| 38 | |
| 39 | if (battval>90) return 5; |
| 40 | if (battval>70) return 4; |
| 41 | if (battval>50) return 3; |
| 42 | if (battval>30) return 2; |
| 43 | if (battval>10) return 1; |
| 44 | } |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | Battery::Battery(SurfaceCollection& sc_) |
| 50 | : sc(sc_) |
| 51 | { |
| 52 | lastUpdate = SDL_GetTicks(); |
| 53 | update(); |
| 54 | } |
| 55 | |
| 56 | const OffscreenSurface *Battery::getIcon() |
| 57 | { |
| 58 | // Check battery status every 60 seconds. |
| 59 | unsigned int now = SDL_GetTicks(); |
| 60 | if (now - lastUpdate >= 60000) { |
| 61 | lastUpdate = now; |
| 62 | update(); |
| 63 | } |
| 64 | |
| 65 | return sc.skinRes(iconPath); |
| 66 | } |
| 67 | |
| 68 | void Battery::update() |
| 69 | { |
| 70 | unsigned short battlevel = getBatteryLevel(); |
| 71 | if (battlevel > 5) { |
| 72 | iconPath = "imgs/battery/ac.png"; |
| 73 | } else { |
| 74 | std::stringstream ss; |
| 75 | ss << "imgs/battery/" << battlevel << ".png"; |
| 76 | ss >> iconPath; |
| 77 | } |
| 78 | } |
| 79 |
Branches:
install_locations
master
opkrun
packages
