PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

185 linhas
3.2KB

  1. // RHGenericDriver.cpp
  2. //
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: RHGenericDriver.cpp,v 1.19 2015/12/11 01:10:24 mikem Exp $
  5. #include <RHGenericDriver.h>
  6. RHGenericDriver::RHGenericDriver()
  7. :
  8. _mode(RHModeInitialising),
  9. _thisAddress(RH_BROADCAST_ADDRESS),
  10. _txHeaderTo(RH_BROADCAST_ADDRESS),
  11. _txHeaderFrom(RH_BROADCAST_ADDRESS),
  12. _txHeaderId(0),
  13. _txHeaderFlags(0),
  14. _rxBad(0),
  15. _rxGood(0),
  16. _txGood(0)
  17. {
  18. }
  19. bool RHGenericDriver::init()
  20. {
  21. return true;
  22. }
  23. // Blocks until a valid message is received
  24. void RHGenericDriver::waitAvailable()
  25. {
  26. while (!available())
  27. YIELD;
  28. }
  29. // Blocks until a valid message is received or timeout expires
  30. // Return true if there is a message available
  31. // Works correctly even on millis() rollover
  32. bool RHGenericDriver::waitAvailableTimeout(uint16_t timeout)
  33. {
  34. unsigned long starttime = millis();
  35. while ((millis() - starttime) < timeout)
  36. {
  37. if (available())
  38. {
  39. return true;
  40. }
  41. YIELD;
  42. }
  43. return false;
  44. }
  45. bool RHGenericDriver::waitPacketSent()
  46. {
  47. while (_mode == RHModeTx)
  48. YIELD; // Wait for any previous transmit to finish
  49. return true;
  50. }
  51. bool RHGenericDriver::waitPacketSent(uint16_t timeout)
  52. {
  53. unsigned long starttime = millis();
  54. while ((millis() - starttime) < timeout)
  55. {
  56. if (_mode != RHModeTx) // Any previous transmit finished?
  57. return true;
  58. YIELD;
  59. }
  60. return false;
  61. }
  62. void RHGenericDriver::setPromiscuous(bool promiscuous)
  63. {
  64. _promiscuous = promiscuous;
  65. }
  66. void RHGenericDriver::setThisAddress(uint8_t address)
  67. {
  68. _thisAddress = address;
  69. }
  70. void RHGenericDriver::setHeaderTo(uint8_t to)
  71. {
  72. _txHeaderTo = to;
  73. }
  74. void RHGenericDriver::setHeaderFrom(uint8_t from)
  75. {
  76. _txHeaderFrom = from;
  77. }
  78. void RHGenericDriver::setHeaderId(uint8_t id)
  79. {
  80. _txHeaderId = id;
  81. }
  82. void RHGenericDriver::setHeaderFlags(uint8_t set, uint8_t clear)
  83. {
  84. _txHeaderFlags &= ~clear;
  85. _txHeaderFlags |= set;
  86. }
  87. uint8_t RHGenericDriver::headerTo()
  88. {
  89. return _rxHeaderTo;
  90. }
  91. uint8_t RHGenericDriver::headerFrom()
  92. {
  93. return _rxHeaderFrom;
  94. }
  95. uint8_t RHGenericDriver::headerId()
  96. {
  97. return _rxHeaderId;
  98. }
  99. uint8_t RHGenericDriver::headerFlags()
  100. {
  101. return _rxHeaderFlags;
  102. }
  103. int8_t RHGenericDriver::lastRssi()
  104. {
  105. return _lastRssi;
  106. }
  107. RHGenericDriver::RHMode RHGenericDriver::mode()
  108. {
  109. return _mode;
  110. }
  111. void RHGenericDriver::setMode(RHMode mode)
  112. {
  113. _mode = mode;
  114. }
  115. bool RHGenericDriver::sleep()
  116. {
  117. return false;
  118. }
  119. // Diagnostic help
  120. void RHGenericDriver::printBuffer(const char* prompt, const uint8_t* buf, uint8_t len)
  121. {
  122. uint8_t i;
  123. #ifdef RH_HAVE_SERIAL
  124. Serial.println(prompt);
  125. for (i = 0; i < len; i++)
  126. {
  127. if (i % 16 == 15)
  128. Serial.println(buf[i], HEX);
  129. else
  130. {
  131. Serial.print(buf[i], HEX);
  132. Serial.print(' ');
  133. }
  134. }
  135. Serial.println("");
  136. #endif
  137. }
  138. uint16_t RHGenericDriver::rxBad()
  139. {
  140. return _rxBad;
  141. }
  142. uint16_t RHGenericDriver::rxGood()
  143. {
  144. return _rxGood;
  145. }
  146. uint16_t RHGenericDriver::txGood()
  147. {
  148. return _txGood;
  149. }
  150. #if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined(RH_PLATFORM_ATTINY)
  151. // Tinycore does not have __cxa_pure_virtual, so without this we
  152. // get linking complaints from the default code generated for pure virtual functions
  153. extern "C" void __cxa_pure_virtual()
  154. {
  155. while (1);
  156. }
  157. #endif