USonicLibに対応したMSC
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
main.cpp
- Committer:
- ke_ix1
- Date:
- 2016-12-08
- Revision:
- 5:29b82cc045b6
- Parent:
- 4:ddf414d64ea9
- Child:
- 6:1331ef9ec5cc
File content as of revision 5:29b82cc045b6:
/* Copyright (c) 2012-2014 RedBearLab Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* * The application works with the BLEController iOS/Android App. * Type something from the Terminal to send * to the BLEController App or vice verse. * Characteristics received from App will print on Terminal. */ #include "mbed.h" #include "ble/BLE.h" #include <string.h> // TX POWER用 #include "ble_gap.h" #define TX_POWER -40 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ #define TXRX_BUF_LEN 20 BLE ble; Serial pc(USBTX, USBRX); Ticker steper; DigitalOut ledori1(LED1); // The Nordic UART Service //static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; // Send One CLK Service static const uint8_t uart_base_uuid[] = {0x53, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x7C}; //Service static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_送信用 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_受信用 static const uint8_t uart_base_uuid_rev[] = {0x7C, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x53}; // TX POWER用 static const uint8_t power_level_uuid[] = {0x71, 0x3D, 0, 4, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用 uint8_t powerPayload[TXRX_BUF_LEN] = {0,}; static uint8_t power_buf[TXRX_BUF_LEN]; static uint8_t power_len=0; GattCharacteristic txPowerCharacteristic (power_level_uuid, powerPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); //tx power characteristic Ticker txSteper; uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; static uint8_t rx_buf[TXRX_BUF_LEN]; static uint8_t rx_len=0; static uint8_t rx_buf_st[20]; int8_t buf_before[40]; static int st = 0; GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic}; GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); // TX POWER用 void txPowerUpdate(int newTxPower) { sd_ble_gap_tx_power_set(newTxPower); //ここから------------- char bufbuf_c[20]; uint8_t bufbuf[40]; sprintf(bufbuf_c, "%d", newTxPower); int a; for(a=0; bufbuf_c[a] != '\0'; a++) { bufbuf[a] = bufbuf_c[a]; } ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); //ここまで------------- //をこぴればデータを送信可能 } void txStep() { //ここから------------- char bufbuf_c[20]; uint8_t bufbuf[40]; sprintf(bufbuf_c, "%d", TX_POWER); int a; for(a=0; bufbuf_c[a] != '\0'; a++) { bufbuf[a] = bufbuf_c[a]; } ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); } //------------------- void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { pc.printf("Disconnected \r\n"); pc.printf("Restart advertising \r\n"); ble.startAdvertising(); } void onScanCallback(const Gap::AdvertisementCallbackParams_t *params) { //printf("onScanNau\n"); // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", //だいじ /* printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type); printf("\n"); printf("Len:%d\n",params->advertisingDataLen); printf("Data:"); for (int i=0; i< params->advertisingDataLen; i++){ printf("%02x,",params->advertisingData[i]); } */ // for (int i=0; i<params->advertisingDataLen; i++){ // printf("%02x,",params->advertisingData[i]); // } printf("\n"); // DEBUG(" \n\r"); //#endif } void WrittenHandler(const GattWriteCallbackParams *Handler) { uint8_t buf[TXRX_BUF_LEN]; uint16_t bytesRead, index; if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); memset(txPayload, 0, TXRX_BUF_LEN); memcpy(txPayload, buf, TXRX_BUF_LEN); pc.printf("WriteHandler \r\n"); pc.printf("Length: "); pc.putc(bytesRead); pc.printf("\r\n"); pc.printf("Data: "); for(index=0; index<bytesRead; index++) { pc.putc(txPayload[index]); } pc.printf("\r\n"); } } void uartCB(void) { while(pc.readable()) { rx_buf[rx_len++] = pc.getc(); if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') { ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); pc.printf("RecHandler \r\n"); pc.printf("Length: "); pc.putc(rx_len); pc.printf("\r\n"); rx_len = 0; break; } } } // 1++していく void step() { ++st; //ここから------------- char bufbuf_c[20]; uint8_t bufbuf[40]; sprintf(bufbuf_c, "%d", st); int a; for(a=0; bufbuf_c[a] != '\0'; a++) { bufbuf[a] = bufbuf_c[a]; } // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a); //ここまで------------- //をこぴればデータを送信可能 pc.printf("%d", st); pc.printf("step! \r\n"); } int main(void) { ledori1 = 0; ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); pc.baud(9600); pc.printf("SimpleChat Init \r\n"); pc.attach( uartCB , pc.RxIrq); // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"KEI_SEND", sizeof("KEI_SEND") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); // 100ms; in multiples of 0.625ms. ble.setAdvertisingInterval(160); ble.addService(uartService); ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN, GapScanningParams::SCAN_WINDOW_MIN, 0); ble.startScan(&onScanCallback); ble.startAdvertising(); pc.printf("Advertising Start \r\n"); steper.attach(&step, 1.0); //TX POWER用 txPowerUpdate(TX_POWER); txSteper.attach(&txStep, 2.0); while(1) { ble.waitForEvent(); } }