This is a simplified version of RedBearLab's SimpleControls for a one input iOS app

Dependencies:   BLE_API mbed nRF51822

Fork of BLENano_SimpleControls by RedBearLab

Committer:
andynovak
Date:
Wed Feb 24 18:38:54 2016 +0000
Revision:
4:9500e2b7cc36
Parent:
3:f530ca03e014
Made a simplified version of RedBearLab's SimpleControls for a one input iOS app

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:81a97eb70d3d 1 /*
RedBearLab 1:81a97eb70d3d 2
andynovak 4:9500e2b7cc36 3 This code is taken from RedBearLab's SimpleControl example, and stripped down and modified slightly.
andynovak 4:9500e2b7cc36 4
RedBearLab 1:81a97eb70d3d 5 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:81a97eb70d3d 6
RedBearLab 1:81a97eb70d3d 7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:81a97eb70d3d 8 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:81a97eb70d3d 9 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:81a97eb70d3d 10 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:81a97eb70d3d 11 subject to the following conditions:
RedBearLab 1:81a97eb70d3d 12 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:81a97eb70d3d 13
RedBearLab 1:81a97eb70d3d 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:81a97eb70d3d 15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:81a97eb70d3d 16 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:81a97eb70d3d 17 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:81a97eb70d3d 18 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:81a97eb70d3d 19
RedBearLab 1:81a97eb70d3d 20 */
RedBearLab 0:be2e4095513a 21
RedBearLab 0:be2e4095513a 22 #include "mbed.h"
RedBearLab 2:3cd654f42efa 23 #include "ble/BLE.h"
RedBearLab 0:be2e4095513a 24
RedBearLab 0:be2e4095513a 25 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:be2e4095513a 26 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:be2e4095513a 27 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:be2e4095513a 28
RedBearLab 0:be2e4095513a 29 #define TXRX_BUF_LEN 20
RedBearLab 0:be2e4095513a 30
andynovak 4:9500e2b7cc36 31 #define DIGITAL_OUT_PIN p19 //TXD
RedBearLab 0:be2e4095513a 32 #define DIGITAL_IN_PIN P0_10 //CTS
RedBearLab 0:be2e4095513a 33
RedBearLab 2:3cd654f42efa 34 BLE ble;
RedBearLab 0:be2e4095513a 35
RedBearLab 0:be2e4095513a 36 DigitalOut LED_SET(DIGITAL_OUT_PIN);
andynovak 4:9500e2b7cc36 37 DigitalOut LEDOut(P0_5);
RedBearLab 0:be2e4095513a 38 DigitalIn BUTTON(DIGITAL_IN_PIN);
RedBearLab 0:be2e4095513a 39
RedBearLab 0:be2e4095513a 40 static uint8_t old_state = 0;
RedBearLab 0:be2e4095513a 41
RedBearLab 0:be2e4095513a 42 // The Nordic UART Service
RedBearLab 0:be2e4095513a 43 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 44 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 45 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 46 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:be2e4095513a 47
RedBearLab 0:be2e4095513a 48
RedBearLab 0:be2e4095513a 49 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 50 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 51
RedBearLab 0:be2e4095513a 52
andynovak 4:9500e2b7cc36 53 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
andynovak 4:9500e2b7cc36 54 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:be2e4095513a 55 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:be2e4095513a 56 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:be2e4095513a 57
RedBearLab 0:be2e4095513a 58
RedBearLab 3:f530ca03e014 59 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
RedBearLab 0:be2e4095513a 60 {
RedBearLab 3:f530ca03e014 61 ble.gap().startAdvertising();
RedBearLab 0:be2e4095513a 62 }
RedBearLab 0:be2e4095513a 63
RedBearLab 2:3cd654f42efa 64 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:be2e4095513a 65 {
RedBearLab 0:be2e4095513a 66 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 67 uint16_t bytesRead;
RedBearLab 0:be2e4095513a 68
RedBearLab 2:3cd654f42efa 69 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:be2e4095513a 70 {
RedBearLab 0:be2e4095513a 71 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:be2e4095513a 72 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 73 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 74
andynovak 4:9500e2b7cc36 75 //executed when phone connected to Nano
andynovak 4:9500e2b7cc36 76 if(buf[0] == 0x04){
andynovak 4:9500e2b7cc36 77 //have LED blink twice to indicate connection established
andynovak 4:9500e2b7cc36 78 LEDOut = 1;
andynovak 4:9500e2b7cc36 79 wait(0.1);
andynovak 4:9500e2b7cc36 80 LEDOut = 0;
andynovak 4:9500e2b7cc36 81 wait(0.1);
andynovak 4:9500e2b7cc36 82 LEDOut = 1;
andynovak 4:9500e2b7cc36 83 wait(0.1);
andynovak 4:9500e2b7cc36 84 LEDOut = 0;
RedBearLab 0:be2e4095513a 85
andynovak 4:9500e2b7cc36 86 old_state = 1;
andynovak 4:9500e2b7cc36 87 }
RedBearLab 0:be2e4095513a 88 }
RedBearLab 0:be2e4095513a 89 }
andynovak 4:9500e2b7cc36 90
RedBearLab 0:be2e4095513a 91 void m_status_check_handle(void)
RedBearLab 0:be2e4095513a 92 {
RedBearLab 0:be2e4095513a 93 uint8_t buf[3];
RedBearLab 0:be2e4095513a 94
RedBearLab 0:be2e4095513a 95 // If digital in changes, report the state
RedBearLab 0:be2e4095513a 96 if (BUTTON != old_state)
RedBearLab 0:be2e4095513a 97 {
RedBearLab 0:be2e4095513a 98 old_state = BUTTON;
RedBearLab 0:be2e4095513a 99
RedBearLab 0:be2e4095513a 100 if (BUTTON == 1)
RedBearLab 0:be2e4095513a 101 {
RedBearLab 0:be2e4095513a 102 buf[0] = (0x0A);
RedBearLab 0:be2e4095513a 103 buf[1] = (0x01);
andynovak 4:9500e2b7cc36 104 buf[2] = (0x00);
andynovak 4:9500e2b7cc36 105 LED_SET = 1;
andynovak 4:9500e2b7cc36 106 LEDOut = 0;
RedBearLab 0:be2e4095513a 107 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 108 }
RedBearLab 0:be2e4095513a 109 else
RedBearLab 0:be2e4095513a 110 {
RedBearLab 0:be2e4095513a 111 buf[0] = (0x0A);
RedBearLab 0:be2e4095513a 112 buf[1] = (0x00);
RedBearLab 0:be2e4095513a 113 buf[2] = (0x00);
andynovak 4:9500e2b7cc36 114 LED_SET = 0;
andynovak 4:9500e2b7cc36 115 LEDOut = 1;
RedBearLab 0:be2e4095513a 116 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 117 }
RedBearLab 0:be2e4095513a 118 }
RedBearLab 0:be2e4095513a 119 }
RedBearLab 0:be2e4095513a 120
RedBearLab 0:be2e4095513a 121
RedBearLab 0:be2e4095513a 122 int main(void)
RedBearLab 0:be2e4095513a 123 {
RedBearLab 0:be2e4095513a 124 Ticker ticker;
RedBearLab 0:be2e4095513a 125 ticker.attach_us(m_status_check_handle, 200000);
RedBearLab 0:be2e4095513a 126
RedBearLab 0:be2e4095513a 127 ble.init();
RedBearLab 0:be2e4095513a 128 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:be2e4095513a 129 ble.onDataWritten(WrittenHandler);
RedBearLab 0:be2e4095513a 130
RedBearLab 0:be2e4095513a 131 // setup advertising
RedBearLab 0:be2e4095513a 132 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:be2e4095513a 133 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:be2e4095513a 134 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:be2e4095513a 135 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:be2e4095513a 136 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:be2e4095513a 137 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:be2e4095513a 138 // 100ms; in multiples of 0.625ms.
RedBearLab 0:be2e4095513a 139 ble.setAdvertisingInterval(160);
RedBearLab 0:be2e4095513a 140
RedBearLab 0:be2e4095513a 141 ble.addService(uartService);
RedBearLab 0:be2e4095513a 142
RedBearLab 0:be2e4095513a 143 ble.startAdvertising();
RedBearLab 0:be2e4095513a 144
RedBearLab 0:be2e4095513a 145
RedBearLab 0:be2e4095513a 146 while(1)
RedBearLab 0:be2e4095513a 147 {
RedBearLab 0:be2e4095513a 148 ble.waitForEvent();
RedBearLab 0:be2e4095513a 149 }
andynovak 4:9500e2b7cc36 150 }