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.

Dependencies:   BLE_API mbed nRF51822

Fork of BLENano_SimpleControls by RedBearLab

Committer:
RedBearLab
Date:
Fri Oct 31 09:47:34 2014 +0000
Revision:
0:be2e4095513a
Child:
1:81a97eb70d3d
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:be2e4095513a 1
RedBearLab 0:be2e4095513a 2 #include "mbed.h"
RedBearLab 0:be2e4095513a 3 #include "BLEDevice.h"
RedBearLab 0:be2e4095513a 4 #include "Servo.h"
RedBearLab 0:be2e4095513a 5
RedBearLab 0:be2e4095513a 6
RedBearLab 0:be2e4095513a 7 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:be2e4095513a 8 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:be2e4095513a 9 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:be2e4095513a 10
RedBearLab 0:be2e4095513a 11 #define TXRX_BUF_LEN 20
RedBearLab 0:be2e4095513a 12
RedBearLab 0:be2e4095513a 13 #define DIGITAL_OUT_PIN P0_9 //TXD
RedBearLab 0:be2e4095513a 14 #define DIGITAL_IN_PIN P0_10 //CTS
RedBearLab 0:be2e4095513a 15 #define PWM_PIN P0_11 //RXD
RedBearLab 0:be2e4095513a 16 #define SERVO_PIN P0_8 //RTS
RedBearLab 0:be2e4095513a 17 #define ANALOG_IN_PIN P0_4 //P04
RedBearLab 0:be2e4095513a 18
RedBearLab 0:be2e4095513a 19 BLEDevice ble;
RedBearLab 0:be2e4095513a 20
RedBearLab 0:be2e4095513a 21 DigitalOut LED_SET(DIGITAL_OUT_PIN);
RedBearLab 0:be2e4095513a 22 DigitalIn BUTTON(DIGITAL_IN_PIN);
RedBearLab 0:be2e4095513a 23 PwmOut PWM(PWM_PIN);
RedBearLab 0:be2e4095513a 24 AnalogIn ANALOG(ANALOG_IN_PIN);
RedBearLab 0:be2e4095513a 25 Servo MYSERVO(SERVO_PIN);
RedBearLab 0:be2e4095513a 26
RedBearLab 0:be2e4095513a 27 //Serial pc(USBTX, USBRX);
RedBearLab 0:be2e4095513a 28
RedBearLab 0:be2e4095513a 29 static uint8_t analog_enabled = 0;
RedBearLab 0:be2e4095513a 30 static uint8_t old_state = 0;
RedBearLab 0:be2e4095513a 31
RedBearLab 0:be2e4095513a 32 // The Nordic UART Service
RedBearLab 0:be2e4095513a 33 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 34 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 35 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 36 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 37
RedBearLab 0:be2e4095513a 38
RedBearLab 0:be2e4095513a 39 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 40 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 41
RedBearLab 0:be2e4095513a 42 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 43 //static uint8_t rx_len=0;
RedBearLab 0:be2e4095513a 44
RedBearLab 0:be2e4095513a 45
RedBearLab 0:be2e4095513a 46 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:be2e4095513a 47
RedBearLab 0:be2e4095513a 48 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:be2e4095513a 49
RedBearLab 0:be2e4095513a 50 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:be2e4095513a 51
RedBearLab 0:be2e4095513a 52 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:be2e4095513a 53
RedBearLab 0:be2e4095513a 54
RedBearLab 0:be2e4095513a 55
RedBearLab 0:be2e4095513a 56 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:be2e4095513a 57 {
RedBearLab 0:be2e4095513a 58 //pc.printf("Disconnected \r\n");
RedBearLab 0:be2e4095513a 59 //pc.printf("Restart advertising \r\n");
RedBearLab 0:be2e4095513a 60 ble.startAdvertising();
RedBearLab 0:be2e4095513a 61 }
RedBearLab 0:be2e4095513a 62
RedBearLab 0:be2e4095513a 63 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler)
RedBearLab 0:be2e4095513a 64 {
RedBearLab 0:be2e4095513a 65 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 66 uint16_t bytesRead;
RedBearLab 0:be2e4095513a 67
RedBearLab 0:be2e4095513a 68 if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:be2e4095513a 69 {
RedBearLab 0:be2e4095513a 70 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:be2e4095513a 71 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 72 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 73
RedBearLab 0:be2e4095513a 74 //for(index=0; index<bytesRead; index++)
RedBearLab 0:be2e4095513a 75 //pc.putc(buf[index]);
RedBearLab 0:be2e4095513a 76
RedBearLab 0:be2e4095513a 77 if(buf[0] == 0x01)
RedBearLab 0:be2e4095513a 78 {
RedBearLab 0:be2e4095513a 79 if(buf[1] == 0x01)
RedBearLab 0:be2e4095513a 80 LED_SET = 1;
RedBearLab 0:be2e4095513a 81 else
RedBearLab 0:be2e4095513a 82 LED_SET = 0;
RedBearLab 0:be2e4095513a 83 }
RedBearLab 0:be2e4095513a 84 else if(buf[0] == 0xA0)
RedBearLab 0:be2e4095513a 85 {
RedBearLab 0:be2e4095513a 86 if(buf[1] == 0x01)
RedBearLab 0:be2e4095513a 87 analog_enabled = 1;
RedBearLab 0:be2e4095513a 88 else
RedBearLab 0:be2e4095513a 89 analog_enabled = 0;
RedBearLab 0:be2e4095513a 90 }
RedBearLab 0:be2e4095513a 91 else if(buf[0] == 0x02)
RedBearLab 0:be2e4095513a 92 {
RedBearLab 0:be2e4095513a 93 float value = (float)buf[1]/255;
RedBearLab 0:be2e4095513a 94 PWM = value;
RedBearLab 0:be2e4095513a 95 }
RedBearLab 0:be2e4095513a 96 else if(buf[0] == 0x03)
RedBearLab 0:be2e4095513a 97 {
RedBearLab 0:be2e4095513a 98 MYSERVO.write(buf[1]);
RedBearLab 0:be2e4095513a 99 }
RedBearLab 0:be2e4095513a 100 else if(buf[0] == 0x04)
RedBearLab 0:be2e4095513a 101 {
RedBearLab 0:be2e4095513a 102 analog_enabled = 0;
RedBearLab 0:be2e4095513a 103 PWM = 0;
RedBearLab 0:be2e4095513a 104 MYSERVO.write(0);
RedBearLab 0:be2e4095513a 105 LED_SET = 0;
RedBearLab 0:be2e4095513a 106 old_state = 0;
RedBearLab 0:be2e4095513a 107 }
RedBearLab 0:be2e4095513a 108
RedBearLab 0:be2e4095513a 109 }
RedBearLab 0:be2e4095513a 110 }
RedBearLab 0:be2e4095513a 111 /*
RedBearLab 0:be2e4095513a 112 void uartCB(void)
RedBearLab 0:be2e4095513a 113 {
RedBearLab 0:be2e4095513a 114 while(pc.readable())
RedBearLab 0:be2e4095513a 115 {
RedBearLab 0:be2e4095513a 116 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:be2e4095513a 117 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:be2e4095513a 118 {
RedBearLab 0:be2e4095513a 119 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:be2e4095513a 120 pc.printf("RecHandler \r\n");
RedBearLab 0:be2e4095513a 121 pc.printf("Length: ");
RedBearLab 0:be2e4095513a 122 pc.putc(rx_len);
RedBearLab 0:be2e4095513a 123 pc.printf("\r\n");
RedBearLab 0:be2e4095513a 124 rx_len = 0;
RedBearLab 0:be2e4095513a 125 break;
RedBearLab 0:be2e4095513a 126 }
RedBearLab 0:be2e4095513a 127 }
RedBearLab 0:be2e4095513a 128 }
RedBearLab 0:be2e4095513a 129 */
RedBearLab 0:be2e4095513a 130 void m_status_check_handle(void)
RedBearLab 0:be2e4095513a 131 {
RedBearLab 0:be2e4095513a 132 uint8_t buf[3];
RedBearLab 0:be2e4095513a 133 if (analog_enabled) // if analog reading enabled
RedBearLab 0:be2e4095513a 134 {
RedBearLab 0:be2e4095513a 135 // Read and send out
RedBearLab 0:be2e4095513a 136 float s = ANALOG;
RedBearLab 0:be2e4095513a 137 uint16_t value = s*1024;
RedBearLab 0:be2e4095513a 138 buf[0] = (0x0B);
RedBearLab 0:be2e4095513a 139 buf[1] = (value >> 8);
RedBearLab 0:be2e4095513a 140 buf[2] = (value);
RedBearLab 0:be2e4095513a 141 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 142 }
RedBearLab 0:be2e4095513a 143
RedBearLab 0:be2e4095513a 144 // If digital in changes, report the state
RedBearLab 0:be2e4095513a 145 if (BUTTON != old_state)
RedBearLab 0:be2e4095513a 146 {
RedBearLab 0:be2e4095513a 147 old_state = BUTTON;
RedBearLab 0:be2e4095513a 148
RedBearLab 0:be2e4095513a 149 if (BUTTON == 1)
RedBearLab 0:be2e4095513a 150 {
RedBearLab 0:be2e4095513a 151 buf[0] = (0x0A);
RedBearLab 0:be2e4095513a 152 buf[1] = (0x01);
RedBearLab 0:be2e4095513a 153 buf[2] = (0x00);
RedBearLab 0:be2e4095513a 154 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 155 }
RedBearLab 0:be2e4095513a 156 else
RedBearLab 0:be2e4095513a 157 {
RedBearLab 0:be2e4095513a 158 buf[0] = (0x0A);
RedBearLab 0:be2e4095513a 159 buf[1] = (0x00);
RedBearLab 0:be2e4095513a 160 buf[2] = (0x00);
RedBearLab 0:be2e4095513a 161 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 162 }
RedBearLab 0:be2e4095513a 163 }
RedBearLab 0:be2e4095513a 164 }
RedBearLab 0:be2e4095513a 165
RedBearLab 0:be2e4095513a 166
RedBearLab 0:be2e4095513a 167 int main(void)
RedBearLab 0:be2e4095513a 168 {
RedBearLab 0:be2e4095513a 169 Ticker ticker;
RedBearLab 0:be2e4095513a 170 ticker.attach_us(m_status_check_handle, 200000);
RedBearLab 0:be2e4095513a 171
RedBearLab 0:be2e4095513a 172 ble.init();
RedBearLab 0:be2e4095513a 173 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:be2e4095513a 174 ble.onDataWritten(WrittenHandler);
RedBearLab 0:be2e4095513a 175
RedBearLab 0:be2e4095513a 176 //pc.baud(9600);
RedBearLab 0:be2e4095513a 177 //pc.printf("SimpleChat Init \r\n");
RedBearLab 0:be2e4095513a 178
RedBearLab 0:be2e4095513a 179 //pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:be2e4095513a 180
RedBearLab 0:be2e4095513a 181 // setup advertising
RedBearLab 0:be2e4095513a 182 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:be2e4095513a 183 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:be2e4095513a 184 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:be2e4095513a 185 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:be2e4095513a 186 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:be2e4095513a 187 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:be2e4095513a 188 // 100ms; in multiples of 0.625ms.
RedBearLab 0:be2e4095513a 189 ble.setAdvertisingInterval(160);
RedBearLab 0:be2e4095513a 190
RedBearLab 0:be2e4095513a 191 ble.addService(uartService);
RedBearLab 0:be2e4095513a 192
RedBearLab 0:be2e4095513a 193 ble.startAdvertising();
RedBearLab 0:be2e4095513a 194
RedBearLab 0:be2e4095513a 195 //pc.printf("Advertising Start \r\n");
RedBearLab 0:be2e4095513a 196
RedBearLab 0:be2e4095513a 197 while(1)
RedBearLab 0:be2e4095513a 198 {
RedBearLab 0:be2e4095513a 199 ble.waitForEvent();
RedBearLab 0:be2e4095513a 200 }
RedBearLab 0:be2e4095513a 201 }
RedBearLab 0:be2e4095513a 202
RedBearLab 0:be2e4095513a 203
RedBearLab 0:be2e4095513a 204
RedBearLab 0:be2e4095513a 205
RedBearLab 0:be2e4095513a 206
RedBearLab 0:be2e4095513a 207
RedBearLab 0:be2e4095513a 208
RedBearLab 0:be2e4095513a 209
RedBearLab 0:be2e4095513a 210
RedBearLab 0:be2e4095513a 211
RedBearLab 0:be2e4095513a 212
RedBearLab 0:be2e4095513a 213
RedBearLab 0:be2e4095513a 214
RedBearLab 0:be2e4095513a 215
RedBearLab 0:be2e4095513a 216
RedBearLab 0:be2e4095513a 217
RedBearLab 0:be2e4095513a 218
RedBearLab 0:be2e4095513a 219
RedBearLab 0:be2e4095513a 220
RedBearLab 0:be2e4095513a 221
RedBearLab 0:be2e4095513a 222
RedBearLab 0:be2e4095513a 223
RedBearLab 0:be2e4095513a 224
RedBearLab 0:be2e4095513a 225
RedBearLab 0:be2e4095513a 226
RedBearLab 0:be2e4095513a 227
RedBearLab 0:be2e4095513a 228
RedBearLab 0:be2e4095513a 229
RedBearLab 0:be2e4095513a 230
RedBearLab 0:be2e4095513a 231
RedBearLab 0:be2e4095513a 232