|
| 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 | +#include <bluefruit.h> |
| 15 | +#include <Adafruit_LittleFS.h> |
| 16 | +#include <InternalFileSystem.h> |
| 17 | +#include <Adafruit_CircuitPlayground.h> |
| 18 | + |
| 19 | +// BLE Service |
| 20 | +BLEDfu bledfu; // OTA DFU service |
| 21 | +BLEDis bledis; // device information |
| 22 | +BLEUart bleuart; // uart over ble |
| 23 | +BLEBas blebas; // battery |
| 24 | + |
| 25 | +// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8 |
| 26 | +BLEAdafruitTemperature bleTemp; |
| 27 | +BLEAdafruitAccel bleAccel; |
| 28 | +BLEAdafruitLightSensor bleLight; |
| 29 | +BLEAdafruitButton bleButton; |
| 30 | + |
| 31 | +uint16_t measure_button(uint8_t* buf, uint16_t bufsize) |
| 32 | +{ |
| 33 | + uint32_t button = 0; |
| 34 | + |
| 35 | + button |= ( CircuitPlayground.slideSwitch() ? 0x01 : 0x00 ); |
| 36 | + button |= ( CircuitPlayground.leftButton() ? 0x02 : 0x00 ); |
| 37 | + button |= ( CircuitPlayground.rightButton() ? 0x04 : 0x00 ); |
| 38 | + |
| 39 | + memcpy(buf, &button, 4); |
| 40 | + return 4; |
| 41 | +} |
| 42 | + |
| 43 | +uint16_t measure_temperature(uint8_t* buf, uint16_t bufsize) |
| 44 | +{ |
| 45 | + float temp = CircuitPlayground.temperature(); |
| 46 | + memcpy(buf, &temp, 4); |
| 47 | + return 4; |
| 48 | +} |
| 49 | + |
| 50 | +uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize) |
| 51 | +{ |
| 52 | + float lux = CircuitPlayground.lightSensor(); |
| 53 | + memcpy(buf, &lux, 4); |
| 54 | + return 4; |
| 55 | +} |
| 56 | + |
| 57 | +uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) |
| 58 | +{ |
| 59 | + float accel[3]; |
| 60 | + accel[0] = CircuitPlayground.motionX(); |
| 61 | + accel[1] = CircuitPlayground.motionY(); |
| 62 | + accel[2] = CircuitPlayground.motionZ(); |
| 63 | + |
| 64 | + memcpy(buf, accel, sizeof(accel)); |
| 65 | + return sizeof(accel); |
| 66 | +} |
| 67 | + |
| 68 | +void setup() |
| 69 | +{ |
| 70 | + Serial.begin(115200); |
| 71 | + //while ( !Serial ) delay(10); // for nrf52840 with native usb |
| 72 | + |
| 73 | + Serial.println("Bluefruit52 BLEUART Example"); |
| 74 | + Serial.println("---------------------------\n"); |
| 75 | + |
| 76 | + CircuitPlayground.begin(); |
| 77 | + |
| 78 | + // Setup the BLE LED to be enabled on CONNECT |
| 79 | + // Note: This is actually the default behaviour, but provided |
| 80 | + // here in case you want to control this LED manually via PIN 19 |
| 81 | + Bluefruit.autoConnLed(false); |
| 82 | + |
| 83 | + // Config the peripheral connection with maximum bandwidth |
| 84 | + // more SRAM required by SoftDevice |
| 85 | + // Note: All config***() function must be called before begin() |
| 86 | + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
| 87 | + |
| 88 | + Bluefruit.begin(); |
| 89 | + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values |
| 90 | + Bluefruit.setName("Bluefruit52"); |
| 91 | + //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections |
| 92 | + Bluefruit.Periph.setConnectCallback(connect_callback); |
| 93 | + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
| 94 | + |
| 95 | + // To be consistent OTA DFU should be added first if it exists |
| 96 | + bledfu.begin(); |
| 97 | + |
| 98 | + // Configure and Start Device Information Service |
| 99 | + bledis.setManufacturer("Adafruit Industries"); |
| 100 | + bledis.setModel("Bluefruit Feather52"); |
| 101 | + bledis.begin(); |
| 102 | + |
| 103 | + // Configure and Start BLE Uart Service |
| 104 | + bleuart.begin(); |
| 105 | + |
| 106 | + // Start BLE Battery Service |
| 107 | + blebas.begin(); |
| 108 | + blebas.write(100); |
| 109 | + |
| 110 | + // Adafruit Service |
| 111 | + bleTemp.begin(); |
| 112 | + bleTemp.setMeasureCallback(measure_temperature); |
| 113 | + |
| 114 | + bleAccel.begin(); |
| 115 | + bleAccel.setMeasureCallback(measure_accel); |
| 116 | + |
| 117 | + bleLight.begin(); |
| 118 | + bleLight.setMeasureCallback(measure_light_sensor); |
| 119 | + |
| 120 | + bleButton.begin(); |
| 121 | + bleButton.setMeasureCallback(measure_button); |
| 122 | + bleButton.setPeriod(0); // only notify if there is changes with buttons |
| 123 | + |
| 124 | + // Set up and start advertising |
| 125 | + startAdv(); |
| 126 | + |
| 127 | + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); |
| 128 | + Serial.println("Once connected, enter character(s) that you wish to send"); |
| 129 | +} |
| 130 | + |
| 131 | +void startAdv(void) |
| 132 | +{ |
| 133 | + // Advertising packet |
| 134 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 135 | + Bluefruit.Advertising.addTxPower(); |
| 136 | + |
| 137 | + // Include bleuart 128-bit uuid |
| 138 | + Bluefruit.Advertising.addService(bleuart); |
| 139 | + |
| 140 | + // Secondary Scan Response packet (optional) |
| 141 | + // Since there is no room for 'Name' in Advertising packet |
| 142 | + Bluefruit.ScanResponse.addName(); |
| 143 | + |
| 144 | + /* Start Advertising |
| 145 | + * - Enable auto advertising if disconnected |
| 146 | + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms |
| 147 | + * - Timeout for fast mode is 30 seconds |
| 148 | + * - Start(timeout) with timeout = 0 will advertise forever (until connected) |
| 149 | + * |
| 150 | + * For recommended advertising interval |
| 151 | + * https://developer.apple.com/library/content/qa/qa1931/_index.html |
| 152 | + */ |
| 153 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 154 | + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms |
| 155 | + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode |
| 156 | + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds |
| 157 | +} |
| 158 | + |
| 159 | +void loop() |
| 160 | +{ |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +// callback invoked when central connects |
| 165 | +void connect_callback(uint16_t conn_handle) |
| 166 | +{ |
| 167 | + // Get the reference to current connection |
| 168 | + BLEConnection* connection = Bluefruit.Connection(conn_handle); |
| 169 | + |
| 170 | + char central_name[32] = { 0 }; |
| 171 | + connection->getPeerName(central_name, sizeof(central_name)); |
| 172 | + |
| 173 | + Serial.print("Connected to "); |
| 174 | + Serial.println(central_name); |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * Callback invoked when a connection is dropped |
| 179 | + * @param conn_handle connection where this event happens |
| 180 | + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h |
| 181 | + */ |
| 182 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 183 | +{ |
| 184 | + (void) conn_handle; |
| 185 | + (void) reason; |
| 186 | + |
| 187 | + Serial.println(); |
| 188 | + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); |
| 189 | +} |
0 commit comments