EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Revision:
17:229d78f063fb
Parent:
15:bc4f8c597c26
Child:
18:08184949ab30
--- a/main.cpp	Mon Feb 27 01:35:10 2017 +0000
+++ b/main.cpp	Fri Jun 16 05:09:17 2017 +0000
@@ -53,7 +53,7 @@
 #define BATT_VALUE_THRESHOLD  0.5360f        // values by experiment 3.6V
 //#define BATT_VALUE_HYSTERYSIS 0.0200f       // about 0.05 volt
 
-#define ACTIVITY_TIMEOUT_SEC    60
+#define ACTIVITY_TIMEOUT_SEC    60      // default value- turn laser off if no measurement for more than this many seconds
 
 #undef NORDIC  // is board nordic DK?
 
@@ -84,6 +84,7 @@
 Timer timer;
 Ticker batteryChecker;
 Ticker activityChecker;
+uint16_t activityTimeout = ACTIVITY_TIMEOUT_SEC;
 
 
 const static char     DEVICE_NAME[]    = "MCS_LRF";
@@ -91,8 +92,8 @@
 const static char     MODEL[]          = "Model 2";
 const static char     SERIAL_NO[]      = "SN 1234";
 const static char     HARDWARE_REV[]   = "hw-rev 1.1";
-const static char     FIRMWARE_REV[]   = "fw-rev 1.3";
-const static char     SOFTWARE_REV[]   = "soft-rev 1.3";
+const static char     FIRMWARE_REV[]   = "fw-rev 1.4";
+const static char     SOFTWARE_REV[]   = "soft-rev 1.4";
 
 // these values must macth definitions in the XML file accompanying this device
 const static uint16_t distanceCmd   = 0x0001;
@@ -100,6 +101,7 @@
 const static uint16_t redDotCmd     = 0x0003;
 const static uint16_t resetCmd      = 0x0004;
 const static uint16_t nSamplesCmd   = 0x0005;
+const static uint16_t activityTimeoutCmd   = 0x0006;
 
 
 void activityCheckerTask();
@@ -172,6 +174,13 @@
                     laserPtr->setNumberOfSamples(nSamples);
                 }
                 break;
+                
+                case activityTimeoutCmd:
+                if(isSetCmd && params->len == 4) {
+                    DEBUG("CMD is nSample\n\r");
+                     activityTimeout = params->data[2] + (params->data[3] << 8);
+                }
+                break;
 
             default:
                 break;
@@ -204,7 +213,7 @@
 void resetActivityCheckerTask()
 {
     activityChecker.detach();
-    activityChecker.attach(activityCheckerTask, ACTIVITY_TIMEOUT_SEC);
+    activityChecker.attach(activityCheckerTask, activityTimeout);
 }
 
 void onDataWritten(const GattWriteCallbackParams *params)
@@ -248,6 +257,12 @@
     bleHelper->sendPacketOverBLE(distanceCmd, buf, sizeof(buf));
 }
 
+void notifyActivityTimeout(){
+    uint8_t buf[1];
+    bleHelper->sendPacketOverBLE(activityTimeoutCmd, buf, 0);
+}
+    
+
 void batteryCheckerTask(void)
 {
     enableBattVoltSense = 1;
@@ -274,6 +289,12 @@
     timer.start();
 }
 
+void turnLaserPowerOff()
+{
+    laserPtr->turnLaserPowerOff();
+    notifyActivityTimeout();
+}
+
 /* interrupt processor for when the button is released. If it has been pushed and held, turn the red dot off on release,
   otherwise debunce and make a measurement */
 void triggerRise()
@@ -281,7 +302,7 @@
     int elapsed = timer.read_ms();
     timer.stop();
     if(elapsed > PB_HOLD_TIME) {
-        laserPtr->turnLaserPowerOff();
+        turnLaserPowerOff();
     }
     //else if(elapsed > PB_DEBUNCE_TIME) {
 //        laserPtr->triggerDistanceMeasurement();
@@ -291,7 +312,7 @@
 void activityCheckerTask()
 {
     // too long with no activity- turn pwer off from laser to preserve power
-    laserPtr->turnLaserPowerOff();
+    turnLaserPowerOff();
 }
 
 int main(void)
@@ -325,7 +346,7 @@
     batteryChecker.attach(batteryCheckerTask, 60);
 
     // check acticity once every 3 minutes
-    activityChecker.attach(activityCheckerTask, ACTIVITY_TIMEOUT_SEC);
+    activityChecker.attach(activityCheckerTask, activityTimeout);
 
     /* Setup uart service */
     UARTService uartService(ble);