EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Revision:
7:8a23a257b66a
Parent:
6:09cdafc3ffeb
Child:
8:ed66e7ef8243
--- a/main.cpp	Mon Nov 09 04:47:27 2015 +0000
+++ b/main.cpp	Wed Dec 09 03:28:42 2015 +0000
@@ -19,8 +19,11 @@
 #include "BLE.h"
 #include "DeviceInformationService.h"
 #include "UARTService.h"
+#include "bleHelper.h"
 #include "CircularBuffer.h"
 
+#include "laser.h"
+
 #undef BLE_DEBUG_OUTPUT
 
 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
@@ -44,11 +47,14 @@
 
 
 
+#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
+
+
 BLEDevice  ble;
 
-DigitalOut trigger(p18);
 
-#define NORDIC  // is board nordic DK?
+#undef NORDIC  // is board nordic DK?
 
 #ifdef NORDIC
 DigitalOut connectionLed(p21);
@@ -56,13 +62,12 @@
 DigitalOut shutdown(p20);  // for nordic DK
 Serial reader(p13, p17);  // tx, rx   === NOTE tx port pin needs to be wired and verified  (for nordic DK)
 #else
-DigitalOut connectionLed(p19);
-DigitalOut triggerLed(p18);
-DigitalOut shutdown(p10); 
-Serial reader(p9, p8);  // tx, rx   === for adafruit BLE UART board (small blue board with red BLE module)
+DigitalOut connectionLed(p23);
+Serial serial(p27, p26);  // tx, rx   === for adafruit BLE UART board (small blue board with red BLE module)
+Laser laser(serial);
 #endif
 
-const static char     DEVICE_NAME[]    = "MCS_RFID";
+const static char     DEVICE_NAME[]    = "MCS_LRF";
 const static char     MANUFACTURER[]   = "MCS";
 const static char     MODEL[]          = "Model 1";
 const static char     SERIAL_NO[]      = "SN 1234";
@@ -71,103 +76,12 @@
 const static char     SOFTWARE_REV[]   = "soft-rev 1";
 
 UARTService *uartServicePtr;
+BLEHelper* bleHelper;
 static uint8_t isConnected = 0;
 
 // these values must macth definitions in the XML file accompanying this device
