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.

117 line
2.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. void powerOn() {
  16. VERBOSE("powerOn");
  17. bool poweredOn = device.powerOnOff(true);
  18. if (!poweredOn) return;
  19. device.init();
  20. }
  21. void powerOff() {
  22. VERBOSE("powerOff");
  23. device.powerOnOff(false);
  24. }
  25. void powerOffIfUnused() {
  26. bool gpsPowered = false;
  27. bool gprsPowered = false;
  28. if ((!device.getGpsPowerState(&gpsPowered) || !gpsPowered) &&
  29. (!device.getGprsPowerState(&gprsPowered) || !gprsPowered)) {
  30. powerOff();
  31. }
  32. }
  33. void setup() {
  34. VERBOSE("setup");
  35. simSerial.begin(4800);
  36. device.begin(simSerial);
  37. }
  38. void gpsPowerOn() {
  39. VERBOSE("gpsPowerOn");
  40. powerOn();
  41. device.enableGps();
  42. }
  43. void gpsPowerOff() {
  44. VERBOSE("gpsPowerOff");
  45. device.disableGps();
  46. powerOffIfUnused();
  47. }
  48. void networkPowerOn() {
  49. VERBOSE("networkPowerOn");
  50. powerOn();
  51. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL);
  52. }
  53. void networkPowerOff() {
  54. VERBOSE("networkPowerOff");
  55. device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM);
  56. device.disableGprs();
  57. powerOffIfUnused();
  58. }
  59. }
  60. #define LOGGER_NAME "Hardware::i2c"
  61. namespace i2c {
  62. E24 eeprom = E24(E24Size_t::E24_512K);
  63. uint8_t poweredCount = 0;
  64. //inline void powered() { digitalRead(I2C_PWR) == HIGH; } //TODO = replace enum with just reading the output pin ?
  65. void powerOn() {
  66. if (!poweredCount) {
  67. VERBOSE("powerOn");
  68. digitalWrite(I2C_PWR, HIGH);
  69. pinMode(I2C_PWR, OUTPUT);
  70. Wire.begin();
  71. }
  72. poweredCount++;
  73. }
  74. void powerOff(bool forced = false) {
  75. if (poweredCount == 1 || forced) {
  76. VERBOSE("powerOff");
  77. pinMode(I2C_PWR, INPUT);
  78. digitalWrite(I2C_PWR, LOW);
  79. //turn off i2c
  80. TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA));
  81. //disable i2c internal pull ups
  82. digitalWrite(A4, LOW);
  83. digitalWrite(A5, LOW);
  84. }
  85. poweredCount--;
  86. }
  87. }
  88. }