USonicLibに対応したMSC
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
main.cpp@6:1331ef9ec5cc, 2017-02-12 (annotated)
- Committer:
- ke_ix1
- Date:
- Sun Feb 12 10:24:35 2017 +0000
- Revision:
- 6:1331ef9ec5cc
- Parent:
- 5:29b82cc045b6
USonicLib?????MSC
Who changed what in which revision?
User | Revision | Line number | New 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:1331ef9ec5cc | 33 | #define TX_POWER -8 |
ke_ix1 | 6:1331ef9ec5cc | 34 | //機器設定 |
ke_ix1 | 6:1331ef9ec5cc | 35 | //#define TX_ZERO |
ke_ix1 | 6:1331ef9ec5cc | 36 | #define TX_POWER_CALI -10 |
ke_ix1 | 6:1331ef9ec5cc | 37 | //const static char DEVICE_NAME[] = "nRF51DK"; // change this |
ke_ix1 | 6:1331ef9ec5cc | 38 | |
ke_ix1 | 6:1331ef9ec5cc | 39 | //#define TX_POWER_CALI -7 |
ke_ix1 | 6:1331ef9ec5cc | 40 | //const static char DEVICE_NAME[] = "51822"; // change this |
ke_ix1 | 6:1331ef9ec5cc | 41 | |
ke_ix1 | 6:1331ef9ec5cc | 42 | //#define TX_POWER_CALI -15 |
ke_ix1 | 6:1331ef9ec5cc | 43 | //const static char DEVICE_NAME[] = "Nano"; // change this |
ke_ix1 | 6:1331ef9ec5cc | 44 | |
ke_ix1 | 6:1331ef9ec5cc | 45 | //#define TX_POWER_CALI -12 |
ke_ix1 | 6:1331ef9ec5cc | 46 | const static char DEVICE_NAME[] = "MN8"; // change this |
ke_ix1 | 6:1331ef9ec5cc | 47 | |
RedBearLab | 0:cffe8ac1bdf0 | 48 | |
RedBearLab | 0:cffe8ac1bdf0 | 49 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:cffe8ac1bdf0 | 50 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 51 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 52 | |
RedBearLab | 0:cffe8ac1bdf0 | 53 | #define TXRX_BUF_LEN 20 |
RedBearLab | 0:cffe8ac1bdf0 | 54 | |
RedBearLab | 2:4b66b69c7ecb | 55 | BLE ble; |
RedBearLab | 0:cffe8ac1bdf0 | 56 | |
ke_ix1 | 6:1331ef9ec5cc | 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:1331ef9ec5cc | 61 | DigitalOut ledori2(LED2); |
ke_ix1 | 6:1331ef9ec5cc | 62 | //DigitalOut USSTrigger(P0_25); // nRF51: P0_25 :超音波センサ トリガ出力 |
ke_ix1 | 6:1331ef9ec5cc | 63 | DigitalOut USSTrigger(P0_20); // 51822: P0_25 :超音波センサ トリガ出力 |
ke_ix1 | 6:1331ef9ec5cc | 64 | Timer ActiveTime; |
ke_ix1 | 6:1331ef9ec5cc | 65 | |
ke_ix1 | 6:1331ef9ec5cc | 66 | //割り込み処理宣言 |
ke_ix1 | 6:1331ef9ec5cc | 67 | Ticker TriggerTiming; //Triggerピン :インターバルタイマ |
ke_ix1 | 6:1331ef9ec5cc | 68 | // |
ke_ix1 | 6:1331ef9ec5cc | 69 | InterruptIn USSEcho(P0_28); //nRF51 P0_28 :超音波センサ エコー入力 |
ke_ix1 | 6:1331ef9ec5cc | 70 | InterruptIn USSEcho(P0_22); //51822 P0_28 :超音波センサ エコー入力 |
ke_ix1 | 6:1331ef9ec5cc | 71 | |
ke_ix1 | 6:1331ef9ec5cc | 72 | //関数宣言 |
ke_ix1 | 6:1331ef9ec5cc | 73 | void initSonic(void); |
ke_ix1 | 6:1331ef9ec5cc | 74 | void Output_Monitor(unsigned short Value); |
ke_ix1 | 6:1331ef9ec5cc | 75 | |
ke_ix1 | 6:1331ef9ec5cc | 76 | //グローバル変数宣言 |
ke_ix1 | 6:1331ef9ec5cc | 77 | unsigned short USSDistance; //USSDistance :超音波センサの測定距離 |
ke_ix1 | 6:1331ef9ec5cc | 78 | |
ke_ix1 | 6:1331ef9ec5cc | 79 | // 60msごとの割り込みでUSSTriggerに10usのON出力 |
ke_ix1 | 6:1331ef9ec5cc | 80 | void Trigger() { |
ke_ix1 | 6:1331ef9ec5cc | 81 | USSTrigger = 1; |
ke_ix1 | 6:1331ef9ec5cc | 82 | wait_us(10); |
ke_ix1 | 6:1331ef9ec5cc | 83 | USSTrigger = 0; |
ke_ix1 | 6:1331ef9ec5cc | 84 | } |
ke_ix1 | 6:1331ef9ec5cc | 85 | |
ke_ix1 | 6:1331ef9ec5cc | 86 | //USSEcho立ち上がりでの割り込み |
ke_ix1 | 6:1331ef9ec5cc | 87 | //Hiの場合ActiveTimeタイマスタート |
ke_ix1 | 6:1331ef9ec5cc | 88 | void RiseEcho() { |
ke_ix1 | 6:1331ef9ec5cc | 89 | ActiveTime.start(); |
ke_ix1 | 6:1331ef9ec5cc | 90 | } |
ke_ix1 | 6:1331ef9ec5cc | 91 | |
ke_ix1 | 6:1331ef9ec5cc | 92 | //USSEcho立ち下がりでの割り込み |
ke_ix1 | 6:1331ef9ec5cc | 93 | //Lowの場合ActiveTime停止+値読み取り |
ke_ix1 | 6:1331ef9ec5cc | 94 | void FallEcho() { |
ke_ix1 | 6:1331ef9ec5cc | 95 | unsigned long ActiveWidth; |
ke_ix1 | 6:1331ef9ec5cc | 96 | ActiveTime.stop(); |
ke_ix1 | 6:1331ef9ec5cc | 97 | ActiveWidth = ActiveTime.read_us(); |
ke_ix1 | 6:1331ef9ec5cc | 98 | USSDistance = ActiveWidth * 0.0170; |
ke_ix1 | 6:1331ef9ec5cc | 99 | ActiveTime.reset(); |
ke_ix1 | 6:1331ef9ec5cc | 100 | } |
ke_ix1 | 6:1331ef9ec5cc | 101 | |
ke_ix1 | 6:1331ef9ec5cc | 102 | void initSonic(void) { |
ke_ix1 | 6:1331ef9ec5cc | 103 | TriggerTiming.attach(Trigger, 0.060); //USSTrigger周期60ms |
ke_ix1 | 6:1331ef9ec5cc | 104 | USSEcho.rise(RiseEcho); //USSEcho立ち上がり時割り込み |
ke_ix1 | 6:1331ef9ec5cc | 105 | USSEcho.fall(FallEcho); //USSEcho立ち下がり時割り込み |
ke_ix1 | 6:1331ef9ec5cc | 106 | } |
ke_ix1 | 6:1331ef9ec5cc | 107 | |
ke_ix1 | 6:1331ef9ec5cc | 108 | void Output_Monitor(unsigned short Value){ |
ke_ix1 | 6:1331ef9ec5cc | 109 | pc.printf("%d[cm]\r\n",Value); |
ke_ix1 | 6:1331ef9ec5cc | 110 | } |
RedBearLab | 0:cffe8ac1bdf0 | 111 | |
RedBearLab | 0:cffe8ac1bdf0 | 112 | |
RedBearLab | 0:cffe8ac1bdf0 | 113 | // The Nordic UART Service |
ke_ix1 | 4:ddf414d64ea9 | 114 | //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 | 115 | //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 | 116 | //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 | 117 | //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 | 118 | |
ke_ix1 | 4:ddf414d64ea9 | 119 | // Send One CLK Service |
ke_ix1 | 6:1331ef9ec5cc | 120 | //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 | 121 | 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 | 122 | 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:1331ef9ec5cc | 123 | //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:1331ef9ec5cc | 124 | 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:1331ef9ec5cc | 125 | 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 | 126 | |
ke_ix1 | 5:29b82cc045b6 | 127 | |
ke_ix1 | 5:29b82cc045b6 | 128 | // TX POWER用 |
ke_ix1 | 5:29b82cc045b6 | 129 | 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 | 130 | uint8_t powerPayload[TXRX_BUF_LEN] = {0,}; |
ke_ix1 | 5:29b82cc045b6 | 131 | static uint8_t power_buf[TXRX_BUF_LEN]; |
ke_ix1 | 5:29b82cc045b6 | 132 | static uint8_t power_len=0; |
ke_ix1 | 6:1331ef9ec5cc | 133 | 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 | 134 | Ticker txSteper; |
RedBearLab | 0:cffe8ac1bdf0 | 135 | |
ke_ix1 | 6:1331ef9ec5cc | 136 | |
ke_ix1 | 6:1331ef9ec5cc | 137 | // MSC UUID |
ke_ix1 | 6:1331ef9ec5cc | 138 | 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:1331ef9ec5cc | 139 | uint8_t mscPayload[TXRX_BUF_LEN] = {0,}; |
ke_ix1 | 6:1331ef9ec5cc | 140 | static uint8_t msc_buf[TXRX_BUF_LEN]; |
ke_ix1 | 6:1331ef9ec5cc | 141 | static uint8_t msc_len=0; |
ke_ix1 | 6:1331ef9ec5cc | 142 | 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:1331ef9ec5cc | 143 | |
ke_ix1 | 6:1331ef9ec5cc | 144 | |
ke_ix1 | 6:1331ef9ec5cc | 145 | //Feature UUID |
ke_ix1 | 6:1331ef9ec5cc | 146 | 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:1331ef9ec5cc | 147 | uint8_t featurePayload[TXRX_BUF_LEN] = {0,}; |
ke_ix1 | 6:1331ef9ec5cc | 148 | static uint8_t feature_buf[TXRX_BUF_LEN]; |
ke_ix1 | 6:1331ef9ec5cc | 149 | static uint8_t feature_len=0; |
ke_ix1 | 6:1331ef9ec5cc | 150 | 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:1331ef9ec5cc | 151 | |
ke_ix1 | 6:1331ef9ec5cc | 152 | |
RedBearLab | 0:cffe8ac1bdf0 | 153 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 154 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 155 | |
RedBearLab | 0:cffe8ac1bdf0 | 156 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 157 | static uint8_t rx_len=0; |
ke_ix1 | 3:175ba3a059cb | 158 | static uint8_t rx_buf_st[20]; |
ke_ix1 | 4:ddf414d64ea9 | 159 | |
ke_ix1 | 4:ddf414d64ea9 | 160 | int8_t buf_before[40]; |
ke_ix1 | 4:ddf414d64ea9 | 161 | |
ke_ix1 | 4:ddf414d64ea9 | 162 | static int st = 0; |
ke_ix1 | 4:ddf414d64ea9 | 163 | |
RedBearLab | 0:cffe8ac1bdf0 | 164 | |
RedBearLab | 0:cffe8ac1bdf0 | 165 | 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 | 166 | |
ke_ix1 | 6:1331ef9ec5cc | 167 | 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:1331ef9ec5cc | 168 | |
ke_ix1 | 6:1331ef9ec5cc | 169 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic, &mscCharacteristic, &featureCharacteristic}; |
ke_ix1 | 6:1331ef9ec5cc | 170 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
ke_ix1 | 6:1331ef9ec5cc | 171 | |
ke_ix1 | 5:29b82cc045b6 | 172 | |
ke_ix1 | 6:1331ef9ec5cc | 173 | // MSC UUID |
ke_ix1 | 6:1331ef9ec5cc | 174 | void initMsc() { |
ke_ix1 | 6:1331ef9ec5cc | 175 | //ここから------------- |
ke_ix1 | 6:1331ef9ec5cc | 176 | char bufbuf_c[20]; |
ke_ix1 | 6:1331ef9ec5cc | 177 | uint8_t bufbuf[40]; |
ke_ix1 | 6:1331ef9ec5cc | 178 | //MSCのUUIDをセット |
ke_ix1 | 6:1331ef9ec5cc | 179 | sprintf(bufbuf_c, "%s", "00000008"); |
ke_ix1 | 6:1331ef9ec5cc | 180 | //sprintf(bufbuf_c, "%d", "02f3f538-8d11-4802-b6fc-fb6616d4cd70"); |
ke_ix1 | 6:1331ef9ec5cc | 181 | int a; |
ke_ix1 | 6:1331ef9ec5cc | 182 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 6:1331ef9ec5cc | 183 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 6:1331ef9ec5cc | 184 | } |
ke_ix1 | 6:1331ef9ec5cc | 185 | ble.updateCharacteristicValue(mscCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 6:1331ef9ec5cc | 186 | //ここまで------------- |
ke_ix1 | 6:1331ef9ec5cc | 187 | //をこぴればデータを送信可能 |
ke_ix1 | 6:1331ef9ec5cc | 188 | } |
RedBearLab | 0:cffe8ac1bdf0 | 189 | |
ke_ix1 | 6:1331ef9ec5cc | 190 | |
ke_ix1 | 6:1331ef9ec5cc | 191 | // Feature |
ke_ix1 | 6:1331ef9ec5cc | 192 | void initFeature() { |
ke_ix1 | 6:1331ef9ec5cc | 193 | //ここから------------- |
ke_ix1 | 6:1331ef9ec5cc | 194 | char bufbuf_c[20]; |
ke_ix1 | 6:1331ef9ec5cc | 195 | uint8_t bufbuf[40]; |
ke_ix1 | 6:1331ef9ec5cc | 196 | sprintf(bufbuf_c, "%s", "In.Sensor.USonic"); |
ke_ix1 | 6:1331ef9ec5cc | 197 | int a; |
ke_ix1 | 6:1331ef9ec5cc | 198 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 6:1331ef9ec5cc | 199 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 6:1331ef9ec5cc | 200 | } |
ke_ix1 | 6:1331ef9ec5cc | 201 | ble.updateCharacteristicValue(featureCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 6:1331ef9ec5cc | 202 | //ここまで------------- |
ke_ix1 | 6:1331ef9ec5cc | 203 | //をこぴればデータを送信可能 |
ke_ix1 | 6:1331ef9ec5cc | 204 | } |
ke_ix1 | 6:1331ef9ec5cc | 205 | |
ke_ix1 | 6:1331ef9ec5cc | 206 | |
RedBearLab | 0:cffe8ac1bdf0 | 207 | |
ke_ix1 | 5:29b82cc045b6 | 208 | // TX POWER用 |
ke_ix1 | 5:29b82cc045b6 | 209 | void txPowerUpdate(int newTxPower) { |
ke_ix1 | 5:29b82cc045b6 | 210 | sd_ble_gap_tx_power_set(newTxPower); |
ke_ix1 | 5:29b82cc045b6 | 211 | //ここから------------- |
ke_ix1 | 5:29b82cc045b6 | 212 | char bufbuf_c[20]; |
ke_ix1 | 5:29b82cc045b6 | 213 | uint8_t bufbuf[40]; |
ke_ix1 | 6:1331ef9ec5cc | 214 | sprintf(bufbuf_c, "%d", newTxPower + TX_POWER_CALI); |
ke_ix1 | 5:29b82cc045b6 | 215 | int a; |
ke_ix1 | 5:29b82cc045b6 | 216 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 5:29b82cc045b6 | 217 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 5:29b82cc045b6 | 218 | } |
ke_ix1 | 5:29b82cc045b6 | 219 | ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 5:29b82cc045b6 | 220 | //ここまで------------- |
ke_ix1 | 5:29b82cc045b6 | 221 | //をこぴればデータを送信可能 |
ke_ix1 | 5:29b82cc045b6 | 222 | } |
ke_ix1 | 5:29b82cc045b6 | 223 | |
ke_ix1 | 5:29b82cc045b6 | 224 | void txStep() { |
ke_ix1 | 5:29b82cc045b6 | 225 | //ここから------------- |
ke_ix1 | 5:29b82cc045b6 | 226 | char bufbuf_c[20]; |
ke_ix1 | 5:29b82cc045b6 | 227 | uint8_t bufbuf[40]; |
ke_ix1 | 6:1331ef9ec5cc | 228 | sprintf(bufbuf_c, "%d", TX_POWER + TX_POWER_CALI); |
ke_ix1 | 5:29b82cc045b6 | 229 | int a; |
ke_ix1 | 5:29b82cc045b6 | 230 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 5:29b82cc045b6 | 231 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 5:29b82cc045b6 | 232 | } |
ke_ix1 | 6:1331ef9ec5cc | 233 | ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 6:1331ef9ec5cc | 234 | } |
ke_ix1 | 6:1331ef9ec5cc | 235 | |
ke_ix1 | 6:1331ef9ec5cc | 236 | |
ke_ix1 | 6:1331ef9ec5cc | 237 | void initChara() { |
ke_ix1 | 6:1331ef9ec5cc | 238 | initMsc(); |
ke_ix1 | 6:1331ef9ec5cc | 239 | initFeature(); |
ke_ix1 | 6:1331ef9ec5cc | 240 | txStep(); |
ke_ix1 | 5:29b82cc045b6 | 241 | } |
ke_ix1 | 5:29b82cc045b6 | 242 | //------------------- |
ke_ix1 | 5:29b82cc045b6 | 243 | |
RedBearLab | 0:cffe8ac1bdf0 | 244 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
RedBearLab | 0:cffe8ac1bdf0 | 245 | { |
RedBearLab | 0:cffe8ac1bdf0 | 246 | pc.printf("Disconnected \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 247 | pc.printf("Restart advertising \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 248 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 249 | } |
RedBearLab | 0:cffe8ac1bdf0 | 250 | |
ke_ix1 | 3:175ba3a059cb | 251 | |
ke_ix1 | 3:175ba3a059cb | 252 | void onScanCallback(const Gap::AdvertisementCallbackParams_t *params) |
ke_ix1 | 3:175ba3a059cb | 253 | { |
ke_ix1 | 4:ddf414d64ea9 | 254 | //printf("onScanNau\n"); |
ke_ix1 | 4:ddf414d64ea9 | 255 | |
ke_ix1 | 4:ddf414d64ea9 | 256 | // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
ke_ix1 | 3:175ba3a059cb | 257 | |
ke_ix1 | 4:ddf414d64ea9 | 258 | //だいじ |
ke_ix1 | 4:ddf414d64ea9 | 259 | /* |
ke_ix1 | 3:175ba3a059cb | 260 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
ke_ix1 | 3:175ba3a059cb | 261 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
ke_ix1 | 3:175ba3a059cb | 262 | params->rssi, params->isScanResponse, params->type); |
ke_ix1 | 3:175ba3a059cb | 263 | printf("\n"); |
ke_ix1 | 3:175ba3a059cb | 264 | printf("Len:%d\n",params->advertisingDataLen); |
ke_ix1 | 3:175ba3a059cb | 265 | printf("Data:"); |
ke_ix1 | 3:175ba3a059cb | 266 | for (int i=0; i< params->advertisingDataLen; i++){ |
ke_ix1 | 3:175ba3a059cb | 267 | printf("%02x,",params->advertisingData[i]); |
ke_ix1 | 3:175ba3a059cb | 268 | } |
ke_ix1 | 4:ddf414d64ea9 | 269 | */ |
ke_ix1 | 4:ddf414d64ea9 | 270 | |
ke_ix1 | 3:175ba3a059cb | 271 | // for (int i=0; i<params->advertisingDataLen; i++){ |
ke_ix1 | 3:175ba3a059cb | 272 | // printf("%02x,",params->advertisingData[i]); |
ke_ix1 | 3:175ba3a059cb | 273 | // } |
ke_ix1 | 3:175ba3a059cb | 274 | |
ke_ix1 | 3:175ba3a059cb | 275 | printf("\n"); |
ke_ix1 | 3:175ba3a059cb | 276 | |
ke_ix1 | 3:175ba3a059cb | 277 | // DEBUG(" \n\r"); |
ke_ix1 | 3:175ba3a059cb | 278 | //#endif |
ke_ix1 | 4:ddf414d64ea9 | 279 | |
ke_ix1 | 4:ddf414d64ea9 | 280 | |
ke_ix1 | 3:175ba3a059cb | 281 | } |
ke_ix1 | 3:175ba3a059cb | 282 | |
ke_ix1 | 3:175ba3a059cb | 283 | |
RedBearLab | 2:4b66b69c7ecb | 284 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:cffe8ac1bdf0 | 285 | { |
RedBearLab | 0:cffe8ac1bdf0 | 286 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 287 | uint16_t bytesRead, index; |
RedBearLab | 0:cffe8ac1bdf0 | 288 | |
RedBearLab | 2:4b66b69c7ecb | 289 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:cffe8ac1bdf0 | 290 | { |
RedBearLab | 0:cffe8ac1bdf0 | 291 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 292 | memset(txPayload, 0, TXRX_BUF_LEN); |
ke_ix1 | 3:175ba3a059cb | 293 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
RedBearLab | 0:cffe8ac1bdf0 | 294 | pc.printf("WriteHandler \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 295 | pc.printf("Length: "); |
RedBearLab | 0:cffe8ac1bdf0 | 296 | pc.putc(bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 297 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 298 | pc.printf("Data: "); |
RedBearLab | 0:cffe8ac1bdf0 | 299 | for(index=0; index<bytesRead; index++) |
RedBearLab | 0:cffe8ac1bdf0 | 300 | { |
ke_ix1 | 6:1331ef9ec5cc | 301 | pc.putc(txPayload[index]); |
RedBearLab | 0:cffe8ac1bdf0 | 302 | } |
RedBearLab | 0:cffe8ac1bdf0 | 303 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 304 | } |
RedBearLab | 0:cffe8ac1bdf0 | 305 | } |
RedBearLab | 0:cffe8ac1bdf0 | 306 | |
RedBearLab | 0:cffe8ac1bdf0 | 307 | void uartCB(void) |
RedBearLab | 0:cffe8ac1bdf0 | 308 | { |
RedBearLab | 0:cffe8ac1bdf0 | 309 | while(pc.readable()) |
RedBearLab | 0:cffe8ac1bdf0 | 310 | { |
ke_ix1 | 3:175ba3a059cb | 311 | rx_buf[rx_len++] = pc.getc(); |
RedBearLab | 0:cffe8ac1bdf0 | 312 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
RedBearLab | 0:cffe8ac1bdf0 | 313 | { |
RedBearLab | 0:cffe8ac1bdf0 | 314 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 315 | pc.printf("RecHandler \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 316 | pc.printf("Length: "); |
RedBearLab | 0:cffe8ac1bdf0 | 317 | pc.putc(rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 318 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 319 | rx_len = 0; |
RedBearLab | 0:cffe8ac1bdf0 | 320 | break; |
RedBearLab | 0:cffe8ac1bdf0 | 321 | } |
RedBearLab | 0:cffe8ac1bdf0 | 322 | } |
RedBearLab | 0:cffe8ac1bdf0 | 323 | } |
RedBearLab | 0:cffe8ac1bdf0 | 324 | |
ke_ix1 | 3:175ba3a059cb | 325 | |
ke_ix1 | 3:175ba3a059cb | 326 | // 1++していく |
ke_ix1 | 3:175ba3a059cb | 327 | void step() { |
ke_ix1 | 6:1331ef9ec5cc | 328 | //++st; |
ke_ix1 | 6:1331ef9ec5cc | 329 | //float pir = pirInAna; |
ke_ix1 | 6:1331ef9ec5cc | 330 | //pc.printf("%f", pir); |
ke_ix1 | 6:1331ef9ec5cc | 331 | |
ke_ix1 | 6:1331ef9ec5cc | 332 | |
ke_ix1 | 4:ddf414d64ea9 | 333 | //ここから------------- |
ke_ix1 | 4:ddf414d64ea9 | 334 | char bufbuf_c[20]; |
ke_ix1 | 4:ddf414d64ea9 | 335 | uint8_t bufbuf[40]; |
ke_ix1 | 6:1331ef9ec5cc | 336 | sprintf(bufbuf_c, "%d", USSDistance); |
ke_ix1 | 4:ddf414d64ea9 | 337 | |
ke_ix1 | 4:ddf414d64ea9 | 338 | int a; |
ke_ix1 | 4:ddf414d64ea9 | 339 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 4:ddf414d64ea9 | 340 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 4:ddf414d64ea9 | 341 | } |
ke_ix1 | 4:ddf414d64ea9 | 342 | |
ke_ix1 | 4:ddf414d64ea9 | 343 | // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
ke_ix1 | 6:1331ef9ec5cc | 344 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 4:ddf414d64ea9 | 345 | //ここまで------------- |
ke_ix1 | 4:ddf414d64ea9 | 346 | //をこぴればデータを送信可能 |
ke_ix1 | 4:ddf414d64ea9 | 347 | |
ke_ix1 | 4:ddf414d64ea9 | 348 | pc.printf("%d", st); |
ke_ix1 | 4:ddf414d64ea9 | 349 | pc.printf("step! \r\n"); |
ke_ix1 | 3:175ba3a059cb | 350 | } |
ke_ix1 | 3:175ba3a059cb | 351 | |
ke_ix1 | 3:175ba3a059cb | 352 | |
RedBearLab | 0:cffe8ac1bdf0 | 353 | int main(void) |
RedBearLab | 0:cffe8ac1bdf0 | 354 | { |
ke_ix1 | 5:29b82cc045b6 | 355 | ledori1 = 0; |
RedBearLab | 0:cffe8ac1bdf0 | 356 | ble.init(); |
RedBearLab | 0:cffe8ac1bdf0 | 357 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:cffe8ac1bdf0 | 358 | ble.onDataWritten(WrittenHandler); |
ke_ix1 | 3:175ba3a059cb | 359 | |
RedBearLab | 0:cffe8ac1bdf0 | 360 | pc.baud(9600); |
RedBearLab | 0:cffe8ac1bdf0 | 361 | pc.printf("SimpleChat Init \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 362 | |
RedBearLab | 0:cffe8ac1bdf0 | 363 | pc.attach( uartCB , pc.RxIrq); |
RedBearLab | 0:cffe8ac1bdf0 | 364 | // setup advertising |
RedBearLab | 0:cffe8ac1bdf0 | 365 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:cffe8ac1bdf0 | 366 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:cffe8ac1bdf0 | 367 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
ke_ix1 | 6:1331ef9ec5cc | 368 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
RedBearLab | 0:cffe8ac1bdf0 | 369 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
RedBearLab | 0:cffe8ac1bdf0 | 370 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
RedBearLab | 0:cffe8ac1bdf0 | 371 | // 100ms; in multiples of 0.625ms. |
RedBearLab | 0:cffe8ac1bdf0 | 372 | ble.setAdvertisingInterval(160); |
RedBearLab | 0:cffe8ac1bdf0 | 373 | |
RedBearLab | 0:cffe8ac1bdf0 | 374 | ble.addService(uartService); |
RedBearLab | 0:cffe8ac1bdf0 | 375 | |
ke_ix1 | 3:175ba3a059cb | 376 | ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN, |
ke_ix1 | 3:175ba3a059cb | 377 | GapScanningParams::SCAN_WINDOW_MIN, |
ke_ix1 | 3:175ba3a059cb | 378 | 0); |
ke_ix1 | 3:175ba3a059cb | 379 | |
ke_ix1 | 3:175ba3a059cb | 380 | ble.startScan(&onScanCallback); |
ke_ix1 | 3:175ba3a059cb | 381 | |
RedBearLab | 0:cffe8ac1bdf0 | 382 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 383 | pc.printf("Advertising Start \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 384 | |
ke_ix1 | 3:175ba3a059cb | 385 | steper.attach(&step, 1.0); |
ke_ix1 | 5:29b82cc045b6 | 386 | |
ke_ix1 | 5:29b82cc045b6 | 387 | //TX POWER用 |
ke_ix1 | 5:29b82cc045b6 | 388 | txPowerUpdate(TX_POWER); |
ke_ix1 | 6:1331ef9ec5cc | 389 | //txSteper.attach(&initChara, 1.0); |
ke_ix1 | 6:1331ef9ec5cc | 390 | |
ke_ix1 | 6:1331ef9ec5cc | 391 | initChara(); |
ke_ix1 | 6:1331ef9ec5cc | 392 | char val; |
ke_ix1 | 6:1331ef9ec5cc | 393 | initSonic(); |
ke_ix1 | 5:29b82cc045b6 | 394 | |
RedBearLab | 0:cffe8ac1bdf0 | 395 | while(1) |
RedBearLab | 0:cffe8ac1bdf0 | 396 | { |
ke_ix1 | 4:ddf414d64ea9 | 397 | ble.waitForEvent(); |
ke_ix1 | 6:1331ef9ec5cc | 398 | //if( pc.readable() ){ |
ke_ix1 | 6:1331ef9ec5cc | 399 | // Output_Monitor( USSDistance ); |
ke_ix1 | 6:1331ef9ec5cc | 400 | //val = pc.getc(); |
ke_ix1 | 6:1331ef9ec5cc | 401 | //} |
RedBearLab | 0:cffe8ac1bdf0 | 402 | } |
RedBearLab | 0:cffe8ac1bdf0 | 403 | } |
RedBearLab | 0:cffe8ac1bdf0 | 404 | |
ke_ix1 | 5:29b82cc045b6 | 405 |