TeleViLibに対応するMSC

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat_LED_kai by EQUUS_KUBOTA

Committer:
ke_ix1
Date:
Sat Dec 03 10:44:01 2016 +0000
Revision:
4:41cf035db84a
Parent:
3:b3f6c612b603
Child:
5:35728098eeae
????????LED????MN

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"
RedBearLab 0:cffe8ac1bdf0 29
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 32 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 33 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35 #define TXRX_BUF_LEN 20
ke_ix1 4:41cf035db84a 36 #define DIGITAL_OUT_PIN P0_17 //D7
ke_ix1 4:41cf035db84a 37
ke_ix1 4:41cf035db84a 38
RedBearLab 0:cffe8ac1bdf0 39
RedBearLab 2:4b66b69c7ecb 40 BLE ble;
RedBearLab 0:cffe8ac1bdf0 41
RedBearLab 0:cffe8ac1bdf0 42 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 43
ke_ix1 4:41cf035db84a 44 DigitalOut led1(DIGITAL_OUT_PIN);
ke_ix1 4:41cf035db84a 45 DigitalOut ledori1(LED1);
RedBearLab 0:cffe8ac1bdf0 46
RedBearLab 0:cffe8ac1bdf0 47 // The Nordic UART Service
ke_ix1 4:41cf035db84a 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:41cf035db84a 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:41cf035db84a 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:41cf035db84a 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:41cf035db84a 53 // The Service
ke_ix1 4:41cf035db84a 54 static const uint8_t uart_base_uuid[] = {0x02, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x70}; //Service
ke_ix1 4:41cf035db84a 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:41cf035db84a 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:41cf035db84a 57 static const uint8_t uart_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x02};
ke_ix1 4:41cf035db84a 58
ke_ix1 4:41cf035db84a 59 //02f3f538-8d11-4802-b6fc-fb6616d4cd70
ke_ix1 4:41cf035db84a 60
ke_ix1 4:41cf035db84a 61 const static char DEVICE_NAME[] = "KEI_LED"; // change this
RedBearLab 0:cffe8ac1bdf0 62
RedBearLab 0:cffe8ac1bdf0 63 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 64 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 65
RedBearLab 0:cffe8ac1bdf0 66 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 67 static uint8_t rx_len=0;
RedBearLab 0:cffe8ac1bdf0 68
RedBearLab 0:cffe8ac1bdf0 69
RedBearLab 0:cffe8ac1bdf0 70 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 71
RedBearLab 0:cffe8ac1bdf0 72 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 73
RedBearLab 0:cffe8ac1bdf0 74 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 75
RedBearLab 0:cffe8ac1bdf0 76 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 77
RedBearLab 0:cffe8ac1bdf0 78
RedBearLab 0:cffe8ac1bdf0 79
RedBearLab 3:b3f6c612b603 80 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
RedBearLab 0:cffe8ac1bdf0 81 {
RedBearLab 0:cffe8ac1bdf0 82 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 83 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 84 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 85 }
RedBearLab 0:cffe8ac1bdf0 86
ke_ix1 4:41cf035db84a 87 void ConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
ke_ix1 4:41cf035db84a 88 {
ke_ix1 4:41cf035db84a 89 pc.printf("Disconnected \r\n");
ke_ix1 4:41cf035db84a 90 pc.printf("Restart advertising \r\n");
ke_ix1 4:41cf035db84a 91 ble.startAdvertising();
ke_ix1 4:41cf035db84a 92 }
ke_ix1 4:41cf035db84a 93
RedBearLab 2:4b66b69c7ecb 94 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 95 {
RedBearLab 0:cffe8ac1bdf0 96 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 97 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 98
RedBearLab 2:4b66b69c7ecb 99 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 100 {
RedBearLab 0:cffe8ac1bdf0 101 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 102 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 103 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 104 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 105 pc.printf("Length: ");
ke_ix1 4:41cf035db84a 106 pc.putc(bytesRead); //受け取った文字の先頭アドレス?
ke_ix1 4:41cf035db84a 107 pc.printf("\r\n");
ke_ix1 4:41cf035db84a 108 pc.printf("[0]: ");
ke_ix1 4:41cf035db84a 109 pc.putc(txPayload[1]); //1文字目を表示
ke_ix1 4:41cf035db84a 110 if(txPayload[1]=='a') {
ke_ix1 4:41cf035db84a 111 pc.printf("yeahhhhhh!!!!!!!!");
ke_ix1 4:41cf035db84a 112 // if(led1 == 0) {
ke_ix1 4:41cf035db84a 113 led1 = 1;
ke_ix1 4:41cf035db84a 114 ledori1 = 1;
ke_ix1 4:41cf035db84a 115 // } else {
ke_ix1 4:41cf035db84a 116 // led1 = 0;
ke_ix1 4:41cf035db84a 117 // }
ke_ix1 4:41cf035db84a 118 } else if(txPayload[1]=='b') {
ke_ix1 4:41cf035db84a 119 led1 = 0;
ke_ix1 4:41cf035db84a 120 ledori1 = 0;
ke_ix1 4:41cf035db84a 121 }
RedBearLab 0:cffe8ac1bdf0 122 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 123 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 124 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 125 {
ke_ix1 4:41cf035db84a 126 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 127 }
RedBearLab 0:cffe8ac1bdf0 128 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 129 }
RedBearLab 0:cffe8ac1bdf0 130 }
RedBearLab 0:cffe8ac1bdf0 131
RedBearLab 0:cffe8ac1bdf0 132 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 133 {
RedBearLab 0:cffe8ac1bdf0 134 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 135 {
RedBearLab 0:cffe8ac1bdf0 136 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 137 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 138 {
RedBearLab 0:cffe8ac1bdf0 139 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 140 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 141 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 142 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 143 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 144 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 145 break;
RedBearLab 0:cffe8ac1bdf0 146 }
RedBearLab 0:cffe8ac1bdf0 147 }
RedBearLab 0:cffe8ac1bdf0 148 }
RedBearLab 0:cffe8ac1bdf0 149
RedBearLab 0:cffe8ac1bdf0 150 int main(void)
RedBearLab 0:cffe8ac1bdf0 151 {
ke_ix1 4:41cf035db84a 152 led1 = 0;
ke_ix1 4:41cf035db84a 153 ledori1 = 0;
RedBearLab 0:cffe8ac1bdf0 154 ble.init();
RedBearLab 0:cffe8ac1bdf0 155 ble.onDisconnection(disconnectionCallback);
ke_ix1 4:41cf035db84a 156 ble.onDataWritten(WrittenHandler);
ke_ix1 4:41cf035db84a 157 ble.onConnection(ConnectionCallback);
RedBearLab 0:cffe8ac1bdf0 158
RedBearLab 0:cffe8ac1bdf0 159 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 160 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 161
RedBearLab 0:cffe8ac1bdf0 162 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 163 // setup advertising
RedBearLab 0:cffe8ac1bdf0 164 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 165 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 166 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
ke_ix1 4:41cf035db84a 167 (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
RedBearLab 0:cffe8ac1bdf0 168 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 169 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 170 // 100ms; in multiples of 0.625ms.
ke_ix1 4:41cf035db84a 171 ble.setAdvertisingInterval(50);
RedBearLab 0:cffe8ac1bdf0 172
RedBearLab 0:cffe8ac1bdf0 173 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 174
RedBearLab 0:cffe8ac1bdf0 175 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 176 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 177
RedBearLab 0:cffe8ac1bdf0 178 while(1)
RedBearLab 0:cffe8ac1bdf0 179 {
RedBearLab 0:cffe8ac1bdf0 180 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 181 }
RedBearLab 0:cffe8ac1bdf0 182 }