You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.7 KiB

  1. /**
  2. * @file TinyGsmBattery.tpp
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef SRC_TINYGSMBATTERY_H_
  9. #define SRC_TINYGSMBATTERY_H_
  10. #include "TinyGsmCommon.h"
  11. #define TINY_GSM_MODEM_HAS_BATTERY
  12. template <class modemType>
  13. class TinyGsmBattery {
  14. public:
  15. /*
  16. * Battery functions
  17. */
  18. uint16_t getBattVoltage() {
  19. return thisModem().getBattVoltageImpl();
  20. }
  21. int8_t getBattPercent() {
  22. return thisModem().getBattPercentImpl();
  23. }
  24. uint8_t getBattChargeState() {
  25. return thisModem().getBattChargeStateImpl();
  26. }
  27. bool getBattStats(uint8_t& chargeState, int8_t& percent,
  28. uint16_t& milliVolts) {
  29. return thisModem().getBattStatsImpl(chargeState, percent, milliVolts);
  30. }
  31. /*
  32. * CRTP Helper
  33. */
  34. protected:
  35. inline const modemType& thisModem() const {
  36. return static_cast<const modemType&>(*this);
  37. }
  38. inline modemType& thisModem() {
  39. return static_cast<modemType&>(*this);
  40. }
  41. /*
  42. * Battery functions
  43. */
  44. protected:
  45. // Use: float vBatt = modem.getBattVoltage() / 1000.0;
  46. uint16_t getBattVoltageImpl() {
  47. thisModem().sendAT(GF("+CBC"));
  48. if (thisModem().waitResponse(GF("+CBC:")) != 1) { return 0; }
  49. thisModem().streamSkipUntil(','); // Skip battery charge status
  50. thisModem().streamSkipUntil(','); // Skip battery charge level
  51. // return voltage in mV
  52. uint16_t res = thisModem().streamGetIntBefore('\n');
  53. // Wait for final OK
  54. thisModem().waitResponse();
  55. return res;
  56. }
  57. int8_t getBattPercentImpl() {
  58. thisModem().sendAT(GF("+CBC"));
  59. if (thisModem().waitResponse(GF("+CBC:")) != 1) { return false; }
  60. thisModem().streamSkipUntil(','); // Skip battery charge status
  61. // Read battery charge level
  62. int8_t res = thisModem().streamGetIntBefore(',');
  63. // Wait for final OK
  64. thisModem().waitResponse();
  65. return res;
  66. }
  67. uint8_t getBattChargeStateImpl() {
  68. thisModem().sendAT(GF("+CBC"));
  69. if (thisModem().waitResponse(GF("+CBC:")) != 1) { return false; }
  70. // Read battery charge status
  71. int8_t res = thisModem().streamGetIntBefore(',');
  72. // Wait for final OK
  73. thisModem().waitResponse();
  74. return res;
  75. }
  76. bool getBattStatsImpl(uint8_t& chargeState, int8_t& percent,
  77. uint16_t& milliVolts) {
  78. thisModem().sendAT(GF("+CBC"));
  79. if (thisModem().waitResponse(GF("+CBC:")) != 1) { return false; }
  80. chargeState = thisModem().streamGetIntBefore(',');
  81. percent = thisModem().streamGetIntBefore(',');
  82. milliVolts = thisModem().streamGetIntBefore('\n');
  83. // Wait for final OK
  84. thisModem().waitResponse();
  85. return true;
  86. }
  87. };
  88. #endif // SRC_TINYGSMBATTERY_H_