|
| 1 | +/********************************************************************* |
| 2 | + This is an example for our nRF52 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 | + |
| 15 | +/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App. |
| 16 | + * FeatherWing OLED is used to display uploaded image |
| 17 | + * - https://www.adafruit.com/product/3315 |
| 18 | + */ |
| 19 | + |
| 20 | +#include <bluefruit.h> |
| 21 | +#include <SPI.h> |
| 22 | +#include <Adafruit_GFX.h> |
| 23 | +#include <Adafruit_ILI9341.h> |
| 24 | + |
| 25 | +BLEUart bleuart; // uart over ble |
| 26 | + |
| 27 | +/* The Image Transfer module sends the image of your choice to Bluefruit LE over UART. |
| 28 | + * Each image sent begins with |
| 29 | + * - A single byte char “!” (0x21) |
| 30 | + * - Image width (uint16 little endian, 2 bytes) |
| 31 | + * - Image height (uint16 little endian, 2 bytes) |
| 32 | + * - Pixel data encoded as RGB 24-bit and suffixed by a single byte CRC. |
| 33 | + * |
| 34 | + * Format: [ ‘!’ ] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ] |
| 35 | + */ |
| 36 | + |
| 37 | +uint16_t imageWidth = 0; |
| 38 | +uint16_t imageHeight = 0; |
| 39 | + |
| 40 | +// Statistics for speed testing |
| 41 | +uint32_t rxCount = 0; |
| 42 | +uint32_t rxStartTime = 0; |
| 43 | +uint32_t rxLastTime = 0; |
| 44 | + |
| 45 | +#ifdef ARDUINO_NRF52832_FEATHER |
| 46 | + #define TFT_DC 11 |
| 47 | + #define TFT_CS 31 |
| 48 | + #define STMPE_CS 30 |
| 49 | + #define SD_CS 27 |
| 50 | +#endif |
| 51 | + |
| 52 | +#ifdef ARDUINO_NRF52840_FEATHER |
| 53 | + #define TFT_DC 10 |
| 54 | + #define TFT_CS 9 |
| 55 | + #define STMPE_CS 6 |
| 56 | + #define SD_CS 5 |
| 57 | +#endif |
| 58 | + |
| 59 | +Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); |
| 60 | + |
| 61 | +void setup() |
| 62 | +{ |
| 63 | + Serial.begin(115200); |
| 64 | + |
| 65 | + tft.begin(); |
| 66 | + tft.fillScreen(ILI9341_BLACK); |
| 67 | + tft.setTextColor(ILI9341_WHITE); |
| 68 | + tft.setTextSize(1); |
| 69 | + |
| 70 | + // Config the peripheral connection with maximum bandwidth |
| 71 | + // more SRAM required by SoftDevice |
| 72 | + // Note: All config***() function must be called before begin() |
| 73 | + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
| 74 | + |
| 75 | + Bluefruit.begin(); |
| 76 | + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values |
| 77 | + Bluefruit.setName("Bluefruit52"); |
| 78 | + Bluefruit.Periph.setConnectCallback(connect_callback); |
| 79 | + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
| 80 | + Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms |
| 81 | + |
| 82 | + // Configure and Start BLE Uart Service |
| 83 | + bleuart.begin(); |
| 84 | + bleuart.setRxCallback(bleuart_rx_callback); |
| 85 | + |
| 86 | + // Set up and start advertising |
| 87 | + startAdv(); |
| 88 | + |
| 89 | + // splash screen effect |
| 90 | + delay(100); |
| 91 | + |
| 92 | + |
| 93 | + tft.println("Advertising ... "); |
| 94 | +} |
| 95 | + |
| 96 | +void startAdv(void) |
| 97 | +{ |
| 98 | + // Advertising packet |
| 99 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 100 | + Bluefruit.Advertising.addTxPower(); |
| 101 | + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_GENERIC_CLOCK); |
| 102 | + |
| 103 | + // Include bleuart 128-bit uuid |
| 104 | + Bluefruit.Advertising.addService(bleuart); |
| 105 | + |
| 106 | + // There is no room for Name in Advertising packet |
| 107 | + // Use Scan response for Name |
| 108 | + Bluefruit.ScanResponse.addName(); |
| 109 | + |
| 110 | + /* Start Advertising |
| 111 | + * - Enable auto advertising if disconnected |
| 112 | + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms |
| 113 | + * - Timeout for fast mode is 30 seconds |
| 114 | + * - Start(timeout) with timeout = 0 will advertise forever (until connected) |
| 115 | + * |
| 116 | + * For recommended advertising interval |
| 117 | + * https://developer.apple.com/library/content/qa/qa1931/_index.html |
| 118 | + */ |
| 119 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 120 | + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms |
| 121 | + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode |
| 122 | + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds |
| 123 | +} |
| 124 | + |
| 125 | +void loop() |
| 126 | +{ |
| 127 | + // 3 seconds has passed and there is no data received |
| 128 | + // then reset rx count |
| 129 | + if ( (rxCount > 0) && (rxLastTime + 1000 < millis()) ) |
| 130 | + { |
| 131 | + print_speed(rxCount, rxLastTime-rxStartTime); |
| 132 | + rxCount = 0; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void connect_callback(uint16_t conn_handle) |
| 137 | +{ |
| 138 | + BLEConnection* conn = Bluefruit.Connection(conn_handle); |
| 139 | + |
| 140 | + tft.println("Connected"); |
| 141 | + |
| 142 | + conn->requestPHY(); |
| 143 | + tft.println("Switching PHY"); |
| 144 | + |
| 145 | + conn->requestDataLengthUpdate(); |
| 146 | + tft.println("Updating Data Length"); |
| 147 | + |
| 148 | + conn->requestMtuExchange(247); |
| 149 | + tft.println("Exchanging MTU"); |
| 150 | + |
| 151 | + tft.println("Ready to receive Image"); |
| 152 | +} |
| 153 | + |
| 154 | +void print_speed(uint32_t count, uint32_t ms) |
| 155 | +{ |
| 156 | + Serial.print("Received "); |
| 157 | + Serial.print(count); |
| 158 | + Serial.print(" bytes in "); |
| 159 | + Serial.print(ms / 1000.0F, 2); |
| 160 | + Serial.println(" seconds."); |
| 161 | + |
| 162 | + Serial.print("Speed : "); |
| 163 | + Serial.print( (count / 1000.0F) / (ms / 1000.0F), 2); |
| 164 | + Serial.println(" KB/s.\r\n"); |
| 165 | +} |
| 166 | + |
| 167 | +void bleuart_rx_callback(uint16_t conn_hdl) |
| 168 | +{ |
| 169 | + (void) conn_hdl; |
| 170 | + |
| 171 | + rxLastTime = millis(); |
| 172 | + |
| 173 | + // first packet |
| 174 | + if ( rxCount == 0 ) |
| 175 | + { |
| 176 | + rxStartTime = millis(); |
| 177 | + |
| 178 | + // Incorrect format, possibly corrupted data |
| 179 | + if ( bleuart.read() != '!' ) bleuart.flush(); |
| 180 | + rxCount++; |
| 181 | + |
| 182 | + tft.fillScreen(ILI9341_BLACK); |
| 183 | + tft.setCursor(0, 0); |
| 184 | + } |
| 185 | + |
| 186 | + rxCount += bleuart.available(); |
| 187 | + bleuart.flush(); // empty rx fifo |
| 188 | +} |
| 189 | + |
| 190 | +/** |
| 191 | + * Callback invoked when a connection is dropped |
| 192 | + * @param conn_handle connection where this event happens |
| 193 | + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h |
| 194 | + */ |
| 195 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 196 | +{ |
| 197 | + (void) reason; |
| 198 | + |
| 199 | + tft.fillScreen(ILI9341_BLACK); |
| 200 | + tft.setCursor(0, 0); |
| 201 | + tft.println("Advertising ..."); |
| 202 | +} |
0 commit comments