LEDをON(文字列'a'を送る)OFF(文字列'b'を送る)できるMSC
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
main.cpp@7:fbaa5e70a60e, 2017-01-06 (annotated)
- Committer:
- ke_ix1
- Date:
- Fri Jan 06 07:24:16 2017 +0000
- Revision:
- 7:fbaa5e70a60e
- Parent:
- 6:b15d219371da
- Child:
- 8:0d98c9aca74c
????UUID??????????
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 | 6:b15d219371da | 29 | |
ke_ix1 | 7:fbaa5e70a60e | 30 | |
ke_ix1 | 7:fbaa5e70a60e | 31 | #include "TxPower.h" |
ke_ix1 | 7:fbaa5e70a60e | 32 | //#include "DFUService.h" |
RedBearLab | 0:cffe8ac1bdf0 | 33 | |
RedBearLab | 0:cffe8ac1bdf0 | 34 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:cffe8ac1bdf0 | 35 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 36 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 37 | |
RedBearLab | 0:cffe8ac1bdf0 | 38 | #define TXRX_BUF_LEN 20 |
ke_ix1 | 4:41cf035db84a | 39 | #define DIGITAL_OUT_PIN P0_17 //D7 |
ke_ix1 | 4:41cf035db84a | 40 | |
ke_ix1 | 7:fbaa5e70a60e | 41 | //構成情報 |
ke_ix1 | 7:fbaa5e70a60e | 42 | const static char CONFIG_INFO[] = "OL"; // change this |
ke_ix1 | 7:fbaa5e70a60e | 43 | |
ke_ix1 | 7:fbaa5e70a60e | 44 | // TX POWER用 |
ke_ix1 | 7:fbaa5e70a60e | 45 | #include "ble_gap.h" |
ke_ix1 | 7:fbaa5e70a60e | 46 | #define TX_POWER 0 |
ke_ix1 | 7:fbaa5e70a60e | 47 | //機器設定 |
ke_ix1 | 7:fbaa5e70a60e | 48 | //#define TX_ZERO |
ke_ix1 | 7:fbaa5e70a60e | 49 | //#define TX_POWER_CALI -10 |
ke_ix1 | 7:fbaa5e70a60e | 50 | //const static char DEVICE_NAME[] = "nRF51DK"; // change this |
ke_ix1 | 7:fbaa5e70a60e | 51 | |
ke_ix1 | 7:fbaa5e70a60e | 52 | #define TX_POWER_CALI -7 |
ke_ix1 | 7:fbaa5e70a60e | 53 | const static char DEVICE_NAME[] = "51822"; // change this |
ke_ix1 | 7:fbaa5e70a60e | 54 | |
ke_ix1 | 7:fbaa5e70a60e | 55 | //#define TX_POWER_CALI -15 |
ke_ix1 | 7:fbaa5e70a60e | 56 | //const static char DEVICE_NAME[] = "Nano"; // change this |
ke_ix1 | 7:fbaa5e70a60e | 57 | |
ke_ix1 | 7:fbaa5e70a60e | 58 | //反映してない 二箇所設定変更する場所ある |
ke_ix1 | 7:fbaa5e70a60e | 59 | //static int tx_power = TX_POWER + TX_POWER_CALI; |
RedBearLab | 0:cffe8ac1bdf0 | 60 | |
RedBearLab | 2:4b66b69c7ecb | 61 | BLE ble; |
RedBearLab | 0:cffe8ac1bdf0 | 62 | |
RedBearLab | 0:cffe8ac1bdf0 | 63 | Serial pc(USBTX, USBRX); |
ke_ix1 | 5:35728098eeae | 64 | Ticker steper; |
ke_ix1 | 4:41cf035db84a | 65 | DigitalOut led1(DIGITAL_OUT_PIN); |
ke_ix1 | 4:41cf035db84a | 66 | DigitalOut ledori1(LED1); |
ke_ix1 | 7:fbaa5e70a60e | 67 | //DFUService dfu(ble); |
RedBearLab | 0:cffe8ac1bdf0 | 68 | |
RedBearLab | 0:cffe8ac1bdf0 | 69 | // The Nordic UART Service |
ke_ix1 | 7:fbaa5e70a60e | 70 | 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 | 7:fbaa5e70a60e | 71 | 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 | 7:fbaa5e70a60e | 72 | 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 | 7:fbaa5e70a60e | 73 | 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 | 74 | |
ke_ix1 | 4:41cf035db84a | 75 | // The Service |
ke_ix1 | 7:fbaa5e70a60e | 76 | //static const uint8_t uart_base_uuid[] = {0x02, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x70}; //Service |
ke_ix1 | 7:fbaa5e70a60e | 77 | //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 | 7:fbaa5e70a60e | 78 | //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 | 7:fbaa5e70a60e | 79 | //static const uint8_t uart_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x02}; |
ke_ix1 | 7:fbaa5e70a60e | 80 | |
ke_ix1 | 7:fbaa5e70a60e | 81 | // MSC UUID |
ke_ix1 | 7:fbaa5e70a60e | 82 | 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 | 7:fbaa5e70a60e | 83 | uint8_t mscPayload[TXRX_BUF_LEN] = {0,}; |
ke_ix1 | 7:fbaa5e70a60e | 84 | static uint8_t msc_buf[TXRX_BUF_LEN]; |
ke_ix1 | 7:fbaa5e70a60e | 85 | static uint8_t msc_len=0; |
ke_ix1 | 7:fbaa5e70a60e | 86 | GattCharacteristic mscCharacteristic (msc_uuid, mscPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); //tx power characteristic |
ke_ix1 | 6:b15d219371da | 87 | |
ke_ix1 | 6:b15d219371da | 88 | // TX POWER用 |
ke_ix1 | 5:35728098eeae | 89 | 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 | 6:b15d219371da | 90 | uint8_t powerPayload[TXRX_BUF_LEN] = {0,}; |
ke_ix1 | 6:b15d219371da | 91 | static uint8_t power_buf[TXRX_BUF_LEN]; |
ke_ix1 | 6:b15d219371da | 92 | static uint8_t power_len=0; |
ke_ix1 | 6:b15d219371da | 93 | GattCharacteristic txPowerCharacteristic (power_level_uuid, powerPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); //tx power characteristic |
ke_ix1 | 6:b15d219371da | 94 | Ticker txSteper; |
ke_ix1 | 6:b15d219371da | 95 | |
ke_ix1 | 7:fbaa5e70a60e | 96 | //Feature UUID |
ke_ix1 | 7:fbaa5e70a60e | 97 | 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 | 7:fbaa5e70a60e | 98 | uint8_t featurePayload[TXRX_BUF_LEN] = {0,}; |
ke_ix1 | 7:fbaa5e70a60e | 99 | static uint8_t feature_buf[TXRX_BUF_LEN]; |
ke_ix1 | 7:fbaa5e70a60e | 100 | static uint8_t feature_len=0; |
ke_ix1 | 7:fbaa5e70a60e | 101 | GattCharacteristic featureCharacteristic (feature_uuid, featurePayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); //tx power characteristic |
ke_ix1 | 6:b15d219371da | 102 | |
ke_ix1 | 4:41cf035db84a | 103 | |
ke_ix1 | 5:35728098eeae | 104 | // Tx Power Service |
ke_ix1 | 5:35728098eeae | 105 | //static const uint8_t power_base_uuid[] = {0x03, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x70}; //Service |
ke_ix1 | 5:35728098eeae | 106 | //static const uint8_t power_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x03}; |
ke_ix1 | 5:35728098eeae | 107 | //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:35728098eeae | 108 | |
ke_ix1 | 5:35728098eeae | 109 | /* |
ke_ix1 | 5:35728098eeae | 110 | static const uint16_t txpower_base_uuid[] = {0x18, 0x04}; |
ke_ix1 | 5:35728098eeae | 111 | static const uint16_t txpower_base_uuid_rev[] = {0x04, 0x18}; |
ke_ix1 | 5:35728098eeae | 112 | */ |
ke_ix1 | 4:41cf035db84a | 113 | //02f3f538-8d11-4802-b6fc-fb6616d4cd70 |
ke_ix1 | 4:41cf035db84a | 114 | |
ke_ix1 | 7:fbaa5e70a60e | 115 | |
RedBearLab | 0:cffe8ac1bdf0 | 116 | |
RedBearLab | 0:cffe8ac1bdf0 | 117 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 118 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 119 | |
RedBearLab | 0:cffe8ac1bdf0 | 120 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 121 | static uint8_t rx_len=0; |
RedBearLab | 0:cffe8ac1bdf0 | 122 | |
ke_ix1 | 5:35728098eeae | 123 | //Uart Service associated |
RedBearLab | 0:cffe8ac1bdf0 | 124 | 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 | 125 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
ke_ix1 | 7:fbaa5e70a60e | 126 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic, &mscCharacteristic, &featureCharacteristic}; |
RedBearLab | 0:cffe8ac1bdf0 | 127 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:cffe8ac1bdf0 | 128 | |
ke_ix1 | 5:35728098eeae | 129 | //TxPowerService *txPowerService = NULL; |
ke_ix1 | 5:35728098eeae | 130 | //BatteryService *batteryService = NULL; |
ke_ix1 | 5:35728098eeae | 131 | //uint8_t batteryLevel = 50; |
RedBearLab | 0:cffe8ac1bdf0 | 132 | |
RedBearLab | 3:b3f6c612b603 | 133 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
RedBearLab | 0:cffe8ac1bdf0 | 134 | { |
RedBearLab | 0:cffe8ac1bdf0 | 135 | pc.printf("Disconnected \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 136 | pc.printf("Restart advertising \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 137 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 138 | } |
RedBearLab | 0:cffe8ac1bdf0 | 139 | |
ke_ix1 | 4:41cf035db84a | 140 | void ConnectionCallback(const Gap::ConnectionCallbackParams_t *params) |
ke_ix1 | 4:41cf035db84a | 141 | { |
ke_ix1 | 4:41cf035db84a | 142 | pc.printf("Disconnected \r\n"); |
ke_ix1 | 4:41cf035db84a | 143 | pc.printf("Restart advertising \r\n"); |
ke_ix1 | 4:41cf035db84a | 144 | ble.startAdvertising(); |
ke_ix1 | 4:41cf035db84a | 145 | } |
ke_ix1 | 4:41cf035db84a | 146 | |
RedBearLab | 2:4b66b69c7ecb | 147 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:cffe8ac1bdf0 | 148 | { |
RedBearLab | 0:cffe8ac1bdf0 | 149 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 150 | uint16_t bytesRead, index; |
RedBearLab | 0:cffe8ac1bdf0 | 151 | |
ke_ix1 | 5:35728098eeae | 152 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:cffe8ac1bdf0 | 153 | { |
RedBearLab | 0:cffe8ac1bdf0 | 154 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 155 | memset(txPayload, 0, TXRX_BUF_LEN); |
ke_ix1 | 5:35728098eeae | 156 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
RedBearLab | 0:cffe8ac1bdf0 | 157 | pc.printf("WriteHandler \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 158 | pc.printf("Length: "); |
ke_ix1 | 4:41cf035db84a | 159 | pc.putc(bytesRead); //受け取った文字の先頭アドレス? |
ke_ix1 | 4:41cf035db84a | 160 | pc.printf("\r\n"); |
ke_ix1 | 4:41cf035db84a | 161 | pc.printf("[0]: "); |
ke_ix1 | 4:41cf035db84a | 162 | pc.putc(txPayload[1]); //1文字目を表示 |
ke_ix1 | 4:41cf035db84a | 163 | if(txPayload[1]=='a') { |
ke_ix1 | 4:41cf035db84a | 164 | pc.printf("yeahhhhhh!!!!!!!!"); |
ke_ix1 | 4:41cf035db84a | 165 | // if(led1 == 0) { |
ke_ix1 | 4:41cf035db84a | 166 | led1 = 1; |
ke_ix1 | 5:35728098eeae | 167 | ledori1 = 0; |
ke_ix1 | 4:41cf035db84a | 168 | // } else { |
ke_ix1 | 4:41cf035db84a | 169 | // led1 = 0; |
ke_ix1 | 4:41cf035db84a | 170 | // } |
ke_ix1 | 4:41cf035db84a | 171 | } else if(txPayload[1]=='b') { |
ke_ix1 | 4:41cf035db84a | 172 | led1 = 0; |
ke_ix1 | 5:35728098eeae | 173 | ledori1 = 1; |
ke_ix1 | 4:41cf035db84a | 174 | } |
RedBearLab | 0:cffe8ac1bdf0 | 175 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 176 | pc.printf("Data: "); |
RedBearLab | 0:cffe8ac1bdf0 | 177 | for(index=0; index<bytesRead; index++) |
RedBearLab | 0:cffe8ac1bdf0 | 178 | { |
ke_ix1 | 4:41cf035db84a | 179 | pc.putc(txPayload[index]); |
RedBearLab | 0:cffe8ac1bdf0 | 180 | } |
RedBearLab | 0:cffe8ac1bdf0 | 181 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 182 | } |
RedBearLab | 0:cffe8ac1bdf0 | 183 | } |
RedBearLab | 0:cffe8ac1bdf0 | 184 | |
ke_ix1 | 6:b15d219371da | 185 | // TX POWER用 |
ke_ix1 | 5:35728098eeae | 186 | void txPowerUpdate(int newTxPower) { |
ke_ix1 | 6:b15d219371da | 187 | sd_ble_gap_tx_power_set(newTxPower); |
ke_ix1 | 5:35728098eeae | 188 | //ここから------------- |
ke_ix1 | 5:35728098eeae | 189 | char bufbuf_c[20]; |
ke_ix1 | 5:35728098eeae | 190 | uint8_t bufbuf[40]; |
ke_ix1 | 7:fbaa5e70a60e | 191 | sprintf(bufbuf_c, "%d", newTxPower + TX_POWER_CALI); |
ke_ix1 | 5:35728098eeae | 192 | int a; |
ke_ix1 | 5:35728098eeae | 193 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 5:35728098eeae | 194 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 5:35728098eeae | 195 | } |
ke_ix1 | 5:35728098eeae | 196 | ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 5:35728098eeae | 197 | //ここまで------------- |
ke_ix1 | 5:35728098eeae | 198 | //をこぴればデータを送信可能 |
ke_ix1 | 5:35728098eeae | 199 | } |
ke_ix1 | 5:35728098eeae | 200 | |
ke_ix1 | 7:fbaa5e70a60e | 201 | |
ke_ix1 | 7:fbaa5e70a60e | 202 | // MSC UUID |
ke_ix1 | 7:fbaa5e70a60e | 203 | void initMsc() { |
ke_ix1 | 6:b15d219371da | 204 | //ここから------------- |
ke_ix1 | 6:b15d219371da | 205 | char bufbuf_c[20]; |
ke_ix1 | 6:b15d219371da | 206 | uint8_t bufbuf[40]; |
ke_ix1 | 7:fbaa5e70a60e | 207 | sprintf(bufbuf_c, "%s", "00000001"); |
ke_ix1 | 7:fbaa5e70a60e | 208 | //sprintf(bufbuf_c, "%d", "02f3f538-8d11-4802-b6fc-fb6616d4cd70"); |
ke_ix1 | 6:b15d219371da | 209 | int a; |
ke_ix1 | 6:b15d219371da | 210 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 6:b15d219371da | 211 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 6:b15d219371da | 212 | } |
ke_ix1 | 7:fbaa5e70a60e | 213 | ble.updateCharacteristicValue(mscCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 7:fbaa5e70a60e | 214 | //ここまで------------- |
ke_ix1 | 7:fbaa5e70a60e | 215 | //をこぴればデータを送信可能 |
ke_ix1 | 7:fbaa5e70a60e | 216 | } |
ke_ix1 | 7:fbaa5e70a60e | 217 | |
ke_ix1 | 7:fbaa5e70a60e | 218 | |
ke_ix1 | 7:fbaa5e70a60e | 219 | // Feature |
ke_ix1 | 7:fbaa5e70a60e | 220 | void initFeature() { |
ke_ix1 | 7:fbaa5e70a60e | 221 | //ここから------------- |
ke_ix1 | 7:fbaa5e70a60e | 222 | char bufbuf_c[20]; |
ke_ix1 | 7:fbaa5e70a60e | 223 | uint8_t bufbuf[40]; |
ke_ix1 | 7:fbaa5e70a60e | 224 | sprintf(bufbuf_c, "%s", "Out.Led"); |
ke_ix1 | 7:fbaa5e70a60e | 225 | int a; |
ke_ix1 | 7:fbaa5e70a60e | 226 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 7:fbaa5e70a60e | 227 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 7:fbaa5e70a60e | 228 | } |
ke_ix1 | 7:fbaa5e70a60e | 229 | ble.updateCharacteristicValue(featureCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 7:fbaa5e70a60e | 230 | //ここまで------------- |
ke_ix1 | 7:fbaa5e70a60e | 231 | //をこぴればデータを送信可能 |
ke_ix1 | 7:fbaa5e70a60e | 232 | } |
ke_ix1 | 7:fbaa5e70a60e | 233 | |
ke_ix1 | 7:fbaa5e70a60e | 234 | |
ke_ix1 | 7:fbaa5e70a60e | 235 | void initChara() { |
ke_ix1 | 7:fbaa5e70a60e | 236 | initMsc(); |
ke_ix1 | 7:fbaa5e70a60e | 237 | initFeature(); |
ke_ix1 | 7:fbaa5e70a60e | 238 | } |
ke_ix1 | 7:fbaa5e70a60e | 239 | |
ke_ix1 | 7:fbaa5e70a60e | 240 | void txStep() { |
ke_ix1 | 7:fbaa5e70a60e | 241 | //ここから------------- |
ke_ix1 | 7:fbaa5e70a60e | 242 | //sd_ble_gap_tx_power_set(TX_POWER); |
ke_ix1 | 7:fbaa5e70a60e | 243 | char bufbuf_c[20]; |
ke_ix1 | 7:fbaa5e70a60e | 244 | uint8_t bufbuf[40]; |
ke_ix1 | 7:fbaa5e70a60e | 245 | sprintf(bufbuf_c, "%d", TX_POWER + TX_POWER_CALI); |
ke_ix1 | 7:fbaa5e70a60e | 246 | int a; |
ke_ix1 | 7:fbaa5e70a60e | 247 | for(a=0; bufbuf_c[a] != '\0'; a++) { |
ke_ix1 | 7:fbaa5e70a60e | 248 | bufbuf[a] = bufbuf_c[a]; |
ke_ix1 | 7:fbaa5e70a60e | 249 | } |
ke_ix1 | 7:fbaa5e70a60e | 250 | ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); |
ke_ix1 | 7:fbaa5e70a60e | 251 | initChara(); |
ke_ix1 | 6:b15d219371da | 252 | } |
ke_ix1 | 6:b15d219371da | 253 | //------------------- |
ke_ix1 | 6:b15d219371da | 254 | |
ke_ix1 | 7:fbaa5e70a60e | 255 | |
RedBearLab | 0:cffe8ac1bdf0 | 256 | void uartCB(void) |
RedBearLab | 0:cffe8ac1bdf0 | 257 | { |
RedBearLab | 0:cffe8ac1bdf0 | 258 | while(pc.readable()) |
RedBearLab | 0:cffe8ac1bdf0 | 259 | { |
RedBearLab | 0:cffe8ac1bdf0 | 260 | rx_buf[rx_len++] = pc.getc(); |
RedBearLab | 0:cffe8ac1bdf0 | 261 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
RedBearLab | 0:cffe8ac1bdf0 | 262 | { |
ke_ix1 | 5:35728098eeae | 263 | txPowerUpdate(TX_POWER); |
ke_ix1 | 5:35728098eeae | 264 | |
RedBearLab | 0:cffe8ac1bdf0 | 265 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 266 | pc.printf("RecHandler \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 267 | pc.printf("Length: "); |
RedBearLab | 0:cffe8ac1bdf0 | 268 | pc.putc(rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 269 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 270 | rx_len = 0; |
RedBearLab | 0:cffe8ac1bdf0 | 271 | break; |
RedBearLab | 0:cffe8ac1bdf0 | 272 | } |
RedBearLab | 0:cffe8ac1bdf0 | 273 | } |
RedBearLab | 0:cffe8ac1bdf0 | 274 | } |
RedBearLab | 0:cffe8ac1bdf0 | 275 | |
RedBearLab | 0:cffe8ac1bdf0 | 276 | int main(void) |
RedBearLab | 0:cffe8ac1bdf0 | 277 | { |
ke_ix1 | 4:41cf035db84a | 278 | led1 = 0; |
ke_ix1 | 4:41cf035db84a | 279 | ledori1 = 0; |
RedBearLab | 0:cffe8ac1bdf0 | 280 | ble.init(); |
RedBearLab | 0:cffe8ac1bdf0 | 281 | ble.onDisconnection(disconnectionCallback); |
ke_ix1 | 4:41cf035db84a | 282 | ble.onDataWritten(WrittenHandler); |
ke_ix1 | 4:41cf035db84a | 283 | ble.onConnection(ConnectionCallback); |
RedBearLab | 0:cffe8ac1bdf0 | 284 | |
RedBearLab | 0:cffe8ac1bdf0 | 285 | pc.baud(9600); |
RedBearLab | 0:cffe8ac1bdf0 | 286 | pc.printf("SimpleChat Init \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 287 | |
RedBearLab | 0:cffe8ac1bdf0 | 288 | pc.attach( uartCB , pc.RxIrq); |
ke_ix1 | 5:35728098eeae | 289 | |
ke_ix1 | 7:fbaa5e70a60e | 290 | //txPowerService = new TxPowerService(ble, -40); |
ke_ix1 | 7:fbaa5e70a60e | 291 | //txPowerService->updateTxPower(TX_POWER); |
ke_ix1 | 5:35728098eeae | 292 | |
ke_ix1 | 5:35728098eeae | 293 | |
ke_ix1 | 5:35728098eeae | 294 | //TxPowerService tx(ble, -40); // setting up the service and assigning an initial value |
ke_ix1 | 5:35728098eeae | 295 | //tx.updateTxPower(TX_POWER); //changing the initial value just for testing .... |
ke_ix1 | 5:35728098eeae | 296 | |
RedBearLab | 0:cffe8ac1bdf0 | 297 | // setup advertising |
ke_ix1 | 5:35728098eeae | 298 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ke_ix1 | 5:35728098eeae | 299 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)txpower_base_uuid_rev, sizeof(txpower_base_uuid_rev)); |
ke_ix1 | 5:35728098eeae | 300 | |
RedBearLab | 0:cffe8ac1bdf0 | 301 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:cffe8ac1bdf0 | 302 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:cffe8ac1bdf0 | 303 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
ke_ix1 | 4:41cf035db84a | 304 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
RedBearLab | 0:cffe8ac1bdf0 | 305 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
RedBearLab | 0:cffe8ac1bdf0 | 306 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
ke_ix1 | 5:35728098eeae | 307 | |
ke_ix1 | 7:fbaa5e70a60e | 308 | |
RedBearLab | 0:cffe8ac1bdf0 | 309 | // 100ms; in multiples of 0.625ms. |
ke_ix1 | 4:41cf035db84a | 310 | ble.setAdvertisingInterval(50); |
RedBearLab | 0:cffe8ac1bdf0 | 311 | |
RedBearLab | 0:cffe8ac1bdf0 | 312 | ble.addService(uartService); |
RedBearLab | 0:cffe8ac1bdf0 | 313 | |
ke_ix1 | 5:35728098eeae | 314 | ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN, |
ke_ix1 | 5:35728098eeae | 315 | GapScanningParams::SCAN_WINDOW_MIN, |
ke_ix1 | 5:35728098eeae | 316 | 0); |
ke_ix1 | 5:35728098eeae | 317 | |
RedBearLab | 0:cffe8ac1bdf0 | 318 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 319 | pc.printf("Advertising Start \r\n"); |
ke_ix1 | 6:b15d219371da | 320 | |
ke_ix1 | 6:b15d219371da | 321 | //TX POWER用 |
ke_ix1 | 6:b15d219371da | 322 | txSteper.attach(&txStep, 2.0); |
ke_ix1 | 6:b15d219371da | 323 | txPowerUpdate(TX_POWER); |
ke_ix1 | 5:35728098eeae | 324 | |
ke_ix1 | 7:fbaa5e70a60e | 325 | initChara(); |
ke_ix1 | 7:fbaa5e70a60e | 326 | |
RedBearLab | 0:cffe8ac1bdf0 | 327 | while(1) |
RedBearLab | 0:cffe8ac1bdf0 | 328 | { |
RedBearLab | 0:cffe8ac1bdf0 | 329 | ble.waitForEvent(); |
RedBearLab | 0:cffe8ac1bdf0 | 330 | } |
RedBearLab | 0:cffe8ac1bdf0 | 331 | } |
ke_ix1 | 6:b15d219371da | 332 | |
ke_ix1 | 7:fbaa5e70a60e | 333 |