BatteryInterface.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "BatteryInterface.h"
  2. #include "lang_var.h"
  3. BatteryInterface::BatteryInterface() {
  4. }
  5. void BatteryInterface::main(uint32_t currentTime) {
  6. #ifndef MARAUDER_FLIPPER
  7. if (currentTime != 0) {
  8. if (currentTime - initTime >= 3000) {
  9. //Serial.println("Checking Battery Level");
  10. this->initTime = millis();
  11. int8_t new_level = this->getBatteryLevel();
  12. //this->battery_level = this->getBatteryLevel();
  13. if (this->battery_level != new_level) {
  14. Serial.println(text00 + (String)new_level);
  15. this->battery_level = new_level;
  16. }
  17. }
  18. }
  19. #endif
  20. }
  21. void BatteryInterface::RunSetup() {
  22. #ifndef MARAUDER_FLIPPER
  23. Wire.begin(I2C_SDA, I2C_SCL);
  24. this->initTime = millis();
  25. #endif
  26. }
  27. int8_t BatteryInterface::getBatteryLevel() {
  28. #ifndef MARAUDER_FLIPPER
  29. Wire.beginTransmission(IP5306_ADDR);
  30. Wire.write(0x78);
  31. if (Wire.endTransmission(false) == 0 &&
  32. Wire.requestFrom(0x75, 1)) {
  33. this->i2c_supported = true;
  34. switch (Wire.read() & 0xF0) {
  35. case 0xE0: return 25;
  36. case 0xC0: return 50;
  37. case 0x80: return 75;
  38. case 0x00: return 100;
  39. default: return 0;
  40. }
  41. }
  42. this->i2c_supported = false;
  43. return -1;
  44. #endif
  45. }