SimpleControls works with the BLEController iOS/Android App. The mbed's pin can acts as DIGITAL_IN, DIGITAL_OUT, PWM, SERVO, ANALOG_IN. The sketch is to show you how to control the pin as one of the abilities. Note that not every pin can own all of the abilities.

Dependencies:   BLE_API mbed nRF51822

Committer:
RedBearLab
Date:
Thu Jan 07 02:49:37 2016 +0000
Revision:
3:823f105078c7
Parent:
2:bbcdd23ba9ba
Update libraries.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:f03072e32ed3 1 /*
RedBearLab 1:f03072e32ed3 2
RedBearLab 1:f03072e32ed3 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:f03072e32ed3 4
RedBearLab 1:f03072e32ed3 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:f03072e32ed3 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:f03072e32ed3 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:f03072e32ed3 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:f03072e32ed3 9 subject to the following conditions:
RedBearLab 1:f03072e32ed3 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:f03072e32ed3 11
RedBearLab 1:f03072e32ed3 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:f03072e32ed3 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:f03072e32ed3 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:f03072e32ed3 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:f03072e32ed3 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:f03072e32ed3 17
RedBearLab 1:f03072e32ed3 18 */
RedBearLab 0:dfcebc1e442a 19
RedBearLab 0:dfcebc1e442a 20 #include "mbed.h"
RedBearLab 2:bbcdd23ba9ba 21 #include "ble/BLE.h"
RedBearLab 0:dfcebc1e442a 22 #include "Servo.h"
RedBearLab 2:bbcdd23ba9ba 23 #include "GattCallbackParamTypes.h"
RedBearLab 0:dfcebc1e442a 24
RedBearLab 0:dfcebc1e442a 25 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:dfcebc1e442a 26 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:dfcebc1e442a 27 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:dfcebc1e442a 28
RedBearLab 0:dfcebc1e442a 29 #define TXRX_BUF_LEN 20
RedBearLab 0:dfcebc1e442a 30
RedBearLab 0:dfcebc1e442a 31 #define DIGITAL_OUT_PIN P0_17 //D7
RedBearLab 0:dfcebc1e442a 32 #define DIGITAL_IN_PIN P0_5 //A4
RedBearLab 0:dfcebc1e442a 33 #define PWM_PIN P0_16 //D6
RedBearLab 0:dfcebc1e442a 34 #define SERVO_PIN P0_14 //D10
RedBearLab 0:dfcebc1e442a 35 #define ANALOG_IN_PIN P0_6 //A5
RedBearLab 0:dfcebc1e442a 36
RedBearLab 2:bbcdd23ba9ba 37 BLE ble;
RedBearLab 0:dfcebc1e442a 38
RedBearLab 0:dfcebc1e442a 39 DigitalOut LED_SET(DIGITAL_OUT_PIN);
RedBearLab 0:dfcebc1e442a 40 DigitalIn BUTTON(DIGITAL_IN_PIN);
RedBearLab 0:dfcebc1e442a 41 PwmOut PWM(PWM_PIN);
RedBearLab 0:dfcebc1e442a 42 AnalogIn ANALOG(ANALOG_IN_PIN);
RedBearLab 0:dfcebc1e442a 43 Servo MYSERVO(SERVO_PIN);
RedBearLab 0:dfcebc1e442a 44
RedBearLab 0:dfcebc1e442a 45 Serial pc(USBTX, USBRX);
RedBearLab 0:dfcebc1e442a 46
RedBearLab 0:dfcebc1e442a 47 static uint8_t analog_enabled = 0;
RedBearLab 0:dfcebc1e442a 48 static uint8_t old_state = 0;
RedBearLab 0:dfcebc1e442a 49
RedBearLab 0:dfcebc1e442a 50 // The Nordic UART Service
RedBearLab 0:dfcebc1e442a 51 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:dfcebc1e442a 52 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:dfcebc1e442a 53 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:dfcebc1e442a 54 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:dfcebc1e442a 55
RedBearLab 0:dfcebc1e442a 56
RedBearLab 0:dfcebc1e442a 57 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:dfcebc1e442a 58 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:dfcebc1e442a 59
RedBearLab 0:dfcebc1e442a 60 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:dfcebc1e442a 61 //static uint8_t rx_len=0;
RedBearLab 0:dfcebc1e442a 62
RedBearLab 0:dfcebc1e442a 63
RedBearLab 0:dfcebc1e442a 64 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
RedBearLab 0:dfcebc1e442a 65
RedBearLab 0:dfcebc1e442a 66 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:dfcebc1e442a 67
RedBearLab 0:dfcebc1e442a 68 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:dfcebc1e442a 69
RedBearLab 0:dfcebc1e442a 70 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:dfcebc1e442a 71
RedBearLab 0:dfcebc1e442a 72
RedBearLab 0:dfcebc1e442a 73
RedBearLab 3:823f105078c7 74 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
RedBearLab 0:dfcebc1e442a 75 {
RedBearLab 0:dfcebc1e442a 76 pc.printf("Disconnected \r\n");
RedBearLab 0:dfcebc1e442a 77 pc.printf("Restart advertising \r\n");
RedBearLab 0:dfcebc1e442a 78 ble.startAdvertising();
RedBearLab 0:dfcebc1e442a 79 }
RedBearLab 0:dfcebc1e442a 80
RedBearLab 2:bbcdd23ba9ba 81 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:dfcebc1e442a 82 {
RedBearLab 0:dfcebc1e442a 83 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:dfcebc1e442a 84 uint16_t bytesRead, index;
RedBearLab 0:dfcebc1e442a 85
RedBearLab 2:bbcdd23ba9ba 86 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:dfcebc1e442a 87 {
RedBearLab 0:dfcebc1e442a 88 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:dfcebc1e442a 89 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:dfcebc1e442a 90 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:dfcebc1e442a 91
RedBearLab 0:dfcebc1e442a 92 for(index=0; index<bytesRead; index++)
RedBearLab 0:dfcebc1e442a 93 pc.putc(buf[index]);
RedBearLab 0:dfcebc1e442a 94
RedBearLab 0:dfcebc1e442a 95 if(buf[0] == 0x01)
RedBearLab 0:dfcebc1e442a 96 {
RedBearLab 0:dfcebc1e442a 97 if(buf[1] == 0x01)
RedBearLab 0:dfcebc1e442a 98 LED_SET = 1;
RedBearLab 0:dfcebc1e442a 99 else
RedBearLab 0:dfcebc1e442a 100 LED_SET = 0;
RedBearLab 0:dfcebc1e442a 101 }
RedBearLab 0:dfcebc1e442a 102 else if(buf[0] == 0xA0)
RedBearLab 0:dfcebc1e442a 103 {
RedBearLab 0:dfcebc1e442a 104 if(buf[1] == 0x01)
RedBearLab 0:dfcebc1e442a 105 analog_enabled = 1;
RedBearLab 0:dfcebc1e442a 106 else
RedBearLab 0:dfcebc1e442a 107 analog_enabled = 0;
RedBearLab 0:dfcebc1e442a 108 }
RedBearLab 0:dfcebc1e442a 109 else if(buf[0] == 0x02)
RedBearLab 0:dfcebc1e442a 110 {
RedBearLab 0:dfcebc1e442a 111 float value = (float)buf[1]/255;
RedBearLab 0:dfcebc1e442a 112 PWM = value;
RedBearLab 0:dfcebc1e442a 113 }
RedBearLab 0:dfcebc1e442a 114 else if(buf[0] == 0x03)
RedBearLab 0:dfcebc1e442a 115 {
RedBearLab 0:dfcebc1e442a 116 MYSERVO.write(buf[1]);
RedBearLab 0:dfcebc1e442a 117 }
RedBearLab 0:dfcebc1e442a 118 else if(buf[0] == 0x04)
RedBearLab 0:dfcebc1e442a 119 {
RedBearLab 0:dfcebc1e442a 120 analog_enabled = 0;
RedBearLab 0:dfcebc1e442a 121 PWM = 0;
RedBearLab 0:dfcebc1e442a 122 MYSERVO.write(0);
RedBearLab 0:dfcebc1e442a 123 LED_SET = 0;
RedBearLab 0:dfcebc1e442a 124 old_state = 0;
RedBearLab 0:dfcebc1e442a 125 }
RedBearLab 0:dfcebc1e442a 126
RedBearLab 0:dfcebc1e442a 127 }
RedBearLab 0:dfcebc1e442a 128 }
RedBearLab 0:dfcebc1e442a 129 /*
RedBearLab 0:dfcebc1e442a 130 void uartCB(void)
RedBearLab 0:dfcebc1e442a 131 {
RedBearLab 0:dfcebc1e442a 132 while(pc.readable())
RedBearLab 0:dfcebc1e442a 133 {
RedBearLab 0:dfcebc1e442a 134 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:dfcebc1e442a 135 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:dfcebc1e442a 136 {
RedBearLab 0:dfcebc1e442a 137 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:dfcebc1e442a 138 pc.printf("RecHandler \r\n");
RedBearLab 0:dfcebc1e442a 139 pc.printf("Length: ");
RedBearLab 0:dfcebc1e442a 140 pc.putc(rx_len);
RedBearLab 0:dfcebc1e442a 141 pc.printf("\r\n");
RedBearLab 0:dfcebc1e442a 142 rx_len = 0;
RedBearLab 0:dfcebc1e442a 143 break;
RedBearLab 0:dfcebc1e442a 144 }
RedBearLab 0:dfcebc1e442a 145 }
RedBearLab 0:dfcebc1e442a 146 }
RedBearLab 0:dfcebc1e442a 147 */
RedBearLab 0:dfcebc1e442a 148 void m_status_check_handle(void)
RedBearLab 0:dfcebc1e442a 149 {
RedBearLab 0:dfcebc1e442a 150 uint8_t buf[3];
RedBearLab 0:dfcebc1e442a 151 if (analog_enabled) // if analog reading enabled
RedBearLab 0:dfcebc1e442a 152 {
RedBearLab 0:dfcebc1e442a 153 // Read and send out
RedBearLab 0:dfcebc1e442a 154 float s = ANALOG;
RedBearLab 0:dfcebc1e442a 155 uint16_t value = s*1024;
RedBearLab 0:dfcebc1e442a 156 buf[0] = (0x0B);
RedBearLab 0:dfcebc1e442a 157 buf[1] = (value >> 8);
RedBearLab 0:dfcebc1e442a 158 buf[2] = (value);
RedBearLab 0:dfcebc1e442a 159 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:dfcebc1e442a 160 }
RedBearLab 0:dfcebc1e442a 161
RedBearLab 0:dfcebc1e442a 162 // If digital in changes, report the state
RedBearLab 0:dfcebc1e442a 163 if (BUTTON != old_state)
RedBearLab 0:dfcebc1e442a 164 {
RedBearLab 0:dfcebc1e442a 165 old_state = BUTTON;
RedBearLab 0:dfcebc1e442a 166
RedBearLab 0:dfcebc1e442a 167 if (BUTTON == 1)
RedBearLab 0:dfcebc1e442a 168 {
RedBearLab 0:dfcebc1e442a 169 buf[0] = (0x0A);
RedBearLab 0:dfcebc1e442a 170 buf[1] = (0x01);
RedBearLab 0:dfcebc1e442a 171 buf[2] = (0x00);
RedBearLab 0:dfcebc1e442a 172 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:dfcebc1e442a 173 }
RedBearLab 0:dfcebc1e442a 174 else
RedBearLab 0:dfcebc1e442a 175 {
RedBearLab 0:dfcebc1e442a 176 buf[0] = (0x0A);
RedBearLab 0:dfcebc1e442a 177 buf[1] = (0x00);
RedBearLab 0:dfcebc1e442a 178 buf[2] = (0x00);
RedBearLab 0:dfcebc1e442a 179 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:dfcebc1e442a 180 }
RedBearLab 0:dfcebc1e442a 181 }
RedBearLab 0:dfcebc1e442a 182 }
RedBearLab 0:dfcebc1e442a 183
RedBearLab 0:dfcebc1e442a 184
RedBearLab 0:dfcebc1e442a 185 int main(void)
RedBearLab 0:dfcebc1e442a 186 {
RedBearLab 0:dfcebc1e442a 187 Ticker ticker;
RedBearLab 0:dfcebc1e442a 188 ticker.attach_us(m_status_check_handle, 200000);
RedBearLab 0:dfcebc1e442a 189
RedBearLab 0:dfcebc1e442a 190 ble.init();
RedBearLab 0:dfcebc1e442a 191 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:dfcebc1e442a 192 ble.onDataWritten(WrittenHandler);
RedBearLab 0:dfcebc1e442a 193
RedBearLab 0:dfcebc1e442a 194 pc.baud(9600);
RedBearLab 0:dfcebc1e442a 195 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:dfcebc1e442a 196
RedBearLab 0:dfcebc1e442a 197 //pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:dfcebc1e442a 198
RedBearLab 0:dfcebc1e442a 199 // setup advertising
RedBearLab 0:dfcebc1e442a 200 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:dfcebc1e442a 201 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:dfcebc1e442a 202 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:dfcebc1e442a 203 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:dfcebc1e442a 204 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:dfcebc1e442a 205 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:dfcebc1e442a 206 // 100ms; in multiples of 0.625ms.
RedBearLab 0:dfcebc1e442a 207 ble.setAdvertisingInterval(160);
RedBearLab 0:dfcebc1e442a 208
RedBearLab 0:dfcebc1e442a 209 ble.addService(uartService);
RedBearLab 0:dfcebc1e442a 210
RedBearLab 0:dfcebc1e442a 211 ble.startAdvertising();
RedBearLab 0:dfcebc1e442a 212
RedBearLab 0:dfcebc1e442a 213 pc.printf("Advertising Start \r\n");
RedBearLab 0:dfcebc1e442a 214
RedBearLab 0:dfcebc1e442a 215 while(1)
RedBearLab 0:dfcebc1e442a 216 {
RedBearLab 0:dfcebc1e442a 217 ble.waitForEvent();
RedBearLab 0:dfcebc1e442a 218 }
RedBearLab 0:dfcebc1e442a 219 }
RedBearLab 0:dfcebc1e442a 220
RedBearLab 0:dfcebc1e442a 221
RedBearLab 0:dfcebc1e442a 222
RedBearLab 0:dfcebc1e442a 223
RedBearLab 0:dfcebc1e442a 224
RedBearLab 0:dfcebc1e442a 225
RedBearLab 0:dfcebc1e442a 226
RedBearLab 0:dfcebc1e442a 227
RedBearLab 0:dfcebc1e442a 228
RedBearLab 0:dfcebc1e442a 229
RedBearLab 0:dfcebc1e442a 230
RedBearLab 0:dfcebc1e442a 231
RedBearLab 0:dfcebc1e442a 232
RedBearLab 0:dfcebc1e442a 233
RedBearLab 0:dfcebc1e442a 234
RedBearLab 0:dfcebc1e442a 235
RedBearLab 0:dfcebc1e442a 236
RedBearLab 0:dfcebc1e442a 237
RedBearLab 0:dfcebc1e442a 238
RedBearLab 0:dfcebc1e442a 239
RedBearLab 0:dfcebc1e442a 240
RedBearLab 0:dfcebc1e442a 241
RedBearLab 0:dfcebc1e442a 242
RedBearLab 0:dfcebc1e442a 243
RedBearLab 0:dfcebc1e442a 244
RedBearLab 0:dfcebc1e442a 245
RedBearLab 0:dfcebc1e442a 246
RedBearLab 0:dfcebc1e442a 247
RedBearLab 0:dfcebc1e442a 248
RedBearLab 0:dfcebc1e442a 249
RedBearLab 0:dfcebc1e442a 250
RedBearLab 0:dfcebc1e442a 251