-const static uint16_t tagIdCmd             = 0x0001;
-const static uint16_t hardwareTriggerCmd   = 0x0002;
-const static uint16_t softwareTriggerCmd   = 0x0003;
-
-// 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 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_PREAMBLE,
-    AWAITING_HEADER,
-    AWAITING_DATA_LEN,
-    AWAITING_READ_CMD,
-    AWAITING_STATUS_1,
-    AWAITING_STATUS_2,
-    AWAITING_EPC_LENGTH,
-    AWAITING_PC_WORD,
-    READING_TAG_EPC,
-    READIG_TAG_CRC
-} packetState_e;
-
-//CircularBuffer cb;
-
-static uint8_t tagEPC[TAG_EPC_BUF_SIZE] = {0};
-static packetState_e state = AWAITING_PREAMBLE;
-static uint8_t tagBufIndex = 0;
-static uint8_t skip = 0;
-static uint16_t tagLen = 0;
-//static uint16_t dataLen = 0;
-
-// the preamble before 96 bit EPC tag data- we look for this before reading the EPC data 
-static uint8_t preamble[] = {0x00, 0x80};
-static uint8_t preambleIdx = 0;
-
-// commmands to initialize the Nano RFID reader- sniffied from coms going from the base edge device to the Nano reader
-// only use the get commands
-const uint8_t rfidInit[] = {
-//                            0xFF, 0x04, 0x06, 0x00, 0x01, 0xC2, 0x00, 0xA4,0x60,   // set baud rate to 115200
-//  0xFF, 0x00, 0x03, 0x1D, 0x0C,
-//  0xFF, 0x00, 0x03, 0x1D, 0x0C,
-//  0xF3, 0xE0, 0xE0, 0xE0, 0xE0, 0xFC, 0x1C, 0x00, 0x00, 0xFE, 0x80, 0xE6, 0xE0,
-//  0xFF, 0x00, 0x03, 0x1D, 0x0C,
-//  0xFF, 0x00, 0x0C, 0x1D, 0x03,                       //get current program
-//  0xFF, 0x00, 0x68, 0x1D, 0x67,                       // get power mode
-//  0xFF, 0x02, 0x6A, 0x01, 0x0E, 0x2E, 0x40,           // get reader configuration
-//  0xFF, 0x02, 0x6A, 0x01, 0x12, 0x2E, 0x5C,           // get reader configuration
-//  0xFF, 0x00, 0x63, 0x1D, 0x6C,                       // get current tag protocol
-    0xFF, 0x02, 0x93, 0x00, 0x05, 0x51, 0x7D,           // set current tag protocol to Gen2)
-//  0xFF, 0x01, 0x61, 0x05, 0xBD, 0xB8,                 // get antenna configuration
-//  0xFF, 0x02, 0x6A, 0x01, 0x0C, 0x2E, 0x42,           // get power mode
-//  0xFF, 0x02, 0x6A, 0x01, 0x0D, 0x2E, 0x43,           // get power mode
-//  0xFF, 0x02, 0x10, 0x00, 0x40, 0xF0, 0xD3,           // get hardware version
-    0xFF, 0x01, 0x97, 0x01, 0x4B, 0xBC,                 // set current region to NA
-    0xFF, 0x03, 0x9B, 0x05, 0x00, 0x00, 0xDC, 0xE8,     // set protocol configuration, Gen2, S0
-    0xFF, 0x04, 0x9B, 0x05, 0x01, 0x01, 0x00, 0xA2, 0xFD,  // set protocol configuration Gen2,
-    0xFF, 0x03, 0x9B, 0x05, 0x02, 0x01, 0xDE, 0xE9,       // set protocol configuration Gen2,
-//  0xFF, 0x01, 0x62, 0x00, 0xBE, 0xBD,                   // get read transmit power
-    0xFF, 0x02, 0x92, 0x0A, 0x8C, 0x4B, 0xD5,              // set read transmit power to 27dbm
-};
-
-// commmands to trigger a tag read- sniffied from coms going from the base edge device to the Nano reader
-uint8_t cmd_1[] = {0xFF, 0x00, 0x72, 0x1D, 0x7D};                               // get current temperature
-uint8_t cmd_2[] = {0xFF, 0x00, 0x2A, 0x1D, 0x25};                               // clear tag buffer
-uint8_t cmd_3[] = {0xFF, 0x03, 0x91, 0x02, 0x01, 0x01, 0x42, 0xC5};             // set antenna port
-uint8_t cmd_4[] = {0xFF, 0x05, 0x22, 0x00, 0x00, 0x13, 0x00, 0xFA, 0x2A, 0x17}; // read Tag Multiple
-uint8_t cmd_5[] = {0xFF, 0x03, 0x29, 0x01, 0xFF, 0x00, 0x1B, 0x03};             // get tag buffer
-
-typedef struct {
-    uint8_t* data;
-    uint8_t len;
-    uint16_t delayms;
-} command_t;
-
-command_t readCommands[] = {
-    {cmd_1, sizeof(cmd_1), 10},
-    {cmd_2, sizeof(cmd_2), 10},
-    {cmd_3, sizeof(cmd_3), 10},
-    {cmd_4, sizeof(cmd_4), 300},
-    {cmd_5, sizeof(cmd_5), 1}
-};
-
+const static uint16_t distanceCmd   = 0x0001;
+const static uint16_t triggerCmd    = 0x0002;
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -186,180 +100,12 @@
 }
 
 
