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.

119 lines
2.7 KiB

8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file TinyGsmCommon.h
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef SRC_TINYGSMCOMMON_H_
  9. #define SRC_TINYGSMCOMMON_H_
  10. // The current library version number
  11. #define TINYGSM_VERSION "0.11.6"
  12. #if defined(SPARK) || defined(PARTICLE)
  13. #include "Particle.h"
  14. #elif defined(ARDUINO)
  15. #if ARDUINO >= 100
  16. #include "Arduino.h"
  17. #else
  18. #include "WProgram.h"
  19. #endif
  20. #endif
  21. #if defined(ARDUINO_DASH)
  22. #include <ArduinoCompat/Client.h>
  23. #else
  24. #include <Client.h>
  25. #endif
  26. #ifndef TINY_GSM_YIELD_MS
  27. #define TINY_GSM_YIELD_MS 0
  28. #endif
  29. #ifndef TINY_GSM_YIELD
  30. #define TINY_GSM_YIELD() \
  31. { delay(TINY_GSM_YIELD_MS); }
  32. #endif
  33. #define TINY_GSM_ATTR_NOT_AVAILABLE \
  34. __attribute__((error("Not available on this modem type")))
  35. #define TINY_GSM_ATTR_NOT_IMPLEMENTED __attribute__((error("Not implemented")))
  36. #if defined(__AVR__) && !defined(__AVR_ATmega4809__)
  37. #define TINY_GSM_PROGMEM PROGMEM
  38. typedef const __FlashStringHelper* GsmConstStr;
  39. #define GFP(x) (reinterpret_cast<GsmConstStr>(x))
  40. #define GF(x) F(x)
  41. #else
  42. #define TINY_GSM_PROGMEM
  43. typedef const char* GsmConstStr;
  44. #define GFP(x) x
  45. #define GF(x) x
  46. #endif
  47. #ifdef TINY_GSM_DEBUG
  48. namespace {
  49. template <typename T>
  50. static void DBG_PLAIN(T last) {
  51. TINY_GSM_DEBUG.println(last);
  52. }
  53. template <typename T, typename... Args>
  54. static void DBG_PLAIN(T head, Args... tail) {
  55. TINY_GSM_DEBUG.print(head);
  56. TINY_GSM_DEBUG.print(' ');
  57. DBG_PLAIN(tail...);
  58. }
  59. template <typename... Args>
  60. static void DBG(Args... args) {
  61. TINY_GSM_DEBUG.print(GF("["));
  62. TINY_GSM_DEBUG.print(millis());
  63. TINY_GSM_DEBUG.print(GF("] "));
  64. DBG_PLAIN(args...);
  65. }
  66. } // namespace
  67. #else
  68. #define DBG_PLAIN(...)
  69. #define DBG(...)
  70. #endif
  71. template <class T>
  72. const T& TinyGsmMin(const T& a, const T& b) {
  73. return (b < a) ? b : a;
  74. }
  75. template <class T>
  76. const T& TinyGsmMax(const T& a, const T& b) {
  77. return (b < a) ? a : b;
  78. }
  79. template <class T>
  80. uint32_t TinyGsmAutoBaud(T& SerialAT, uint32_t minimum = 9600,
  81. uint32_t maximum = 115200) {
  82. static uint32_t rates[] = {115200, 57600, 38400, 19200, 9600, 74400, 74880,
  83. 230400, 460800, 2400, 4800, 14400, 28800};
  84. for (uint8_t i = 0; i < sizeof(rates) / sizeof(rates[0]); i++) {
  85. uint32_t rate = rates[i];
  86. if (rate < minimum || rate > maximum) continue;
  87. DBG("Trying baud rate", rate, "...");
  88. SerialAT.begin(rate);
  89. delay(10);
  90. for (int j = 0; j < 10; j++) {
  91. SerialAT.print("AT\r\n");
  92. String input = SerialAT.readString();
  93. if (input.indexOf("OK") >= 0) {
  94. DBG("Modem responded at rate", rate);
  95. return rate;
  96. }
  97. }
  98. }
  99. SerialAT.begin(minimum);
  100. return 0;
  101. }
  102. #endif // SRC_TINYGSMCOMMON_H_