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