![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
CdsLibに対応したMSC
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
Diff: main.cpp
- Revision:
- 5:29b82cc045b6
- Parent:
- 4:ddf414d64ea9
- Child:
- 6:92a65bca83cd
--- a/main.cpp Sat Dec 03 03:22:17 2016 +0000 +++ b/main.cpp Thu Dec 08 13:53:36 2016 +0000 @@ -28,8 +28,9 @@ #include "ble/BLE.h" #include <string.h> -//#include <stdio.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. */ @@ -41,7 +42,7 @@ Serial pc(USBTX, USBRX); Ticker steper; - +DigitalOut ledori1(LED1); // The Nordic UART Service @@ -56,7 +57,14 @@ 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}; -//aaaaaaaaaaaaaaa + +// 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,}; @@ -73,11 +81,40 @@ 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}; + +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"); @@ -184,8 +221,10 @@ + int main(void) { + ledori1 = 0; ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); @@ -216,10 +255,15 @@ 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(); } } +