EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Revision:
8:ed66e7ef8243
Parent:
7:8a23a257b66a
Child:
10:d37cd13dd529
--- a/main.cpp	Wed Dec 09 03:28:42 2015 +0000
+++ b/main.cpp	Thu Dec 17 01:04:59 2015 +0000
@@ -20,20 +20,20 @@
 #include "DeviceInformationService.h"
 #include "UARTService.h"
 #include "bleHelper.h"
-#include "CircularBuffer.h"
+#include "laser.h"
 
-#include "laser.h"
 
 #undef BLE_DEBUG_OUTPUT
 
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
+/* Set this if you need debug messages on the console;
 * it will have an impact on code-size and power consumption. */
+#define NEED_CONSOLE_OUTPUT 0 
 
-#define NEED_PARSE_TRACE   0        // only used for parsing tag data- will not work if parse function is called from within serial interrupt
+// only used for parsing tag data- will not work if parse function is called from within serial interrupt
+#define NEED_PARSE_TRACE   0        
 
 Serial  pc(USBTX, USBRX);
 #if NEED_CONSOLE_OUTPUT
-//Serial  pc(USBTX, USBRX);
 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
 #else
 #define DEBUG(...) /* nothing */
@@ -45,15 +45,10 @@
 #define TRACE(...)
 #endif /* #if NEED_TRACE */
 
-
-
 #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;
-
-
 #undef NORDIC  // is board nordic DK?
 
 #ifdef NORDIC
@@ -65,8 +60,14 @@
 DigitalOut connectionLed(p23);
 Serial serial(p27, p26);  // tx, rx   === for adafruit BLE UART board (small blue board with red BLE module)
 Laser laser(serial);
+InterruptIn triggerButton(p22);
 #endif
 
+BLEDevice  ble;
+UARTService *uartServicePtr;
+BLEHelper* bleHelper;
+static uint8_t isConnected = 0;
+
 const static char     DEVICE_NAME[]    = "MCS_LRF";
 const static char     MANUFACTURER[]   = "MCS";
 const static char     MODEL[]          = "Model 1";
@@ -75,10 +76,6 @@
 const static char     FIRMWARE_REV[]   = "fw-rev 1";
 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 distanceCmd   = 0x0001;
 const static uint16_t triggerCmd    = 0x0002;
@@ -99,14 +96,6 @@
     connectionLed = isConnected;
 }
 
-
-uint16_t idx = 0;
-const uint16_t storeSize = 300;
-uint8_t store[storeSize];
-bool reading = false;
-bool sending = false;
-
-
 static void processData(const GattWriteCallbackParams *params)
 {
     if(params->len >= 2) {
@@ -119,28 +108,16 @@
                 if(!isSetCmd && params->len == 2) {
                     // form the reply to send
                     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
-                   // 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);
+                    laser.triggerDistanceMeasurement();
                 }
                 break;
-                
+
                 // TODO not needed really- can just use the distance command
-                case triggerCmd:
+            case triggerCmd:
                 if(isSetCmd && params->len == 3) {
                     // 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));
+                    laser.triggerDistanceMeasurement();
                 }
                 break;
 
@@ -167,56 +144,35 @@
     }
 }
 
-// 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);
-//
-//    
-//}
 
-
-uint16_t length;
-//uint8_t tempBuf[1000];
 void periodicCallback(void)
 {
-    #ifdef BLE_DEBUG_OUTPUT
-    //for( idx = 0; idx < 64; idx++){         // 64 is the limit
-//        store[idx] = idx;
-//    }
-     
-    if(reading == false) {
-        if(idx > 0) {
-            sending = true;
-            length = idx;            
-            //memcpy(&tempBuf[0], &store[0], idx);                    
-            sendPacketOverBLE(tagIdCmd, store, idx);
-            idx = 0;        // now that we have sent this, reset the index
-        }
-    }
-#endif
-
-    sending = false;
 }
 
 // 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
-   // 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;
+{
+    //if(serial.readable()) {
+//        laser.processRxData(serial.getc());
 //    }
 }
 
+/* send distance measurement to the connected BLE client */
+void distanceCallcack(float distance, float elapsedTime)
+{
+    uint8_t buf[10];
+    uint16_t arrayLen = 2;
+    memcpy(&buf[0], &arrayLen, sizeof(uint16_t));
+    memcpy(&buf[2], &distance, sizeof(float));
+    memcpy(&buf[6], &elapsedTime, sizeof(float));
+    bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf));
+}
+
+ /* processor for the hardware trigger button */
+void trigger(){
+     laser.triggerDistanceMeasurement();
+}
+
 int main(void)
 {
     // default state is unknown
@@ -249,19 +205,18 @@
     /* Setup bleHelper */
     BLEHelper helper(&ble, uartServicePtr);
     bleHelper = &helper;
-    
-    // setup serial port to LRF 
+
+    // setup serial port to LRF
     serial.baud(READER_BAUD_RATE);
- //   serial.attach(&readerCallback);
+    // serial.attach(&readerCallback);
     
-    #ifdef BLE_DEBUG_OUTPUT
-    Ticker ticker;
-    ticker.attach(periodicCallback, 5);
-    #endif
-    
+    // processor for the trigger button
+    triggerButton.fall(&trigger);  
+
     laser.enableMeasurement(true);
+    laser.setDistaceCallback(&distanceCallcack);
 
     while (true) {
         ble.waitForEvent();
     }
-} 
+}