|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +#ifdef ESP32 |
| 4 | + #include <Preferences.h> // CRITICAL FOR ESP32 DEVICES! |
| 5 | +#endif |
| 6 | + |
| 7 | +#include <LithiumPowered.hpp> |
| 8 | + |
| 9 | +#define BATTERY_RATED_CAPACITY 900.00 // Set this to the Rated Capacity of your Battery in mAh! |
| 10 | + |
| 11 | +class MyBatteryCallbacks: public BatteryCallbacks { |
| 12 | + public: |
| 13 | + virtual void onBatteryNowCharging() { |
| 14 | + Serial.println("Battery is now Charging!"); |
| 15 | + }; |
| 16 | + |
| 17 | + virtual void onBatteryNowDischarging() { |
| 18 | + Serial.println("Battery is no longer Charging!"); |
| 19 | + }; |
| 20 | + |
| 21 | + virtual void onBatteryRemainingCapacityChanged() { |
| 22 | + Serial.print("Battery Update: "); |
| 23 | + Serial.print(Battery.getPercentage()); |
| 24 | + Serial.print("% "); |
| 25 | + Serial.print(Battery.getCurrentCapacity()); |
| 26 | + Serial.print("mAh remaining. "); |
| 27 | + Serial.print(Battery.getIsCharging() ? "Charging at +" : "Discharging at "); |
| 28 | + Serial.print(Battery.getChangeCapacityWithPolarity()); |
| 29 | + Serial.print("mAh"); |
| 30 | + if (Battery.getIsCharging()) { |
| 31 | + Serial.print(" - Time to full: "); |
| 32 | + Serial.print(Battery.getTimeToChargeInMinutes()); |
| 33 | + } |
| 34 | + else { |
| 35 | + Serial.print(" - Time to empty: "); |
| 36 | + Serial.print(Battery.getTimeToDischargeInMinutes()); |
| 37 | + } |
| 38 | + Serial.println(" min"); |
| 39 | + }; |
| 40 | + |
| 41 | + virtual void onBatteryRecalibrated() { |
| 42 | + Serial.print("Maximum Battery Capacity has been Recalibrated!"); |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +//#define USE_CUSTOM_GPIO // Uncomment this line if you need to use Custom GPIO Pins! |
| 47 | + |
| 48 | +#ifdef USE_CUSTOM_GPIO |
| 49 | + class MyBatteryGPIO: public BatteryGPIO { |
| 50 | + public: |
| 51 | + // Override this to provide the GPIO Pin Number for the Coloumb Counter's Polarity Pin |
| 52 | + virtual uint8_t getPinPolarity() { |
| 53 | + return 4; |
| 54 | + } |
| 55 | + |
| 56 | + // Override this to provide the GPIO Pin Number for the Coloumb Counter's Interrupt Pin |
| 57 | + virtual uint8_t getPinInterrupt() { |
| 58 | + return 35; |
| 59 | + } |
| 60 | + |
| 61 | + // Override this to provide the GPIO Pin Number for the Coloumb Counter's High State Reference Pin |
| 62 | + virtual uint8_t getPinRefHigh() { |
| 63 | + return 23; |
| 64 | + } |
| 65 | + |
| 66 | + // Override this to provide the GPIO Pin Number for the Coloumb Counter's Low State Reference Pin |
| 67 | + virtual uint8_t getPinRefLow() { |
| 68 | + return 5; |
| 69 | + } |
| 70 | + }; |
| 71 | +#endif |
| 72 | + |
| 73 | +void setup() { |
| 74 | + Serial.begin(115200); // Initialise Serial interface at 115200 Baud |
| 75 | + |
| 76 | + #ifdef USE_CUSTOM_GPIO |
| 77 | + Battery.setGpio(new MyBatteryGPIO()); // We want to use specific GPIO Pins |
| 78 | + #endif |
| 79 | + Battery.setCallbacks(new MyBatteryCallbacks()); // Use our custom Callbacks for Battery Events |
| 80 | + Battery.setup(BATTERY_RATED_CAPACITY); // Initialise the LithiumPowered Battery System |
| 81 | +} |
| 82 | + |
| 83 | +void loop() { |
| 84 | + Battery.loop(); // Required to perform State Updates for the LithiumPowered Battery System. |
| 85 | +} |
0 commit comments