2/*********************************************************************
3 This is an example for our nRF52 based Bluefruit LE modules
5 Pick one up today in the adafruit shop!
7 Adafruit invests time and resources providing this open source code,
8 please support Adafruit and open-source hardware by purchasing
9 products from Adafruit!
11 MIT license, check LICENSE for more information
12 All text above, and the splash screen below must be included in
17 This works to set up a serial connection - the nRF toolbox > UART works, as does the ardiuino IDE
18I have it intercepting the character ' a ' and toggleing the LED.
21Can add an app front end, and set up UART control.
24*********************************************************************/
26#include <Adafruit_LittleFS.h>
27#include <InternalFileSystem.h>
32BLEDfu bledfu; // OTA DFU service
33BLEDis bledis; // device information
34BLEUart bleuart; // uart over ble
35BLEBas blebas; // battery
41 pinMode(LED_GREEN, OUTPUT);
47 // Blocking wait for connection when debug mode is enabled via IDE
48 while ( !Serial ) yield();
51 Serial.println("Camera Slider");
52 Serial.println("---------------------------\n");
54 // Setup the BLE LED to be enabled on CONNECT
55 // Note: This is actually the default behavior, but provided
56 // here in case you want to control this LED manually via PIN 19
57 Bluefruit.autoConnLed(true);
59 // Config the peripheral connection with maximum bandwidth
60 // more SRAM required by SoftDevice
61 // Note: All config***() function must be called before begin()
62 Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
65 Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
66 //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
67 Bluefruit.Periph.setConnectCallback(connect_callback);
68 Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
69 Bluefruit.setName("Camera Slider");
71 // To be consistent OTA DFU should be added first if it exists
74 // Configure and Start Device Information Service
75 bledis.setManufacturer("Adafruit Industries");
76 bledis.setModel("Bluefruit Feather52");
79 // Configure and Start BLE Uart Service
82 // Start BLE Battery Service
86 // Set up and start advertising
89 Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
90 Serial.println("Once connected, enter character(s) that you wish to send");
96 Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
97 Bluefruit.Advertising.addTxPower();
99 // Include bleuart 128-bit uuid
100 Bluefruit.Advertising.addService(bleuart);
102 // Secondary Scan Response packet (optional)
103 // Since there is no room for 'Name' in Advertising packet
104 Bluefruit.ScanResponse.addName();
107 * - Enable auto advertising if disconnected
108 * - Interval: fast mode = 20 ms, slow mode = 152.5 ms
109 * - Timeout for fast mode is 30 seconds
110 * - Start(timeout) with timeout = 0 will advertise forever (until connected)
112 * For recommended advertising interval
113 * https://developer.apple.com/library/content/qa/qa1931/_index.html
115 Bluefruit.Advertising.restartOnDisconnect(true);
116 Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
117 Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
118 Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
123 // Forward data from HW Serial to BLEUART
124 while (Serial.available())
126 // Delay to wait for enough input, since we have a limited transmission buffer
130 int count = Serial.readBytes(buf, sizeof(buf));
131 bleuart.write( buf, count );
134 // Forward from BLEUART to HW Serial
135 while ( bleuart.available() )
138 ch = (uint8_t) bleuart.read();
141 Serial.write("a intercept - change dir");
142 digitalToggle(LED_GREEN);
144 } else if (ch == 'b' ) {
145 Serial.write("b intercept - change dir");
146 digitalToggle(LED_GREEN);
153 // slider_stepper.run();
157// callback invoked when central connects
158void connect_callback(uint16_t conn_handle)
160 // Get the reference to current connection
161 BLEConnection* connection = Bluefruit.Connection(conn_handle);
163 char central_name[32] = { 0 };
164 connection->getPeerName(central_name, sizeof(central_name));
166 Serial.print("Connected to ");
167 Serial.println(central_name);
171 * Callback invoked when a connection is dropped
172 * @param conn_handle connection where this event happens
173 * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
175void disconnect_callback(uint16_t conn_handle, uint8_t reason)
181 Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);