InfraredLibに対応したMSC

Dependencies:   BLE_API RemoteIR mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Committer:
ke_ix1
Date:
Sun Feb 12 10:31:54 2017 +0000
Revision:
7:66243b10532c
Parent:
6:976503121a46
InfraredLib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:1c058e553423 1 /*
RedBearLab 0:cffe8ac1bdf0 2
RedBearLab 1:1c058e553423 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:1c058e553423 4
RedBearLab 1:1c058e553423 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:1c058e553423 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:1c058e553423 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:1c058e553423 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:1c058e553423 9 subject to the following conditions:
RedBearLab 1:1c058e553423 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:1c058e553423 11
RedBearLab 1:1c058e553423 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:1c058e553423 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:1c058e553423 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:1c058e553423 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:1c058e553423 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:1c058e553423 17
RedBearLab 1:1c058e553423 18 */
RedBearLab 1:1c058e553423 19
RedBearLab 1:1c058e553423 20 /*
RedBearLab 1:1c058e553423 21 * The application works with the BLEController iOS/Android App.
RedBearLab 1:1c058e553423 22 * Type something from the Terminal to send
RedBearLab 1:1c058e553423 23 * to the BLEController App or vice verse.
RedBearLab 1:1c058e553423 24 * Characteristics received from App will print on Terminal.
RedBearLab 1:1c058e553423 25 */
RedBearLab 1:1c058e553423 26
RedBearLab 0:cffe8ac1bdf0 27 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 28 #include "ble/BLE.h"
ke_ix1 3:175ba3a059cb 29 #include <string.h>
ke_ix1 4:f1923d9f536e 30 #include "ReceiverIR.h"
ke_ix1 4:f1923d9f536e 31 #include "TransmitterIR.h"
ke_ix1 4:f1923d9f536e 32
ke_ix1 3:175ba3a059cb 33 //#include <stdio.h>
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35
RedBearLab 0:cffe8ac1bdf0 36 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 37 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 38 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 39
RedBearLab 0:cffe8ac1bdf0 40 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 41
RedBearLab 2:4b66b69c7ecb 42 BLE ble;
RedBearLab 0:cffe8ac1bdf0 43
RedBearLab 0:cffe8ac1bdf0 44 Serial pc(USBTX, USBRX);
ke_ix1 3:175ba3a059cb 45 Ticker steper;
ke_ix1 6:976503121a46 46 DigitalOut led1(P0_17);
ke_ix1 7:66243b10532c 47 DigitalOut ledori1(LED1);
ke_ix1 7:66243b10532c 48 //DigitalOut ledori2(LED2);
RedBearLab 0:cffe8ac1bdf0 49
ke_ix1 7:66243b10532c 50 //ReceiverIR ir_rx(P0_25); //nRF51
ke_ix1 7:66243b10532c 51 //ReceiverIR ir_rx(P0_4); //Nano できんかった
ke_ix1 7:66243b10532c 52 ReceiverIR ir_rx(P0_2); //51822 nrf51
ke_ix1 7:66243b10532c 53
ke_ix1 4:f1923d9f536e 54 TransmitterIR ir_tx(P0_18);
ke_ix1 4:f1923d9f536e 55
ke_ix1 4:f1923d9f536e 56 //ReceiverIR ir_rx(P0_25);
ke_ix1 4:f1923d9f536e 57
RedBearLab 0:cffe8ac1bdf0 58
ke_ix1 7:66243b10532c 59 // TX POWER用
ke_ix1 7:66243b10532c 60 #include "ble_gap.h"
ke_ix1 7:66243b10532c 61 #define TX_POWER 0
ke_ix1 7:66243b10532c 62 //機器設定
ke_ix1 7:66243b10532c 63 //#define TX_ZERO
ke_ix1 7:66243b10532c 64 //#define TX_POWER_CALI -10
ke_ix1 7:66243b10532c 65 //const static char DEVICE_NAME[] = "nRF51DK"; // change this
ke_ix1 7:66243b10532c 66
ke_ix1 7:66243b10532c 67 //#define TX_POWER_CALI -7
ke_ix1 7:66243b10532c 68 //const static char DEVICE_NAME[] = "51822"; // change this
ke_ix1 7:66243b10532c 69
ke_ix1 7:66243b10532c 70 //#define TX_POWER_CALI -15
ke_ix1 7:66243b10532c 71 //const static char DEVICE_NAME[] = "Nano"; // change this
ke_ix1 7:66243b10532c 72
ke_ix1 7:66243b10532c 73 #define TX_POWER_CALI -7
ke_ix1 7:66243b10532c 74 const static char DEVICE_NAME[] = "MN6"; // change this
RedBearLab 0:cffe8ac1bdf0 75
ke_ix1 4:f1923d9f536e 76 // The Nordic UART Service
ke_ix1 7:66243b10532c 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:66243b10532c 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:66243b10532c 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:66243b10532c 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};
ke_ix1 7:66243b10532c 81
ke_ix1 7:66243b10532c 82
ke_ix1 7:66243b10532c 83
ke_ix1 7:66243b10532c 84 // MSC UUID
ke_ix1 7:66243b10532c 85 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:66243b10532c 86 uint8_t mscPayload[TXRX_BUF_LEN] = {0,};
ke_ix1 7:66243b10532c 87 static uint8_t msc_buf[TXRX_BUF_LEN];
ke_ix1 7:66243b10532c 88 static uint8_t msc_len=0;
ke_ix1 7:66243b10532c 89 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 4:f1923d9f536e 90
ke_ix1 7:66243b10532c 91 // TX POWER用
ke_ix1 7:66243b10532c 92 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 7:66243b10532c 93 uint8_t powerPayload[TXRX_BUF_LEN] = {0,};
ke_ix1 7:66243b10532c 94 static uint8_t power_buf[TXRX_BUF_LEN];
ke_ix1 7:66243b10532c 95 static uint8_t power_len=0;
ke_ix1 7:66243b10532c 96 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 7:66243b10532c 97 Ticker txSteper;
ke_ix1 7:66243b10532c 98
ke_ix1 7:66243b10532c 99 //Feature UUID
ke_ix1 7:66243b10532c 100 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:66243b10532c 101 uint8_t featurePayload[TXRX_BUF_LEN] = {0,};
ke_ix1 7:66243b10532c 102 static uint8_t feature_buf[TXRX_BUF_LEN];
ke_ix1 7:66243b10532c 103 static uint8_t feature_len=0;
ke_ix1 7:66243b10532c 104 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 7:66243b10532c 105
ke_ix1 7:66243b10532c 106
ke_ix1 7:66243b10532c 107
ke_ix1 7:66243b10532c 108
ke_ix1 7:66243b10532c 109
RedBearLab 0:cffe8ac1bdf0 110
RedBearLab 0:cffe8ac1bdf0 111 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 112 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 113
RedBearLab 0:cffe8ac1bdf0 114 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 115 static uint8_t rx_len=0;
ke_ix1 3:175ba3a059cb 116 static uint8_t rx_buf_st[20];
ke_ix1 4:f1923d9f536e 117
ke_ix1 4:f1923d9f536e 118 int8_t buf_before[40];
ke_ix1 4:f1923d9f536e 119
ke_ix1 4:f1923d9f536e 120 static int st = 0;
ke_ix1 4:f1923d9f536e 121
ke_ix1 5:1bf7a84598a1 122 int flagMargine = -1;
ke_ix1 4:f1923d9f536e 123 int receivedFlag = 0;
RedBearLab 0:cffe8ac1bdf0 124
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);
RedBearLab 0:cffe8ac1bdf0 126
ke_ix1 7:66243b10532c 127 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:66243b10532c 128
ke_ix1 7:66243b10532c 129 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic, &mscCharacteristic, &featureCharacteristic};
RedBearLab 0:cffe8ac1bdf0 130
RedBearLab 0:cffe8ac1bdf0 131 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 132
ke_ix1 7:66243b10532c 133
ke_ix1 7:66243b10532c 134
ke_ix1 7:66243b10532c 135
ke_ix1 7:66243b10532c 136
ke_ix1 7:66243b10532c 137 // TX POWER用
ke_ix1 7:66243b10532c 138 void txPowerUpdate(int newTxPower) {
ke_ix1 7:66243b10532c 139 sd_ble_gap_tx_power_set(newTxPower);
ke_ix1 7:66243b10532c 140 //ここから-------------
ke_ix1 7:66243b10532c 141 char bufbuf_c[20];
ke_ix1 7:66243b10532c 142 uint8_t bufbuf[40];
ke_ix1 7:66243b10532c 143 sprintf(bufbuf_c, "%d", newTxPower + TX_POWER_CALI);
ke_ix1 7:66243b10532c 144 int a;
ke_ix1 7:66243b10532c 145 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 7:66243b10532c 146 bufbuf[a] = bufbuf_c[a];
ke_ix1 7:66243b10532c 147 }
ke_ix1 7:66243b10532c 148 ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 7:66243b10532c 149 //ここまで-------------
ke_ix1 7:66243b10532c 150 //をこぴればデータを送信可能
ke_ix1 7:66243b10532c 151 }
ke_ix1 7:66243b10532c 152
ke_ix1 7:66243b10532c 153
ke_ix1 7:66243b10532c 154 // MSC UUID
ke_ix1 7:66243b10532c 155 void initMsc() {
ke_ix1 7:66243b10532c 156 //ここから-------------
ke_ix1 7:66243b10532c 157 char bufbuf_c[20];
ke_ix1 7:66243b10532c 158 uint8_t bufbuf[40];
ke_ix1 7:66243b10532c 159 sprintf(bufbuf_c, "%s", "00000003");
ke_ix1 7:66243b10532c 160 //sprintf(bufbuf_c, "%d", "02f3f538-8d11-4802-b6fc-fb6616d4cd70");
ke_ix1 7:66243b10532c 161 int a;
ke_ix1 7:66243b10532c 162 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 7:66243b10532c 163 bufbuf[a] = bufbuf_c[a];
ke_ix1 7:66243b10532c 164 }
ke_ix1 7:66243b10532c 165 ble.updateCharacteristicValue(mscCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 7:66243b10532c 166 //ここまで-------------
ke_ix1 7:66243b10532c 167 //をこぴればデータを送信可能
ke_ix1 7:66243b10532c 168 }
ke_ix1 7:66243b10532c 169
ke_ix1 7:66243b10532c 170
ke_ix1 7:66243b10532c 171 // Feature
ke_ix1 7:66243b10532c 172 void initFeature() {
ke_ix1 7:66243b10532c 173 //ここから-------------
ke_ix1 7:66243b10532c 174 char bufbuf_c[20];
ke_ix1 7:66243b10532c 175 uint8_t bufbuf[40];
ke_ix1 7:66243b10532c 176 sprintf(bufbuf_c, "%s", "In.Sensor.Infrared");
ke_ix1 7:66243b10532c 177 int a;
ke_ix1 7:66243b10532c 178 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 7:66243b10532c 179 bufbuf[a] = bufbuf_c[a];
ke_ix1 7:66243b10532c 180 }
ke_ix1 7:66243b10532c 181 ble.updateCharacteristicValue(featureCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 7:66243b10532c 182 //ここまで-------------
ke_ix1 7:66243b10532c 183 //をこぴればデータを送信可能
ke_ix1 7:66243b10532c 184 }
ke_ix1 7:66243b10532c 185
ke_ix1 7:66243b10532c 186
ke_ix1 7:66243b10532c 187 void txStep() {
ke_ix1 7:66243b10532c 188 //ここから-------------
ke_ix1 7:66243b10532c 189 //sd_ble_gap_tx_power_set(TX_POWER);
ke_ix1 7:66243b10532c 190 char bufbuf_c[20];
ke_ix1 7:66243b10532c 191 uint8_t bufbuf[40];
ke_ix1 7:66243b10532c 192 sprintf(bufbuf_c, "%d", TX_POWER + TX_POWER_CALI);
ke_ix1 7:66243b10532c 193 int a;
ke_ix1 7:66243b10532c 194 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 7:66243b10532c 195 bufbuf[a] = bufbuf_c[a];
ke_ix1 7:66243b10532c 196 }
ke_ix1 7:66243b10532c 197 ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 7:66243b10532c 198 }
ke_ix1 7:66243b10532c 199
ke_ix1 7:66243b10532c 200
ke_ix1 7:66243b10532c 201 void initChara() {
ke_ix1 7:66243b10532c 202 initMsc();
ke_ix1 7:66243b10532c 203 initFeature();
ke_ix1 7:66243b10532c 204 txStep();
ke_ix1 7:66243b10532c 205 }
ke_ix1 7:66243b10532c 206
ke_ix1 7:66243b10532c 207 //-------------------
ke_ix1 7:66243b10532c 208
ke_ix1 7:66243b10532c 209
ke_ix1 7:66243b10532c 210
ke_ix1 7:66243b10532c 211
RedBearLab 0:cffe8ac1bdf0 212 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 213 {
RedBearLab 0:cffe8ac1bdf0 214 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 215 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 216 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 217 }
RedBearLab 0:cffe8ac1bdf0 218
ke_ix1 3:175ba3a059cb 219
ke_ix1 3:175ba3a059cb 220 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
ke_ix1 3:175ba3a059cb 221 {
ke_ix1 4:f1923d9f536e 222 //printf("onScanNau\n");
ke_ix1 4:f1923d9f536e 223
ke_ix1 4:f1923d9f536e 224 // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 225
ke_ix1 4:f1923d9f536e 226 //だいじ
ke_ix1 4:f1923d9f536e 227 /*
ke_ix1 3:175ba3a059cb 228 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 229 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
ke_ix1 3:175ba3a059cb 230 params->rssi, params->isScanResponse, params->type);
ke_ix1 3:175ba3a059cb 231 printf("\n");
ke_ix1 3:175ba3a059cb 232 printf("Len:%d\n",params->advertisingDataLen);
ke_ix1 3:175ba3a059cb 233 printf("Data:");
ke_ix1 3:175ba3a059cb 234 for (int i=0; i< params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 235 printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 236 }
ke_ix1 4:f1923d9f536e 237 */
ke_ix1 4:f1923d9f536e 238
ke_ix1 3:175ba3a059cb 239 // for (int i=0; i<params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 240 // printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 241 // }
ke_ix1 3:175ba3a059cb 242
ke_ix1 3:175ba3a059cb 243 printf("\n");
ke_ix1 3:175ba3a059cb 244
ke_ix1 3:175ba3a059cb 245 // DEBUG(" \n\r");
ke_ix1 3:175ba3a059cb 246 //#endif
ke_ix1 4:f1923d9f536e 247
ke_ix1 4:f1923d9f536e 248
ke_ix1 3:175ba3a059cb 249 }
ke_ix1 3:175ba3a059cb 250
ke_ix1 3:175ba3a059cb 251
RedBearLab 2:4b66b69c7ecb 252 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 253 {
RedBearLab 0:cffe8ac1bdf0 254 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 255 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 256
RedBearLab 2:4b66b69c7ecb 257 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 258 {
RedBearLab 0:cffe8ac1bdf0 259 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 260 memset(txPayload, 0, TXRX_BUF_LEN);
ke_ix1 3:175ba3a059cb 261 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 262 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 263 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 264 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 265 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 266 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 267 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 268 {
RedBearLab 0:cffe8ac1bdf0 269 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 270 }
RedBearLab 0:cffe8ac1bdf0 271 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 272 }
RedBearLab 0:cffe8ac1bdf0 273 }
RedBearLab 0:cffe8ac1bdf0 274
RedBearLab 0:cffe8ac1bdf0 275 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 276 {
RedBearLab 0:cffe8ac1bdf0 277 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 278 {
ke_ix1 3:175ba3a059cb 279 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 280 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 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
ke_ix1 3:175ba3a059cb 293
ke_ix1 3:175ba3a059cb 294 // 1++していく
ke_ix1 3:175ba3a059cb 295 void step() {
ke_ix1 4:f1923d9f536e 296 ++st;
ke_ix1 4:f1923d9f536e 297 //ここから-------------
ke_ix1 4:f1923d9f536e 298 char bufbuf_c[20];
ke_ix1 4:f1923d9f536e 299 uint8_t bufbuf[40];
ke_ix1 4:f1923d9f536e 300 sprintf(bufbuf_c, "%d", st);
ke_ix1 4:f1923d9f536e 301
ke_ix1 4:f1923d9f536e 302 int a;
ke_ix1 4:f1923d9f536e 303 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 4:f1923d9f536e 304 bufbuf[a] = bufbuf_c[a];
ke_ix1 4:f1923d9f536e 305 }
ke_ix1 4:f1923d9f536e 306
ke_ix1 4:f1923d9f536e 307 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 4:f1923d9f536e 308 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 4:f1923d9f536e 309 //ここまで-------------
ke_ix1 4:f1923d9f536e 310 //をこぴればデータを送信可能
ke_ix1 4:f1923d9f536e 311
ke_ix1 3:175ba3a059cb 312 //pc.printf("%d", st);
ke_ix1 3:175ba3a059cb 313 //pc.printf("step! \r\n");
ke_ix1 3:175ba3a059cb 314 }
ke_ix1 3:175ba3a059cb 315
ke_ix1 4:f1923d9f536e 316 void flagSet() {
ke_ix1 4:f1923d9f536e 317 //ここから-------------
ke_ix1 4:f1923d9f536e 318 char bufbuf_c[20];
ke_ix1 4:f1923d9f536e 319 uint8_t bufbuf[40];
ke_ix1 4:f1923d9f536e 320 sprintf(bufbuf_c, "%d", receivedFlag);
ke_ix1 6:976503121a46 321 led1 = receivedFlag;
ke_ix1 7:66243b10532c 322 //ledori2 = led1;
ke_ix1 4:f1923d9f536e 323 int a;
ke_ix1 4:f1923d9f536e 324 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 4:f1923d9f536e 325 bufbuf[a] = bufbuf_c[a];
ke_ix1 4:f1923d9f536e 326 }
ke_ix1 4:f1923d9f536e 327
ke_ix1 4:f1923d9f536e 328 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 4:f1923d9f536e 329 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 4:f1923d9f536e 330 //ここまで-------------
ke_ix1 4:f1923d9f536e 331 //をこぴればデータを送信可能
ke_ix1 4:f1923d9f536e 332 }
ke_ix1 4:f1923d9f536e 333
ke_ix1 4:f1923d9f536e 334
ke_ix1 4:f1923d9f536e 335
ke_ix1 4:f1923d9f536e 336 /**
ke_ix1 4:f1923d9f536e 337 * Receive.
ke_ix1 4:f1923d9f536e 338 *
ke_ix1 4:f1923d9f536e 339 * @param format Pointer to a format.
ke_ix1 4:f1923d9f536e 340 * @param buf Pointer to a buffer.
ke_ix1 4:f1923d9f536e 341 * @param bufsiz Size of the buffer.
ke_ix1 4:f1923d9f536e 342 *
ke_ix1 4:f1923d9f536e 343 * @return Bit length of the received data.
ke_ix1 4:f1923d9f536e 344 */
ke_ix1 4:f1923d9f536e 345 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) {
ke_ix1 4:f1923d9f536e 346 int cnt = 0;
ke_ix1 6:976503121a46 347 // while (ir_rx.getState() != ReceiverIR::Received) {
ke_ix1 6:976503121a46 348 if (ir_rx.getState() != ReceiverIR::Received) {
ke_ix1 4:f1923d9f536e 349 //pc.printf("no receive \r\n");
ke_ix1 4:f1923d9f536e 350 return -1;
ke_ix1 4:f1923d9f536e 351 }
ke_ix1 4:f1923d9f536e 352 return ir_rx.getData(format, buf, bufsiz * 8);
ke_ix1 4:f1923d9f536e 353 }
ke_ix1 4:f1923d9f536e 354
ke_ix1 4:f1923d9f536e 355 /**
ke_ix1 4:f1923d9f536e 356 * Transmit.
ke_ix1 4:f1923d9f536e 357 *
ke_ix1 4:f1923d9f536e 358 * @param format Format.
ke_ix1 4:f1923d9f536e 359 * @param buf Pointer to a buffer.
ke_ix1 4:f1923d9f536e 360 * @param bitlength Bit length of the data.
ke_ix1 4:f1923d9f536e 361 *
ke_ix1 4:f1923d9f536e 362 * @return Bit length of the received data.
ke_ix1 4:f1923d9f536e 363 */
ke_ix1 4:f1923d9f536e 364 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) {
ke_ix1 4:f1923d9f536e 365 int cnt = 0;
ke_ix1 4:f1923d9f536e 366 while (ir_tx.getState() != TransmitterIR::Idle) {
ke_ix1 4:f1923d9f536e 367 cnt++;
ke_ix1 4:f1923d9f536e 368 if (timeout < cnt) {
ke_ix1 4:f1923d9f536e 369 return -1;
ke_ix1 4:f1923d9f536e 370 }
ke_ix1 4:f1923d9f536e 371 }
ke_ix1 4:f1923d9f536e 372 return ir_tx.setData(format, buf, bitlength);
ke_ix1 4:f1923d9f536e 373 }
ke_ix1 4:f1923d9f536e 374
ke_ix1 4:f1923d9f536e 375 /**
ke_ix1 4:f1923d9f536e 376 * Display a data.
ke_ix1 4:f1923d9f536e 377 *
ke_ix1 4:f1923d9f536e 378 * @param buf Pointer to a buffer.
ke_ix1 4:f1923d9f536e 379 * @param bitlength Bit length of a data.
ke_ix1 4:f1923d9f536e 380 */
ke_ix1 4:f1923d9f536e 381 void display_data(uint8_t *buf, int bitlength) {
ke_ix1 4:f1923d9f536e 382 //lcd.locate(0, 1);
ke_ix1 4:f1923d9f536e 383 const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
ke_ix1 4:f1923d9f536e 384 for (int i = 0; i < n; i++) {
ke_ix1 4:f1923d9f536e 385
ke_ix1 4:f1923d9f536e 386 pc.printf("%02X", buf[i]);
ke_ix1 4:f1923d9f536e 387 }
ke_ix1 4:f1923d9f536e 388 for (int i = 0; i < 8 - n; i++) {
ke_ix1 4:f1923d9f536e 389 pc.printf("--");
ke_ix1 4:f1923d9f536e 390 }
ke_ix1 4:f1923d9f536e 391 }
ke_ix1 4:f1923d9f536e 392
ke_ix1 4:f1923d9f536e 393 /**
ke_ix1 4:f1923d9f536e 394 * Display a current status.
ke_ix1 4:f1923d9f536e 395 */
ke_ix1 4:f1923d9f536e 396 void display_status(char *status, int bitlength) {
ke_ix1 4:f1923d9f536e 397 //lcd.locate(8, 0);
ke_ix1 4:f1923d9f536e 398 pc.printf("%-5.5s:%02d", status, bitlength);
ke_ix1 4:f1923d9f536e 399 }
ke_ix1 4:f1923d9f536e 400
ke_ix1 4:f1923d9f536e 401 /**
ke_ix1 4:f1923d9f536e 402 * Display a format of a data.
ke_ix1 4:f1923d9f536e 403 */
ke_ix1 4:f1923d9f536e 404 void display_format(RemoteIR::Format format) {
ke_ix1 4:f1923d9f536e 405 //lcd.locate(0, 0);
ke_ix1 4:f1923d9f536e 406 switch (format) {
ke_ix1 4:f1923d9f536e 407 case RemoteIR::UNKNOWN:
ke_ix1 4:f1923d9f536e 408 pc.printf("????????");
ke_ix1 4:f1923d9f536e 409 break;
ke_ix1 4:f1923d9f536e 410 case RemoteIR::NEC:
ke_ix1 4:f1923d9f536e 411 pc.printf("NEC ");
ke_ix1 4:f1923d9f536e 412 break;
ke_ix1 4:f1923d9f536e 413 case RemoteIR::NEC_REPEAT:
ke_ix1 4:f1923d9f536e 414 pc.printf("NEC (R)");
ke_ix1 4:f1923d9f536e 415 break;
ke_ix1 4:f1923d9f536e 416 case RemoteIR::AEHA:
ke_ix1 4:f1923d9f536e 417 pc.printf("AEHA ");
ke_ix1 4:f1923d9f536e 418 break;
ke_ix1 4:f1923d9f536e 419 case RemoteIR::AEHA_REPEAT:
ke_ix1 4:f1923d9f536e 420 pc.printf("AEHA (R)");
ke_ix1 4:f1923d9f536e 421 break;
ke_ix1 4:f1923d9f536e 422 case RemoteIR::SONY:
ke_ix1 4:f1923d9f536e 423 pc.printf("SONY ");
ke_ix1 4:f1923d9f536e 424 break;
ke_ix1 4:f1923d9f536e 425 }
ke_ix1 4:f1923d9f536e 426 }
ke_ix1 4:f1923d9f536e 427
ke_ix1 3:175ba3a059cb 428
ke_ix1 3:175ba3a059cb 429
RedBearLab 0:cffe8ac1bdf0 430 int main(void)
RedBearLab 0:cffe8ac1bdf0 431 {
ke_ix1 7:66243b10532c 432 ledori1 = 0;
ke_ix1 7:66243b10532c 433 //ledori2 = 0;
ke_ix1 6:976503121a46 434 led1 = 0;
RedBearLab 0:cffe8ac1bdf0 435 ble.init();
RedBearLab 0:cffe8ac1bdf0 436 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 437 ble.onDataWritten(WrittenHandler);
ke_ix1 3:175ba3a059cb 438
RedBearLab 0:cffe8ac1bdf0 439 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 440 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 441
RedBearLab 0:cffe8ac1bdf0 442 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 443 // setup advertising
RedBearLab 0:cffe8ac1bdf0 444 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 445 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 446 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
ke_ix1 7:66243b10532c 447 (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
RedBearLab 0:cffe8ac1bdf0 448 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 449 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 450 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 451 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 452
RedBearLab 0:cffe8ac1bdf0 453 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 454
ke_ix1 3:175ba3a059cb 455 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN,
ke_ix1 3:175ba3a059cb 456 GapScanningParams::SCAN_WINDOW_MIN,
ke_ix1 3:175ba3a059cb 457 0);
ke_ix1 3:175ba3a059cb 458
ke_ix1 3:175ba3a059cb 459 ble.startScan(&onScanCallback);
ke_ix1 3:175ba3a059cb 460
RedBearLab 0:cffe8ac1bdf0 461 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 462 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 463
ke_ix1 7:66243b10532c 464 steper.attach(&flagSet, 0.5);
ke_ix1 7:66243b10532c 465
ke_ix1 7:66243b10532c 466 //TX POWER用
ke_ix1 7:66243b10532c 467 //txSteper.attach(&initChara, 3.0);
ke_ix1 7:66243b10532c 468 txPowerUpdate(TX_POWER);
ke_ix1 3:175ba3a059cb 469
ke_ix1 7:66243b10532c 470 initChara();
RedBearLab 0:cffe8ac1bdf0 471 while(1)
RedBearLab 0:cffe8ac1bdf0 472 {
ke_ix1 7:66243b10532c 473 //pc.printf("-------- \r\n");
ke_ix1 4:f1923d9f536e 474 ble.waitForEvent();
ke_ix1 4:f1923d9f536e 475
ke_ix1 4:f1923d9f536e 476 uint8_t buf1[32];
ke_ix1 4:f1923d9f536e 477 int bitlength1;
ke_ix1 4:f1923d9f536e 478 RemoteIR::Format format;
ke_ix1 4:f1923d9f536e 479
ke_ix1 4:f1923d9f536e 480 //memset(buf1, 0x00, sizeof(buf1));
ke_ix1 4:f1923d9f536e 481
ke_ix1 4:f1923d9f536e 482 {
ke_ix1 4:f1923d9f536e 483 bitlength1 = receive(&format, buf1, sizeof(buf1));
ke_ix1 4:f1923d9f536e 484 if (bitlength1 < 0) {
ke_ix1 5:1bf7a84598a1 485 if(flagMargine <= 0) {
ke_ix1 5:1bf7a84598a1 486 receivedFlag = 0;
ke_ix1 5:1bf7a84598a1 487 } else {
ke_ix1 5:1bf7a84598a1 488 flagMargine--;
ke_ix1 5:1bf7a84598a1 489 }
ke_ix1 5:1bf7a84598a1 490
ke_ix1 5:1bf7a84598a1 491 if(flagMargine < -1000) {
ke_ix1 5:1bf7a84598a1 492 flagMargine = -1;
ke_ix1 5:1bf7a84598a1 493 }
ke_ix1 4:f1923d9f536e 494 continue;
ke_ix1 4:f1923d9f536e 495 }
ke_ix1 7:66243b10532c 496 /*
ke_ix1 4:f1923d9f536e 497 pc.printf("receive ok! \r\n");
ke_ix1 4:f1923d9f536e 498 display_status("RECV", bitlength1);
ke_ix1 4:f1923d9f536e 499 display_data(buf1, bitlength1);
ke_ix1 4:f1923d9f536e 500 display_format(format);
ke_ix1 7:66243b10532c 501 */
ke_ix1 4:f1923d9f536e 502 receivedFlag = 1;
ke_ix1 7:66243b10532c 503 flagMargine = 30000;
ke_ix1 4:f1923d9f536e 504 }
RedBearLab 0:cffe8ac1bdf0 505 }
RedBearLab 0:cffe8ac1bdf0 506 }
RedBearLab 0:cffe8ac1bdf0 507