|
| 1 | +/* |
| 2 | + BluefruitLE_SPI_Stream.h |
| 3 | +
|
| 4 | + Documentation for the various AT commands used below is available at |
| 5 | + https://learn.adafruit.com/adafruit-feather-m0-bluefruit-le/at-commands |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef _BLUEFRUIT_LE_SPI_STREAM_H_ |
| 9 | +#define _BLUEFRUIT_LE_SPI_STREAM_H_ |
| 10 | + |
| 11 | +#include <Adafruit_BluefruitLE_SPI.h> |
| 12 | + |
| 13 | + |
| 14 | +class BluefruitLE_SPI_Stream : public Stream |
| 15 | +{ |
| 16 | + public: |
| 17 | + BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin); |
| 18 | + |
| 19 | + void begin(); |
| 20 | + bool poll(); |
| 21 | + void end(); |
| 22 | + |
| 23 | + // Print overrides |
| 24 | + size_t write(uint8_t byte); |
| 25 | + using Print::write; // Expose other write variants |
| 26 | + |
| 27 | + // Stream overrides |
| 28 | + int available(); |
| 29 | + int read(); |
| 30 | + int peek(); |
| 31 | + void flush(); |
| 32 | + |
| 33 | + private: |
| 34 | + Adafruit_BluefruitLE_SPI ble; |
| 35 | + |
| 36 | + uint8_t txBuffer[SDEP_MAX_PACKETSIZE]; |
| 37 | + size_t txCount; |
| 38 | +}; |
| 39 | + |
| 40 | + |
| 41 | +BluefruitLE_SPI_Stream::BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin) : |
| 42 | + ble(csPin, irqPin, rstPin), |
| 43 | + txCount(0) |
| 44 | +{ } |
| 45 | + |
| 46 | +void BluefruitLE_SPI_Stream::begin() |
| 47 | +{ |
| 48 | + // Initialize the SPI interface |
| 49 | + ble.begin(); |
| 50 | + |
| 51 | + // Perform a factory reset to make sure everything is in a known state |
| 52 | + ble.factoryReset(); |
| 53 | + |
| 54 | + // Disable command echo from Bluefruit |
| 55 | + ble.echo(false); |
| 56 | + |
| 57 | + // Change the MODE LED to indicate BLE UART activity |
| 58 | + ble.println("AT+HWMODELED=BLEUART"); |
| 59 | + |
| 60 | + // Set local name |
| 61 | + ble.print("AT+GAPDEVNAME="); |
| 62 | + ble.println(FIRMATA_BLE_LOCAL_NAME); |
| 63 | + |
| 64 | + // Set connection interval |
| 65 | + ble.print("AT+GAPINTERVALS="); |
| 66 | + ble.print(FIRMATA_BLE_MIN_INTERVAL); |
| 67 | + ble.print(","); |
| 68 | + ble.print(FIRMATA_BLE_MAX_INTERVAL); |
| 69 | + ble.println(",,,"); |
| 70 | + |
| 71 | + // Disable real and simulated mode switch (i.e. "+++") command |
| 72 | + ble.println("AT+MODESWITCHEN=local,0"); |
| 73 | + ble.enableModeSwitchCommand(false); |
| 74 | + |
| 75 | + // Switch to data mode |
| 76 | + ble.setMode(BLUEFRUIT_MODE_DATA); |
| 77 | +} |
| 78 | + |
| 79 | +bool BluefruitLE_SPI_Stream::poll() |
| 80 | +{ |
| 81 | + // If there's outgoing data in the buffer, just send it. The firmware on |
| 82 | + // the nRF51822 will decide when to transmit the data in its TX FIFO. |
| 83 | + if (txCount) flush(); |
| 84 | + |
| 85 | + // In order to check for a connection, we would need to switch from data to |
| 86 | + // command mode and back again. However, due to the internal workings of |
| 87 | + // Adafruit_BluefruitLE_SPI, this can lead to unread incoming data being |
| 88 | + // lost. Therefore, we always return true. |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +void BluefruitLE_SPI_Stream::end() |
| 93 | +{ |
| 94 | + flush(); |
| 95 | + ble.end(); |
| 96 | +} |
| 97 | + |
| 98 | +size_t BluefruitLE_SPI_Stream::write(uint8_t byte) |
| 99 | +{ |
| 100 | + txBuffer[txCount++] = byte; |
| 101 | + if (txCount == sizeof(txBuffer)) flush(); |
| 102 | + return 1; |
| 103 | +} |
| 104 | + |
| 105 | +int BluefruitLE_SPI_Stream::available() |
| 106 | +{ |
| 107 | + return ble.available(); |
| 108 | +} |
| 109 | + |
| 110 | +int BluefruitLE_SPI_Stream::read() |
| 111 | +{ |
| 112 | + return ble.read(); |
| 113 | +} |
| 114 | + |
| 115 | +int BluefruitLE_SPI_Stream::peek() |
| 116 | +{ |
| 117 | + return ble.peek(); |
| 118 | +} |
| 119 | + |
| 120 | +void BluefruitLE_SPI_Stream::flush() |
| 121 | +{ |
| 122 | + ble.write(txBuffer, txCount); |
| 123 | + txCount = 0; |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +#endif // _BLUEFRUIT_LE_SPI_STREAM_H_ |
0 commit comments