EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Revision:
3:de77a4ebbf21
Parent:
2:79a9dad8bc5e
Child:
4:76bd50c41d39
--- a/main.cpp	Thu Oct 08 04:49:36 2015 +0000
+++ b/main.cpp	Fri Oct 09 03:42:39 2015 +0000
@@ -60,23 +60,22 @@
 const static uint16_t tagIdCmd      = 0x0001;
 const static uint16_t triggerCmd   = 0x0002;
 
+// definition of the RFID packet
+#define HEADER_FLAG             0xFF
+#define EPC_READ_CMD            0x29
+#define BIT_64_TAG_FLAG         0x60
+#define BIT_96_TAG_FLAG         0x80
+#define SKIP_TO_BIT_FLAG        22
+#define BIT_64_TAG_PC_WORD      0x20
+#define BIT_96_TAG_PC_WORD      0x30
+#define SKIP_TO_TAG_EPC         2
 
-#define HEADER_FLAG         0xFF
-#define CMD_OFFSET          2
-#define EPC_READ_CMD        0x29
-#define STATUS_OFFSET       3
-#define EPC_LENGTH_OFFSET   26
-#define BIT_64_TAG_FLAG     0x60
-#define BIT_96_TAG_FLAG     0x80
-#define PC_WORD_OFFSET      27
-#define BIT_64_TAG_PC_WORD  0x20
-#define BIT_96_TAG_PC_WORD  0x30
-#define TAG_EPC_OFFSET      29
-
-#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
-#define READER_BAUD_RATE        115200  // TODO check this
-#define READ_BUF_SIZE 255
-#define TAG_EPC_BUF_SIZE    12
+     
+#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
+#define READER_BAUD_RATE        115200  
+#define READ_BUF_SIZE           255
+#define TAG_EPC_BUF_SIZE        12
+       
 
 typedef enum {
     AWAITING_HEADER,
@@ -97,13 +96,6 @@
 static uint16_t tagLen = 0;
 static uint16_t dataLen = 0;
 
-#if 0
-static uint8_t head = 0;
-static uint8_t tail = 0;
-static bool receiving = 0;
-static uint8_t readBuf[READ_BUF_SIZE] = {0};
-#endif
-
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -112,7 +104,6 @@
     ble.startAdvertising();
     isConnected = 0;
     led = isConnected;
-
 }
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
@@ -148,6 +139,7 @@
                 if(!isSetCmd && params->len == 2) {
                     // form the reply to send
                     DEBUG("CMD is GET code\n\r");
+                    // TODO can do a trigger for a new read before transimiting, For now just send the latest read
                     sendBLENotification();
                 }
                 break;
@@ -177,53 +169,16 @@
             DEBUG("0x%X ", params->data[i]);
         }
         DEBUG("\n\r", bytesRead);
-
+        
+        // echo?
         // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
 
         processData(params);
     }
 }
 
-// This function requires the entire scanned data to be available. It takes a pointer to the beginnig of the data array
-// and does not do any checking of available data. ie very easy to break (currently not used- Instead use the parsePacket()
-// function with each byte received from the serial port.
-#if 0
-void checkForEPC(uint8_t* d)
-{
-     DEBUG("%02X    ", *d);
-    if(*d == HEADER_FLAG && *(d + CMD_OFFSET) == EPC_READ_CMD) {
-         DEBUG("GOT READ COmmand =======\r\n");
-        // this is response to a read tage EPC command
-        if(*(d + STATUS_OFFSET) == 0 && *(d + STATUS_OFFSET + 1) == 0) {
-            DEBUG("STATUS is OK =======\r\n");
-            // state is Ok
-            if(*(d + EPC_LENGTH_OFFSET) == BIT_64_TAG_FLAG && *(d + PC_WORD_OFFSET) == BIT_64_TAG_PC_WORD) {
-                DEBUG("GOT 64bit EPC =======\r\n");
-                // this is a 64 bit tag so the next 8 bytes are tag id and further 2 bytes are the tag CRC
-                memset(&tagEPC[0], 0, TAG_EPC_BUF_SIZE);
-                memcpy(&tagEPC[0], d + TAG_EPC_OFFSET, 8);       // do not worry about the CRC for now
-                
-                for(int j = 0; j < 8; j++) {
-                    DEBUG("%02X ", tagEPC[j]);
-                }
-                DEBUG("\r\n");
-            } else if(*(d + EPC_LENGTH_OFFSET) == BIT_96_TAG_FLAG && *(d + PC_WORD_OFFSET) == BIT_96_TAG_PC_WORD) {
-                 DEBUG("GOT 96bit EPC =======\r\n");
-                // this is a 96 bit tag so the next 12 bytes are tag id and further 2 bytes are the tag CRC
-                memset(&tagEPC[0], 0, TAG_EPC_BUF_SIZE);
-                memcpy(&tagEPC[0], d + TAG_EPC_OFFSET, 12);       // do not worry about the CRC for now
-               
-                for(int j = 0; j < 12; j++) {
-                    DEBUG("%02X ", tagEPC[j]);
-                }
-                DEBUG("\r\n");
-            }
-        } // Status OK
-    }  // response to EPC
-}
-#endif
 
-// State machine to parse the tag data received from serial port. It looks for EPC data in the packet
+// State machine to parse the tag data received from serial port. It only looks for EPC data in the packet
 static void parsePacket(uint8_t d)
 {
     TRACE("%02X    ", d);
@@ -274,7 +229,7 @@
             break;
 
         case AWAITING_EPC_LENGTH:
-         if(++skip >= 22) {
+         if(++skip >= SKIP_TO_BIT_FLAG) {
             if(d == BIT_64_TAG_FLAG) {
                 state = AWAITING_PC_WORD;
                 tagLen = 8;
@@ -301,7 +256,7 @@
             break;
 
         case READING_TAG_EPC:
-            if(++skip >= 2) {
+            if(++skip >= SKIP_TO_TAG_EPC) {
                 if(tagBufIndex < tagLen) {
                     tagEPC[tagBufIndex++] = d;
                     TRACE("%02X ", d);
@@ -329,23 +284,16 @@
     //  led1 = !led1;
 }
 
+// this is an ISR, so do not spend too much time here and be careful with printing debug info
 void readerCallback()
 {
     // Note: Need to actually read from the serial to clear the RX interrupt
-   // receiving = true;
-    while(reader.readable()) {
-        uint8_t c = reader.getc();        
-       // readBuf[head++] = c;
-//        if(head >= READ_BUF_SIZE) {
-//            head = 0;
-//        }        
-        parsePacket(c);
+    while(reader.readable()) {           
+        parsePacket(reader.getc());
     }
     
     // make sure LED stays on
     led = isConnected;
-    
- //   receiving = false;
 }
 
 int main(void)