From 785dfca77862e04c35543ef5abd05fc77700fbb0 Mon Sep 17 00:00:00 2001
From: Peter Svihra <peter.svihra@cern.ch>
Date: Sun, 26 Apr 2020 03:17:39 +0200
Subject: [PATCH] bugfixes changes for payload modifications

- wrong data assignment
- removed printing
- improved data retrieving
---
 arduino/common/lib/CommsControl/CommsCommon.h | 40 +++++++++++++++----
 .../common/lib/CommsControl/CommsControl.cpp  | 14 +++----
 .../common/lib/CommsControl/CommsControl.h    |  2 +-
 arduino/hev_prototype_v1/src/UILoop.cpp       | 24 +++++------
 arduino/hev_prototype_v1/src/UILoop.h         | 12 +++---
 arduino/hev_prototype_v1/src/main.cpp         |  5 ++-
 6 files changed, 61 insertions(+), 36 deletions(-)

diff --git a/arduino/common/lib/CommsControl/CommsCommon.h b/arduino/common/lib/CommsControl/CommsCommon.h
index b9e7e90e..1d38defa 100644
--- a/arduino/common/lib/CommsControl/CommsCommon.h
+++ b/arduino/common/lib/CommsControl/CommsCommon.h
@@ -11,11 +11,11 @@
 
 // UNO struggles with the RAM size for ring buffers
 #ifdef ARDUINO_AVR_UNO
-#define COMMS_MAX_SIZE_RB_RECEIVING 1
-#define COMMS_MAX_SIZE_RB_SENDING 1
+#define COMMS_MAX_SIZE_RB_RECEIVING  1
+#define COMMS_MAX_SIZE_RB_SENDING    1
 #else
 #define COMMS_MAX_SIZE_RB_RECEIVING 10
-#define COMMS_MAX_SIZE_RB_SENDING 5
+#define COMMS_MAX_SIZE_RB_SENDING    5
 #endif
 
 #define COMMS_MAX_SIZE_PACKET 64
@@ -55,16 +55,18 @@ public:
     Payload(PAYLOAD_TYPE type = PAYLOAD_TYPE::UNSET)  {_type = type; }
     Payload(const Payload &other) {
         _type = other._type;
-        memcpy(&_buffer, &other._buffer, sizeof (other._size));
+        _size = other._size;
+        memcpy(_buffer, other._buffer, other._size);
     }
     Payload& operator=(const Payload& other) {
         _type = other._type;
-        memcpy(&_buffer, &other._buffer, sizeof (other._size));
+        _size = other._size;
+        memcpy(_buffer, other._buffer, other._size);
         return *this;
     }
 
     ~Payload() { unset(); }
-    void unset() { memset( _buffer, 0, COMMS_MAX_SIZE_BUFFER); _type = PAYLOAD_TYPE::UNSET; }
+    void unset() { memset( _buffer, 0, PAYLOAD_MAX_SIZE_BUFFER); _type = PAYLOAD_TYPE::UNSET; _size = 0;}
 
     void setType(PAYLOAD_TYPE type) { _type = type; }
     PAYLOAD_TYPE getType() {return _type; }
@@ -72,10 +74,34 @@ public:
     void setSize(uint8_t size) { _size = size; }
     uint8_t getSize() { return _size; }
 
