EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
main.cpp@6:09cdafc3ffeb, 2015-11-09 (annotated)
- Committer:
- Farshad
- Date:
- Mon Nov 09 04:47:27 2015 +0000
- Revision:
- 6:09cdafc3ffeb
- Parent:
- 5:207d4b6dface
- Child:
- 7:8a23a257b66a
Added multi-packet BLE transmission. This app only uses it for sending debug data using BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Farshad | 0:d58d1cdf43a9 | 1 | /* mbed Microcontroller Library |
Farshad | 0:d58d1cdf43a9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Farshad | 0:d58d1cdf43a9 | 3 | * |
Farshad | 0:d58d1cdf43a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Farshad | 0:d58d1cdf43a9 | 5 | * you may not use this file except in compliance with the License. |
Farshad | 0:d58d1cdf43a9 | 6 | * You may obtain a copy of the License at |
Farshad | 0:d58d1cdf43a9 | 7 | * |
Farshad | 0:d58d1cdf43a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Farshad | 0:d58d1cdf43a9 | 9 | * |
Farshad | 0:d58d1cdf43a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
Farshad | 0:d58d1cdf43a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Farshad | 0:d58d1cdf43a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Farshad | 0:d58d1cdf43a9 | 13 | * See the License for the specific language governing permissions and |
Farshad | 0:d58d1cdf43a9 | 14 | * limitations under the License. |
Farshad | 0:d58d1cdf43a9 | 15 | */ |
Farshad | 0:d58d1cdf43a9 | 16 | |
Farshad | 0:d58d1cdf43a9 | 17 | #include "mbed.h" |
Farshad | 5:207d4b6dface | 18 | #include "Serial.h" |
Farshad | 0:d58d1cdf43a9 | 19 | #include "BLE.h" |
Farshad | 0:d58d1cdf43a9 | 20 | #include "DeviceInformationService.h" |
Farshad | 0:d58d1cdf43a9 | 21 | #include "UARTService.h" |
Farshad | 6:09cdafc3ffeb | 22 | #include "CircularBuffer.h" |
Farshad | 6:09cdafc3ffeb | 23 | |
Farshad | 6:09cdafc3ffeb | 24 | #undef BLE_DEBUG_OUTPUT |
Farshad | 0:d58d1cdf43a9 | 25 | |
Farshad | 0:d58d1cdf43a9 | 26 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Farshad | 0:d58d1cdf43a9 | 27 | * it will have an impact on code-size and power consumption. */ |
Farshad | 0:d58d1cdf43a9 | 28 | |
Farshad | 2:79a9dad8bc5e | 29 | #define NEED_PARSE_TRACE 0 // only used for parsing tag data- will not work if parse function is called from within serial interrupt |
Farshad | 2:79a9dad8bc5e | 30 | |
Farshad | 2:79a9dad8bc5e | 31 | Serial pc(USBTX, USBRX); |
Farshad | 0:d58d1cdf43a9 | 32 | #if NEED_CONSOLE_OUTPUT |
Farshad | 2:79a9dad8bc5e | 33 | //Serial pc(USBTX, USBRX); |
Farshad | 0:d58d1cdf43a9 | 34 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
Farshad | 0:d58d1cdf43a9 | 35 | #else |
Farshad | 0:d58d1cdf43a9 | 36 | #define DEBUG(...) /* nothing */ |
Farshad | 0:d58d1cdf43a9 | 37 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
Farshad | 0:d58d1cdf43a9 | 38 | |
Farshad | 5:207d4b6dface | 39 | #if NEED_PARSE_TRACE |
Farshad | 2:79a9dad8bc5e | 40 | #define TRACE(...) { pc.printf(__VA_ARGS__); } |
Farshad | 2:79a9dad8bc5e | 41 | #else |
Farshad | 2:79a9dad8bc5e | 42 | #define TRACE(...) |
Farshad | 2:79a9dad8bc5e | 43 | #endif /* #if NEED_TRACE */ |
Farshad | 2:79a9dad8bc5e | 44 | |
Farshad | 5:207d4b6dface | 45 | |
Farshad | 5:207d4b6dface | 46 | |
Farshad | 0:d58d1cdf43a9 | 47 | BLEDevice ble; |
Farshad | 5:207d4b6dface | 48 | |
Farshad | 4:76bd50c41d39 | 49 | DigitalOut trigger(p18); |
Farshad | 5:207d4b6dface | 50 | |
Farshad | 6:09cdafc3ffeb | 51 | #define NORDIC // is board nordic DK? |
Farshad | 5:207d4b6dface | 52 | |
Farshad | 6:09cdafc3ffeb | 53 | #ifdef NORDIC |
Farshad | 5:207d4b6dface | 54 | DigitalOut connectionLed(p21); |
Farshad | 5:207d4b6dface | 55 | DigitalOut triggerLed(p22); |
Farshad | 5:207d4b6dface | 56 | DigitalOut shutdown(p20); // for nordic DK |
Farshad | 5:207d4b6dface | 57 | Serial reader(p13, p17); // tx, rx === NOTE tx port pin needs to be wired and verified (for nordic DK) |
Farshad | 5:207d4b6dface | 58 | #else |
Farshad | 5:207d4b6dface | 59 | DigitalOut connectionLed(p19); |
Farshad | 5:207d4b6dface | 60 | DigitalOut triggerLed(p18); |
Farshad | 5:207d4b6dface | 61 | DigitalOut shutdown(p10); |
Farshad | 5:207d4b6dface | 62 | Serial reader(p9, p8); // tx, rx === for adafruit BLE UART board (small blue board with red BLE module) |
Farshad | 5:207d4b6dface | 63 | #endif |
Farshad | 0:d58d1cdf43a9 | 64 | |
Farshad | 2:79a9dad8bc5e | 65 | const static char DEVICE_NAME[] = "MCS_RFID"; |
Farshad | 0:d58d1cdf43a9 | 66 | const static char MANUFACTURER[] = "MCS"; |
Farshad | 0:d58d1cdf43a9 | 67 | const static char MODEL[] = "Model 1"; |
Farshad | 0:d58d1cdf43a9 | 68 | const static char SERIAL_NO[] = "SN 1234"; |
Farshad | 0:d58d1cdf43a9 | 69 | const static char HARDWARE_REV[] = "hw-rev 1"; |
Farshad | 0:d58d1cdf43a9 | 70 | const static char FIRMWARE_REV[] = "fw-rev 1"; |
Farshad | 0:d58d1cdf43a9 | 71 | const static char SOFTWARE_REV[] = "soft-rev 1"; |
Farshad | 0:d58d1cdf43a9 | 72 | |
Farshad | 0:d58d1cdf43a9 | 73 | UARTService *uartServicePtr; |
Farshad | 2:79a9dad8bc5e | 74 | static uint8_t isConnected = 0; |
Farshad | 0:d58d1cdf43a9 | 75 | |
Farshad | 2:79a9dad8bc5e | 76 | // these values must macth definitions in the XML file accompanying this device |
Farshad | 5:207d4b6dface | 77 | const static uint16_t tagIdCmd = 0x0001; |
Farshad | 5:207d4b6dface | 78 | const static uint16_t hardwareTriggerCmd = 0x0002; |
Farshad | 5:207d4b6dface | 79 | const static uint16_t softwareTriggerCmd = 0x0003; |
Farshad | 2:79a9dad8bc5e | 80 | |
Farshad | 3:de77a4ebbf21 | 81 | // definition of the RFID packet |
Farshad | 3:de77a4ebbf21 | 82 | #define HEADER_FLAG 0xFF |
Farshad | 3:de77a4ebbf21 | 83 | #define EPC_READ_CMD 0x29 |
Farshad | 3:de77a4ebbf21 | 84 | #define BIT_64_TAG_FLAG 0x60 |
Farshad | 3:de77a4ebbf21 | 85 | #define BIT_96_TAG_FLAG 0x80 |
Farshad | 3:de77a4ebbf21 | 86 | #define SKIP_TO_BIT_FLAG 22 |
Farshad | 3:de77a4ebbf21 | 87 | #define BIT_64_TAG_PC_WORD 0x20 |
Farshad | 3:de77a4ebbf21 | 88 | #define BIT_96_TAG_PC_WORD 0x30 |
Farshad | 3:de77a4ebbf21 | 89 | #define SKIP_TO_TAG_EPC 2 |
Farshad | 2:79a9dad8bc5e | 90 | |
Farshad | 5:207d4b6dface | 91 | |
Farshad | 3:de77a4ebbf21 | 92 | #define SET_PARAM_CMD_MASK 0x8000 // commands with MSB set to 0 are to get the parameter and MSB of 1 to set the parameter |
Farshad | 5:207d4b6dface | 93 | #define READER_BAUD_RATE 115200 |
Farshad | 3:de77a4ebbf21 | 94 | #define READ_BUF_SIZE 255 |
Farshad | 3:de77a4ebbf21 | 95 | #define TAG_EPC_BUF_SIZE 12 |
Farshad | 5:207d4b6dface | 96 | |
Farshad | 2:79a9dad8bc5e | 97 | |
Farshad | 2:79a9dad8bc5e | 98 | typedef enum { |
Farshad | 5:207d4b6dface | 99 | AWAITING_PREAMBLE, |
Farshad | 2:79a9dad8bc5e | 100 | AWAITING_HEADER, |
Farshad | 2:79a9dad8bc5e | 101 | AWAITING_DATA_LEN, |
Farshad | 2:79a9dad8bc5e | 102 | AWAITING_READ_CMD, |
Farshad | 2:79a9dad8bc5e | 103 | AWAITING_STATUS_1, |
Farshad | 2:79a9dad8bc5e | 104 | AWAITING_STATUS_2, |
Farshad | 2:79a9dad8bc5e | 105 | AWAITING_EPC_LENGTH, |
Farshad | 2:79a9dad8bc5e | 106 | AWAITING_PC_WORD, |
Farshad | 2:79a9dad8bc5e | 107 | READING_TAG_EPC, |
Farshad | 2:79a9dad8bc5e | 108 | READIG_TAG_CRC |
Farshad | 2:79a9dad8bc5e | 109 | } packetState_e; |
Farshad | 2:79a9dad8bc5e | 110 | |
Farshad | 6:09cdafc3ffeb | 111 | //CircularBuffer cb; |
Farshad | 6:09cdafc3ffeb | 112 | |
Farshad | 2:79a9dad8bc5e | 113 | static uint8_t tagEPC[TAG_EPC_BUF_SIZE] = {0}; |
Farshad | 5:207d4b6dface | 114 | static packetState_e state = AWAITING_PREAMBLE; |
Farshad | 2:79a9dad8bc5e | 115 | static uint8_t tagBufIndex = 0; |
Farshad | 6:09cdafc3ffeb | 116 | static uint8_t skip = 0; |
Farshad | 2:79a9dad8bc5e | 117 | static uint16_t tagLen = 0; |
Farshad | 5:207d4b6dface | 118 | //static uint16_t dataLen = 0; |
Farshad | 5:207d4b6dface | 119 | |
Farshad | 5:207d4b6dface | 120 | // the preamble before 96 bit EPC tag data- we look for this before reading the EPC data |
Farshad | 6:09cdafc3ffeb | 121 | static uint8_t preamble[] = {0x00, 0x80}; |
Farshad | 5:207d4b6dface | 122 | static uint8_t preambleIdx = 0; |
Farshad | 5:207d4b6dface | 123 | |
Farshad | 5:207d4b6dface | 124 | // commmands to initialize the Nano RFID reader- sniffied from coms going from the base edge device to the Nano reader |
Farshad | 5:207d4b6dface | 125 | // only use the get commands |
Farshad | 5:207d4b6dface | 126 | const uint8_t rfidInit[] = { |
Farshad | 5:207d4b6dface | 127 | // 0xFF, 0x04, 0x06, 0x00, 0x01, 0xC2, 0x00, 0xA4,0x60, // set baud rate to 115200 |
Farshad | 5:207d4b6dface | 128 | // 0xFF, 0x00, 0x03, 0x1D, 0x0C, |
Farshad | 5:207d4b6dface | 129 | // 0xFF, 0x00, 0x03, 0x1D, 0x0C, |
Farshad | 5:207d4b6dface | 130 | // 0xF3, 0xE0, 0xE0, 0xE0, 0xE0, 0xFC, 0x1C, 0x00, 0x00, 0xFE, 0x80, 0xE6, 0xE0, |
Farshad | 5:207d4b6dface | 131 | // 0xFF, 0x00, 0x03, 0x1D, 0x0C, |
Farshad | 5:207d4b6dface | 132 | // 0xFF, 0x00, 0x0C, 0x1D, 0x03, //get current program |
Farshad | 5:207d4b6dface | 133 | // 0xFF, 0x00, 0x68, 0x1D, 0x67, // get power mode |
Farshad | 5:207d4b6dface | 134 | // 0xFF, 0x02, 0x6A, 0x01, 0x0E, 0x2E, 0x40, // get reader configuration |
Farshad | 5:207d4b6dface | 135 | // 0xFF, 0x02, 0x6A, 0x01, 0x12, 0x2E, 0x5C, // get reader configuration |
Farshad | 5:207d4b6dface | 136 | // 0xFF, 0x00, 0x63, 0x1D, 0x6C, // get current tag protocol |
Farshad | 5:207d4b6dface | 137 | 0xFF, 0x02, 0x93, 0x00, 0x05, 0x51, 0x7D, // set current tag protocol to Gen2) |
Farshad | 5:207d4b6dface | 138 | // 0xFF, 0x01, 0x61, 0x05, 0xBD, 0xB8, // get antenna configuration |
Farshad | 5:207d4b6dface | 139 | // 0xFF, 0x02, 0x6A, 0x01, 0x0C, 0x2E, 0x42, // get power mode |
Farshad | 5:207d4b6dface | 140 | // 0xFF, 0x02, 0x6A, 0x01, 0x0D, 0x2E, 0x43, // get power mode |
Farshad | 5:207d4b6dface | 141 | // 0xFF, 0x02, 0x10, 0x00, 0x40, 0xF0, 0xD3, // get hardware version |
Farshad | 5:207d4b6dface | 142 | 0xFF, 0x01, 0x97, 0x01, 0x4B, 0xBC, // set current region to NA |
Farshad | 5:207d4b6dface | 143 | 0xFF, 0x03, 0x9B, 0x05, 0x00, 0x00, 0xDC, 0xE8, // set protocol configuration, Gen2, S0 |
Farshad | 5:207d4b6dface | 144 | 0xFF, 0x04, 0x9B, 0x05, 0x01, 0x01, 0x00, 0xA2, 0xFD, // set protocol configuration Gen2, |
Farshad | 5:207d4b6dface | 145 | 0xFF, 0x03, 0x9B, 0x05, 0x02, 0x01, 0xDE, 0xE9, // set protocol configuration Gen2, |
Farshad | 5:207d4b6dface | 146 | // 0xFF, 0x01, 0x62, 0x00, 0xBE, 0xBD, // get read transmit power |
Farshad | 5:207d4b6dface | 147 | 0xFF, 0x02, 0x92, 0x0A, 0x8C, 0x4B, 0xD5, // set read transmit power to 27dbm |
Farshad | 5:207d4b6dface | 148 | }; |
Farshad | 5:207d4b6dface | 149 | |
Farshad | 5:207d4b6dface | 150 | // commmands to trigger a tag read- sniffied from coms going from the base edge device to the Nano reader |
Farshad | 5:207d4b6dface | 151 | uint8_t cmd_1[] = {0xFF, 0x00, 0x72, 0x1D, 0x7D}; // get current temperature |
Farshad | 5:207d4b6dface | 152 | uint8_t cmd_2[] = {0xFF, 0x00, 0x2A, 0x1D, 0x25}; // clear tag buffer |
Farshad | 5:207d4b6dface | 153 | uint8_t cmd_3[] = {0xFF, 0x03, 0x91, 0x02, 0x01, 0x01, 0x42, 0xC5}; // set antenna port |
Farshad | 5:207d4b6dface | 154 | uint8_t cmd_4[] = {0xFF, 0x05, 0x22, 0x00, 0x00, 0x13, 0x00, 0xFA, 0x2A, 0x17}; // read Tag Multiple |
Farshad | 5:207d4b6dface | 155 | uint8_t cmd_5[] = {0xFF, 0x03, 0x29, 0x01, 0xFF, 0x00, 0x1B, 0x03}; // get tag buffer |
Farshad | 5:207d4b6dface | 156 | |
Farshad | 5:207d4b6dface | 157 | typedef struct { |
Farshad | 5:207d4b6dface | 158 | uint8_t* data; |
Farshad | 5:207d4b6dface | 159 | uint8_t len; |
Farshad | 5:207d4b6dface | 160 | uint16_t delayms; |
Farshad | 5:207d4b6dface | 161 | } command_t; |
Farshad | 5:207d4b6dface | 162 | |
Farshad | 5:207d4b6dface | 163 | command_t readCommands[] = { |
Farshad | 5:207d4b6dface | 164 | {cmd_1, sizeof(cmd_1), 10}, |
Farshad | 5:207d4b6dface | 165 | {cmd_2, sizeof(cmd_2), 10}, |
Farshad | 5:207d4b6dface | 166 | {cmd_3, sizeof(cmd_3), 10}, |
Farshad | 5:207d4b6dface | 167 | {cmd_4, sizeof(cmd_4), 300}, |
Farshad | 5:207d4b6dface | 168 | {cmd_5, sizeof(cmd_5), 1} |
Farshad | 5:207d4b6dface | 169 | }; |
Farshad | 2:79a9dad8bc5e | 170 | |
Farshad | 2:79a9dad8bc5e | 171 | |
Farshad | 2:79a9dad8bc5e | 172 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Farshad | 0:d58d1cdf43a9 | 173 | { |
Farshad | 0:d58d1cdf43a9 | 174 | DEBUG("Disconnected!\n\r"); |
Farshad | 0:d58d1cdf43a9 | 175 | DEBUG("Restarting the advertising process\n\r"); |
Farshad | 0:d58d1cdf43a9 | 176 | ble.startAdvertising(); |
Farshad | 2:79a9dad8bc5e | 177 | isConnected = 0; |
Farshad | 5:207d4b6dface | 178 | connectionLed = isConnected; |
Farshad | 2:79a9dad8bc5e | 179 | } |
Farshad | 2:79a9dad8bc5e | 180 | |
Farshad | 2:79a9dad8bc5e | 181 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
Farshad | 2:79a9dad8bc5e | 182 | { |
Farshad | 2:79a9dad8bc5e | 183 | DEBUG("Connected!\n\r"); |
Farshad | 2:79a9dad8bc5e | 184 | isConnected = 1; |
Farshad | 5:207d4b6dface | 185 | connectionLed = isConnected; |
Farshad | 0:d58d1cdf43a9 | 186 | } |
Farshad | 0:d58d1cdf43a9 | 187 | |
Farshad | 5:207d4b6dface | 188 | |
Farshad | 5:207d4b6dface | 189 | //// State machine to parse the tag data received from serial port. It only looks for EPC data in the packet |
Farshad | 5:207d4b6dface | 190 | //static void parsePacket_2(uint8_t d) |
Farshad | 5:207d4b6dface | 191 | //{ |
Farshad | 6:09cdafc3ffeb | 192 | //// // TRACE("%02X ", d); |
Farshad | 5:207d4b6dface | 193 | // switch (state) { |
Farshad | 5:207d4b6dface | 194 | // case AWAITING_HEADER: |
Farshad | 5:207d4b6dface | 195 | // if(d == HEADER_FLAG) { |
Farshad | 5:207d4b6dface | 196 | // TRACE("AWAITING_DATA_LEN \r\n"); |
Farshad | 5:207d4b6dface | 197 | // state = AWAITING_DATA_LEN; |
Farshad | 5:207d4b6dface | 198 | // skip = 0; |
Farshad | 5:207d4b6dface | 199 | // } |
Farshad | 5:207d4b6dface | 200 | // break; |
Farshad | 5:207d4b6dface | 201 | // |
Farshad | 5:207d4b6dface | 202 | // case AWAITING_DATA_LEN: |
Farshad | 5:207d4b6dface | 203 | // if(d > 0) { |
Farshad | 5:207d4b6dface | 204 | // dataLen = d; |
Farshad | 5:207d4b6dface | 205 | // state = AWAITING_READ_CMD; |
Farshad | 5:207d4b6dface | 206 | // TRACE("AWAITING_READ_CMD \r\n"); |
Farshad | 5:207d4b6dface | 207 | // } else { |
Farshad | 5:207d4b6dface | 208 | // state = AWAITING_HEADER; |
Farshad | 5:207d4b6dface | 209 | // } |
Farshad | 5:207d4b6dface | 210 | // break; |
Farshad | 5:207d4b6dface | 211 | // |
Farshad | 5:207d4b6dface | 212 | // case AWAITING_READ_CMD: |
Farshad | 5:207d4b6dface | 213 | // if(d == EPC_READ_CMD) { |
Farshad | 5:207d4b6dface | 214 | // state = AWAITING_STATUS_1; |
Farshad | 5:207d4b6dface | 215 | // TRACE("AWAITING_STATUS_1 \r\n"); |
Farshad | 5:207d4b6dface | 216 | // } else { |
Farshad | 5:207d4b6dface | 217 | // state = AWAITING_HEADER; |
Farshad | 5:207d4b6dface | 218 | // } |
Farshad | 5:207d4b6dface | 219 | // break; |
Farshad | 5:207d4b6dface | 220 | // |
Farshad | 5:207d4b6dface | 221 | // case AWAITING_STATUS_1: |
Farshad | 5:207d4b6dface | 222 | // if(d == 0) { |
Farshad | 5:207d4b6dface | 223 | // state = AWAITING_STATUS_2; |
Farshad | 5:207d4b6dface | 224 | // TRACE("AWAITING_STATUS_2 \r\n"); |
Farshad | 5:207d4b6dface | 225 | // } else { |
Farshad | 5:207d4b6dface | 226 | // state = AWAITING_HEADER; |
Farshad | 5:207d4b6dface | 227 | // } |
Farshad | 5:207d4b6dface | 228 | // break; |
Farshad | 5:207d4b6dface | 229 | // |
Farshad | 5:207d4b6dface | 230 | // case AWAITING_STATUS_2: |
Farshad | 5:207d4b6dface | 231 | // if(d == 0) { |
Farshad | 5:207d4b6dface | 232 | // state = AWAITING_EPC_LENGTH; |
Farshad | 5:207d4b6dface | 233 | // TRACE("AWAITING_EPC_LENGTH_1 \r\n"); |
Farshad | 5:207d4b6dface | 234 | // } else { |
Farshad | 5:207d4b6dface | 235 | // state = AWAITING_HEADER; |
Farshad | 5:207d4b6dface | 236 | // } |
Farshad | 5:207d4b6dface | 237 | // break; |
Farshad | 5:207d4b6dface | 238 | // |
Farshad | 5:207d4b6dface | 239 | // case AWAITING_EPC_LENGTH: |
Farshad | 5:207d4b6dface | 240 | // if(++skip >= SKIP_TO_BIT_FLAG) { |
Farshad | 5:207d4b6dface | 241 | // if(d == BIT_64_TAG_FLAG) { |
Farshad | 5:207d4b6dface | 242 | // state = AWAITING_PC_WORD; |
Farshad | 5:207d4b6dface | 243 | // tagLen = 8; |
Farshad | 5:207d4b6dface | 244 | // TRACE("AWAITING_PC_WORD \r\n"); |
Farshad | 5:207d4b6dface | 245 | // } else if (d == BIT_96_TAG_FLAG) { |
Farshad | 5:207d4b6dface | 246 | // state = AWAITING_PC_WORD; |
Farshad | 5:207d4b6dface | 247 | // tagLen = 12; |
Farshad | 5:207d4b6dface | 248 | // TRACE("AWAITING_PC_WORD \r\n"); |
Farshad | 5:207d4b6dface | 249 | // } else { |
Farshad | 5:207d4b6dface | 250 | // state = AWAITING_HEADER; |
Farshad | 5:207d4b6dface | 251 | // } |
Farshad | 5:207d4b6dface | 252 | // } |
Farshad | 5:207d4b6dface | 253 | // break; |
Farshad | 5:207d4b6dface | 254 | // |
Farshad | 5:207d4b6dface | 255 | // case AWAITING_PC_WORD: |
Farshad | 6:09cdafc3ffeb | 256 | //// if((tagLen == 8 && d == BIT_64_TAG_PC_WORD) || (tagLen == 12 && d == BIT_96_TAG_PC_WORD)) |
Farshad | 6:09cdafc3ffeb | 257 | // { |
Farshad | 5:207d4b6dface | 258 | // state = READING_TAG_EPC; |
Farshad | 5:207d4b6dface | 259 | // tagBufIndex = 0; |
Farshad | 5:207d4b6dface | 260 | // TRACE("READING_TAG_EPC \r\n"); |
Farshad | 5:207d4b6dface | 261 | // skip = 0; |
Farshad | 6:09cdafc3ffeb | 262 | // } |
Farshad | 6:09cdafc3ffeb | 263 | // //else { |
Farshad | 6:09cdafc3ffeb | 264 | //// state = AWAITING_HEADER; |
Farshad | 6:09cdafc3ffeb | 265 | //// } |
Farshad | 5:207d4b6dface | 266 | // break; |
Farshad | 5:207d4b6dface | 267 | // |
Farshad | 5:207d4b6dface | 268 | // case READING_TAG_EPC: |
Farshad | 5:207d4b6dface | 269 | // if(++skip >= SKIP_TO_TAG_EPC) { |
Farshad | 5:207d4b6dface | 270 | // if(tagBufIndex < tagLen) { |
Farshad | 5:207d4b6dface | 271 | // tagEPC[tagBufIndex++] = d; |
Farshad | 5:207d4b6dface | 272 | // TRACE("%02X ", d); |
Farshad | 5:207d4b6dface | 273 | // } else { |
Farshad | 5:207d4b6dface | 274 | // // don't worry about CRC for now |
Farshad | 5:207d4b6dface | 275 | // state = AWAITING_HEADER; |
Farshad | 5:207d4b6dface | 276 | // TRACE("AWAITING_HEADER \r\n"); |
Farshad | 5:207d4b6dface | 277 | // |
Farshad | 5:207d4b6dface | 278 | // TRACE("TAG EPC: ============\r\n"); |
Farshad | 5:207d4b6dface | 279 | // for(int i = 0; i < tagLen; i++) { |
Farshad | 5:207d4b6dface | 280 | // TRACE("%02X ", tagEPC[i]); |
Farshad | 5:207d4b6dface | 281 | // // pc.printf("%02X ", tagEPC[i]); |
Farshad | 5:207d4b6dface | 282 | // } |
Farshad | 5:207d4b6dface | 283 | // // pc.printf("\r\n"); |
Farshad | 5:207d4b6dface | 284 | // |
Farshad | 5:207d4b6dface | 285 | // sendBLENotification(); |
Farshad | 5:207d4b6dface | 286 | // } |
Farshad | 5:207d4b6dface | 287 | // } |
Farshad | 5:207d4b6dface | 288 | // break; |
Farshad | 5:207d4b6dface | 289 | // } |
Farshad | 5:207d4b6dface | 290 | //} |
Farshad | 5:207d4b6dface | 291 | |
Farshad | 6:09cdafc3ffeb | 292 | uint16_t idx = 0; |
Farshad | 6:09cdafc3ffeb | 293 | const uint16_t storeSize = 300; |
Farshad | 6:09cdafc3ffeb | 294 | uint8_t store[storeSize]; |
Farshad | 6:09cdafc3ffeb | 295 | bool reading = false; |
Farshad | 6:09cdafc3ffeb | 296 | bool sending = false; |
Farshad | 5:207d4b6dface | 297 | |
Farshad | 6:09cdafc3ffeb | 298 | typedef enum{ |
Farshad | 6:09cdafc3ffeb | 299 | PS_FIRST_AND_ONLY = 0b0000000000000000, |
Farshad | 6:09cdafc3ffeb | 300 | PS_FIRST_AND_NOT_LAST = 0b0010000000000000, |
Farshad | 6:09cdafc3ffeb | 301 | PS_LAST = 0b0100000000000000, |
Farshad | 6:09cdafc3ffeb | 302 | PS_MIDDLE = 0b0110000000000000 |
Farshad | 6:09cdafc3ffeb | 303 | } PacketStatus_e; |
Farshad | 6:09cdafc3ffeb | 304 | |
Farshad | 6:09cdafc3ffeb | 305 | static PacketStatus_e packetStatus = PS_FIRST_AND_ONLY; |
Farshad | 6:09cdafc3ffeb | 306 | |
Farshad | 6:09cdafc3ffeb | 307 | static void sendOverBLE(uint16_t cmd, uint8_t data[], uint16_t len, PacketStatus_e status){ |
Farshad | 6:09cdafc3ffeb | 308 | |
Farshad | 6:09cdafc3ffeb | 309 | if(len > 16){ |
Farshad | 6:09cdafc3ffeb | 310 | // this is a problem |
Farshad | 6:09cdafc3ffeb | 311 | } |
Farshad | 6:09cdafc3ffeb | 312 | |
Farshad | 6:09cdafc3ffeb | 313 | uint8_t buf[len + 4]; // should set it to the max single packet size |
Farshad | 6:09cdafc3ffeb | 314 | uint8_t offset = 0; |
Farshad | 6:09cdafc3ffeb | 315 | uint16_t tmp = cmd | (uint16_t)status; |
Farshad | 6:09cdafc3ffeb | 316 | memcpy(buf, &tmp, sizeof(uint16_t)); // command |
Farshad | 6:09cdafc3ffeb | 317 | offset+=sizeof(uint16_t); |
Farshad | 6:09cdafc3ffeb | 318 | memcpy(&buf[offset], &len, sizeof(uint16_t)); // length of array |
Farshad | 6:09cdafc3ffeb | 319 | offset+=sizeof(uint16_t); |
Farshad | 6:09cdafc3ffeb | 320 | memcpy(&buf[offset], &data[0], len); // data |
Farshad | 6:09cdafc3ffeb | 321 | offset+= len; |
Farshad | 6:09cdafc3ffeb | 322 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset); |
Farshad | 6:09cdafc3ffeb | 323 | } |
Farshad | 6:09cdafc3ffeb | 324 | |
Farshad | 6:09cdafc3ffeb | 325 | static void sendPacketOverBLE(uint16_t cmd, uint8_t data[], uint16_t len) |
Farshad | 6:09cdafc3ffeb | 326 | { |
Farshad | 6:09cdafc3ffeb | 327 | uint8_t nPackets = (len / 17) + 1; |
Farshad | 6:09cdafc3ffeb | 328 | uint8_t remainingPackets = nPackets; |
Farshad | 6:09cdafc3ffeb | 329 | uint16_t offset = 0; |
Farshad | 6:09cdafc3ffeb | 330 | bool first = true; |
Farshad | 6:09cdafc3ffeb | 331 | const uint8_t maxDataPayload = 16; // max is 20 but we are sending a command (2) plus the length (2) |
Farshad | 6:09cdafc3ffeb | 332 | |
Farshad | 6:09cdafc3ffeb | 333 | do { |
Farshad | 6:09cdafc3ffeb | 334 | if(nPackets == 1) { |
Farshad | 6:09cdafc3ffeb | 335 | // first and only |
Farshad | 6:09cdafc3ffeb | 336 | packetStatus = PS_FIRST_AND_ONLY; |
Farshad | 6:09cdafc3ffeb | 337 | sendOverBLE(cmd, &data[offset], len, packetStatus); |
Farshad | 6:09cdafc3ffeb | 338 | remainingPackets = 0; |
Farshad | 6:09cdafc3ffeb | 339 | } else if(remainingPackets == 1) { |
Farshad | 6:09cdafc3ffeb | 340 | // last one |
Farshad | 6:09cdafc3ffeb | 341 | packetStatus = PS_LAST; |
Farshad | 6:09cdafc3ffeb | 342 | sendOverBLE(cmd, &data[offset], len-offset, packetStatus); |
Farshad | 6:09cdafc3ffeb | 343 | remainingPackets = 0; |
Farshad | 6:09cdafc3ffeb | 344 | } else if(remainingPackets > 1 && first == true) { |
Farshad | 6:09cdafc3ffeb | 345 | // first and more to come |
Farshad | 6:09cdafc3ffeb | 346 | packetStatus = PS_FIRST_AND_NOT_LAST; |
Farshad | 6:09cdafc3ffeb | 347 | sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); |
Farshad | 6:09cdafc3ffeb | 348 | offset += maxDataPayload; |
Farshad | 6:09cdafc3ffeb | 349 | remainingPackets -= 1; |
Farshad | 6:09cdafc3ffeb | 350 | first = false; |
Farshad | 6:09cdafc3ffeb | 351 | } else if(remainingPackets > 1 && first == false) { |
Farshad | 6:09cdafc3ffeb | 352 | // middle |
Farshad | 6:09cdafc3ffeb | 353 | packetStatus = PS_MIDDLE; |
Farshad | 6:09cdafc3ffeb | 354 | sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); |
Farshad | 6:09cdafc3ffeb | 355 | offset += maxDataPayload; |
Farshad | 6:09cdafc3ffeb | 356 | remainingPackets -= 1; |
Farshad | 6:09cdafc3ffeb | 357 | } |
Farshad | 6:09cdafc3ffeb | 358 | |
Farshad | 6:09cdafc3ffeb | 359 | wait_ms(50); // 40ms delay seems to be a limit |
Farshad | 6:09cdafc3ffeb | 360 | } while (remainingPackets > 0); |
Farshad | 6:09cdafc3ffeb | 361 | } |
Farshad | 6:09cdafc3ffeb | 362 | |
Farshad | 6:09cdafc3ffeb | 363 | |
Farshad | 6:09cdafc3ffeb | 364 | static void processData(const GattWriteCallbackParams *params) |
Farshad | 6:09cdafc3ffeb | 365 | { |
Farshad | 6:09cdafc3ffeb | 366 | if(params->len >= 2) { |
Farshad | 6:09cdafc3ffeb | 367 | uint16_t command = params->data[0] + (params->data[1] << 8); |
Farshad | 6:09cdafc3ffeb | 368 | bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK; |
Farshad | 6:09cdafc3ffeb | 369 | DEBUG("command: %d \r\n", command); |
Farshad | 6:09cdafc3ffeb | 370 | |
Farshad | 6:09cdafc3ffeb | 371 | switch(command & ~SET_PARAM_CMD_MASK) { |
Farshad | 6:09cdafc3ffeb | 372 | case tagIdCmd: |
Farshad | 6:09cdafc3ffeb | 373 | if(!isSetCmd && params->len == 2) { |
Farshad | 6:09cdafc3ffeb | 374 | // form the reply to send |
Farshad | 6:09cdafc3ffeb | 375 | DEBUG("CMD is GET code\n\r"); |
Farshad | 6:09cdafc3ffeb | 376 | // TODO can do a trigger for a new read before transimiting, For now just send the latest read |
Farshad | 6:09cdafc3ffeb | 377 | sendPacketOverBLE(tagIdCmd, tagEPC, tagLen); |
Farshad | 6:09cdafc3ffeb | 378 | } |
Farshad | 6:09cdafc3ffeb | 379 | break; |
Farshad | 6:09cdafc3ffeb | 380 | |
Farshad | 6:09cdafc3ffeb | 381 | case hardwareTriggerCmd: |
Farshad | 6:09cdafc3ffeb | 382 | if(isSetCmd && params->len == 3) { |
Farshad | 6:09cdafc3ffeb | 383 | DEBUG("CMD is SET hardwareTriggerCmd\n\r"); |
Farshad | 6:09cdafc3ffeb | 384 | trigger = 0; |
Farshad | 6:09cdafc3ffeb | 385 | triggerLed = 1; |
Farshad | 6:09cdafc3ffeb | 386 | wait_ms(100); |
Farshad | 6:09cdafc3ffeb | 387 | trigger = 1; |
Farshad | 6:09cdafc3ffeb | 388 | triggerLed = 0; |
Farshad | 6:09cdafc3ffeb | 389 | } |
Farshad | 6:09cdafc3ffeb | 390 | break; |
Farshad | 6:09cdafc3ffeb | 391 | |
Farshad | 6:09cdafc3ffeb | 392 | case softwareTriggerCmd: |
Farshad | 6:09cdafc3ffeb | 393 | if(isSetCmd && params->len == 3) { // TODO this command does not need a value |
Farshad | 6:09cdafc3ffeb | 394 | DEBUG("CMD is SET softwareTriggerCmd\n\r"); |
Farshad | 6:09cdafc3ffeb | 395 | triggerLed = 1; |
Farshad | 6:09cdafc3ffeb | 396 | for(int i = 0; i < sizeof(readCommands) / sizeof(command_t); i++) { |
Farshad | 6:09cdafc3ffeb | 397 | uint8_t* d = readCommands[i].data; |
Farshad | 6:09cdafc3ffeb | 398 | uint8_t len = readCommands[i].len; |
Farshad | 6:09cdafc3ffeb | 399 | for(int j = 0; j < len; j++) { |
Farshad | 6:09cdafc3ffeb | 400 | // DEBUG("%02X ", d[j]); |
Farshad | 6:09cdafc3ffeb | 401 | reader.putc(d[j]); |
Farshad | 6:09cdafc3ffeb | 402 | } |
Farshad | 6:09cdafc3ffeb | 403 | wait_ms(readCommands[i].delayms); |
Farshad | 6:09cdafc3ffeb | 404 | DEBUG("\r\n "); |
Farshad | 6:09cdafc3ffeb | 405 | } |
Farshad | 6:09cdafc3ffeb | 406 | triggerLed = 0; |
Farshad | 6:09cdafc3ffeb | 407 | } |
Farshad | 6:09cdafc3ffeb | 408 | break; |
Farshad | 6:09cdafc3ffeb | 409 | |
Farshad | 6:09cdafc3ffeb | 410 | default: |
Farshad | 6:09cdafc3ffeb | 411 | break; |
Farshad | 6:09cdafc3ffeb | 412 | } |
Farshad | 6:09cdafc3ffeb | 413 | } |
Farshad | 6:09cdafc3ffeb | 414 | } |
Farshad | 6:09cdafc3ffeb | 415 | |
Farshad | 6:09cdafc3ffeb | 416 | void onDataWritten(const GattWriteCallbackParams *params) |
Farshad | 6:09cdafc3ffeb | 417 | { |
Farshad | 6:09cdafc3ffeb | 418 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
Farshad | 6:09cdafc3ffeb | 419 | uint16_t bytesRead = params->len; |
Farshad | 6:09cdafc3ffeb | 420 | DEBUG("received %u bytes\n\r", bytesRead); |
Farshad | 6:09cdafc3ffeb | 421 | for(int i = 0; i < bytesRead; i++) { |
Farshad | 6:09cdafc3ffeb | 422 | DEBUG("0x%X ", params->data[i]); |
Farshad | 6:09cdafc3ffeb | 423 | } |
Farshad | 6:09cdafc3ffeb | 424 | DEBUG("\n\r", bytesRead); |
Farshad | 6:09cdafc3ffeb | 425 | |
Farshad | 6:09cdafc3ffeb | 426 | // echo? |
Farshad | 6:09cdafc3ffeb | 427 | // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
Farshad | 6:09cdafc3ffeb | 428 | |
Farshad | 6:09cdafc3ffeb | 429 | processData(params); |
Farshad | 6:09cdafc3ffeb | 430 | } |
Farshad | 6:09cdafc3ffeb | 431 | } |
Farshad | 6:09cdafc3ffeb | 432 | |
Farshad | 6:09cdafc3ffeb | 433 | // State machine to parse the tag data received from serial port. It only looks for EPC data in the packet |
Farshad | 6:09cdafc3ffeb | 434 | static void parsePacket(uint8_t d) |
Farshad | 6:09cdafc3ffeb | 435 | { |
Farshad | 6:09cdafc3ffeb | 436 | TRACE("%02X ", d); |
Farshad | 6:09cdafc3ffeb | 437 | |
Farshad | 6:09cdafc3ffeb | 438 | tagLen = 12; |
Farshad | 6:09cdafc3ffeb | 439 | switch (state) { |
Farshad | 6:09cdafc3ffeb | 440 | case AWAITING_PREAMBLE: |
Farshad | 6:09cdafc3ffeb | 441 | // if(d == preamble[preambleIdx] || preambleIdx > 1) { |
Farshad | 6:09cdafc3ffeb | 442 | if(d == preamble[preambleIdx]) { |
Farshad | 6:09cdafc3ffeb | 443 | preambleIdx++; |
Farshad | 6:09cdafc3ffeb | 444 | if(preambleIdx == sizeof(preamble)) { |
Farshad | 6:09cdafc3ffeb | 445 | state = READING_TAG_EPC; |
Farshad | 6:09cdafc3ffeb | 446 | skip = 0; |
Farshad | 6:09cdafc3ffeb | 447 | } |
Farshad | 6:09cdafc3ffeb | 448 | } else { |
Farshad | 6:09cdafc3ffeb | 449 | preambleIdx = 0; |
Farshad | 6:09cdafc3ffeb | 450 | } |
Farshad | 6:09cdafc3ffeb | 451 | break; |
Farshad | 6:09cdafc3ffeb | 452 | |
Farshad | 6:09cdafc3ffeb | 453 | case READING_TAG_EPC: |
Farshad | 6:09cdafc3ffeb | 454 | if(++skip > SKIP_TO_TAG_EPC) { |
Farshad | 6:09cdafc3ffeb | 455 | if(tagBufIndex < tagLen) { |
Farshad | 6:09cdafc3ffeb | 456 | tagEPC[tagBufIndex++] = d; |
Farshad | 6:09cdafc3ffeb | 457 | TRACE("%02X ", d); |
Farshad | 6:09cdafc3ffeb | 458 | } else { |
Farshad | 6:09cdafc3ffeb | 459 | // don't worry about CRC for now |
Farshad | 6:09cdafc3ffeb | 460 | state = AWAITING_PREAMBLE; |
Farshad | 6:09cdafc3ffeb | 461 | preambleIdx = 0; |
Farshad | 6:09cdafc3ffeb | 462 | tagBufIndex = 0; |
Farshad | 6:09cdafc3ffeb | 463 | TRACE("AWAITING_PREAMBLE \r\n"); |
Farshad | 6:09cdafc3ffeb | 464 | |
Farshad | 6:09cdafc3ffeb | 465 | TRACE("TAG EPC: ============\r\n"); |
Farshad | 6:09cdafc3ffeb | 466 | for(int i = 0; i < tagLen; i++) { |
Farshad | 6:09cdafc3ffeb | 467 | TRACE("%02X ", tagEPC[i]); |
Farshad | 6:09cdafc3ffeb | 468 | // pc.printf("%02X ", tagEPC[i]); |
Farshad | 6:09cdafc3ffeb | 469 | } |
Farshad | 6:09cdafc3ffeb | 470 | // pc.printf("\r\n"); |
Farshad | 6:09cdafc3ffeb | 471 | |
Farshad | 6:09cdafc3ffeb | 472 | sendPacketOverBLE(tagIdCmd, tagEPC, tagLen); |
Farshad | 6:09cdafc3ffeb | 473 | triggerLed = 0; |
Farshad | 6:09cdafc3ffeb | 474 | } |
Farshad | 6:09cdafc3ffeb | 475 | } |
Farshad | 6:09cdafc3ffeb | 476 | |
Farshad | 6:09cdafc3ffeb | 477 | break; |
Farshad | 6:09cdafc3ffeb | 478 | } |
Farshad | 6:09cdafc3ffeb | 479 | } |
Farshad | 6:09cdafc3ffeb | 480 | |
Farshad | 6:09cdafc3ffeb | 481 | |
Farshad | 6:09cdafc3ffeb | 482 | uint16_t length; |
Farshad | 6:09cdafc3ffeb | 483 | //uint8_t tempBuf[1000]; |
Farshad | 0:d58d1cdf43a9 | 484 | void periodicCallback(void) |
Farshad | 0:d58d1cdf43a9 | 485 | { |
Farshad | 6:09cdafc3ffeb | 486 | #ifdef BLE_DEBUG_OUTPUT |
Farshad | 6:09cdafc3ffeb | 487 | //for( idx = 0; idx < 64; idx++){ // 64 is the limit |
Farshad | 6:09cdafc3ffeb | 488 | // store[idx] = idx; |
Farshad | 6:09cdafc3ffeb | 489 | // } |
Farshad | 6:09cdafc3ffeb | 490 | |
Farshad | 6:09cdafc3ffeb | 491 | if(reading == false) { |
Farshad | 6:09cdafc3ffeb | 492 | if(idx > 0) { |
Farshad | 6:09cdafc3ffeb | 493 | sending = true; |
Farshad | 6:09cdafc3ffeb | 494 | length = idx; |
Farshad | 6:09cdafc3ffeb | 495 | //memcpy(&tempBuf[0], &store[0], idx); |
Farshad | 6:09cdafc3ffeb | 496 | sendPacketOverBLE(tagIdCmd, store, idx); |
Farshad | 6:09cdafc3ffeb | 497 | idx = 0; // now that we have sent this, reset the index |
Farshad | 6:09cdafc3ffeb | 498 | } |
Farshad | 6:09cdafc3ffeb | 499 | } |
Farshad | 6:09cdafc3ffeb | 500 | #endif |
Farshad | 6:09cdafc3ffeb | 501 | |
Farshad | 6:09cdafc3ffeb | 502 | sending = false; |
Farshad | 0:d58d1cdf43a9 | 503 | } |
Farshad | 0:d58d1cdf43a9 | 504 | |
Farshad | 3:de77a4ebbf21 | 505 | // this is an ISR, so do not spend too much time here and be careful with printing debug info |
Farshad | 2:79a9dad8bc5e | 506 | void readerCallback() |
Farshad | 6:09cdafc3ffeb | 507 | { |
Farshad | 2:79a9dad8bc5e | 508 | // Note: Need to actually read from the serial to clear the RX interrupt |
Farshad | 6:09cdafc3ffeb | 509 | if(sending == false) { |
Farshad | 6:09cdafc3ffeb | 510 | reading = true; |
Farshad | 6:09cdafc3ffeb | 511 | while(reader.readable()) { |
Farshad | 6:09cdafc3ffeb | 512 | uint8_t c = reader.getc(); |
Farshad | 6:09cdafc3ffeb | 513 | parsePacket(c); |
Farshad | 6:09cdafc3ffeb | 514 | #ifdef BLE_DEBUG_OUTPUT |
Farshad | 6:09cdafc3ffeb | 515 | store[idx] = c; |
Farshad | 6:09cdafc3ffeb | 516 | if(idx < (storeSize-1)) idx++; |
Farshad | 6:09cdafc3ffeb | 517 | #endif |
Farshad | 6:09cdafc3ffeb | 518 | } |
Farshad | 6:09cdafc3ffeb | 519 | reading = false; |
Farshad | 2:79a9dad8bc5e | 520 | } |
Farshad | 2:79a9dad8bc5e | 521 | } |
Farshad | 2:79a9dad8bc5e | 522 | |
Farshad | 0:d58d1cdf43a9 | 523 | int main(void) |
Farshad | 0:d58d1cdf43a9 | 524 | { |
Farshad | 2:79a9dad8bc5e | 525 | // default state is unknown |
Farshad | 5:207d4b6dface | 526 | connectionLed = 0; |
Farshad | 5:207d4b6dface | 527 | |
Farshad | 4:76bd50c41d39 | 528 | trigger = 1; |
Farshad | 5:207d4b6dface | 529 | triggerLed = 0; |
Farshad | 5:207d4b6dface | 530 | |
Farshad | 5:207d4b6dface | 531 | // make sure Reader is not shutdown |
Farshad | 5:207d4b6dface | 532 | shutdown = 1; |
Farshad | 2:79a9dad8bc5e | 533 | |
Farshad | 0:d58d1cdf43a9 | 534 | DEBUG("Initialising the nRF51822\n\r"); |
Farshad | 0:d58d1cdf43a9 | 535 | ble.init(); |
Farshad | 0:d58d1cdf43a9 | 536 | |
Farshad | 0:d58d1cdf43a9 | 537 | ble.onDisconnection(disconnectionCallback); |
Farshad | 2:79a9dad8bc5e | 538 | ble.onConnection(connectionCallback); |
Farshad | 0:d58d1cdf43a9 | 539 | ble.onDataWritten(onDataWritten); |
Farshad | 0:d58d1cdf43a9 | 540 | |
Farshad | 0:d58d1cdf43a9 | 541 | /* setup advertising */ |
Farshad | 0:d58d1cdf43a9 | 542 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Farshad | 0:d58d1cdf43a9 | 543 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Farshad | 0:d58d1cdf43a9 | 544 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
Farshad | 0:d58d1cdf43a9 | 545 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
Farshad | 0:d58d1cdf43a9 | 546 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
Farshad | 1:e18634cb382a | 547 | (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE)); |
Farshad | 2:79a9dad8bc5e | 548 | ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); |
Farshad | 0:d58d1cdf43a9 | 549 | ble.startAdvertising(); |
Farshad | 0:d58d1cdf43a9 | 550 | |
Farshad | 0:d58d1cdf43a9 | 551 | /* Setup uart service */ |
Farshad | 0:d58d1cdf43a9 | 552 | UARTService uartService(ble); |
Farshad | 0:d58d1cdf43a9 | 553 | uartServicePtr = &uartService; |
Farshad | 0:d58d1cdf43a9 | 554 | |
Farshad | 0:d58d1cdf43a9 | 555 | /* Setup auxiliary service. */ |
Farshad | 0:d58d1cdf43a9 | 556 | DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV); |
Farshad | 0:d58d1cdf43a9 | 557 | |
Farshad | 2:79a9dad8bc5e | 558 | // setup serial port to RFID reader |
Farshad | 2:79a9dad8bc5e | 559 | reader.baud(READER_BAUD_RATE); |
Farshad | 2:79a9dad8bc5e | 560 | reader.attach(&readerCallback); |
Farshad | 6:09cdafc3ffeb | 561 | |
Farshad | 6:09cdafc3ffeb | 562 | #ifdef BLE_DEBUG_OUTPUT |
Farshad | 6:09cdafc3ffeb | 563 | Ticker ticker; |
Farshad | 6:09cdafc3ffeb | 564 | ticker.attach(periodicCallback, 5); |
Farshad | 6:09cdafc3ffeb | 565 | #endif |
Farshad | 2:79a9dad8bc5e | 566 | |
Farshad | 5:207d4b6dface | 567 | // initialise reader |
Farshad | 5:207d4b6dface | 568 | wait_ms(100); |
Farshad | 5:207d4b6dface | 569 | for(int i = 0; i < sizeof(rfidInit)/sizeof(rfidInit[0]); i++) { |
Farshad | 5:207d4b6dface | 570 | if(rfidInit[i] == 0xFF) { |
Farshad | 5:207d4b6dface | 571 | wait_ms(50); |
Farshad | 5:207d4b6dface | 572 | } |
Farshad | 5:207d4b6dface | 573 | reader.putc(rfidInit[i]); |
Farshad | 5:207d4b6dface | 574 | } |
Farshad | 5:207d4b6dface | 575 | |
Farshad | 0:d58d1cdf43a9 | 576 | while (true) { |
Farshad | 0:d58d1cdf43a9 | 577 | ble.waitForEvent(); |
Farshad | 0:d58d1cdf43a9 | 578 | } |
Farshad | 6:09cdafc3ffeb | 579 | } |