-//// State machine to parse the tag data received from serial port. It only looks for EPC data in the packet
-//static void parsePacket_2(uint8_t d)
-//{
-////  //  TRACE("%02X ", d);
-//    switch (state) {
-//        case AWAITING_HEADER:
-//            if(d == HEADER_FLAG) {
-//                TRACE("AWAITING_DATA_LEN \r\n");
-//                state = AWAITING_DATA_LEN;
-//                skip = 0;
-//            }
-//            break;
-//
-//        case AWAITING_DATA_LEN:
-//            if(d > 0) {
-//                dataLen = d;
-//                state = AWAITING_READ_CMD;
-//                TRACE("AWAITING_READ_CMD \r\n");
-//            } else {
-//                state = AWAITING_HEADER;
-//            }
-//            break;
-//
-//        case AWAITING_READ_CMD:
-//            if(d == EPC_READ_CMD) {
-//                state = AWAITING_STATUS_1;
-//                TRACE("AWAITING_STATUS_1 \r\n");
-//            } else {
-//                state = AWAITING_HEADER;
-//            }
-//            break;
-//
-//        case AWAITING_STATUS_1:
-//            if(d == 0) {
-//                state = AWAITING_STATUS_2;
-//                TRACE("AWAITING_STATUS_2 \r\n");
-//            } else {
-//                state = AWAITING_HEADER;
-//            }
-//            break;
-//
-//        case AWAITING_STATUS_2:
-//            if(d == 0) {
-//                state = AWAITING_EPC_LENGTH;
-//                TRACE("AWAITING_EPC_LENGTH_1 \r\n");
-//            } else {
-//                state = AWAITING_HEADER;
-//            }
-//            break;
-//
-//        case AWAITING_EPC_LENGTH:
-//         if(++skip >= SKIP_TO_BIT_FLAG) {
-//            if(d == BIT_64_TAG_FLAG) {
-//                state = AWAITING_PC_WORD;
-//                tagLen = 8;
-//                TRACE("AWAITING_PC_WORD \r\n");
-//            } else if (d == BIT_96_TAG_FLAG) {
-//                state = AWAITING_PC_WORD;
-//                tagLen = 12;
-//                TRACE("AWAITING_PC_WORD \r\n");
-//            } else {
-//                state = AWAITING_HEADER;
-//            }
-//            }
-//            break;
-//
-//        case AWAITING_PC_WORD:
-////            if((tagLen == 8 && d == BIT_64_TAG_PC_WORD) || (tagLen == 12 && d == BIT_96_TAG_PC_WORD)) 
-//                {
-//                state = READING_TAG_EPC;
-//                tagBufIndex = 0;
-//                TRACE("READING_TAG_EPC \r\n");
-//                skip = 0;
-//            } 
-//            //else {
-////                state = AWAITING_HEADER;
-////            }
-//            break;
-//
-//        case READING_TAG_EPC:
-//            if(++skip >= SKIP_TO_TAG_EPC) {
-//                if(tagBufIndex < tagLen) {
-//                    tagEPC[tagBufIndex++] = d;
-//                    TRACE("%02X ", d);
-//                } else {
-//                    // don't worry about CRC for now
-//                    state = AWAITING_HEADER;
-//                    TRACE("AWAITING_HEADER \r\n");
-//
-//                    TRACE("TAG EPC: ============\r\n");
-//                    for(int i = 0; i < tagLen; i++) {
-//                        TRACE("%02X ", tagEPC[i]);
-//                  //      pc.printf("%02X ", tagEPC[i]);
-//                    }
-//                   // pc.printf("\r\n");
-//
-//                    sendBLENotification();
-//                }
-//            }
-//            break;
-//    }
-//}
-
 uint16_t idx = 0;
 const uint16_t storeSize = 300;
 uint8_t store[storeSize];
 bool reading = false;
 bool sending = false;
 