-    void setPayload(PAYLOAD_TYPE type, void* information, uint8_t size) {
+    bool setPayload(PAYLOAD_TYPE type, void* information, uint8_t size) {
+        if (information == nullptr) {
+            return false;
+        }
+
         setType(type);
         setSize(size);
         setInformation(information);
+
+        return true;
+    }
+
+    bool getPayload(void* information) {
+        PAYLOAD_TYPE type;
+        uint8_t size;
+        return getPayload(information, type, size);
+    }
+
+    bool getPayload(void* information, PAYLOAD_TYPE &type, uint8_t &size) {
+        if (information == nullptr) {
+            return false;
+        }
+
+        type = getType();
+        size = getSize();
+        memcpy(information, getInformation(), _size);
+
+        return true;
     }
 
     void setInformation(void* information) { memcpy(_buffer, information, _size); }
diff --git a/arduino/common/lib/CommsControl/CommsControl.cpp b/arduino/common/lib/CommsControl/CommsControl.cpp
index bb57819f..5a22a93a 100644
--- a/arduino/common/lib/CommsControl/CommsControl.cpp
+++ b/arduino/common/lib/CommsControl/CommsControl.cpp
@@ -85,9 +85,10 @@ void CommsControl::receiver() {
                             _sequence_receive = (*(_comms_tmp.getControl()) >> 1 ) & 0x7F;
                             // to decide ACK/NACK/other; for other gain sequenceReceive
                             uint8_t control = *(_comms_tmp.getControl() + 1);
+                            uint8_t address = *_comms_tmp.getAddress();
 
                             // to decide what kind of packets received
-                            PAYLOAD_TYPE type = getInfoType(_comms_tmp.getAddress());
+                            PAYLOAD_TYPE type = getInfoType(address);
 
                             // switch on received data to know what to do - received ACK/NACK or other
                             switch(control & COMMS_CONTROL_TYPES) {
@@ -101,14 +102,11 @@ void CommsControl::receiver() {
                                     finishPacket(type);
                                     break;
                                 default:
-                                    Serial.print("add: ");
                                     uint8_t sequence_receive = (control >> 1 ) & 0x7F;
                                     sequence_receive += 1;
-                                    uint8_t address = *_comms_tmp.getAddress();
-                                    // received DATA
+                                    // received INFORMATION
                                     if (receivePacket(type)) {
                                         _comms_ack.setAddress(&address);
-                                        Serial.println(*_comms_ack.getAddress());
                                         _comms_ack.setSequenceReceive(sequence_receive);
                                         sendPacket(_comms_ack);
                                     } else {
@@ -247,7 +245,7 @@ void CommsControl::resendPacket(RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING>
 // receiving anything of commsFormat
 bool CommsControl::receivePacket(PAYLOAD_TYPE &type) {
     _payload_tmp.unset();
-    _payload_tmp.setPayload(type, _comms_tmp.getInformation(), _comms_tmp.getInfoSize());
+    _payload_tmp.setPayload(type, reinterpret_cast<void *>(_comms_tmp.getInformation()), _comms_tmp.getInfoSize());
 
     // remove first entry if queue is full
     if (_ring_buff_received.isFull()) {
@@ -276,8 +274,8 @@ void CommsControl::finishPacket(PAYLOAD_TYPE &type) {
     }
 }
 
-PAYLOAD_TYPE CommsControl::getInfoType(uint8_t *address) {
-    switch (*address & PACKET_TYPE) {
+PAYLOAD_TYPE CommsControl::getInfoType(uint8_t &address) {
+    switch (address & PACKET_TYPE) {
         case PACKET_ALARM:
             return PAYLOAD_TYPE::ALARM;
         case PACKET_CMD:
diff --git a/arduino/common/lib/CommsControl/CommsControl.h b/arduino/common/lib/CommsControl/CommsControl.h
index 65fc62f1..13ebe690 100644
--- a/arduino/common/lib/CommsControl/CommsControl.h
+++ b/arduino/common/lib/CommsControl/CommsControl.h
@@ -27,7 +27,7 @@ public:
 
 private:
     RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *getQueue(PAYLOAD_TYPE &type);
-    PAYLOAD_TYPE getInfoType(uint8_t *address);
+    PAYLOAD_TYPE getInfoType(uint8_t &address);
 
     void sendQueue    (RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *queue);
     void resendPacket (RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *queue);
diff --git a/arduino/hev_prototype_v1/src/UILoop.cpp b/arduino/hev_prototype_v1/src/UILoop.cpp
index af5411ee..9f0f6cb8 100644
--- a/arduino/hev_prototype_v1/src/UILoop.cpp
+++ b/arduino/hev_prototype_v1/src/UILoop.cpp
@@ -10,9 +10,9 @@ UILoop::UILoop(BreathingLoop *bl, AlarmLoop *al)
 UILoop::~UILoop()
 {;}
 
-int UILoop::doCommand(cmd_format *cf)
+int UILoop::doCommand(cmd_format &cf)
 {
-    switch(cf->cmd_type) {
+    switch(cf.cmd_type) {
         case CMD_TYPE::GENERAL:
             cmdGeneral(cf);
             break;
@@ -34,31 +34,31 @@ int UILoop::doCommand(cmd_format *cf)
     return 0;
 }
 
-void UILoop::cmdGeneral(cmd_format *cf) {
-    switch (cf->cmd_code) {
+void UILoop::cmdGeneral(cmd_format &cf) {
+    switch (cf.cmd_code) {
         case CMD_GENERAL::START : _breathing_loop->doStart();
             break;
-        case CMD_GENERAL::STOP : _breathing_loop->doStop();
+        case CMD_GENERAL::STOP  : _breathing_loop->doStop();
             break;
         default:
             break;
     }
 }
 
-void UILoop::cmdSetDuration(cmd_format *cf) {
-    setDuration(static_cast<CMD_SET_DURATION>(cf->cmd_code), _breathing_loop->getDurations(), cf->param);
+void UILoop::cmdSetDuration(cmd_format &cf) {
+    setDuration(static_cast<CMD_SET_DURATION>(cf.cmd_code), _breathing_loop->getDurations(), cf.param);
 }
 
-void UILoop::cmdSetMode(cmd_format *cf) {
+void UILoop::cmdSetMode(cmd_format &cf) {
     ;
 }
 
-void UILoop::cmdSetThresholdMin(cmd_format *cf) {
-    setThreshold(static_cast<ALARM_CODES>(cf->cmd_code), _alarm_loop->getThresholdsMin(), cf->param);
+void UILoop::cmdSetThresholdMin(cmd_format &cf) {
+    setThreshold(static_cast<ALARM_CODES>(cf.cmd_code), _alarm_loop->getThresholdsMin(), cf.param);
 }
 
-void UILoop::cmdSetThresholdMax(cmd_format *cf) {
-    setThreshold(static_cast<ALARM_CODES>(cf->cmd_code), _alarm_loop->getThresholdsMax(), cf->param);
+void UILoop::cmdSetThresholdMax(cmd_format &cf) {
+    setThreshold(static_cast<ALARM_CODES>(cf.cmd_code), _alarm_loop->getThresholdsMax(), cf.param);
 }
 
 
diff --git a/arduino/hev_prototype_v1/src/UILoop.h b/arduino/hev_prototype_v1/src/UILoop.h
index 74549143..b25fb31f 100644
--- a/arduino/hev_prototype_v1/src/UILoop.h
+++ b/arduino/hev_prototype_v1/src/UILoop.h
@@ -13,13 +13,13 @@ class UILoop
 public:
     UILoop(BreathingLoop *bl, AlarmLoop *al);
     ~UILoop();
-    int doCommand(cmd_format *cf);
+    int doCommand(cmd_format &cf);
 private:
-    void cmdGeneral(cmd_format *cf);
-    void cmdSetDuration(cmd_format *cf);
-    void cmdSetMode(cmd_format *cf);
-    void cmdSetThresholdMin(cmd_format *cf);
-    void cmdSetThresholdMax(cmd_format *cf);
+    void cmdGeneral(cmd_format &cf);
+    void cmdSetDuration(cmd_format &cf);
+    void cmdSetMode(cmd_format &cf);
+    void cmdSetThresholdMin(cmd_format &cf);
+    void cmdSetThresholdMax(cmd_format &cf);
 
     BreathingLoop *_breathing_loop;
     AlarmLoop     *_alarm_loop    ;
diff --git a/arduino/hev_prototype_v1/src/main.cpp b/arduino/hev_prototype_v1/src/main.cpp
index 070d79dc..45e6f63a 100644
--- a/arduino/hev_prototype_v1/src/main.cpp
+++ b/arduino/hev_prototype_v1/src/main.cpp
@@ -15,6 +15,7 @@
 
 int ventilation_mode = HEV_MODE_PS;
 
+uint8_t prev_state = LOW;
 uint32_t report_timeout = 50; //ms
 uint32_t report_time = 0;
 
@@ -179,8 +180,8 @@ void loop()
       if (plReceive.getType() == PAYLOAD_TYPE::CMD) {
           // apply received cmd to ui loop
           cmd_format cmd;
-          memcpy(reinterpret_cast<void*>(&cmd), plReceive.getInformation(), plReceive.getSize());
-          ui_loop.doCommand(&cmd);
+          plReceive.getPayload(reinterpret_cast<void*>(&cmd));
+          ui_loop.doCommand(cmd);
       }
 
       // unset received type not to read it again
-- 
GitLab