Camera Slider Firmware 1.0
Firmware for camera slider with stepper motor control and BLE communication
bleuart.ino
Go to the documentation of this file.
1/**@file*/
2/*********************************************************************
3 This is an example for our nRF52 based Bluefruit LE modules
4
5 Pick one up today in the adafruit shop!
6
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!
10
11 MIT license, check LICENSE for more information
12 All text above, and the splash screen below must be included in
13 any redistribution
14
15
16
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.
19Working pretty easily.
20
21Can add an app front end, and set up UART control.
22
23
24*********************************************************************/
25#include <bluefruit.h>
26#include <Adafruit_LittleFS.h>
27#include <InternalFileSystem.h>
28#include <motors.h>
29
30
31// BLE Service
32BLEDfu bledfu; // OTA DFU service
33BLEDis bledis; // device information
34BLEUart bleuart; // uart over ble
35BLEBas blebas; // battery
36
37
38void setup()
39{
40 Serial.begin(115200);
41 pinMode(LED_GREEN, OUTPUT);
42
43 setup_steppers();
44
45
46#if CFG_DEBUG
47 // Blocking wait for connection when debug mode is enabled via IDE
48 while ( !Serial ) yield();
49#endif
50
51 Serial.println("Camera Slider");
52 Serial.println("---------------------------\n");
53
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);
58
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);
63
64 Bluefruit.begin();
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");
70
71 // To be consistent OTA DFU should be added first if it exists
72 bledfu.begin();
73
74 // Configure and Start Device Information Service
75 bledis.setManufacturer("Adafruit Industries");
76 bledis.setModel("Bluefruit Feather52");
77 bledis.begin();
78
79 // Configure and Start BLE Uart Service
80 bleuart.begin();
81
82 // Start BLE Battery Service
83 blebas.begin();
84 blebas.write(100);
85
86 // Set up and start advertising
87 startAdv();
88
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");
91}
92
93void startAdv(void)
94{
95 // Advertising packet
96 Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
97 Bluefruit.Advertising.addTxPower();
98
99 // Include bleuart 128-bit uuid
100 Bluefruit.Advertising.addService(bleuart);
101
102 // Secondary Scan Response packet (optional)
103 // Since there is no room for 'Name' in Advertising packet
104 Bluefruit.ScanResponse.addName();
105
106 /* Start Advertising
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)
111 *
112 * For recommended advertising interval
113 * https://developer.apple.com/library/content/qa/qa1931/_index.html
114 */
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
119}
120
121void loop()
122{
123 // Forward data from HW Serial to BLEUART
124 while (Serial.available())
125 {
126 // Delay to wait for enough input, since we have a limited transmission buffer
127 delay(2);
128
129 uint8_t buf[64];
130 int count = Serial.readBytes(buf, sizeof(buf));
131 bleuart.write( buf, count );
132 }
133
134 // Forward from BLEUART to HW Serial
135 while ( bleuart.available() )
136 {
137 uint8_t ch;
138 ch = (uint8_t) bleuart.read();
139
140 if ( ch == 'a'){
141 Serial.write("a intercept - change dir");
142 digitalToggle(LED_GREEN);
143 slide_dist(50);
144 } else if (ch == 'b' ) {
145 Serial.write("b intercept - change dir");
146 digitalToggle(LED_GREEN);
147 slide_dist(-50);
148 } else {
149 Serial.write(ch);
150 }
151 }
152
153 // slider_stepper.run();
154 run_or_off();
155}
156
157// callback invoked when central connects
158void connect_callback(uint16_t conn_handle)
159{
160 // Get the reference to current connection
161 BLEConnection* connection = Bluefruit.Connection(conn_handle);
162
163 char central_name[32] = { 0 };
164 connection->getPeerName(central_name, sizeof(central_name));
165
166 Serial.print("Connected to ");
167 Serial.println(central_name);
168}
169
170/**
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
174 */
175void disconnect_callback(uint16_t conn_handle, uint8_t reason)
176{
177 (void) conn_handle;
178 (void) reason;
179
180 Serial.println();
181 Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
182}