InfraredLibに対応したMSC

Dependencies:   BLE_API RemoteIR mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Committer:
ke_ix1
Date:
Fri Dec 02 02:14:10 2016 +0000
Revision:
4:f1923d9f536e
Parent:
3:175ba3a059cb
Child:
5:1bf7a84598a1
?????????Characteristic???????KEI_IRRX

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