CdsLibに対応したMSC

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Committer:
ke_ix1
Date:
Sun Feb 12 10:35:07 2017 +0000
Revision:
6:92a65bca83cd
Parent:
5:29b82cc045b6
Cds init;

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 4:ddf414d64ea9 30
ke_ix1 5:29b82cc045b6 31 // TX POWER用
ke_ix1 5:29b82cc045b6 32 #include "ble_gap.h"
ke_ix1 6:92a65bca83cd 33 #define TX_POWER -16
ke_ix1 6:92a65bca83cd 34 //機器設定
ke_ix1 6:92a65bca83cd 35 //#define TX_ZERO
ke_ix1 6:92a65bca83cd 36 //#define TX_POWER_CALI -10
ke_ix1 6:92a65bca83cd 37 //const static char DEVICE_NAME[] = "nRF51DK"; // change this
ke_ix1 6:92a65bca83cd 38
ke_ix1 6:92a65bca83cd 39 //#define TX_POWER_CALI -7
ke_ix1 6:92a65bca83cd 40 //const static char DEVICE_NAME[] = "51822"; // change this
ke_ix1 6:92a65bca83cd 41
ke_ix1 6:92a65bca83cd 42 //#define TX_POWER_CALI -15
ke_ix1 6:92a65bca83cd 43 //const static char DEVICE_NAME[] = "Nano"; // change this
ke_ix1 6:92a65bca83cd 44
ke_ix1 6:92a65bca83cd 45
ke_ix1 6:92a65bca83cd 46 #define TX_POWER_CALI -8
ke_ix1 6:92a65bca83cd 47 const static char DEVICE_NAME[] = "MN9"; // change this
ke_ix1 6:92a65bca83cd 48
RedBearLab 0:cffe8ac1bdf0 49
RedBearLab 0:cffe8ac1bdf0 50 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 51 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 52 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 53
RedBearLab 0:cffe8ac1bdf0 54 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 55
RedBearLab 2:4b66b69c7ecb 56 BLE ble;
RedBearLab 0:cffe8ac1bdf0 57
RedBearLab 0:cffe8ac1bdf0 58 Serial pc(USBTX, USBRX);
ke_ix1 3:175ba3a059cb 59 Ticker steper;
ke_ix1 5:29b82cc045b6 60 DigitalOut ledori1(LED1);
ke_ix1 6:92a65bca83cd 61 AnalogIn cdsIn(P0_1);
RedBearLab 0:cffe8ac1bdf0 62
RedBearLab 0:cffe8ac1bdf0 63 // The Nordic UART Service
ke_ix1 4:ddf414d64ea9 64 //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:ddf414d64ea9 65 //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:ddf414d64ea9 66 //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:ddf414d64ea9 67 //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 68
ke_ix1 4:ddf414d64ea9 69 // Send One CLK Service
ke_ix1 6:92a65bca83cd 70 //static const uint8_t uart_base_uuid[] = {0x53, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x7C}; //Service
ke_ix1 4:ddf414d64ea9 71 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:ddf414d64ea9 72 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 6:92a65bca83cd 73 //static const uint8_t uart_base_uuid_rev[] = {0x7C, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x53};
ke_ix1 6:92a65bca83cd 74 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 6:92a65bca83cd 75 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
ke_ix1 4:ddf414d64ea9 76
ke_ix1 5:29b82cc045b6 77
ke_ix1 5:29b82cc045b6 78 // TX POWER用
ke_ix1 5:29b82cc045b6 79 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受信用
ke_ix1 5:29b82cc045b6 80 uint8_t powerPayload[TXRX_BUF_LEN] = {0,};
ke_ix1 5:29b82cc045b6 81 static uint8_t power_buf[TXRX_BUF_LEN];
ke_ix1 5:29b82cc045b6 82 static uint8_t power_len=0;
ke_ix1 6:92a65bca83cd 83 GattCharacteristic txPowerCharacteristic (power_level_uuid, powerPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
ke_ix1 5:29b82cc045b6 84 Ticker txSteper;
RedBearLab 0:cffe8ac1bdf0 85
ke_ix1 6:92a65bca83cd 86
ke_ix1 6:92a65bca83cd 87 // MSC UUID
ke_ix1 6:92a65bca83cd 88 static const uint8_t msc_uuid[] = {0x71, 0x3D, 0, 6, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用
ke_ix1 6:92a65bca83cd 89 uint8_t mscPayload[TXRX_BUF_LEN] = {0,};
ke_ix1 6:92a65bca83cd 90 static uint8_t msc_buf[TXRX_BUF_LEN];
ke_ix1 6:92a65bca83cd 91 static uint8_t msc_len=0;
ke_ix1 6:92a65bca83cd 92 GattCharacteristic mscCharacteristic (msc_uuid, mscPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
ke_ix1 6:92a65bca83cd 93
ke_ix1 6:92a65bca83cd 94
ke_ix1 6:92a65bca83cd 95 //Feature UUID
ke_ix1 6:92a65bca83cd 96 static const uint8_t feature_uuid[] = {0x71, 0x3D, 0, 5, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用
ke_ix1 6:92a65bca83cd 97 uint8_t featurePayload[TXRX_BUF_LEN] = {0,};
ke_ix1 6:92a65bca83cd 98 static uint8_t feature_buf[TXRX_BUF_LEN];
ke_ix1 6:92a65bca83cd 99 static uint8_t feature_len=0;
ke_ix1 6:92a65bca83cd 100 GattCharacteristic featureCharacteristic (feature_uuid, featurePayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
ke_ix1 6:92a65bca83cd 101
ke_ix1 6:92a65bca83cd 102
RedBearLab 0:cffe8ac1bdf0 103 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 104 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 105
RedBearLab 0:cffe8ac1bdf0 106 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 107 static uint8_t rx_len=0;
ke_ix1 3:175ba3a059cb 108 static uint8_t rx_buf_st[20];
ke_ix1 4:ddf414d64ea9 109
ke_ix1 4:ddf414d64ea9 110 int8_t buf_before[40];
ke_ix1 4:ddf414d64ea9 111
ke_ix1 4:ddf414d64ea9 112 static int st = 0;
ke_ix1 4:ddf414d64ea9 113
RedBearLab 0:cffe8ac1bdf0 114
RedBearLab 0:cffe8ac1bdf0 115 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 116
ke_ix1 6:92a65bca83cd 117 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
ke_ix1 6:92a65bca83cd 118
ke_ix1 6:92a65bca83cd 119 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic, &mscCharacteristic, &featureCharacteristic};
ke_ix1 6:92a65bca83cd 120 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
ke_ix1 6:92a65bca83cd 121
ke_ix1 5:29b82cc045b6 122
ke_ix1 6:92a65bca83cd 123 // MSC UUID
ke_ix1 6:92a65bca83cd 124 void initMsc() {
ke_ix1 6:92a65bca83cd 125 //ここから-------------
ke_ix1 6:92a65bca83cd 126 char bufbuf_c[20];
ke_ix1 6:92a65bca83cd 127 uint8_t bufbuf[40];
ke_ix1 6:92a65bca83cd 128 //MSCのUUIDをセット
ke_ix1 6:92a65bca83cd 129 sprintf(bufbuf_c, "%s", "00000006");
ke_ix1 6:92a65bca83cd 130 //sprintf(bufbuf_c, "%d", "02f3f538-8d11-4802-b6fc-fb6616d4cd70");
ke_ix1 6:92a65bca83cd 131 int a;
ke_ix1 6:92a65bca83cd 132 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 6:92a65bca83cd 133 bufbuf[a] = bufbuf_c[a];
ke_ix1 6:92a65bca83cd 134 }
ke_ix1 6:92a65bca83cd 135 ble.updateCharacteristicValue(mscCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 6:92a65bca83cd 136 //ここまで-------------
ke_ix1 6:92a65bca83cd 137 //をこぴればデータを送信可能
ke_ix1 6:92a65bca83cd 138 }
RedBearLab 0:cffe8ac1bdf0 139
ke_ix1 6:92a65bca83cd 140
ke_ix1 6:92a65bca83cd 141 // Feature
ke_ix1 6:92a65bca83cd 142 void initFeature() {
ke_ix1 6:92a65bca83cd 143 //ここから-------------
ke_ix1 6:92a65bca83cd 144 char bufbuf_c[20];
ke_ix1 6:92a65bca83cd 145 uint8_t bufbuf[40];
ke_ix1 6:92a65bca83cd 146 sprintf(bufbuf_c, "%s", "In.Sensor.CdS");
ke_ix1 6:92a65bca83cd 147 int a;
ke_ix1 6:92a65bca83cd 148 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 6:92a65bca83cd 149 bufbuf[a] = bufbuf_c[a];
ke_ix1 6:92a65bca83cd 150 }
ke_ix1 6:92a65bca83cd 151 ble.updateCharacteristicValue(featureCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 6:92a65bca83cd 152 //ここまで-------------
ke_ix1 6:92a65bca83cd 153 //をこぴればデータを送信可能
ke_ix1 6:92a65bca83cd 154 }
ke_ix1 6:92a65bca83cd 155
ke_ix1 6:92a65bca83cd 156
RedBearLab 0:cffe8ac1bdf0 157
ke_ix1 5:29b82cc045b6 158 // TX POWER用
ke_ix1 5:29b82cc045b6 159 void txPowerUpdate(int newTxPower) {
ke_ix1 5:29b82cc045b6 160 sd_ble_gap_tx_power_set(newTxPower);
ke_ix1 5:29b82cc045b6 161 //ここから-------------
ke_ix1 5:29b82cc045b6 162 char bufbuf_c[20];
ke_ix1 5:29b82cc045b6 163 uint8_t bufbuf[40];
ke_ix1 6:92a65bca83cd 164 sprintf(bufbuf_c, "%d", newTxPower + TX_POWER_CALI);
ke_ix1 5:29b82cc045b6 165 int a;
ke_ix1 5:29b82cc045b6 166 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 5:29b82cc045b6 167 bufbuf[a] = bufbuf_c[a];
ke_ix1 5:29b82cc045b6 168 }
ke_ix1 5:29b82cc045b6 169 ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 5:29b82cc045b6 170 //ここまで-------------
ke_ix1 5:29b82cc045b6 171 //をこぴればデータを送信可能
ke_ix1 5:29b82cc045b6 172 }
ke_ix1 5:29b82cc045b6 173
ke_ix1 5:29b82cc045b6 174 void txStep() {
ke_ix1 5:29b82cc045b6 175 //ここから-------------
ke_ix1 5:29b82cc045b6 176 char bufbuf_c[20];
ke_ix1 5:29b82cc045b6 177 uint8_t bufbuf[40];
ke_ix1 6:92a65bca83cd 178 sprintf(bufbuf_c, "%d", TX_POWER + TX_POWER_CALI);
ke_ix1 5:29b82cc045b6 179 int a;
ke_ix1 5:29b82cc045b6 180 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 5:29b82cc045b6 181 bufbuf[a] = bufbuf_c[a];
ke_ix1 5:29b82cc045b6 182 }
ke_ix1 6:92a65bca83cd 183 ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 5:29b82cc045b6 184 }
ke_ix1 5:29b82cc045b6 185 //-------------------
ke_ix1 5:29b82cc045b6 186
ke_ix1 6:92a65bca83cd 187
ke_ix1 6:92a65bca83cd 188 void initChara() {
ke_ix1 6:92a65bca83cd 189 initMsc();
ke_ix1 6:92a65bca83cd 190 initFeature();
ke_ix1 6:92a65bca83cd 191 txStep();
ke_ix1 6:92a65bca83cd 192 }
ke_ix1 6:92a65bca83cd 193
ke_ix1 6:92a65bca83cd 194
ke_ix1 6:92a65bca83cd 195
RedBearLab 0:cffe8ac1bdf0 196 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 197 {
RedBearLab 0:cffe8ac1bdf0 198 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 199 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 200 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 201 }
RedBearLab 0:cffe8ac1bdf0 202
ke_ix1 3:175ba3a059cb 203
ke_ix1 3:175ba3a059cb 204 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
ke_ix1 3:175ba3a059cb 205 {
ke_ix1 4:ddf414d64ea9 206 //printf("onScanNau\n");
ke_ix1 4:ddf414d64ea9 207
ke_ix1 4:ddf414d64ea9 208 // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 209
ke_ix1 4:ddf414d64ea9 210 //だいじ
ke_ix1 4:ddf414d64ea9 211 /*
ke_ix1 3:175ba3a059cb 212 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 213 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
ke_ix1 3:175ba3a059cb 214 params->rssi, params->isScanResponse, params->type);
ke_ix1 3:175ba3a059cb 215 printf("\n");
ke_ix1 3:175ba3a059cb 216 printf("Len:%d\n",params->advertisingDataLen);
ke_ix1 3:175ba3a059cb 217 printf("Data:");
ke_ix1 3:175ba3a059cb 218 for (int i=0; i< params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 219 printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 220 }
ke_ix1 4:ddf414d64ea9 221 */
ke_ix1 4:ddf414d64ea9 222
ke_ix1 3:175ba3a059cb 223 // for (int i=0; i<params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 224 // printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 225 // }
ke_ix1 3:175ba3a059cb 226
ke_ix1 3:175ba3a059cb 227 printf("\n");
ke_ix1 3:175ba3a059cb 228
ke_ix1 3:175ba3a059cb 229 // DEBUG(" \n\r");
ke_ix1 3:175ba3a059cb 230 //#endif
ke_ix1 4:ddf414d64ea9 231
ke_ix1 4:ddf414d64ea9 232
ke_ix1 3:175ba3a059cb 233 }
ke_ix1 3:175ba3a059cb 234
ke_ix1 3:175ba3a059cb 235
RedBearLab 2:4b66b69c7ecb 236 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 237 {
RedBearLab 0:cffe8ac1bdf0 238 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 239 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 240
RedBearLab 2:4b66b69c7ecb 241 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 242 {
RedBearLab 0:cffe8ac1bdf0 243 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 244 memset(txPayload, 0, TXRX_BUF_LEN);
ke_ix1 3:175ba3a059cb 245 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 246 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 247 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 248 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 249 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 250 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 251 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 252 {
ke_ix1 6:92a65bca83cd 253 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 254 }
RedBearLab 0:cffe8ac1bdf0 255 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 256 }
RedBearLab 0:cffe8ac1bdf0 257 }
RedBearLab 0:cffe8ac1bdf0 258
RedBearLab 0:cffe8ac1bdf0 259 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 260 {
RedBearLab 0:cffe8ac1bdf0 261 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 262 {
ke_ix1 3:175ba3a059cb 263 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 264 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 265 {
RedBearLab 0:cffe8ac1bdf0 266 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 267 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 268 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 269 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 270 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 271 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 272 break;
RedBearLab 0:cffe8ac1bdf0 273 }
RedBearLab 0:cffe8ac1bdf0 274 }
RedBearLab 0:cffe8ac1bdf0 275 }
RedBearLab 0:cffe8ac1bdf0 276
ke_ix1 3:175ba3a059cb 277
ke_ix1 3:175ba3a059cb 278 // 1++していく
ke_ix1 3:175ba3a059cb 279 void step() {
ke_ix1 6:92a65bca83cd 280
ke_ix1 6:92a65bca83cd 281 float cds = cdsIn;
ke_ix1 4:ddf414d64ea9 282 //ここから-------------
ke_ix1 4:ddf414d64ea9 283 char bufbuf_c[20];
ke_ix1 4:ddf414d64ea9 284 uint8_t bufbuf[40];
ke_ix1 6:92a65bca83cd 285 sprintf(bufbuf_c, "%f", cds);
ke_ix1 4:ddf414d64ea9 286
ke_ix1 4:ddf414d64ea9 287 int a;
ke_ix1 4:ddf414d64ea9 288 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 4:ddf414d64ea9 289 bufbuf[a] = bufbuf_c[a];
ke_ix1 4:ddf414d64ea9 290 }
ke_ix1 4:ddf414d64ea9 291
ke_ix1 4:ddf414d64ea9 292 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 4:ddf414d64ea9 293 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 4:ddf414d64ea9 294 //ここまで-------------
ke_ix1 4:ddf414d64ea9 295 //をこぴればデータを送信可能
ke_ix1 4:ddf414d64ea9 296
ke_ix1 6:92a65bca83cd 297 pc.printf("%f", cds);
ke_ix1 6:92a65bca83cd 298 pc.printf(" cds! \r\n");
ke_ix1 3:175ba3a059cb 299 }
ke_ix1 3:175ba3a059cb 300
ke_ix1 3:175ba3a059cb 301
RedBearLab 0:cffe8ac1bdf0 302 int main(void)
RedBearLab 0:cffe8ac1bdf0 303 {
ke_ix1 5:29b82cc045b6 304 ledori1 = 0;
RedBearLab 0:cffe8ac1bdf0 305 ble.init();
RedBearLab 0:cffe8ac1bdf0 306 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 307 ble.onDataWritten(WrittenHandler);
ke_ix1 3:175ba3a059cb 308
RedBearLab 0:cffe8ac1bdf0 309 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 310 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 311
RedBearLab 0:cffe8ac1bdf0 312 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 313 // setup advertising
RedBearLab 0:cffe8ac1bdf0 314 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 315 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 316 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
ke_ix1 6:92a65bca83cd 317 (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
RedBearLab 0:cffe8ac1bdf0 318 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 319 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 320 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 321 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 322
RedBearLab 0:cffe8ac1bdf0 323 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 324
ke_ix1 3:175ba3a059cb 325 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN,
ke_ix1 3:175ba3a059cb 326 GapScanningParams::SCAN_WINDOW_MIN,
ke_ix1 3:175ba3a059cb 327 0);
ke_ix1 3:175ba3a059cb 328
ke_ix1 3:175ba3a059cb 329 ble.startScan(&onScanCallback);
ke_ix1 3:175ba3a059cb 330
RedBearLab 0:cffe8ac1bdf0 331 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 332 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 333
ke_ix1 3:175ba3a059cb 334 steper.attach(&step, 1.0);
ke_ix1 5:29b82cc045b6 335
ke_ix1 5:29b82cc045b6 336 //TX POWER用
ke_ix1 5:29b82cc045b6 337 txPowerUpdate(TX_POWER);
ke_ix1 6:92a65bca83cd 338 //txSteper.attach(&initChara, 1.0);
ke_ix1 6:92a65bca83cd 339 initChara();
ke_ix1 6:92a65bca83cd 340
ke_ix1 5:29b82cc045b6 341
RedBearLab 0:cffe8ac1bdf0 342 while(1)
RedBearLab 0:cffe8ac1bdf0 343 {
ke_ix1 4:ddf414d64ea9 344 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 345 }
RedBearLab 0:cffe8ac1bdf0 346 }
RedBearLab 0:cffe8ac1bdf0 347
ke_ix1 5:29b82cc045b6 348