PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

293 rindas
12KB

  1. // VirtualWire.h
  2. //
  3. // Virtual Wire implementation for Arduino
  4. // See the README file in this directory fdor documentation
  5. //
  6. // Author: Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS
  7. // Copyright (C) 2008 Mike McCauley
  8. // $Id: VirtualWire.h,v 1.6 2013/02/14 22:02:11 mikem Exp mikem $
  9. /// \mainpage VirtualWire library for Arduino
  10. ///
  11. /// This is the Arduino VirtualWire library.
  12. ///
  13. /// VirtualWire is an Arduino library that provides features to send short
  14. /// messages, without addressing, retransmit or acknowledgment, a bit like UDP
  15. /// over wireless, using ASK (amplitude shift keying). Supports a number of
  16. /// inexpensive radio transmitters and receivers. All that is required is
  17. /// transmit data, receive data and (for transmitters, optionally) a PTT
  18. /// transmitter enable.
  19. ///
  20. /// It is intended to be compatible with the RF Monolithics (www.rfm.com)
  21. /// Virtual Wire protocol, but this has not been tested.
  22. ///
  23. /// Does not use the Arduino UART. Messages are sent with a training preamble,
  24. /// message length and checksum. Messages are sent with 4-to-6 bit encoding
  25. /// for good DC balance, and a CRC checksum for message integrity.
  26. ///
  27. /// Why not just use the Arduino UART connected directly to the
  28. /// transmitter/receiver? As discussed in the RFM documentation, ASK receivers
  29. /// require a burst of training pulses to synchronize the transmitter and
  30. /// receiver, and also requires good balance between 0s and 1s in the message
  31. /// stream in order to maintain the DC balance of the message. UARTs do not
  32. /// provide these. They work a bit with ASK wireless, but not as well as this
  33. /// code.
  34. ///
  35. /// This library provides classes for
  36. /// - VirtualWire: unaddressed, unreliable messages
  37. ///
  38. /// Example Arduino programs are included to show the main modes of use.
  39. ///
  40. /// The version of the package that this documentation refers to can be downloaded
  41. /// from http://www.airspayce.com/mikem/arduino/VirtualWire/VirtualWire-1.15.zip
  42. /// You can find the latest version at http://www.airspayce.com/mikem/arduino/VirtualWire
  43. ///
  44. /// You can also find online help and disussion at http://groups.google.com/group/virtualwire
  45. /// Please use that group for all questions and discussions on this topic.
  46. /// Do not contact the author directly, unless it is to discuss commercial licensing.
  47. ///
  48. /// \par Supported Hardware
  49. /// A range of communications hardware is supported. The ones listed blow are
  50. /// available in common retail outlets in Australian and other countries for
  51. /// under $10 per unit. Many other modules may also work with this software.
  52. /// Runs on ATmega8/168 (Arduino Diecimila, Uno etc) and ATmega328 and possibly
  53. /// others. Also runs on on Energia with MSP430G2553 / G2452 and Arduino with
  54. /// ATMega328 (courtesy Yannick DEVOS - XV4Y).
  55. /// Also compiles and runs on ATtiny85 in Arduino environment, courtesy r4z0r7o3.
  56. ///
  57. /// - Receivers
  58. /// - RX-B1 (433.92MHz) (also known as ST-RX04-ASK)
  59. /// - Transmitters:
  60. /// - TX-C1 (433.92MHz)
  61. /// - Transceivers
  62. /// - DR3100 (433.92MHz)
  63. ///
  64. /// \par Installation
  65. /// To install, unzip the library into the libraries sub-directory of your
  66. /// Arduino application directory. Then launch the Arduino environment; you
  67. /// should see the library in the Sketch->Import Library menu, and example
  68. /// code in
  69. /// File->Sketchbook->Examples->VirtualWire menu.
  70. ///
  71. /// \par Open Source Licensing GPL V2
  72. ///
  73. /// This is the appropriate option if you want to share the source code of your
  74. /// application with everyone you distribute it to, and you also want to give them
  75. /// the right to share who uses it. If you wish to use this software under Open
  76. /// Source Licensing, you must contribute all your source code to the open source
  77. /// community in accordance with the GPL Version 2 when your application is
  78. /// distributed. See http://www.gnu.org/copyleft/gpl.html
  79. ///
  80. /// \par Commercial Licensing
  81. ///
  82. /// This is the appropriate option if you are creating proprietary applications
  83. /// and you are not prepared to distribute and share the source code of your
  84. /// application. Contact info@airspayce.com for details.
  85. ///
  86. /// \par Revision History
  87. /// \version 1.0 Original release
  88. ///
  89. /// \version 1.1 2008-06-24
  90. /// Now can compile for atmega8
  91. /// Reported by creatrope
  92. /// \version 1.2 2009-03-30
  93. /// Fixed a problem that prevented compiling with arduino-0015
  94. /// Reported by Jaime Castro
  95. /// \version 1.3 2009-04-01
  96. /// Fixed a compatibility problem with ATMEGA328 of the new arduino
  97. /// Now use SIGNAL(TIMER1_COMPA_vect) instead of ISR(SIG_OUTPUT_COMPARE1A)
  98. /// as discussed in
  99. /// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237714550/11
  100. /// and reported by Jaime Castro.
  101. /// \version 1.4 2010-01-29
  102. /// Added vx_tx_active(), suggested by Alan Burlison.
  103. /// \version 1.5 2011-09-09
  104. /// Added vx_tx_active() function.
  105. /// \version 1.6 2012-01-10
  106. /// Fixed a problem where the receiver was always reenabled after
  107. /// transmission. Reported by David Bath
  108. /// \version 1.9 2012-02-07 Documentation updates
  109. /// Documentation updates
  110. /// \version 1.10 Updated CHANGES file with changes since 1.4.
  111. /// \version 1.11 Converted documentation to Doxygen. Moved CHANGES log to this version history.
  112. /// Ensure vw_rx_pin is not accessed unless receiver is enabled
  113. /// \version 1.12 Compiles and runs on on Energia with MSP430G2553 / G2452 and Arduino with ATMega328.
  114. /// Patches contributed by Yannick DEVOS - XV4Y
  115. /// \version 1.13 util/crc16.h needed for compiling on Energia with MSP430G2553 / G2452 was accidentally
  116. /// left out of the distribution
  117. /// \version 1.14 Added support ATtiny85 on Arduino, patch provided by r4z0r7o3.
  118. /// \version 1.15 Updated author and distribution location details to airspayce.com
  119. ///
  120. /// \par Implementation Details
  121. /// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
  122. ///
  123. /// \par Performance
  124. /// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
  125. ///
  126. /// \par Connections
  127. /// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
  128. ///
  129. /// \file VirtualWire.h
  130. /// \brief VirtualWire API
  131. ///
  132. /// To use the VirtualWire library, you must have
  133. /// \code
  134. /// #include <VirtualWire.h>
  135. /// \endcode
  136. /// At the top of your sketch.
  137. ///
  138. #ifndef VirtualWire_h
  139. #define VirtualWire_h
  140. #include <stdlib.h>
  141. #if defined(ARDUINO)
  142. #if ARDUINO >= 100
  143. #include <Arduino.h>
  144. #else
  145. #include <wiring.h>
  146. #endif
  147. #elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific
  148. #include "legacymsp430.h"
  149. #include "Energia.h"
  150. #else // error
  151. #error Platform not defined
  152. #endif
  153. // These defs cause trouble on some versions of Arduino
  154. #undef abs
  155. #undef double
  156. #undef round
  157. /// Maximum number of bytes in a message, counting the byte count and FCS
  158. #define VW_MAX_MESSAGE_LEN 30
  159. /// The maximum payload length
  160. #define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3
  161. /// The size of the receiver ramp. Ramp wraps modulu this number
  162. #define VW_RX_RAMP_LEN 160
  163. /// Number of samples per bit
  164. #define VW_RX_SAMPLES_PER_BIT 8
  165. // Ramp adjustment parameters
  166. // Standard is if a transition occurs before VW_RAMP_TRANSITION (80) in the ramp,
  167. // the ramp is retarded by adding VW_RAMP_INC_RETARD (11)
  168. // else by adding VW_RAMP_INC_ADVANCE (29)
  169. // If there is no transition it is adjusted by VW_RAMP_INC (20)
  170. /// Internal ramp adjustment parameter
  171. #define VW_RAMP_INC (VW_RX_RAMP_LEN/VW_RX_SAMPLES_PER_BIT)
  172. /// Internal ramp adjustment parameter
  173. #define VW_RAMP_TRANSITION VW_RX_RAMP_LEN/2
  174. /// Internal ramp adjustment parameter
  175. #define VW_RAMP_ADJUST 9
  176. /// Internal ramp adjustment parameter
  177. #define VW_RAMP_INC_RETARD (VW_RAMP_INC-VW_RAMP_ADJUST)
  178. /// Internal ramp adjustment parameter
  179. #define VW_RAMP_INC_ADVANCE (VW_RAMP_INC+VW_RAMP_ADJUST)
  180. /// Outgoing message bits grouped as 6-bit words
  181. /// 36 alternating 1/0 bits, followed by 12 bits of start symbol
  182. /// Followed immediately by the 4-6 bit encoded byte count,
  183. /// message buffer and 2 byte FCS
  184. /// Each byte from the byte count on is translated into 2x6-bit words
  185. /// Caution, each symbol is transmitted LSBit first,
  186. /// but each byte is transmitted high nybble first
  187. #define VW_HEADER_LEN 8
  188. // Cant really do this as a real C++ class, since we need to have
  189. // an ISR
  190. extern "C"
  191. {
  192. /// Set the digital IO pin to be for transmit data.
  193. /// This pin will only be accessed if
  194. /// the transmitter is enabled
  195. /// \param[in] pin The Arduino pin number for transmitting data. Defaults to 12.
  196. extern void vw_set_tx_pin(uint8_t pin);
  197. /// Set the digital IO pin to be for receive data.
  198. /// This pin will only be accessed if
  199. /// the receiver is enabled
  200. /// \param[in] pin The Arduino pin number for receiving data. Defaults to 11.
  201. extern void vw_set_rx_pin(uint8_t pin);
  202. // Set the digital IO pin to enable the transmitter (press to talk, PTT)'
  203. /// This pin will only be accessed if
  204. /// the transmitter is enabled
  205. /// \param[in] pin The Arduino pin number to enable the transmitter. Defaults to 10.
  206. extern void vw_set_ptt_pin(uint8_t pin);
  207. /// By default the PTT pin goes high when the transmitter is enabled.
  208. /// This flag forces it low when the transmitter is enabled.
  209. /// \param[in] inverted True to invert PTT
  210. extern void vw_set_ptt_inverted(uint8_t inverted);
  211. /// Initialise the VirtualWire software, to operate at speed bits per second
  212. /// Call this one in your setup() after any vw_set_* calls
  213. /// Must call vw_rx_start() before you will get any messages
  214. /// \param[in] speed Desired speed in bits per second
  215. extern void vw_setup(uint16_t speed);
  216. /// Start the Phase Locked Loop listening to the receiver
  217. /// Must do this before you can receive any messages
  218. /// When a message is available (good checksum or not), vw_have_message();
  219. /// will return true.
  220. extern void vw_rx_start();
  221. /// Stop the Phase Locked Loop listening to the receiver
  222. /// No messages will be received until vw_rx_start() is called again
  223. /// Saves interrupt processing cycles
  224. extern void vw_rx_stop();
  225. /// Returns the state of the
  226. /// transmitter
  227. /// \return true if the transmitter is active else false
  228. extern uint8_t vx_tx_active();
  229. /// Block until the transmitter is idle
  230. /// then returns
  231. extern void vw_wait_tx();
  232. /// Block until a message is available
  233. /// then returns
  234. extern void vw_wait_rx();
  235. /// Block until a message is available or for a max time
  236. /// \param[in] milliseconds Maximum time to wait in milliseconds.
  237. /// \return true if a message is available, false if the wait timed out.
  238. extern uint8_t vw_wait_rx_max(unsigned long milliseconds);
  239. /// Send a message with the given length. Returns almost immediately,
  240. /// and message will be sent at the right timing by interrupts
  241. /// \param[in] buf Pointer to the data to transmit
  242. /// \param[in] len Number of octetes to transmit
  243. /// \return true if the message was accepted for transmission, false if the message is too long (>VW_MAX_MESSAGE_LEN - 3)
  244. extern uint8_t vw_send(uint8_t* buf, uint8_t len);
  245. // Returns true if an unread message is available
  246. /// \return true if a message is available to read
  247. extern uint8_t vw_have_message();
  248. // If a message is available (good checksum or not), copies
  249. // up to *len octets to buf.
  250. /// \param[in] buf Pointer to location to save the read data (must be at least *len bytes.
  251. /// \param[in,out] len Available space in buf. Will be set to the actual number of octets read
  252. /// \return true if there was a message and the checksum was good
  253. extern uint8_t vw_get_message(uint8_t* buf, uint8_t* len);
  254. }
  255. /// @example client.pde
  256. /// Client side of simple client/server pair using VirtualWire
  257. /// @example server.pde
  258. /// Server side of simple client/server pair using VirtualWire
  259. /// @example transmitter.pde
  260. /// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire
  261. /// @example receiver.pde
  262. /// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire
  263. #endif