PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // RHGenericDriver.h
  2. // Author: Mike McCauley (mikem@airspayce.com)
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: RHGenericDriver.h,v 1.17 2016/04/04 01:40:12 mikem Exp $
  5. #ifndef RHGenericDriver_h
  6. #define RHGenericDriver_h
  7. #include <RadioHead.h>
  8. // Defines bits of the FLAGS header reserved for use by the RadioHead library and
  9. // the flags available for use by applications
  10. #define RH_FLAGS_RESERVED 0xf0
  11. #define RH_FLAGS_APPLICATION_SPECIFIC 0x0f
  12. #define RH_FLAGS_NONE 0
  13. /////////////////////////////////////////////////////////////////////
  14. /// \class RHGenericDriver RHGenericDriver.h <RHGenericDriver.h>
  15. /// \brief Abstract base class for a RadioHead driver.
  16. ///
  17. /// This class defines the functions that must be provided by any RadioHead driver.
  18. /// Different types of driver will implement all the abstract functions, and will perhaps override
  19. /// other functions in this subclass, or perhaps add new functions specifically required by that driver.
  20. /// Do not directly instantiate this class: it is only to be subclassed by driver classes.
  21. ///
  22. /// Subclasses are expected to implement a half-duplex, unreliable, error checked, unaddressed packet transport.
  23. /// They are expected to carry a message payload with an appropriate maximum length for the transport hardware
  24. /// and to also carry unaltered 4 message headers: TO, FROM, ID, FLAGS
  25. ///
  26. /// \par Headers
  27. ///
  28. /// Each message sent and received by a RadioHead driver includes 4 headers:
  29. /// -TO The node address that the message is being sent to (broadcast RH_BROADCAST_ADDRESS (255) is permitted)
  30. /// -FROM The node address of the sending node
  31. /// -ID A message ID, distinct (over short time scales) for each message sent by a particilar node
  32. /// -FLAGS A bitmask of flags. The most significant 4 bits are reserved for use by RadioHead. The least
  33. /// significant 4 bits are reserved for applications.
  34. class RHGenericDriver
  35. {
  36. public:
  37. /// \brief Defines different operating modes for the transport hardware
  38. ///
  39. /// These are the different values that can be adopted by the _mode variable and
  40. /// returned by the mode() member function,
  41. typedef enum
  42. {
  43. RHModeInitialising = 0, ///< Transport is initialising. Initial default value until init() is called..
  44. RHModeSleep, ///< Transport hardware is in low power sleep mode (if supported)
  45. RHModeIdle, ///< Transport is idle.
  46. RHModeTx, ///< Transport is in the process of transmitting a message.
  47. RHModeRx ///< Transport is in the process of receiving a message.
  48. } RHMode;
  49. /// Constructor
  50. RHGenericDriver();
  51. /// Initialise the Driver transport hardware and software.
  52. /// Make sure the Driver is properly configured before calling init().
  53. /// \return true if initialisation succeeded.
  54. virtual bool init();
  55. /// Tests whether a new message is available
  56. /// from the Driver.
  57. /// On most drivers, if there is an uncollected received message, and there is no message
  58. /// currently bing transmitted, this will also put the Driver into RHModeRx mode until
  59. /// a message is actually received by the transport, when it will be returned to RHModeIdle.
  60. /// This can be called multiple times in a timeout loop.
  61. /// \return true if a new, complete, error-free uncollected message is available to be retreived by recv().
  62. virtual bool available() = 0;
  63. /// Turns the receiver on if it not already on.
  64. /// If there is a valid message available, copy it to buf and return true
  65. /// else return false.
  66. /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted).
  67. /// You should be sure to call this function frequently enough to not miss any messages
  68. /// It is recommended that you call it in your main loop.
  69. /// \param[in] buf Location to copy the received message
  70. /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied.
  71. /// \return true if a valid message was copied to buf
  72. virtual bool recv(uint8_t* buf, uint8_t* len) = 0;
  73. /// Waits until any previous transmit packet is finished being transmitted with waitPacketSent().
  74. /// Then loads a message into the transmitter and starts the transmitter. Note that a message length
  75. /// of 0 is NOT permitted. If the message is too long for the underlying radio technology, send() will
  76. /// return false and will not send the message.
  77. /// \param[in] data Array of data to be sent
  78. /// \param[in] len Number of bytes of data to send (> 0)
  79. /// \return true if the message length was valid and it was correctly queued for transmit
  80. virtual bool send(const uint8_t* data, uint8_t len) = 0;
  81. /// Returns the maximum message length
  82. /// available in this Driver.
  83. /// \return The maximum legal message length
  84. virtual uint8_t maxMessageLength() = 0;
  85. /// Starts the receiver and blocks until a valid received
  86. /// message is available.
  87. virtual void waitAvailable();
  88. /// Blocks until the transmitter
  89. /// is no longer transmitting.
  90. virtual bool waitPacketSent();
  91. /// Blocks until the transmitter is no longer transmitting.
  92. /// or until the timeout occuers, whichever happens first
  93. /// \param[in] timeout Maximum time to wait in milliseconds.
  94. /// \return true if the RF22 completed transmission within the timeout period. False if it timed out.
  95. virtual bool waitPacketSent(uint16_t timeout);
  96. /// Starts the receiver and blocks until a received message is available or a timeout
  97. /// \param[in] timeout Maximum time to wait in milliseconds.
  98. /// \return true if a message is available
  99. virtual bool waitAvailableTimeout(uint16_t timeout);
  100. /// Sets the address of this node. Defaults to 0xFF. Subclasses or the user may want to change this.
  101. /// This will be used to test the adddress in incoming messages. In non-promiscuous mode,
  102. /// only messages with a TO header the same as thisAddress or the broadcast addess (0xFF) will be accepted.
  103. /// In promiscuous mode, all messages will be accepted regardless of the TO header.
  104. /// In a conventional multinode system, all nodes will have a unique address
  105. /// (which you could store in EEPROM).
  106. /// You would normally set the header FROM address to be the same as thisAddress (though you dont have to,
  107. /// allowing the possibilty of address spoofing).
  108. /// \param[in] thisAddress The address of this node.
  109. virtual void setThisAddress(uint8_t thisAddress);
  110. /// Sets the TO header to be sent in all subsequent messages
  111. /// \param[in] to The new TO header value
  112. virtual void setHeaderTo(uint8_t to);
  113. /// Sets the FROM header to be sent in all subsequent messages
  114. /// \param[in] from The new FROM header value
  115. virtual void setHeaderFrom(uint8_t from);
  116. /// Sets the ID header to be sent in all subsequent messages
  117. /// \param[in] id The new ID header value
  118. virtual void setHeaderId(uint8_t id);
  119. /// Sets and clears bits in the FLAGS header to be sent in all subsequent messages
  120. /// First it clears he FLAGS according to the clear argument, then sets the flags according to the
  121. /// set argument. The default for clear always clears the application specific flags.
  122. /// \param[in] set bitmask of bits to be set. Flags are cleared with the clear mask before being set.
  123. /// \param[in] clear bitmask of flags to clear. Defaults to RH_FLAGS_APPLICATION_SPECIFIC
  124. /// which clears the application specific flags, resulting in new application specific flags
  125. /// identical to the set.
  126. virtual void setHeaderFlags(uint8_t set, uint8_t clear = RH_FLAGS_APPLICATION_SPECIFIC);
  127. /// Tells the receiver to accept messages with any TO address, not just messages
  128. /// addressed to thisAddress or the broadcast address
  129. /// \param[in] promiscuous true if you wish to receive messages with any TO address
  130. virtual void setPromiscuous(bool promiscuous);
  131. /// Returns the TO header of the last received message
  132. /// \return The TO header
  133. virtual uint8_t headerTo();
  134. /// Returns the FROM header of the last received message
  135. /// \return The FROM header
  136. virtual uint8_t headerFrom();
  137. /// Returns the ID header of the last received message
  138. /// \return The ID header
  139. virtual uint8_t headerId();
  140. /// Returns the FLAGS header of the last received message
  141. /// \return The FLAGS header
  142. virtual uint8_t headerFlags();
  143. /// Returns the most recent RSSI (Receiver Signal Strength Indicator).
  144. /// Usually it is the RSSI of the last received message, which is measured when the preamble is received.
  145. /// If you called readRssi() more recently, it will return that more recent value.
  146. /// \return The most recent RSSI measurement in dBm.
  147. int8_t lastRssi();
  148. /// Returns the operating mode of the library.
  149. /// \return the current mode, one of RF69_MODE_*
  150. RHMode mode();
  151. /// Sets the operating mode of the transport.
  152. void setMode(RHMode mode);
  153. /// Sets the transport hardware into low-power sleep mode
  154. /// (if supported). May be overridden by specific drivers to initialte sleep mode.
  155. /// If successful, the transport will stay in sleep mode until woken by
  156. /// changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc)
  157. /// \return true if sleep mode is supported by transport hardware and the RadioHead driver, and if sleep mode
  158. /// was successfully entered. If sleep mode is not suported, return false.
  159. virtual bool sleep();
  160. /// Prints a data buffer in HEX.
  161. /// For diagnostic use
  162. /// \param[in] prompt string to preface the print
  163. /// \param[in] buf Location of the buffer to print
  164. /// \param[in] len Length of the buffer in octets.
  165. static void printBuffer(const char* prompt, const uint8_t* buf, uint8_t len);
  166. /// Returns the count of the number of bad received packets (ie packets with bad lengths, checksum etc)
  167. /// which were rejected and not delivered to the application.
  168. /// Caution: not all drivers can correctly report this count. Some underlying hardware only report
  169. /// good packets.
  170. /// \return The number of bad packets received.
  171. uint16_t rxBad();
  172. /// Returns the count of the number of
  173. /// good received packets
  174. /// \return The number of good packets received.
  175. uint16_t rxGood();
  176. /// Returns the count of the number of
  177. /// packets successfully transmitted (though not necessarily received by the destination)
  178. /// \return The number of packets successfully transmitted
  179. uint16_t txGood();
  180. protected:
  181. /// The current transport operating mode
  182. volatile RHMode _mode;
  183. /// This node id
  184. uint8_t _thisAddress;
  185. /// Whether the transport is in promiscuous mode
  186. bool _promiscuous;
  187. /// TO header in the last received mesasge
  188. volatile uint8_t _rxHeaderTo;
  189. /// FROM header in the last received mesasge
  190. volatile uint8_t _rxHeaderFrom;
  191. /// ID header in the last received mesasge
  192. volatile uint8_t _rxHeaderId;
  193. /// FLAGS header in the last received mesasge
  194. volatile uint8_t _rxHeaderFlags;
  195. /// TO header to send in all messages
  196. uint8_t _txHeaderTo;
  197. /// FROM header to send in all messages
  198. uint8_t _txHeaderFrom;
  199. /// ID header to send in all messages
  200. uint8_t _txHeaderId;
  201. /// FLAGS header to send in all messages
  202. uint8_t _txHeaderFlags;
  203. /// The value of the last received RSSI value, in some transport specific units
  204. volatile int8_t _lastRssi;
  205. /// Count of the number of bad messages (eg bad checksum etc) received
  206. volatile uint16_t _rxBad;
  207. /// Count of the number of successfully transmitted messaged
  208. volatile uint16_t _rxGood;
  209. /// Count of the number of bad messages (correct checksum etc) received
  210. volatile uint16_t _txGood;
  211. private:
  212. };
  213. #endif