From 7c0b901de632decb802a004928a49bee88ea5897 Mon Sep 17 00:00:00 2001
From: Peter Svihra <peter.svihra@cern.ch>
Date: Mon, 13 Apr 2020 00:08:36 +0200
Subject: [PATCH] updated lib location

---
 arduino/protocol/platformio.ini   |  1 +
 arduino/protocol/platformio.pro   | 12 +++++-----
 arduino/protocol/src/protocol.cpp | 37 ++++++++++++++++++++++++-------
 3 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/arduino/protocol/platformio.ini b/arduino/protocol/platformio.ini
index 46ff7840..62149f68 100644
--- a/arduino/protocol/platformio.ini
+++ b/arduino/protocol/platformio.ini
@@ -17,6 +17,7 @@ lib_deps =
     5390 ; uCRC16Lib
     5418 ; RingBuffer
 build_flags = -fpermissive -I../common/include/
+lib_extra_dirs = ../common/lib
 
 [env:uno]
 platform = atmelavr
diff --git a/arduino/protocol/platformio.pro b/arduino/protocol/platformio.pro
index 7aa95f36..f85d5659 100644
--- a/arduino/protocol/platformio.pro
+++ b/arduino/protocol/platformio.pro
@@ -6,7 +6,7 @@ else {
 }
 INCLUDEPATH += "$${_PRO_FILE_PWD_}/include"
 INCLUDEPATH += "$${_PRO_FILE_PWD_}/src"
-INCLUDEPATH += "$${_PRO_FILE_PWD_}/lib/commsControl"
+INCLUDEPATH += "$${_PRO_FILE_PWD_}/../common/lib/commsControl"
 INCLUDEPATH += "$${_PRO_FILE_PWD_}/.pio/libdeps/uno/uCRC16Lib_ID5390/src"
 INCLUDEPATH += "$${_PRO_FILE_PWD_}/.pio/libdeps/uno/RingBuffer_ID5418/src"
 INCLUDEPATH += "$${HOMEDIR}/.platformio/packages/framework-arduino-avr/cores/arduino"
@@ -31,9 +31,9 @@ DEFINES += "__AVR_ATmega328P__"
 OTHER_FILES += platformio.ini
 
 SOURCES +=  src/protocol.cpp \
-            lib/commsControl/commsControl.cpp \
-            lib/commsControl/commsFormat.cpp
+            ../common/lib/commsControl/commsControl.cpp \
+            ../common/lib/commsControl/commsFormat.cpp
 
-HEADERS +=  lib/commsControl/commsConstants.h \
-            lib/commsControl/commsControl.h \
-            lib/commsControl/commsFormat.h
+HEADERS +=  ../common/lib/commsControl/commsConstants.h \
+            ../common/lib/commsControl/commsControl.h \
+            ../common/lib/commsControl/commsFormat.h
diff --git a/arduino/protocol/src/protocol.cpp b/arduino/protocol/src/protocol.cpp
index 3b942330..5bcb3686 100644
--- a/arduino/protocol/src/protocol.cpp
+++ b/arduino/protocol/src/protocol.cpp
@@ -21,6 +21,11 @@ bool blue_ = false;
 bool green_ = false;
 bool red_ = false;
 
+bool enabled_ = false;
+
+uint32_t lastTime_ = 0;
+
+uint32_t offset_ = 10;
 
 // dirty function to switch one of the LEDs
 void switchLED(int led) {
@@ -76,23 +81,40 @@ void loop() {
         }
         previousState_ = currentState_;
     }
-//    switchLED(LED_BLUE);
-    // counter increase on button press
-//    plSend_.getData()->fsm_state += 1;
-//    comms_.writePayload(plSend_);
+
+    if (enabled_ & (millis() > (lastTime_ + offset_)))
+    {
+        lastTime_ = millis();
+
+        plSend_.getData()->readback_valve_air_in = static_cast<uint8_t>((lastTime_ >> 24) & 0xFF);
+        plSend_.getData()->readback_valve_o2_in  = static_cast<uint8_t>((lastTime_ >> 16) & 0xFF);
+        plSend_.getData()->readback_valve_inhale = static_cast<uint8_t>((lastTime_ >> 8 ) & 0xFF);
+        plSend_.getData()->readback_valve_exhale = static_cast<uint8_t>((lastTime_ >> 0 ) & 0xFF);
+
+        switchLED(LED_BLUE);
+        plSend_.setType(payloadType::payloadData);
+        plSend_.getData()->fsm_state += 1;
+        comms_.writePayload(plSend_);
+    }
 
     // per cycle sender
     comms_.sender();
     // per cycle receiver
     comms_.receiver();
 
-    // per cycle data checker - the received entry is automatically removed from the buffer
     if (comms_.readPayload(plReceive_)) {
 
         switch (plReceive_.getType()) {
             case payloadType::payloadCmd:
-                switchLED(plReceive_.getCmd()->cmdCode);
-                alarm_.alarmCode = plReceive_.getCmd()->cmdCode + 1;
+                if (plReceive_.getCmd()->cmdCode % 2 == 0) {
+                    enabled_ = false;
+                } else {
+                    enabled_ = true;
+                }
+                offset_ = plReceive_.getCmd()->param;
+
+                alarm_.alarmCode = plReceive_.getCmd()->cmdCode;
+                alarm_.param     = millis() & 0xFFFFFFFF;
                 plSend_.setAlarm(&alarm_);
                 comms_.writePayload(plSend_);
                 break;
@@ -102,5 +124,4 @@ void loop() {
 
         plReceive_.setType(payloadType::payloadUnset);
     }
-    delay(50);
 }
-- 
GitLab