-typedef enum{
-    PS_FIRST_AND_ONLY =     0b0000000000000000,
-    PS_FIRST_AND_NOT_LAST = 0b0010000000000000,
-    PS_LAST =               0b0100000000000000,
-    PS_MIDDLE =             0b0110000000000000
-} PacketStatus_e;
-
-static PacketStatus_e packetStatus = PS_FIRST_AND_ONLY;
-
-static void sendOverBLE(uint16_t cmd, uint8_t data[], uint16_t len, PacketStatus_e status){
-    
-    if(len > 16){ 
-        // this is a problem
-    }
-    
-    uint8_t buf[len + 4];        // should set it to the max single packet size
-    uint8_t offset = 0;
-    uint16_t tmp = cmd | (uint16_t)status;
-    memcpy(buf, &tmp, sizeof(uint16_t));       // command
-    offset+=sizeof(uint16_t);
-    memcpy(&buf[offset], &len, sizeof(uint16_t));     // length of array
-    offset+=sizeof(uint16_t);
-    memcpy(&buf[offset], &data[0], len);              // data
-    offset+= len;
-    ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset);
-}
-
-static void sendPacketOverBLE(uint16_t cmd, uint8_t data[], uint16_t len)
-{
-    uint8_t nPackets = (len / 17) + 1;
-    uint8_t remainingPackets = nPackets;
-    uint16_t offset = 0;
-    bool first = true;
-    const uint8_t maxDataPayload = 16;   // max is 20 but we are sending a command (2) plus the length (2)
-
-    do {
-        if(nPackets == 1) {
-            // first and only
-            packetStatus = PS_FIRST_AND_ONLY;
-            sendOverBLE(cmd, &data[offset], len, packetStatus);
-            remainingPackets = 0;
-        } else if(remainingPackets == 1) {
-            // last one
-            packetStatus = PS_LAST;
-            sendOverBLE(cmd, &data[offset], len-offset, packetStatus);
-            remainingPackets = 0;
-        } else if(remainingPackets > 1 && first == true) {
-            // first and more to come
-            packetStatus = PS_FIRST_AND_NOT_LAST;
-            sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus);
-            offset += maxDataPayload;
-            remainingPackets -= 1;
-            first = false;
-        } else if(remainingPackets > 1 && first == false) {
-            // middle
-            packetStatus = PS_MIDDLE;
-            sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus);
-            offset += maxDataPayload;
-            remainingPackets -= 1;
-        }
-        
-        wait_ms(50);        // 40ms delay seems to be a limit
-    } while (remainingPackets > 0);
-}
-
 
 static void processData(const GattWriteCallbackParams *params)
 {
@@ -369,41 +115,32 @@
         DEBUG("command: %d   \r\n", command);
 
         switch(command & ~SET_PARAM_CMD_MASK) {
-            case tagIdCmd:
+            case distanceCmd:
                 if(!isSetCmd && params->len == 2) {
                     // form the reply to send
-                    DEBUG("CMD is GET code\n\r");
+                    DEBUG("CMD is GET distance\n\r");
                     // TODO can do a trigger for a new read before transimiting, For now just send the latest read
-                     sendPacketOverBLE(tagIdCmd, tagEPC, tagLen);
+                   // laser.Connect();
+                 //   laser.SetRedDot(true);
+                    float distance = laser.getDistance();
+                    uint8_t buf[5];
+                    memcpy(&buf[0], &distance, sizeof(distance));
+                    bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(distance));
+                   // laser.SetRedDot(false);
                 }
                 break;
-
-            case hardwareTriggerCmd:
+                
+                // TODO not needed really- can just use the distance command
+                case triggerCmd:
                 if(isSetCmd && params->len == 3) {
-                    DEBUG("CMD is SET hardwareTriggerCmd\n\r");
-                    trigger = 0;
-                    triggerLed = 1;
-                    wait_ms(100);
-                    trigger = 1;
-                    triggerLed = 0;
-                }
-                break;
-
-            case softwareTriggerCmd:
-                if(isSetCmd && params->len == 3) {      // TODO this command does not need a value
-                    DEBUG("CMD is SET softwareTriggerCmd\n\r");
-                    triggerLed = 1;
-                    for(int i = 0; i < sizeof(readCommands) / sizeof(command_t); i++) {
-                        uint8_t* d = readCommands[i].data;
-                        uint8_t len = readCommands[i].len;
-                        for(int j = 0; j < len; j++) {
-                            // DEBUG("%02X ", d[j]);
-                            reader.putc(d[j]);
-                        }
-                        wait_ms(readCommands[i].delayms);                        
-                        DEBUG("\r\n ");
-                    }
-                    triggerLed = 0;
+                    // form the reply to send
+                    DEBUG("CMD is SET trigger\n\r");
+                    // TODO can do a trigger for a new read before transimiting, For now just send the latest read
+                    // make sure it is active                    
+                    float distance = laser.getDistance();
+                    uint8_t buf[5];
+                    memcpy(&buf[0], &distance, sizeof(distance));
+                     bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(distance));
                 }
                 break;
 
