Advent Calendar 2015用BLEPeripheral

Dependencies:   BLE_API mbed nRF51822

Fork of BLENano_SimpleControls by RedBearLab

Committer:
yueee_yt
Date:
Fri Dec 18 13:53:29 2015 +0000
Revision:
3:793b1f3e439b
Parent:
2:3cd654f42efa
Advent Calndar 2015

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:81a97eb70d3d 1 /*
RedBearLab 1:81a97eb70d3d 2
RedBearLab 1:81a97eb70d3d 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:81a97eb70d3d 4
RedBearLab 1:81a97eb70d3d 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:81a97eb70d3d 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:81a97eb70d3d 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:81a97eb70d3d 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:81a97eb70d3d 9 subject to the following conditions:
RedBearLab 1:81a97eb70d3d 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:81a97eb70d3d 11
RedBearLab 1:81a97eb70d3d 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:81a97eb70d3d 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:81a97eb70d3d 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:81a97eb70d3d 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:81a97eb70d3d 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:81a97eb70d3d 17
RedBearLab 1:81a97eb70d3d 18 */
RedBearLab 0:be2e4095513a 19
RedBearLab 0:be2e4095513a 20 #include "mbed.h"
RedBearLab 2:3cd654f42efa 21 #include "ble/BLE.h"
RedBearLab 0:be2e4095513a 22
RedBearLab 0:be2e4095513a 23
RedBearLab 0:be2e4095513a 24 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:be2e4095513a 25 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:be2e4095513a 26 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:be2e4095513a 27
RedBearLab 0:be2e4095513a 28 #define TXRX_BUF_LEN 20
RedBearLab 0:be2e4095513a 29
yueee_yt 3:793b1f3e439b 30 #define PowerLED_PIN P0_4
yueee_yt 3:793b1f3e439b 31 #define ControlLED_PIN P0_5
RedBearLab 0:be2e4095513a 32
RedBearLab 2:3cd654f42efa 33 BLE ble;
RedBearLab 0:be2e4095513a 34
yueee_yt 3:793b1f3e439b 35 DigitalOut LED_POWER(PowerLED_PIN);
yueee_yt 3:793b1f3e439b 36 DigitalOut LED_CONTROL(ControlLED_PIN);
RedBearLab 0:be2e4095513a 37
RedBearLab 0:be2e4095513a 38 //Serial pc(USBTX, USBRX);
RedBearLab 0:be2e4095513a 39
yueee_yt 3:793b1f3e439b 40 //static uint8_t analog_enabled = 0;
yueee_yt 3:793b1f3e439b 41 //static uint8_t old_state = 0;
RedBearLab 0:be2e4095513a 42
RedBearLab 0:be2e4095513a 43 // The Nordic UART Service
RedBearLab 0:be2e4095513a 44 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 45 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 46 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 47 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 48
RedBearLab 0:be2e4095513a 49
RedBearLab 0:be2e4095513a 50 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 51 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 52
RedBearLab 0:be2e4095513a 53 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 54 //static uint8_t rx_len=0;
RedBearLab 0:be2e4095513a 55
RedBearLab 0:be2e4095513a 56
RedBearLab 0:be2e4095513a 57 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 58
RedBearLab 0:be2e4095513a 59 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:be2e4095513a 60
RedBearLab 0:be2e4095513a 61 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:be2e4095513a 62
RedBearLab 0:be2e4095513a 63 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:be2e4095513a 64
yueee_yt 3:793b1f3e439b 65 void connectionCallback(const Gap::ConnectionCallbackParams_t *param)
yueee_yt 3:793b1f3e439b 66 {
yueee_yt 3:793b1f3e439b 67 LED_POWER=1;
yueee_yt 3:793b1f3e439b 68 }
RedBearLab 0:be2e4095513a 69
yueee_yt 3:793b1f3e439b 70 //void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yueee_yt 3:793b1f3e439b 71 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *param)
RedBearLab 0:be2e4095513a 72 {
yueee_yt 3:793b1f3e439b 73 LED_POWER=0;
RedBearLab 0:be2e4095513a 74 //pc.printf("Disconnected \r\n");
RedBearLab 0:be2e4095513a 75 //pc.printf("Restart advertising \r\n");
RedBearLab 0:be2e4095513a 76 ble.startAdvertising();
RedBearLab 0:be2e4095513a 77 }
RedBearLab 0:be2e4095513a 78
RedBearLab 2:3cd654f42efa 79 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:be2e4095513a 80 {
RedBearLab 0:be2e4095513a 81 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 82 uint16_t bytesRead;
RedBearLab 0:be2e4095513a 83
RedBearLab 2:3cd654f42efa 84 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:be2e4095513a 85 {
RedBearLab 0:be2e4095513a 86 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:be2e4095513a 87 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 88 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 89
RedBearLab 0:be2e4095513a 90 //for(index=0; index<bytesRead; index++)
RedBearLab 0:be2e4095513a 91 //pc.putc(buf[index]);
RedBearLab 0:be2e4095513a 92
yueee_yt 3:793b1f3e439b 93 if(buf[0] == 0x01) LED_CONTROL=1;
yueee_yt 3:793b1f3e439b 94 else if(buf[0] == 0x00) LED_CONTROL=0;
RedBearLab 0:be2e4095513a 95 }
RedBearLab 0:be2e4095513a 96 }
RedBearLab 0:be2e4095513a 97 /*
RedBearLab 0:be2e4095513a 98 void uartCB(void)
RedBearLab 0:be2e4095513a 99 {
RedBearLab 0:be2e4095513a 100 while(pc.readable())
RedBearLab 0:be2e4095513a 101 {
RedBearLab 0:be2e4095513a 102 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:be2e4095513a 103 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:be2e4095513a 104 {
RedBearLab 0:be2e4095513a 105 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:be2e4095513a 106 pc.printf("RecHandler \r\n");
RedBearLab 0:be2e4095513a 107 pc.printf("Length: ");
RedBearLab 0:be2e4095513a 108 pc.putc(rx_len);
RedBearLab 0:be2e4095513a 109 pc.printf("\r\n");
RedBearLab 0:be2e4095513a 110 rx_len = 0;
RedBearLab 0:be2e4095513a 111 break;
RedBearLab 0:be2e4095513a 112 }
RedBearLab 0:be2e4095513a 113 }
RedBearLab 0:be2e4095513a 114 }
RedBearLab 0:be2e4095513a 115 */
RedBearLab 0:be2e4095513a 116 void m_status_check_handle(void)
RedBearLab 0:be2e4095513a 117 {
RedBearLab 0:be2e4095513a 118 }
RedBearLab 0:be2e4095513a 119
RedBearLab 0:be2e4095513a 120
RedBearLab 0:be2e4095513a 121 int main(void)
RedBearLab 0:be2e4095513a 122 {
yueee_yt 3:793b1f3e439b 123 // Ticker ticker;
yueee_yt 3:793b1f3e439b 124 // ticker.attach_us(m_status_check_handle, 200000);
RedBearLab 0:be2e4095513a 125
RedBearLab 0:be2e4095513a 126 ble.init();
yueee_yt 3:793b1f3e439b 127 ble.onConnection(connectionCallback);
RedBearLab 0:be2e4095513a 128 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:be2e4095513a 129 ble.onDataWritten(WrittenHandler);
RedBearLab 0:be2e4095513a 130
RedBearLab 0:be2e4095513a 131 //pc.baud(9600);
RedBearLab 0:be2e4095513a 132 //pc.printf("SimpleChat Init \r\n");
RedBearLab 0:be2e4095513a 133
RedBearLab 0:be2e4095513a 134 //pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:be2e4095513a 135
RedBearLab 0:be2e4095513a 136 // setup advertising
RedBearLab 0:be2e4095513a 137 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:be2e4095513a 138 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
yueee_yt 3:793b1f3e439b 139 //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
yueee_yt 3:793b1f3e439b 140 // (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
yueee_yt 3:793b1f3e439b 141 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
RedBearLab 0:be2e4095513a 142 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:be2e4095513a 143 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:be2e4095513a 144 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:be2e4095513a 145 // 100ms; in multiples of 0.625ms.
RedBearLab 0:be2e4095513a 146 ble.setAdvertisingInterval(160);
RedBearLab 0:be2e4095513a 147
RedBearLab 0:be2e4095513a 148 ble.addService(uartService);
RedBearLab 0:be2e4095513a 149
RedBearLab 0:be2e4095513a 150 ble.startAdvertising();
RedBearLab 0:be2e4095513a 151
RedBearLab 0:be2e4095513a 152 //pc.printf("Advertising Start \r\n");
RedBearLab 0:be2e4095513a 153
RedBearLab 0:be2e4095513a 154 while(1)
RedBearLab 0:be2e4095513a 155 {
RedBearLab 0:be2e4095513a 156 ble.waitForEvent();
RedBearLab 0:be2e4095513a 157 }
RedBearLab 0:be2e4095513a 158 }
RedBearLab 0:be2e4095513a 159
RedBearLab 0:be2e4095513a 160
RedBearLab 0:be2e4095513a 161
RedBearLab 0:be2e4095513a 162
RedBearLab 0:be2e4095513a 163
RedBearLab 0:be2e4095513a 164
RedBearLab 0:be2e4095513a 165
RedBearLab 0:be2e4095513a 166
RedBearLab 0:be2e4095513a 167
RedBearLab 0:be2e4095513a 168
RedBearLab 0:be2e4095513a 169
RedBearLab 0:be2e4095513a 170
RedBearLab 0:be2e4095513a 171
RedBearLab 0:be2e4095513a 172
RedBearLab 0:be2e4095513a 173
RedBearLab 0:be2e4095513a 174
RedBearLab 0:be2e4095513a 175
RedBearLab 0:be2e4095513a 176
RedBearLab 0:be2e4095513a 177
RedBearLab 0:be2e4095513a 178
RedBearLab 0:be2e4095513a 179
RedBearLab 0:be2e4095513a 180
RedBearLab 0:be2e4095513a 181
RedBearLab 0:be2e4095513a 182
RedBearLab 0:be2e4095513a 183
RedBearLab 0:be2e4095513a 184
RedBearLab 0:be2e4095513a 185
RedBearLab 0:be2e4095513a 186
RedBearLab 0:be2e4095513a 187
RedBearLab 0:be2e4095513a 188
RedBearLab 0:be2e4095513a 189