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