@@ -431,52 +168,12 @@
 }
 
 // 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);
-
-    tagLen = 12;
-    switch (state) {
-        case AWAITING_PREAMBLE:
-            // if(d == preamble[preambleIdx] || preambleIdx > 1) {
-            if(d == preamble[preambleIdx]) {
-                preambleIdx++;
-                if(preambleIdx == sizeof(preamble)) {
-                    state = READING_TAG_EPC;
-                    skip = 0;
-                }
-            } else {
-                preambleIdx = 0;
-            }
-            break;
-
-        case READING_TAG_EPC:
-            if(++skip > SKIP_TO_TAG_EPC) {
-                if(tagBufIndex < tagLen) {
-                    tagEPC[tagBufIndex++] = d;
-                    TRACE("%02X ", d);
-                } else {
-                    // don't worry about CRC for now
-                    state = AWAITING_PREAMBLE;
-                    preambleIdx = 0;
-                    tagBufIndex = 0;
-                    TRACE("AWAITING_PREAMBLE \r\n");
-
-                    TRACE("TAG EPC: ============\r\n");
-                    for(int i = 0; i < tagLen; i++) {
-                        TRACE("%02X ", tagEPC[i]);
-                        //      pc.printf("%02X ", tagEPC[i]);
-                    }
-                    // pc.printf("\r\n");
-
-                    sendPacketOverBLE(tagIdCmd, tagEPC, tagLen);
-                    triggerLed = 0;
-                }
-            }
-
-            break;
-    }
-}
+//static void parsePacket(uint8_t d)
+//{
+//    TRACE("%02X ", d);
+//
+//    
+//}
 
 
 uint16_t length;
@@ -506,18 +203,18 @@
 void readerCallback()
 {    
     // Note: Need to actually read from the serial to clear the RX interrupt
-    if(sending == false) {
-        reading = true;
-        while(reader.readable()) {
-            uint8_t c = reader.getc();
-            parsePacket(c);
-            #ifdef BLE_DEBUG_OUTPUT
-            store[idx] = c;
-            if(idx < (storeSize-1)) idx++;
-            #endif
-        }
-        reading = false;
-    }
+   // if(sending == false) {
+//        reading = true;
+//        while(serial.readable()) {
+//            uint8_t c = serial.getc();
+//            parsePacket(c);
+//            #ifdef BLE_DEBUG_OUTPUT
+//            store[idx] = c;
+//            if(idx < (storeSize-1)) idx++;
+//            #endif
+//        }
+//        reading = false;
+//    }
 }
 
 int main(void)
@@ -525,12 +222,6 @@
     // default state is unknown
     connectionLed = 0;
 
-    trigger = 1;
-    triggerLed = 0;
-
-    // make sure Reader is not shutdown
-    shutdown = 1;
-
     DEBUG("Initialising the nRF51822\n\r");
     ble.init();
 
@@ -555,23 +246,20 @@
     /* Setup auxiliary service. */
     DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV);
 
-    // setup serial port to RFID reader
-    reader.baud(READER_BAUD_RATE);
-    reader.attach(&readerCallback);
+    /* Setup bleHelper */
+    BLEHelper helper(&ble, uartServicePtr);
+    bleHelper = &helper;
+    
+    // setup serial port to LRF 
+    serial.baud(READER_BAUD_RATE);
+ //   serial.attach(&readerCallback);
     
     #ifdef BLE_DEBUG_OUTPUT
     Ticker ticker;
     ticker.attach(periodicCallback, 5);
     #endif
-
-    // initialise reader
-    wait_ms(100);
-    for(int i = 0; i < sizeof(rfidInit)/sizeof(rfidInit[0]); i++) {
-        if(rfidInit[i] == 0xFF) {
-            wait_ms(50);
-        }
-        reader.putc(rfidInit[i]);
-    }
+    
+    laser.enableMeasurement(true);
 
     while (true) {
         ble.waitForEvent();