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.

156 linhas
3.0 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. VEBOSE("gpsPowerOn");
  51. powerOn();
  52. device.enableGps();
  53. }
  54. void gpsPowerOff() {
  55. if (!device.powered()) {
  56. networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0
  57. return;
  58. }
  59. if(gpsPoweredCount > 1) {
  60. gpsPoweredCount--;
  61. return;
  62. }
  63. VERBOSE("gpsPowerOff");
  64. device.disableGps();
  65. powerOffIfUnused();
  66. }
  67. void networkPowerOn() {
  68. if(networkPoweredCount) {
  69. networkPoweredCount++;
  70. return;
  71. }
  72. VERBOSE("networkPowerOn");
  73. powerOn();
  74. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL);
  75. }
  76. void networkPowerOff() {
  77. if (!device.powered()) {
  78. networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0
  79. return;
  80. }
  81. if(networkPoweredCount > 1) {
  82. networkPoweredCount--;
  83. return;
  84. }
  85. VERBOSE("networkPowerOff");
  86. device.disableGprs();
  87. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM);
  88. powerOffIfUnused();
  89. }
  90. }
  91. #define LOGGER_NAME "Hardware::i2c"
  92. namespace i2c {
  93. E24 eeprom = E24(E24Size_t::E24_512K);
  94. uint8_t poweredCount = 0;
  95. void powerOn() {
  96. if(poweredCount) {
  97. poweredCount++;
  98. return;
  99. }
  100. VERBOSE("powerOn");
  101. digitalWrite(I2C_PWR, HIGH);
  102. pinMode(I2C_PWR, OUTPUT);
  103. Wire.begin();
  104. poweredCount = 1;
  105. }
  106. void powerOff(bool forced = false) {
  107. if(poweredCount > 1 && !forced) {
  108. poweredCount--;
  109. return;
  110. }
  111. VERBOSE("powerOff");
  112. pinMode(I2C_PWR, INPUT);
  113. digitalWrite(I2C_PWR, LOW);
  114. //turn off i2c
  115. TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA));
  116. //disable i2c internal pull ups
  117. digitalWrite(A4, LOW);
  118. digitalWrite(A5, LOW);
  119. poweredCount = 0;
  120. }
  121. }
  122. }