InfraredLibに対応したMSC

Dependencies:   BLE_API RemoteIR mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Committer:
ke_ix1
Date:
Sat Dec 03 03:21:22 2016 +0000
Revision:
5:1bf7a84598a1
Parent:
4:f1923d9f536e
Child:
6:976503121a46
????????????????????1or0???MSC

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 3:175ba3a059cb 46
RedBearLab 0:cffe8ac1bdf0 47
ke_ix1 4:f1923d9f536e 48 ReceiverIR ir_rx(P0_25);
ke_ix1 4:f1923d9f536e 49 TransmitterIR ir_tx(P0_18);
ke_ix1 4:f1923d9f536e 50
ke_ix1 4:f1923d9f536e 51 //ReceiverIR ir_rx(P0_25);
ke_ix1 4:f1923d9f536e 52
RedBearLab 0:cffe8ac1bdf0 53
RedBearLab 0:cffe8ac1bdf0 54 // The Nordic UART Service
ke_ix1 4:f1923d9f536e 55 //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 4:f1923d9f536e 56 //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 4:f1923d9f536e 57 //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 4:f1923d9f536e 58 //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 59
ke_ix1 4:f1923d9f536e 60 // The Nordic UART Service
ke_ix1 4:f1923d9f536e 61 static const uint8_t uart_base_uuid[] = {0x01, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x70}; //Service
ke_ix1 4:f1923d9f536e 62 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 4:f1923d9f536e 63 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 4:f1923d9f536e 64 static const uint8_t uart_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x01};
ke_ix1 4:f1923d9f536e 65
ke_ix1 4:f1923d9f536e 66 //01f3f538-8d11-4802-b6fc-fb6616d4cd70
RedBearLab 0:cffe8ac1bdf0 67
RedBearLab 0:cffe8ac1bdf0 68 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 69 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 70
RedBearLab 0:cffe8ac1bdf0 71 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 72 static uint8_t rx_len=0;
ke_ix1 3:175ba3a059cb 73 static uint8_t rx_buf_st[20];
ke_ix1 4:f1923d9f536e 74
ke_ix1 4:f1923d9f536e 75 int8_t buf_before[40];
ke_ix1 4:f1923d9f536e 76
ke_ix1 4:f1923d9f536e 77 static int st = 0;
ke_ix1 4:f1923d9f536e 78
ke_ix1 5:1bf7a84598a1 79 int flagMargine = -1;
ke_ix1 4:f1923d9f536e 80 int receivedFlag = 0;
RedBearLab 0:cffe8ac1bdf0 81
RedBearLab 0:cffe8ac1bdf0 82 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 83
RedBearLab 0:cffe8ac1bdf0 84 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 85
RedBearLab 0:cffe8ac1bdf0 86 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 87
RedBearLab 0:cffe8ac1bdf0 88 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 89
RedBearLab 0:cffe8ac1bdf0 90 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 91 {
RedBearLab 0:cffe8ac1bdf0 92 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 93 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 94 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 95 }
RedBearLab 0:cffe8ac1bdf0 96
ke_ix1 3:175ba3a059cb 97
ke_ix1 3:175ba3a059cb 98 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
ke_ix1 3:175ba3a059cb 99 {
ke_ix1 4:f1923d9f536e 100 //printf("onScanNau\n");
ke_ix1 4:f1923d9f536e 101
ke_ix1 4:f1923d9f536e 102 // printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 103
ke_ix1 4:f1923d9f536e 104 //だいじ
ke_ix1 4:f1923d9f536e 105 /*
ke_ix1 3:175ba3a059cb 106 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
ke_ix1 3:175ba3a059cb 107 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
ke_ix1 3:175ba3a059cb 108 params->rssi, params->isScanResponse, params->type);
ke_ix1 3:175ba3a059cb 109 printf("\n");
ke_ix1 3:175ba3a059cb 110 printf("Len:%d\n",params->advertisingDataLen);
ke_ix1 3:175ba3a059cb 111 printf("Data:");
ke_ix1 3:175ba3a059cb 112 for (int i=0; i< params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 113 printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 114 }
ke_ix1 4:f1923d9f536e 115 */
ke_ix1 4:f1923d9f536e 116
ke_ix1 3:175ba3a059cb 117 // for (int i=0; i<params->advertisingDataLen; i++){
ke_ix1 3:175ba3a059cb 118 // printf("%02x,",params->advertisingData[i]);
ke_ix1 3:175ba3a059cb 119 // }
ke_ix1 3:175ba3a059cb 120
ke_ix1 3:175ba3a059cb 121 printf("\n");
ke_ix1 3:175ba3a059cb 122
ke_ix1 3:175ba3a059cb 123 // DEBUG(" \n\r");
ke_ix1 3:175ba3a059cb 124 //#endif
ke_ix1 4:f1923d9f536e 125
ke_ix1 4:f1923d9f536e 126
ke_ix1 3:175ba3a059cb 127 }
ke_ix1 3:175ba3a059cb 128
ke_ix1 3:175ba3a059cb 129
RedBearLab 2:4b66b69c7ecb 130 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 131 {
RedBearLab 0:cffe8ac1bdf0 132 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 133 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 134
RedBearLab 2:4b66b69c7ecb 135 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 136 {
RedBearLab 0:cffe8ac1bdf0 137 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 138 memset(txPayload, 0, TXRX_BUF_LEN);
ke_ix1 3:175ba3a059cb 139 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 140 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 141 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 142 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 143 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 144 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 145 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 146 {
RedBearLab 0:cffe8ac1bdf0 147 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 148 }
RedBearLab 0:cffe8ac1bdf0 149 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 150 }
RedBearLab 0:cffe8ac1bdf0 151 }
RedBearLab 0:cffe8ac1bdf0 152
RedBearLab 0:cffe8ac1bdf0 153 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 154 {
RedBearLab 0:cffe8ac1bdf0 155 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 156 {
ke_ix1 3:175ba3a059cb 157 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 158 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 159 {
RedBearLab 0:cffe8ac1bdf0 160 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 161 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 162 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 163 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 164 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 165 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 166 break;
RedBearLab 0:cffe8ac1bdf0 167 }
RedBearLab 0:cffe8ac1bdf0 168 }
RedBearLab 0:cffe8ac1bdf0 169 }
RedBearLab 0:cffe8ac1bdf0 170
ke_ix1 3:175ba3a059cb 171
ke_ix1 3:175ba3a059cb 172 // 1++していく
ke_ix1 3:175ba3a059cb 173 void step() {
ke_ix1 4:f1923d9f536e 174 ++st;
ke_ix1 4:f1923d9f536e 175 //ここから-------------
ke_ix1 4:f1923d9f536e 176 char bufbuf_c[20];
ke_ix1 4:f1923d9f536e 177 uint8_t bufbuf[40];
ke_ix1 4:f1923d9f536e 178 sprintf(bufbuf_c, "%d", st);
ke_ix1 4:f1923d9f536e 179
ke_ix1 4:f1923d9f536e 180 int a;
ke_ix1 4:f1923d9f536e 181 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 4:f1923d9f536e 182 bufbuf[a] = bufbuf_c[a];
ke_ix1 4:f1923d9f536e 183 }
ke_ix1 4:f1923d9f536e 184
ke_ix1 4:f1923d9f536e 185 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 4:f1923d9f536e 186 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 4:f1923d9f536e 187 //ここまで-------------
ke_ix1 4:f1923d9f536e 188 //をこぴればデータを送信可能
ke_ix1 4:f1923d9f536e 189
ke_ix1 3:175ba3a059cb 190 //pc.printf("%d", st);
ke_ix1 3:175ba3a059cb 191 //pc.printf("step! \r\n");
ke_ix1 3:175ba3a059cb 192 }
ke_ix1 3:175ba3a059cb 193
ke_ix1 4:f1923d9f536e 194 void flagSet() {
ke_ix1 4:f1923d9f536e 195 //ここから-------------
ke_ix1 4:f1923d9f536e 196 char bufbuf_c[20];
ke_ix1 4:f1923d9f536e 197 uint8_t bufbuf[40];
ke_ix1 4:f1923d9f536e 198 sprintf(bufbuf_c, "%d", receivedFlag);
ke_ix1 4:f1923d9f536e 199
ke_ix1 4:f1923d9f536e 200 int a;
ke_ix1 4:f1923d9f536e 201 for(a=0; bufbuf_c[a] != '\0'; a++) {
ke_ix1 4:f1923d9f536e 202 bufbuf[a] = bufbuf_c[a];
ke_ix1 4:f1923d9f536e 203 }
ke_ix1 4:f1923d9f536e 204
ke_ix1 4:f1923d9f536e 205 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
ke_ix1 4:f1923d9f536e 206 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
ke_ix1 4:f1923d9f536e 207 //ここまで-------------
ke_ix1 4:f1923d9f536e 208 //をこぴればデータを送信可能
ke_ix1 4:f1923d9f536e 209 }
ke_ix1 4:f1923d9f536e 210
ke_ix1 4:f1923d9f536e 211
ke_ix1 4:f1923d9f536e 212
ke_ix1 4:f1923d9f536e 213 /**
ke_ix1 4:f1923d9f536e 214 * Receive.
ke_ix1 4:f1923d9f536e 215 *
ke_ix1 4:f1923d9f536e 216 * @param format Pointer to a format.
ke_ix1 4:f1923d9f536e 217 * @param buf Pointer to a buffer.
ke_ix1 4:f1923d9f536e 218 * @param bufsiz Size of the buffer.
ke_ix1 4:f1923d9f536e 219 *
ke_ix1 4:f1923d9f536e 220 * @return Bit length of the received data.
ke_ix1 4:f1923d9f536e 221 */
ke_ix1 4:f1923d9f536e 222 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) {
ke_ix1 4:f1923d9f536e 223 int cnt = 0;
ke_ix1 4:f1923d9f536e 224 while (ir_rx.getState() != ReceiverIR::Received) {
ke_ix1 4:f1923d9f536e 225 //pc.printf("no receive \r\n");
ke_ix1 4:f1923d9f536e 226 cnt++;
ke_ix1 4:f1923d9f536e 227 if (timeout < cnt) {
ke_ix1 4:f1923d9f536e 228 return -1;
ke_ix1 4:f1923d9f536e 229 }
ke_ix1 4:f1923d9f536e 230 }
ke_ix1 4:f1923d9f536e 231 return ir_rx.getData(format, buf, bufsiz * 8);
ke_ix1 4:f1923d9f536e 232 }
ke_ix1 4:f1923d9f536e 233
ke_ix1 4:f1923d9f536e 234 /**
ke_ix1 4:f1923d9f536e 235 * Transmit.
ke_ix1 4:f1923d9f536e 236 *
ke_ix1 4:f1923d9f536e 237 * @param format Format.
ke_ix1 4:f1923d9f536e 238 * @param buf Pointer to a buffer.
ke_ix1 4:f1923d9f536e 239 * @param bitlength Bit length of the data.
ke_ix1 4:f1923d9f536e 240 *
ke_ix1 4:f1923d9f536e 241 * @return Bit length of the received data.
ke_ix1 4:f1923d9f536e 242 */
ke_ix1 4:f1923d9f536e 243 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) {
ke_ix1 4:f1923d9f536e 244 int cnt = 0;
ke_ix1 4:f1923d9f536e 245 while (ir_tx.getState() != TransmitterIR::Idle) {
ke_ix1 4:f1923d9f536e 246 cnt++;
ke_ix1 4:f1923d9f536e 247 if (timeout < cnt) {
ke_ix1 4:f1923d9f536e 248 return -1;
ke_ix1 4:f1923d9f536e 249 }
ke_ix1 4:f1923d9f536e 250 }
ke_ix1 4:f1923d9f536e 251 return ir_tx.setData(format, buf, bitlength);
ke_ix1 4:f1923d9f536e 252 }
ke_ix1 4:f1923d9f536e 253
ke_ix1 4:f1923d9f536e 254 /**
ke_ix1 4:f1923d9f536e 255 * Display a data.
ke_ix1 4:f1923d9f536e 256 *
ke_ix1 4:f1923d9f536e 257 * @param buf Pointer to a buffer.
ke_ix1 4:f1923d9f536e 258 * @param bitlength Bit length of a data.
ke_ix1 4:f1923d9f536e 259 */
ke_ix1 4:f1923d9f536e 260 void display_data(uint8_t *buf, int bitlength) {
ke_ix1 4:f1923d9f536e 261 //lcd.locate(0, 1);
ke_ix1 4:f1923d9f536e 262 const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
ke_ix1 4:f1923d9f536e 263 for (int i = 0; i < n; i++) {
ke_ix1 4:f1923d9f536e 264
ke_ix1 4:f1923d9f536e 265 pc.printf("%02X", buf[i]);
ke_ix1 4:f1923d9f536e 266 }
ke_ix1 4:f1923d9f536e 267 for (int i = 0; i < 8 - n; i++) {
ke_ix1 4:f1923d9f536e 268 pc.printf("--");
ke_ix1 4:f1923d9f536e 269 }
ke_ix1 4:f1923d9f536e 270 }
ke_ix1 4:f1923d9f536e 271
ke_ix1 4:f1923d9f536e 272 /**
ke_ix1 4:f1923d9f536e 273 * Display a current status.
ke_ix1 4:f1923d9f536e 274 */
ke_ix1 4:f1923d9f536e 275 void display_status(char *status, int bitlength) {
ke_ix1 4:f1923d9f536e 276 //lcd.locate(8, 0);
ke_ix1 4:f1923d9f536e 277 pc.printf("%-5.5s:%02d", status, bitlength);
ke_ix1 4:f1923d9f536e 278 }
ke_ix1 4:f1923d9f536e 279
ke_ix1 4:f1923d9f536e 280 /**
ke_ix1 4:f1923d9f536e 281 * Display a format of a data.
ke_ix1 4:f1923d9f536e 282 */
ke_ix1 4:f1923d9f536e 283 void display_format(RemoteIR::Format format) {
ke_ix1 4:f1923d9f536e 284 //lcd.locate(0, 0);
ke_ix1 4:f1923d9f536e 285 switch (format) {
ke_ix1 4:f1923d9f536e 286 case RemoteIR::UNKNOWN:
ke_ix1 4:f1923d9f536e 287 pc.printf("????????");
ke_ix1 4:f1923d9f536e 288 break;
ke_ix1 4:f1923d9f536e 289 case RemoteIR::NEC:
ke_ix1 4:f1923d9f536e 290 pc.printf("NEC ");
ke_ix1 4:f1923d9f536e 291 break;
ke_ix1 4:f1923d9f536e 292 case RemoteIR::NEC_REPEAT:
ke_ix1 4:f1923d9f536e 293 pc.printf("NEC (R)");
ke_ix1 4:f1923d9f536e 294 break;
ke_ix1 4:f1923d9f536e 295 case RemoteIR::AEHA:
ke_ix1 4:f1923d9f536e 296 pc.printf("AEHA ");
ke_ix1 4:f1923d9f536e 297 break;
ke_ix1 4:f1923d9f536e 298 case RemoteIR::AEHA_REPEAT:
ke_ix1 4:f1923d9f536e 299 pc.printf("AEHA (R)");
ke_ix1 4:f1923d9f536e 300 break;
ke_ix1 4:f1923d9f536e 301 case RemoteIR::SONY:
ke_ix1 4:f1923d9f536e 302 pc.printf("SONY ");
ke_ix1 4:f1923d9f536e 303 break;
ke_ix1 4:f1923d9f536e 304 }
ke_ix1 4:f1923d9f536e 305 }
ke_ix1 4:f1923d9f536e 306
ke_ix1 3:175ba3a059cb 307
ke_ix1 3:175ba3a059cb 308
RedBearLab 0:cffe8ac1bdf0 309 int main(void)
RedBearLab 0:cffe8ac1bdf0 310 {
RedBearLab 0:cffe8ac1bdf0 311 ble.init();
RedBearLab 0:cffe8ac1bdf0 312 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 313 ble.onDataWritten(WrittenHandler);
ke_ix1 3:175ba3a059cb 314
RedBearLab 0:cffe8ac1bdf0 315 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 316 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 317
RedBearLab 0:cffe8ac1bdf0 318 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 319 // setup advertising
RedBearLab 0:cffe8ac1bdf0 320 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 321 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 322 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
ke_ix1 4:f1923d9f536e 323 (const uint8_t *)"KEI_IRRX", sizeof("KEI_IRRX") - 1);
RedBearLab 0:cffe8ac1bdf0 324 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 325 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 326 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 327 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 328
RedBearLab 0:cffe8ac1bdf0 329 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 330
ke_ix1 3:175ba3a059cb 331 ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN,
ke_ix1 3:175ba3a059cb 332 GapScanningParams::SCAN_WINDOW_MIN,
ke_ix1 3:175ba3a059cb 333 0);
ke_ix1 3:175ba3a059cb 334
ke_ix1 3:175ba3a059cb 335 ble.startScan(&onScanCallback);
ke_ix1 3:175ba3a059cb 336
RedBearLab 0:cffe8ac1bdf0 337 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 338 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 339
ke_ix1 4:f1923d9f536e 340 steper.attach(&flagSet, 1.0);
ke_ix1 3:175ba3a059cb 341
RedBearLab 0:cffe8ac1bdf0 342 while(1)
RedBearLab 0:cffe8ac1bdf0 343 {
ke_ix1 4:f1923d9f536e 344 pc.printf("-------- \r\n");
ke_ix1 4:f1923d9f536e 345 ble.waitForEvent();
ke_ix1 4:f1923d9f536e 346
ke_ix1 4:f1923d9f536e 347 uint8_t buf1[32];
ke_ix1 4:f1923d9f536e 348 int bitlength1;
ke_ix1 4:f1923d9f536e 349 RemoteIR::Format format;
ke_ix1 4:f1923d9f536e 350
ke_ix1 4:f1923d9f536e 351 //memset(buf1, 0x00, sizeof(buf1));
ke_ix1 4:f1923d9f536e 352
ke_ix1 4:f1923d9f536e 353 {
ke_ix1 4:f1923d9f536e 354 bitlength1 = receive(&format, buf1, sizeof(buf1));
ke_ix1 4:f1923d9f536e 355 if (bitlength1 < 0) {
ke_ix1 5:1bf7a84598a1 356 if(flagMargine <= 0) {
ke_ix1 5:1bf7a84598a1 357 receivedFlag = 0;
ke_ix1 5:1bf7a84598a1 358 } else {
ke_ix1 5:1bf7a84598a1 359 flagMargine--;
ke_ix1 5:1bf7a84598a1 360 }
ke_ix1 5:1bf7a84598a1 361
ke_ix1 5:1bf7a84598a1 362 if(flagMargine < -1000) {
ke_ix1 5:1bf7a84598a1 363 flagMargine = -1;
ke_ix1 5:1bf7a84598a1 364 }
ke_ix1 4:f1923d9f536e 365 continue;
ke_ix1 4:f1923d9f536e 366 }
ke_ix1 4:f1923d9f536e 367 pc.printf("receive ok! \r\n");
ke_ix1 4:f1923d9f536e 368 display_status("RECV", bitlength1);
ke_ix1 4:f1923d9f536e 369 display_data(buf1, bitlength1);
ke_ix1 4:f1923d9f536e 370 display_format(format);
ke_ix1 4:f1923d9f536e 371 receivedFlag = 1;
ke_ix1 5:1bf7a84598a1 372 flagMargine = 100;
ke_ix1 4:f1923d9f536e 373 }
RedBearLab 0:cffe8ac1bdf0 374 }
RedBearLab 0:cffe8ac1bdf0 375 }
RedBearLab 0:cffe8ac1bdf0 376