InfraredLibに対応したMSC

Dependencies:   BLE_API RemoteIR mbed nRF51822

Fork of nRF51822_SimpleChat_IRRX_nRF51DK by Keiichi Matsuoka

Committer:
ke_ix1
Date:
Mon Feb 29 07:05:55 2016 +0000
Revision:
3:175ba3a059cb
Parent:
2:4b66b69c7ecb
Child:
4:f1923d9f536e
???BLE Nano????Android?RSSI?MAC???

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 3:175ba3a059cb 30 //#include <stdio.h>
RedBearLab 0:cffe8ac1bdf0 31
RedBearLab 0:cffe8ac1bdf0 32
RedBearLab 0:cffe8ac1bdf0 33 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 34 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 35 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 36
RedBearLab 0:cffe8ac1bdf0 37 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 38
RedBearLab 2:4b66b69c7ecb 39 BLE ble;
RedBearLab 0:cffe8ac1bdf0 40
RedBearLab 0:cffe8ac1bdf0 41 Serial pc(USBTX, USBRX);
ke_ix1 3:175ba3a059cb 42 Ticker steper;
ke_ix1 3:175ba3a059cb 43
RedBearLab 0:cffe8ac1bdf0 44
RedBearLab 0:cffe8ac1bdf0 45
RedBearLab 0:cffe8ac1bdf0 46 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 47 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:cffe8ac1bdf0 48 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:cffe8ac1bdf0 49 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:cffe8ac1bdf0 50 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 51
RedBearLab 0:cffe8ac1bdf0 52
RedBearLab 0:cffe8ac1bdf0 53 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 54 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 55
RedBearLab 0:cffe8ac1bdf0 56 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 57 static uint8_t rx_len=0;
ke_ix1 3:175ba3a059cb 58 static uint8_t rx_buf_st[20];
ke_ix1 3:175ba3a059cb 59 //static int st = 0;
RedBearLab 0:cffe8ac1bdf0 60
RedBearLab 0:cffe8ac1bdf0 61 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 62
RedBearLab 0:cffe8ac1bdf0 63 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 64
RedBearLab 0:cffe8ac1bdf0 65 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 66
RedBearLab 0:cffe8ac1bdf0 67 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 68
RedBearLab 0:cffe8ac1bdf0 69 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 70 {
RedBearLab 0:cffe8ac1bdf0 71 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 72 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 73 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 74 }
RedBearLab 0:cffe8ac1bdf0 75
ke_ix1 3:175ba3a059cb 76
ke_ix1 3:175ba3a059cb 77 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
ke_ix1 3:175ba3a059cb 78 {
ke_ix1 3:175ba3a059cb 79
ke_ix1 3:175ba3a059cb 80
ke_ix1 3:175ba3a059cb 81 //#if NEED_CONSOLE_OUTPUT
ke_ix1 3:175ba3a059cb 82 // DEBUG("onScan() ");
ke_ix1 3:175ba3a059cb 83 // DEBUG("RSSI:%d ",params->rssi);
ke_ix1 3:175ba3a059cb 84 // DEBUG("Addr:%d ",params->peerAddr);
ke_ix1 3:175ba3a059cb 85 // DEBUG("Len:%d ",params->advertisingDataLen);
ke_ix1 3:175ba3a059cb 86 // DEBUG("Data:");
ke_ix1 3:175ba3a059cb 87 // for (int i=0; i< params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 88 // DEBUG("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 89 // }
ke_ix1 3:175ba3a059cb 90 //rx_buf_st[0] = params->rssi;
ke_ix1 3:175ba3a059cb 91 uint8_t bufbuf[40];
ke_ix1 3:175ba3a059cb 92 char bufbuf_c[20];
ke_ix1 3:175ba3a059cb 93 char bufbuf_addr[20];
ke_ix1 3:175ba3a059cb 94 sprintf(bufbuf_c, "%d", params->rssi);
ke_ix1 3:175ba3a059cb 95
ke_ix1 3:175ba3a059cb 96 //strcat(bufbuf_c, '¥0');
ke_ix1 3:175ba3a059cb 97 int a;
ke_ix1 3:175ba3a059cb 98 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 3:175ba3a059cb 99 bufbuf[a] = bufbuf_c[a];
ke_ix1 3:175ba3a059cb 100 }
ke_ix1 3:175ba3a059cb 101 bufbuf[a] = ',';
ke_ix1 3:175ba3a059cb 102 a++;
ke_ix1 3:175ba3a059cb 103 int b;
ke_ix1 3:175ba3a059cb 104 // for(b=5; b >= 0; b--) {
ke_ix1 3:175ba3a059cb 105 // sprintf(bufbuf_addr,"%02x",params->peerAddr[b]);
ke_ix1 3:175ba3a059cb 106 // bufbuf[a] = bufbuf_addr[b];
ke_ix1 3:175ba3a059cb 107 // memset(bufbuf_addr,NULL,sizeof(bufbuf_addr));
ke_ix1 3:175ba3a059cb 108 // bufbuf[a++] = ',';
ke_ix1 3:175ba3a059cb 109 // a++;
ke_ix1 3:175ba3a059cb 110 // }
ke_ix1 3:175ba3a059cb 111 for(b=5; b >= 0; b--) {
ke_ix1 3:175ba3a059cb 112 // sprintf(bufbuf_addr,"%02x",params->peerAddr[b]);
ke_ix1 3:175ba3a059cb 113 bufbuf[a] = params->peerAddr[b];//bufbuf_addr[b];
ke_ix1 3:175ba3a059cb 114 // memset(bufbuf_addr,'\0',sizeof(bufbuf_addr));
ke_ix1 3:175ba3a059cb 115 a++;
ke_ix1 3:175ba3a059cb 116 }
ke_ix1 3:175ba3a059cb 117
ke_ix1 3:175ba3a059cb 118 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 3:175ba3a059cb 119
ke_ix1 3:175ba3a059cb 120 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 121 // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 122 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
ke_ix1 3:175ba3a059cb 123 params->rssi, params->isScanResponse, params->type);
ke_ix1 3:175ba3a059cb 124 printf("\n");
ke_ix1 3:175ba3a059cb 125 printf("Len:%d\n",params->advertisingDataLen);
ke_ix1 3:175ba3a059cb 126 printf("Data:");
ke_ix1 3:175ba3a059cb 127 for (int i=0; i< params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 128 printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 129 }
ke_ix1 3:175ba3a059cb 130 // for (int i=0; i<params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 131 // printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 132 // }
ke_ix1 3:175ba3a059cb 133
ke_ix1 3:175ba3a059cb 134 printf("\n");
ke_ix1 3:175ba3a059cb 135
ke_ix1 3:175ba3a059cb 136 // DEBUG(" \n\r");
ke_ix1 3:175ba3a059cb 137 //#endif
ke_ix1 3:175ba3a059cb 138
ke_ix1 3:175ba3a059cb 139
ke_ix1 3:175ba3a059cb 140 }
ke_ix1 3:175ba3a059cb 141
ke_ix1 3:175ba3a059cb 142
RedBearLab 2:4b66b69c7ecb 143 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 144 {
RedBearLab 0:cffe8ac1bdf0 145 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 146 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 147
RedBearLab 2:4b66b69c7ecb 148 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 149 {
RedBearLab 0:cffe8ac1bdf0 150 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 151 memset(txPayload, 0, TXRX_BUF_LEN);
ke_ix1 3:175ba3a059cb 152 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 153 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 154 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 155 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 156 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 157 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 158 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 159 {
RedBearLab 0:cffe8ac1bdf0 160 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 161 }
RedBearLab 0:cffe8ac1bdf0 162 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 163 }
RedBearLab 0:cffe8ac1bdf0 164 }
RedBearLab 0:cffe8ac1bdf0 165
RedBearLab 0:cffe8ac1bdf0 166 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 167 {
RedBearLab 0:cffe8ac1bdf0 168 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 169 {
ke_ix1 3:175ba3a059cb 170 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 171 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 172 {
RedBearLab 0:cffe8ac1bdf0 173 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 174 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 175 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 176 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 177 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 178 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 179 break;
RedBearLab 0:cffe8ac1bdf0 180 }
RedBearLab 0:cffe8ac1bdf0 181 }
RedBearLab 0:cffe8ac1bdf0 182 }
RedBearLab 0:cffe8ac1bdf0 183
ke_ix1 3:175ba3a059cb 184
ke_ix1 3:175ba3a059cb 185 // 1++していく
ke_ix1 3:175ba3a059cb 186 void step() {
ke_ix1 3:175ba3a059cb 187 //++st;
ke_ix1 3:175ba3a059cb 188 //rx_buf_st[0] = st;
ke_ix1 3:175ba3a059cb 189 //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 3:175ba3a059cb 190 //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf_st, sizeof(rx_buf_st));
ke_ix1 3:175ba3a059cb 191 //pc.printf("%d", st);
ke_ix1 3:175ba3a059cb 192 //pc.printf("step! \r\n");
ke_ix1 3:175ba3a059cb 193 }
ke_ix1 3:175ba3a059cb 194
ke_ix1 3:175ba3a059cb 195
ke_ix1 3:175ba3a059cb 196
RedBearLab 0:cffe8ac1bdf0 197 int main(void)
RedBearLab 0:cffe8ac1bdf0 198 {
RedBearLab 0:cffe8ac1bdf0 199 ble.init();
RedBearLab 0:cffe8ac1bdf0 200 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 201 ble.onDataWritten(WrittenHandler);
ke_ix1 3:175ba3a059cb 202
RedBearLab 0:cffe8ac1bdf0 203 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 204 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 205
RedBearLab 0:cffe8ac1bdf0 206 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 207 // setup advertising
RedBearLab 0:cffe8ac1bdf0 208 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 209 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 210 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
ke_ix1 3:175ba3a059cb 211 (const uint8_t *)"keiichi", sizeof("keiichi") - 1);
RedBearLab 0:cffe8ac1bdf0 212 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 213 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 214 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 215 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 216
RedBearLab 0:cffe8ac1bdf0 217 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 218
ke_ix1 3:175ba3a059cb 219 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN,
ke_ix1 3:175ba3a059cb 220 GapScanningParams::SCAN_WINDOW_MIN,
ke_ix1 3:175ba3a059cb 221 0);
ke_ix1 3:175ba3a059cb 222
ke_ix1 3:175ba3a059cb 223 ble.startScan(&onScanCallback);
ke_ix1 3:175ba3a059cb 224
RedBearLab 0:cffe8ac1bdf0 225 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 226 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 227
ke_ix1 3:175ba3a059cb 228 steper.attach(&step, 1.0);
ke_ix1 3:175ba3a059cb 229
RedBearLab 0:cffe8ac1bdf0 230 while(1)
RedBearLab 0:cffe8ac1bdf0 231 {
RedBearLab 0:cffe8ac1bdf0 232 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 233 }
RedBearLab 0:cffe8ac1bdf0 234 }
RedBearLab 0:cffe8ac1bdf0 235