Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.15 KB | None | 0 0
  1. /*********************************************************************
  2. This is an example for our nRF51822 based Bluefruit LE modules
  3.  
  4. Pick one up today in the adafruit shop!
  5.  
  6. Adafruit invests time and resources providing this open source code,
  7. please support Adafruit and open-source hardware by purchasing
  8. products from Adafruit!
  9.  
  10. MIT license, check LICENSE for more information
  11. All text above, and the splash screen below must be included in
  12. any redistribution
  13. *********************************************************************/
  14. // Buzzer Imports
  15. #include <Adafruit_DRV2605.h>
  16. #include <Wire.h>
  17.  
  18. // UART Imports
  19. #include <Arduino.h>
  20. #include <SPI.h>
  21. #include "Adafruit_BLE.h"
  22. #include "Adafruit_BluefruitLE_SPI.h"
  23. #include "Adafruit_BluefruitLE_UART.h"
  24.  
  25. #include "BluefruitConfig.h"
  26.  
  27. #if SOFTWARE_SERIAL_AVAILABLE
  28. #include <SoftwareSerial.h>
  29. #endif
  30.  
  31. boolean buzz = false; // Control the buzzer
  32.  
  33. /*=========================================================================
  34. APPLICATION SETTINGS
  35.  
  36.     FACTORYRESET_ENABLE    Perform a factory reset when running this sketch
  37.    
  38.     Enabling this will put your Bluefruit LE module
  39. in a 'known good' state and clear any config
  40. data set in previous sketches or projects, so
  41.     running this at least once is a good idea.
  42.    
  43.     When deploying your project, however, you will
  44. want to disable factory reset by setting this
  45. value to 0.  If you are making changes to your
  46.     Bluefruit LE device via AT commands, and those
  47. changes aren't persisting across resets, this
  48. is the reason why.  Factory reset will erase
  49. the non-volatile memory where config data is
  50. stored, setting it back to factory default
  51. values.
  52.        
  53.     Some sketches that require you to bond to a
  54. central device (HID mouse, keyboard, etc.)
  55. won't work at all with this feature enabled
  56. since the factory reset will clear all of the
  57. bonding data stored on the chip, meaning the
  58. central device won't be able to reconnect.
  59. MINIMUM_FIRMWARE_VERSION Minimum firmware version to have some new features
  60. MODE_LED_BEHAVIOUR LED activity, valid options are
  61. "DISABLE" or "MODE" or "BLEUART" or
  62. "HWUART" or "SPI" or "MANUAL"
  63. -----------------------------------------------------------------------*/
  64. #define FACTORYRESET_ENABLE 1
  65. #define MINIMUM_FIRMWARE_VERSION "0.6.6"
  66. #define MODE_LED_BEHAVIOUR "MODE"
  67. /*=========================================================================*/
  68.  
  69. // Create the bluefruit object, either software serial...uncomment these lines
  70. /*
  71. SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
  72.  
  73. Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
  74. BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
  75. */
  76.  
  77. /* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
  78. // Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);
  79.  
  80. /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
  81. Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
  82.  
  83. /* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
  84. //Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
  85. // BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
  86. // BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
  87.  
  88. // Buzzer Variables
  89. Adafruit_DRV2605 drv;
  90.  
  91. uint8_t effect = 7;
  92. uint32_t wait = 10; // Time between reminders, in seconds
  93.  
  94. // A small helper
  95. void error(const __FlashStringHelper*err) {
  96. Serial.println(err);
  97. while (1);
  98. }
  99.  
  100. /**************************************************************************/
  101. /*!
  102. @brief Sets up the HW an the BLE module (this function is called
  103. automatically on startup)
  104. */
  105. /**************************************************************************/
  106. void setup(void)
  107. {
  108. while (!Serial); // required for Flora & Micro
  109. delay(500);
  110.  
  111. Serial.begin(115200);
  112. Serial.println(F("Adafruit Bluefruit Command Mode Example"));
  113. Serial.println(F("---------------------------------------"));
  114.  
  115. /* Initialise the module */
  116. Serial.print(F("Initialising the Bluefruit LE module: "));
  117.  
  118. if ( !ble.begin(VERBOSE_MODE) )
  119. {
  120. error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  121. }
  122. Serial.println( F("OK!") );
  123.  
  124. if ( FACTORYRESET_ENABLE )
  125. {
  126. /* Perform a factory reset to make sure everything is in a known state */
  127. Serial.println(F("Performing a factory reset: "));
  128. if ( ! ble.factoryReset() ){
  129. error(F("Couldn't factory reset"));
  130. }
  131. }
  132.  
  133. /* Disable command echo from Bluefruit */
  134. ble.echo(false);
  135.  
  136. Serial.println("Requesting Bluefruit info:");
  137. /* Print Bluefruit information */
  138. ble.info();
  139.  
  140. Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
  141. Serial.println(F("Then Enter characters to send to Bluefruit"));
  142. Serial.println();
  143.  
  144. ble.verbose(false); // debug info is a little annoying after this point!
  145.  
  146. /* Wait for connection */
  147. while (! ble.isConnected()) {
  148. delay(500);
  149. }
  150.  
  151. // LED Activity command is only supported from 0.6.6
  152. if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  153. {
  154. // Change Mode LED Activity
  155. Serial.println(F("******************************"));
  156. Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
  157. ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
  158. Serial.println(F("******************************"));
  159. }
  160.  
  161. drv.begin();
  162.  
  163. drv.selectLibrary(1);
  164.  
  165. // I2C trigger by sending 'go' command
  166. // default, internal trigger when sending GO command
  167. drv.setMode(DRV2605_MODE_INTTRIG);
  168. }
  169.  
  170. /**************************************************************************/
  171. /*!
  172. @brief Constantly poll for new command or response data
  173. */
  174. /**************************************************************************/
  175. void loop(void)
  176. {
  177. // Check for user input
  178. char inputs[BUFSIZE+1];
  179.  
  180. if ( getUserInput(inputs, BUFSIZE) )
  181. {
  182. // Send characters to Bluefruit
  183. Serial.print("[Send] ");
  184. Serial.println(inputs);
  185.  
  186. ble.print("AT+BLEUARTTX=");
  187. ble.println(inputs);
  188.  
  189. // check response stastus
  190. if (! ble.waitForOK() ) {
  191. Serial.println(F("Failed to send?"));
  192. }
  193. }
  194.  
  195. // Check for incoming characters from Bluefruit
  196. ble.println("AT+BLEUARTRX");
  197. ble.readline();
  198. if (strcmp(ble.buffer, "OK") == 0) {
  199. // no data
  200.  
  201. // Signal to the buzzer to start or stop
  202. if (buzz == true) {
  203. drv.go();
  204. } else {
  205. drv.stop();
  206. }
  207. return;
  208. }
  209. // Some data was found, its in the buffer
  210. Serial.print(F("[Recv] ")); Serial.println(ble.buffer);
  211. ble.waitForOK();
  212.  
  213. // set the effect to play
  214. drv.setWaveform(0, effect); // play effect
  215. drv.setWaveform(1, 0); // end waveform
  216.  
  217. String data = ble.buffer;
  218. if (data == "1") {
  219. buzz = true;
  220. } else {
  221. buzz = false;
  222. }
  223.  
  224.  
  225. // delay(wait * 60 * 1000);
  226. delay(wait);
  227. }
  228.  
  229. /**************************************************************************/
  230. /*!
  231. @brief Checks for user input (via the Serial Monitor)
  232. */
  233. /**************************************************************************/
  234. bool getUserInput(char buffer[], uint8_t maxSize)
  235. {
  236. // timeout in 100 milliseconds
  237. TimeoutTimer timeout(100);
  238.  
  239. memset(buffer, 0, maxSize);
  240. while( (!Serial.available()) && !timeout.expired() ) { delay(1); }
  241.  
  242. if ( timeout.expired() ) return false;
  243.  
  244. delay(2);
  245. uint8_t count=0;
  246. do
  247. {
  248. count += Serial.readBytes(buffer+count, maxSize);
  249. delay(2);
  250. } while( (count < maxSize) && (Serial.available()) );
  251.  
  252. return true;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement