CdsLibに対応したMSC
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
Diff: main.cpp
- Revision:
- 3:175ba3a059cb
- Parent:
- 2:4b66b69c7ecb
- Child:
- 4:ddf414d64ea9
--- a/main.cpp Mon Jul 27 06:52:44 2015 +0000 +++ b/main.cpp Mon Feb 29 07:05:55 2016 +0000 @@ -26,6 +26,8 @@ #include "mbed.h" #include "ble/BLE.h" +#include <string.h> +//#include <stdio.h> #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ @@ -37,6 +39,8 @@ BLE ble; Serial pc(USBTX, USBRX); +Ticker steper; + // The Nordic UART Service @@ -51,7 +55,8 @@ static uint8_t rx_buf[TXRX_BUF_LEN]; static uint8_t rx_len=0; - +static uint8_t rx_buf_st[20]; +//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); @@ -61,8 +66,6 @@ GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); - - void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { pc.printf("Disconnected \r\n"); @@ -70,6 +73,73 @@ ble.startAdvertising(); } + +void onScanCallback(const Gap::AdvertisementCallbackParams_t *params) +{ + + +//#if NEED_CONSOLE_OUTPUT +// DEBUG("onScan() "); +// DEBUG("RSSI:%d ",params->rssi); +// DEBUG("Addr:%d ",params->peerAddr); +// DEBUG("Len:%d ",params->advertisingDataLen); +// DEBUG("Data:"); +// for (int i=0; i< params->advertisingDataLen; i++){ +// DEBUG("%02x,",params->advertisingData[i]); +// } + //rx_buf_st[0] = params->rssi; + uint8_t bufbuf[40]; + char bufbuf_c[20]; + char bufbuf_addr[20]; + sprintf(bufbuf_c, "%d", params->rssi); + + //strcat(bufbuf_c, '¥0'); + int a; + for(a=0; bufbuf_c[a] != '\0'; a++) { + bufbuf[a] = bufbuf_c[a]; + } + bufbuf[a] = ','; + a++; + int b; +// for(b=5; b >= 0; b--) { +// sprintf(bufbuf_addr,"%02x",params->peerAddr[b]); +// bufbuf[a] = bufbuf_addr[b]; +// memset(bufbuf_addr,NULL,sizeof(bufbuf_addr)); +// bufbuf[a++] = ','; +// a++; +// } + for(b=5; b >= 0; b--) { + // sprintf(bufbuf_addr,"%02x",params->peerAddr[b]); + bufbuf[a] = params->peerAddr[b];//bufbuf_addr[b]; + // memset(bufbuf_addr,'\0',sizeof(bufbuf_addr)); + a++; + } + + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a); + + printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", +// printf("adv peerAddr[%d %d %d %d %d %d] 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]; @@ -79,7 +149,7 @@ { ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); memset(txPayload, 0, TXRX_BUF_LEN); - memcpy(txPayload, buf, TXRX_BUF_LEN); + memcpy(txPayload, buf, TXRX_BUF_LEN); pc.printf("WriteHandler \r\n"); pc.printf("Length: "); pc.putc(bytesRead); @@ -97,7 +167,7 @@ { while(pc.readable()) { - rx_buf[rx_len++] = pc.getc(); + 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); @@ -111,12 +181,25 @@ } } + +// 1++していく +void step() { + //++st; + //rx_buf_st[0] = st; + //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); + //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf_st, sizeof(rx_buf_st)); + //pc.printf("%d", st); + //pc.printf("step! \r\n"); +} + + + int main(void) { ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); - + pc.baud(9600); pc.printf("SimpleChat Init \r\n"); @@ -125,7 +208,7 @@ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); + (const uint8_t *)"keiichi", sizeof("keiichi") - 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. @@ -133,27 +216,20 @@ 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); + while(1) { ble.waitForEvent(); } } - - - - - - - - - - - - - - -