PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

104 líneas
3.5KB

  1. /*********************************************************************
  2. This is an example for our nRF8001 Bluetooth Low Energy Breakout
  3. Pick one up today in the adafruit shop!
  4. ------> http://www.adafruit.com/products/1697
  5. Adafruit invests time and resources providing this open source code,
  6. please support Adafruit and open-source hardware by purchasing
  7. products from Adafruit!
  8. Written by Kevin Townsend/KTOWN for Adafruit Industries.
  9. MIT license, check LICENSE for more information
  10. All text above, and the splash screen below must be included in any redistribution
  11. *********************************************************************/
  12. // This version uses the internal data queing so you can treat it like Serial (kinda)!
  13. #include <SPI.h>
  14. #include "Adafruit_BLE_UART.h"
  15. // Connect CLK/MISO/MOSI to hardware SPI
  16. // e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
  17. #define ADAFRUITBLE_REQ 10
  18. #define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
  19. #define ADAFRUITBLE_RST 9
  20. Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
  21. /**************************************************************************/
  22. /*!
  23. Configure the Arduino and start advertising with the radio
  24. */
  25. /**************************************************************************/
  26. void setup(void)
  27. {
  28. Serial.begin(9600);
  29. while(!Serial); // Leonardo/Micro should wait for serial init
  30. Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
  31. // BTLEserial.setDeviceName("NEWNAME"); /* 7 characters max! */
  32. BTLEserial.begin();
  33. }
  34. /**************************************************************************/
  35. /*!
  36. Constantly checks for new events on the nRF8001
  37. */
  38. /**************************************************************************/
  39. aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
  40. void loop()
  41. {
  42. // Tell the nRF8001 to do whatever it should be working on.
  43. BTLEserial.pollACI();
  44. // Ask what is our current status
  45. aci_evt_opcode_t status = BTLEserial.getState();
  46. // If the status changed....
  47. if (status != laststatus) {
  48. // print it out!
  49. if (status == ACI_EVT_DEVICE_STARTED) {
  50. Serial.println(F("* Advertising started"));
  51. }
  52. if (status == ACI_EVT_CONNECTED) {
  53. Serial.println(F("* Connected!"));
  54. }
  55. if (status == ACI_EVT_DISCONNECTED) {
  56. Serial.println(F("* Disconnected or advertising timed out"));
  57. }
  58. // OK set the last status change to this one
  59. laststatus = status;
  60. }
  61. if (status == ACI_EVT_CONNECTED) {
  62. // Lets see if there's any data for us!
  63. if (BTLEserial.available()) {
  64. Serial.print("* "); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
  65. }
  66. // OK while we still have something to read, get a character and print it out
  67. while (BTLEserial.available()) {
  68. char c = BTLEserial.read();
  69. Serial.print(c);
  70. }
  71. // Next up, see if we have any data to get from the Serial console
  72. if (Serial.available()) {
  73. // Read a line from Serial
  74. Serial.setTimeout(100); // 100 millisecond timeout
  75. String s = Serial.readString();
  76. // We need to convert the line to bytes, no more than 20 at this time
  77. uint8_t sendbuffer[20];
  78. s.getBytes(sendbuffer, 20);
  79. char sendbuffersize = min(20, s.length());
  80. Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.println("\"");
  81. // write the data
  82. BTLEserial.write(sendbuffer, sendbuffersize);
  83. }
  84. }
  85. }