Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

160 linhas
3.2 KiB

  1. #include "Config.h"
  2. #include "Hardware.h"
  3. #include "Pins.h"
  4. #include "Debug.h"
  5. #include <SoftwareSerial.h>
  6. #include <SIM808.h>
  7. #include <SIM808_Types.h>
  8. #include <Wire.h>
  9. #include <E24.h>
  10. namespace hardware {
  11. #define LOGGER_NAME "Hardware::sim808"
  12. namespace sim808 {
  13. SoftwareSerial simSerial = SoftwareSerial(SIM_TX, SIM_RX);
  14. SIM808 device = SIM808(SIM_RST, SIM_PWR, SIM_STATUS);
  15. uint8_t networkPoweredCount = 0;
  16. uint8_t gpsPoweredCount = 0;
  17. void powerOn() {
  18. VERBOSE("powerOn");
  19. bool poweredOn = device.powerOnOff(true);
  20. if (!poweredOn) return;
  21. device.init();
  22. networkPoweredCount = gpsPoweredCount = 0;
  23. }
  24. void powerOff() {
  25. VERBOSE("powerOff");
  26. device.powerOnOff(false);
  27. networkPoweredCount = gpsPoweredCount = 0;
  28. }
  29. void powerOffIfUnused() {
  30. //does not rely on count for safety
  31. //if there is a bug somewhere, the device will consume more battery,
  32. //but will not fail due to an over aggressive battery saving strategy
  33. bool gpsPowered = false;
  34. if ((!device.getGpsPowerState(&gpsPowered) || !gpsPowered) &&
  35. (device.getPhoneFunctionality() != SIM808_PHONE_FUNCTIONALITY::FULL)) {
  36. powerOff();
  37. }
  38. }
  39. void setup() {
  40. NOTICE("setup");
  41. simSerial.begin(SIM808_BAUDRATE);
  42. device.begin(simSerial);
  43. powerOff(); //ensure powerOff on start
  44. }
  45. void gpsPowerOn() {
  46. if(gpsPoweredCount) {
  47. gpsPoweredCount++;
  48. return;
  49. }
  50. VERBOSE("gpsPowerOn");
  51. powerOn();
  52. if(!networkPoweredCount) {
  53. //SIM808 turns phone on by default but we don't need it for gps only
  54. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM);
  55. }
  56. device.enableGps();
  57. }
  58. void gpsPowerOff() {
  59. if (!device.powered()) {
  60. networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0
  61. return;
  62. }
  63. if(gpsPoweredCount > 1) {
  64. gpsPoweredCount--;
  65. return;
  66. }
  67. VERBOSE("gpsPowerOff");
  68. device.disableGps();
  69. powerOffIfUnused();
  70. }
  71. void networkPowerOn() {
  72. if(networkPoweredCount) {
  73. networkPoweredCount++;
  74. return;
  75. }
  76. VERBOSE("networkPowerOn");
  77. powerOn();
  78. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL);
  79. }
  80. void networkPowerOff() {
  81. if (!device.powered()) {
  82. networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0
  83. return;
  84. }
  85. if(networkPoweredCount > 1) {
  86. networkPoweredCount--;
  87. return;
  88. }
  89. VERBOSE("networkPowerOff");
  90. device.disableGprs();
  91. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM);
  92. powerOffIfUnused();
  93. }
  94. }
  95. #define LOGGER_NAME "Hardware::i2c"
  96. namespace i2c {
  97. E24 eeprom = E24(E24Size_t::E24_512K);
  98. uint8_t poweredCount = 0;
  99. void powerOn() {
  100. if(poweredCount) {
  101. poweredCount++;
  102. return;
  103. }
  104. VERBOSE("powerOn");
  105. digitalWrite(I2C_PWR, HIGH);
  106. pinMode(I2C_PWR, OUTPUT);
  107. Wire.begin();
  108. poweredCount = 1;
  109. }
  110. void powerOff(bool forced = false) {
  111. if(poweredCount > 1 && !forced) {
  112. poweredCount--;
  113. return;
  114. }
  115. VERBOSE("powerOff");
  116. pinMode(I2C_PWR, INPUT);
  117. digitalWrite(I2C_PWR, LOW);
  118. //turn off i2c
  119. TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA));
  120. //disable i2c internal pull ups
  121. digitalWrite(A4, LOW);
  122. digitalWrite(A5, LOW);
  123. poweredCount = 0;
  124. }
  125. }
  126. }