USonicLibに対応したMSC

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Committer:
ke_ix1
Date:
Sat Dec 03 03:22:17 2016 +0000
Revision:
4:ddf414d64ea9
Parent:
3:175ba3a059cb
Child:
5:29b82cc045b6
??1????????MSC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:1c058e553423 1 /*
RedBearLab 0:cffe8ac1bdf0 2
RedBearLab 1:1c058e553423 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:1c058e553423 4
RedBearLab 1:1c058e553423 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:1c058e553423 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:1c058e553423 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:1c058e553423 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:1c058e553423 9 subject to the following conditions:
RedBearLab 1:1c058e553423 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:1c058e553423 11
RedBearLab 1:1c058e553423 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:1c058e553423 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:1c058e553423 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:1c058e553423 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:1c058e553423 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:1c058e553423 17
RedBearLab 1:1c058e553423 18 */
RedBearLab 1:1c058e553423 19
RedBearLab 1:1c058e553423 20 /*
RedBearLab 1:1c058e553423 21 * The application works with the BLEController iOS/Android App.
RedBearLab 1:1c058e553423 22 * Type something from the Terminal to send
RedBearLab 1:1c058e553423 23 * to the BLEController App or vice verse.
RedBearLab 1:1c058e553423 24 * Characteristics received from App will print on Terminal.
RedBearLab 1:1c058e553423 25 */
RedBearLab 1:1c058e553423 26
RedBearLab 0:cffe8ac1bdf0 27 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 28 #include "ble/BLE.h"
ke_ix1 3:175ba3a059cb 29 #include <string.h>
ke_ix1 4:ddf414d64ea9 30
ke_ix1 3:175ba3a059cb 31 //#include <stdio.h>
RedBearLab 0:cffe8ac1bdf0 32
RedBearLab 0:cffe8ac1bdf0 33
RedBearLab 0:cffe8ac1bdf0 34 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 35 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 36 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 37
RedBearLab 0:cffe8ac1bdf0 38 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 39
RedBearLab 2:4b66b69c7ecb 40 BLE ble;
RedBearLab 0:cffe8ac1bdf0 41
RedBearLab 0:cffe8ac1bdf0 42 Serial pc(USBTX, USBRX);
ke_ix1 3:175ba3a059cb 43 Ticker steper;
ke_ix1 3:175ba3a059cb 44
RedBearLab 0:cffe8ac1bdf0 45
RedBearLab 0:cffe8ac1bdf0 46
RedBearLab 0:cffe8ac1bdf0 47 // The Nordic UART Service
ke_ix1 4:ddf414d64ea9 48 //static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
ke_ix1 4:ddf414d64ea9 49 //static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
ke_ix1 4:ddf414d64ea9 50 //static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
ke_ix1 4:ddf414d64ea9 51 //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:cffe8ac1bdf0 52
ke_ix1 4:ddf414d64ea9 53 // Send One CLK Service
ke_ix1 4:ddf414d64ea9 54 static const uint8_t uart_base_uuid[] = {0x53, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x7C}; //Service
ke_ix1 4:ddf414d64ea9 55 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_送信用
ke_ix1 4:ddf414d64ea9 56 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_受信用
ke_ix1 4:ddf414d64ea9 57 static const uint8_t uart_base_uuid_rev[] = {0x7C, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x53};
ke_ix1 4:ddf414d64ea9 58
ke_ix1 4:ddf414d64ea9 59 //aaaaaaaaaaaaaaa
RedBearLab 0:cffe8ac1bdf0 60
RedBearLab 0:cffe8ac1bdf0 61 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 62 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 63
RedBearLab 0:cffe8ac1bdf0 64 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 65 static uint8_t rx_len=0;
ke_ix1 3:175ba3a059cb 66 static uint8_t rx_buf_st[20];
ke_ix1 4:ddf414d64ea9 67
ke_ix1 4:ddf414d64ea9 68 int8_t buf_before[40];
ke_ix1 4:ddf414d64ea9 69
ke_ix1 4:ddf414d64ea9 70 static int st = 0;
ke_ix1 4:ddf414d64ea9 71
RedBearLab 0:cffe8ac1bdf0 72
RedBearLab 0:cffe8ac1bdf0 73 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:cffe8ac1bdf0 74
RedBearLab 0:cffe8ac1bdf0 75 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 76
RedBearLab 0:cffe8ac1bdf0 77 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 78
RedBearLab 0:cffe8ac1bdf0 79 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 80
RedBearLab 0:cffe8ac1bdf0 81 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 82 {
RedBearLab 0:cffe8ac1bdf0 83 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 84 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 85 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 86 }
RedBearLab 0:cffe8ac1bdf0 87
ke_ix1 3:175ba3a059cb 88
ke_ix1 3:175ba3a059cb 89 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
ke_ix1 3:175ba3a059cb 90 {
ke_ix1 4:ddf414d64ea9 91 //printf("onScanNau\n");
ke_ix1 4:ddf414d64ea9 92
ke_ix1 4:ddf414d64ea9 93 // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 94
ke_ix1 4:ddf414d64ea9 95 //だいじ
ke_ix1 4:ddf414d64ea9 96 /*
ke_ix1 3:175ba3a059cb 97 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 98 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
ke_ix1 3:175ba3a059cb 99 params->rssi, params->isScanResponse, params->type);
ke_ix1 3:175ba3a059cb 100 printf("\n");
ke_ix1 3:175ba3a059cb 101 printf("Len:%d\n",params->advertisingDataLen);
ke_ix1 3:175ba3a059cb 102 printf("Data:");
ke_ix1 3:175ba3a059cb 103 for (int i=0; i< params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 104 printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 105 }
ke_ix1 4:ddf414d64ea9 106 */
ke_ix1 4:ddf414d64ea9 107
ke_ix1 3:175ba3a059cb 108 // for (int i=0; i<params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 109 // printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 110 // }
ke_ix1 3:175ba3a059cb 111
ke_ix1 3:175ba3a059cb 112 printf("\n");
ke_ix1 3:175ba3a059cb 113
ke_ix1 3:175ba3a059cb 114 // DEBUG(" \n\r");
ke_ix1 3:175ba3a059cb 115 //#endif
ke_ix1 4:ddf414d64ea9 116
ke_ix1 4:ddf414d64ea9 117
ke_ix1 3:175ba3a059cb 118 }
ke_ix1 3:175ba3a059cb 119
ke_ix1 3:175ba3a059cb 120
RedBearLab 2:4b66b69c7ecb 121 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 122 {
RedBearLab 0:cffe8ac1bdf0 123 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 124 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 125
RedBearLab 2:4b66b69c7ecb 126 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 127 {
RedBearLab 0:cffe8ac1bdf0 128 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 129 memset(txPayload, 0, TXRX_BUF_LEN);
ke_ix1 3:175ba3a059cb 130 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 131 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 132 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 133 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 134 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 135 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 136 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 137 {
RedBearLab 0:cffe8ac1bdf0 138 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 139 }
RedBearLab 0:cffe8ac1bdf0 140 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 141 }
RedBearLab 0:cffe8ac1bdf0 142 }
RedBearLab 0:cffe8ac1bdf0 143
RedBearLab 0:cffe8ac1bdf0 144 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 145 {
RedBearLab 0:cffe8ac1bdf0 146 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 147 {
ke_ix1 3:175ba3a059cb 148 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 149 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 150 {
RedBearLab 0:cffe8ac1bdf0 151 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 152 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 153 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 154 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 155 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 156 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 157 break;
RedBearLab 0:cffe8ac1bdf0 158 }
RedBearLab 0:cffe8ac1bdf0 159 }
RedBearLab 0:cffe8ac1bdf0 160 }
RedBearLab 0:cffe8ac1bdf0 161
ke_ix1 3:175ba3a059cb 162
ke_ix1 3:175ba3a059cb 163 // 1++していく
ke_ix1 3:175ba3a059cb 164 void step() {
ke_ix1 4:ddf414d64ea9 165 ++st;
ke_ix1 4:ddf414d64ea9 166 //ここから-------------
ke_ix1 4:ddf414d64ea9 167 char bufbuf_c[20];
ke_ix1 4:ddf414d64ea9 168 uint8_t bufbuf[40];
ke_ix1 4:ddf414d64ea9 169 sprintf(bufbuf_c, "%d", st);
ke_ix1 4:ddf414d64ea9 170
ke_ix1 4:ddf414d64ea9 171 int a;
ke_ix1 4:ddf414d64ea9 172 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 4:ddf414d64ea9 173 bufbuf[a] = bufbuf_c[a];
ke_ix1 4:ddf414d64ea9 174 }
ke_ix1 4:ddf414d64ea9 175
ke_ix1 4:ddf414d64ea9 176 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 4:ddf414d64ea9 177 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 4:ddf414d64ea9 178 //ここまで-------------
ke_ix1 4:ddf414d64ea9 179 //をこぴればデータを送信可能
ke_ix1 4:ddf414d64ea9 180
ke_ix1 4:ddf414d64ea9 181 pc.printf("%d", st);
ke_ix1 4:ddf414d64ea9 182 pc.printf("step! \r\n");
ke_ix1 3:175ba3a059cb 183 }
ke_ix1 3:175ba3a059cb 184
ke_ix1 3:175ba3a059cb 185
ke_ix1 3:175ba3a059cb 186
RedBearLab 0:cffe8ac1bdf0 187 int main(void)
RedBearLab 0:cffe8ac1bdf0 188 {
RedBearLab 0:cffe8ac1bdf0 189 ble.init();
RedBearLab 0:cffe8ac1bdf0 190 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 191 ble.onDataWritten(WrittenHandler);
ke_ix1 3:175ba3a059cb 192
RedBearLab 0:cffe8ac1bdf0 193 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 194 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 195
RedBearLab 0:cffe8ac1bdf0 196 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 197 // setup advertising
RedBearLab 0:cffe8ac1bdf0 198 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 199 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 200 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
ke_ix1 4:ddf414d64ea9 201 (const uint8_t *)"KEI_SEND", sizeof("KEI_SEND") - 1);
RedBearLab 0:cffe8ac1bdf0 202 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 203 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 204 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 205 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 206
RedBearLab 0:cffe8ac1bdf0 207 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 208
ke_ix1 3:175ba3a059cb 209 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN,
ke_ix1 3:175ba3a059cb 210 GapScanningParams::SCAN_WINDOW_MIN,
ke_ix1 3:175ba3a059cb 211 0);
ke_ix1 3:175ba3a059cb 212
ke_ix1 3:175ba3a059cb 213 ble.startScan(&onScanCallback);
ke_ix1 3:175ba3a059cb 214
RedBearLab 0:cffe8ac1bdf0 215 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 216 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 217
ke_ix1 3:175ba3a059cb 218 steper.attach(&step, 1.0);
ke_ix1 3:175ba3a059cb 219
RedBearLab 0:cffe8ac1bdf0 220 while(1)
RedBearLab 0:cffe8ac1bdf0 221 {
ke_ix1 4:ddf414d64ea9 222 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 223 }
RedBearLab 0:cffe8ac1bdf0 224 }
RedBearLab 0:cffe8ac1bdf0 225