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