您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

147 行
2.9 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. bool gpsPowered = false;
  31. if ((!device.getGpsPowerState(&gpsPowered) || !gpsPowered) &&
  32. (device.getPhoneFunctionality() != SIM808_PHONE_FUNCTIONALITY::FULL)) {
  33. powerOff();
  34. }
  35. }
  36. void setup() {
  37. NOTICE("setup");
  38. simSerial.begin(SIM808_BAUDRATE);
  39. device.begin(simSerial);
  40. powerOff(); //ensure powerOff on start
  41. }
  42. void gpsPowerOn() {
  43. if (!gpsPoweredCount) {
  44. VERBOSE("gpsPowerOn");
  45. powerOn();
  46. device.enableGps();
  47. }
  48. gpsPoweredCount++;
  49. }
  50. void gpsPowerOff() {
  51. if (!device.powered()) {
  52. networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0
  53. return;
  54. }
  55. if (gpsPoweredCount == 1) {
  56. VERBOSE("gpsPowerOff");
  57. device.disableGps();
  58. powerOffIfUnused();
  59. }
  60. if (gpsPoweredCount) gpsPoweredCount--; //avoid 255 if 0--
  61. }
  62. void networkPowerOn() {
  63. if (!networkPoweredCount) {
  64. VERBOSE("networkPowerOn");
  65. powerOn();
  66. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL);
  67. }
  68. networkPoweredCount++;
  69. }
  70. void networkPowerOff() {
  71. if (!device.powered()) {
  72. networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0
  73. return;
  74. }
  75. if (networkPoweredCount == 1) {
  76. VERBOSE("networkPowerOff");
  77. device.disableGprs();
  78. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM);
  79. powerOffIfUnused();
  80. }
  81. if (networkPoweredCount) networkPoweredCount--; //avoid 255 if 0--
  82. }
  83. }
  84. #define LOGGER_NAME "Hardware::i2c"
  85. namespace i2c {
  86. E24 eeprom = E24(E24Size_t::E24_512K);
  87. uint8_t poweredCount = 0;
  88. void powerOn() {
  89. if (!poweredCount) {
  90. VERBOSE("powerOn");
  91. digitalWrite(I2C_PWR, HIGH);
  92. pinMode(I2C_PWR, OUTPUT);
  93. Wire.begin();
  94. poweredCount = 1;
  95. }
  96. else poweredCount++;
  97. }
  98. void powerOff(bool forced = false) {
  99. if (poweredCount == 1 || forced) {
  100. VERBOSE("powerOff");
  101. pinMode(I2C_PWR, INPUT);
  102. digitalWrite(I2C_PWR, LOW);
  103. //turn off i2c
  104. TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA));
  105. //disable i2c internal pull ups
  106. digitalWrite(A4, LOW);
  107. digitalWrite(A5, LOW);
  108. poweredCount = 0;
  109. }
  110. else if(poweredCount > 1) poweredCount--; //avoid decrement if == 0
  111. }
  112. }
  113. }