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.

97 lines
2.1 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 TinyGsmCommon_h
  9. #define TinyGsmCommon_h
  10. #if defined(SPARK) || defined(PARTICLE)
  11. #include "Particle.h"
  12. #elif defined(ARDUINO)
  13. #if ARDUINO >= 100
  14. #include "Arduino.h"
  15. #else
  16. #include "WProgram.h"
  17. #endif
  18. #endif
  19. #include <Client.h>
  20. #include <TinyGsmFifo.h>
  21. #ifndef TINY_GSM_YIELD
  22. #define TINY_GSM_YIELD() { delay(0); }
  23. #endif
  24. #define TINY_GSM_ATTR_NOT_AVAILABLE __attribute__((error("Not available on this modem type")))
  25. #define TINY_GSM_ATTR_NOT_IMPLEMENTED __attribute__((error("Not implemented")))
  26. #if defined(__AVR__)
  27. #define TINY_GSM_PROGMEM PROGMEM
  28. typedef const __FlashStringHelper* GsmConstStr;
  29. #define GFP(x) (reinterpret_cast<GsmConstStr>(x))
  30. #define GF(x) F(x)
  31. #else
  32. #define TINY_GSM_PROGMEM
  33. typedef const char* GsmConstStr;
  34. #define GFP(x) x
  35. #define GF(x) x
  36. #endif
  37. #ifdef TINY_GSM_DEBUG
  38. namespace {
  39. template<typename T>
  40. static void DBG(T last) {
  41. // TINY_GSM_DEBUG.println(last);
  42. TINY_GSM_DEBUG.print(last);
  43. }
  44. template<typename T, typename... Args>
  45. static void DBG(T head, Args... tail) {
  46. TINY_GSM_DEBUG.print(head);
  47. // TINY_GSM_DEBUG.print(' ');
  48. DBG(tail...);
  49. }
  50. }
  51. #else
  52. #define DBG(...)
  53. #endif
  54. template<class T>
  55. const T& TinyGsmMin(const T& a, const T& b)
  56. {
  57. return (b < a) ? b : a;
  58. }
  59. template<class T>
  60. const T& TinyGsmMax(const T& a, const T& b)
  61. {
  62. return (b < a) ? a : b;
  63. }
  64. template<class T>
  65. uint32_t TinyGsmAutoBaud(T& SerialAT)
  66. {
  67. static uint32_t rates[] = { 115200, 57600, 38400, 19200, 9600, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 };
  68. for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
  69. uint32_t rate = rates[i];
  70. DBG("Trying baud rate", rate, "...");
  71. SerialAT.begin(rate);
  72. delay(10);
  73. for (int i=0; i<3; i++) {
  74. SerialAT.print("AT\r\n");
  75. String input = SerialAT.readString();
  76. if (input.indexOf("OK") >= 0) {
  77. DBG("Modem responded at rate", rate);
  78. return rate;
  79. }
  80. }
  81. }
  82. return 0;
  83. }
  84. #endif