Skip to content
Snippets Groups Projects
Commit 785dfca7 authored by Peter Švihra's avatar Peter Švihra
Browse files

bugfixes changes for payload modifications

- wrong data assignment
- removed printing
- improved data retrieving
parent a86c8ed1
Branches
No related merge requests found
...@@ -11,11 +11,11 @@ ...@@ -11,11 +11,11 @@
// UNO struggles with the RAM size for ring buffers // UNO struggles with the RAM size for ring buffers
#ifdef ARDUINO_AVR_UNO #ifdef ARDUINO_AVR_UNO
#define COMMS_MAX_SIZE_RB_RECEIVING 1 #define COMMS_MAX_SIZE_RB_RECEIVING 1
#define COMMS_MAX_SIZE_RB_SENDING 1 #define COMMS_MAX_SIZE_RB_SENDING 1
#else #else
#define COMMS_MAX_SIZE_RB_RECEIVING 10 #define COMMS_MAX_SIZE_RB_RECEIVING 10
#define COMMS_MAX_SIZE_RB_SENDING 5 #define COMMS_MAX_SIZE_RB_SENDING 5
#endif #endif
#define COMMS_MAX_SIZE_PACKET 64 #define COMMS_MAX_SIZE_PACKET 64
...@@ -55,16 +55,18 @@ public: ...@@ -55,16 +55,18 @@ public:
Payload(PAYLOAD_TYPE type = PAYLOAD_TYPE::UNSET) {_type = type; } Payload(PAYLOAD_TYPE type = PAYLOAD_TYPE::UNSET) {_type = type; }
Payload(const Payload &other) { Payload(const Payload &other) {
_type = other._type; _type = other._type;
memcpy(&_buffer, &other._buffer, sizeof (other._size)); _size = other._size;
memcpy(_buffer, other._buffer, other._size);
} }
Payload& operator=(const Payload& other) { Payload& operator=(const Payload& other) {
_type = other._type; _type = other._type;
memcpy(&_buffer, &other._buffer, sizeof (other._size)); _size = other._size;
memcpy(_buffer, other._buffer, other._size);
return *this; return *this;
} }
~Payload() { unset(); } ~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; } void setType(PAYLOAD_TYPE type) { _type = type; }
PAYLOAD_TYPE getType() {return _type; } PAYLOAD_TYPE getType() {return _type; }
...@@ -72,10 +74,34 @@ public: ...@@ -72,10 +74,34 @@ public:
void setSize(uint8_t size) { _size = size; } void setSize(uint8_t size) { _size = size; }
uint8_t getSize() { return _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); setType(type);
setSize(size); setSize(size);
setInformation(information); 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); } void setInformation(void* information) { memcpy(_buffer, information, _size); }
......
...@@ -85,9 +85,10 @@ void CommsControl::receiver() { ...@@ -85,9 +85,10 @@ void CommsControl::receiver() {
_sequence_receive = (*(_comms_tmp.getControl()) >> 1 ) & 0x7F; _sequence_receive = (*(_comms_tmp.getControl()) >> 1 ) & 0x7F;
// to decide ACK/NACK/other; for other gain sequenceReceive // to decide ACK/NACK/other; for other gain sequenceReceive
uint8_t control = *(_comms_tmp.getControl() + 1); uint8_t control = *(_comms_tmp.getControl() + 1);
uint8_t address = *_comms_tmp.getAddress();
// to decide what kind of packets received // 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 on received data to know what to do - received ACK/NACK or other
switch(control & COMMS_CONTROL_TYPES) { switch(control & COMMS_CONTROL_TYPES) {
...@@ -101,14 +102,11 @@ void CommsControl::receiver() { ...@@ -101,14 +102,11 @@ void CommsControl::receiver() {
finishPacket(type); finishPacket(type);
break; break;
default: default:
Serial.print("add: ");
uint8_t sequence_receive = (control >> 1 ) & 0x7F; uint8_t sequence_receive = (control >> 1 ) & 0x7F;
sequence_receive += 1; sequence_receive += 1;
uint8_t address = *_comms_tmp.getAddress(); // received INFORMATION
// received DATA
if (receivePacket(type)) { if (receivePacket(type)) {
_comms_ack.setAddress(&address); _comms_ack.setAddress(&address);
Serial.println(*_comms_ack.getAddress());
_comms_ack.setSequenceReceive(sequence_receive); _comms_ack.setSequenceReceive(sequence_receive);
sendPacket(_comms_ack); sendPacket(_comms_ack);
} else { } else {
...@@ -247,7 +245,7 @@ void CommsControl::resendPacket(RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> ...@@ -247,7 +245,7 @@ void CommsControl::resendPacket(RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING>
// receiving anything of commsFormat // receiving anything of commsFormat
bool CommsControl::receivePacket(PAYLOAD_TYPE &type) { bool CommsControl::receivePacket(PAYLOAD_TYPE &type) {
_payload_tmp.unset(); _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 // remove first entry if queue is full
if (_ring_buff_received.isFull()) { if (_ring_buff_received.isFull()) {
...@@ -276,8 +274,8 @@ void CommsControl::finishPacket(PAYLOAD_TYPE &type) { ...@@ -276,8 +274,8 @@ void CommsControl::finishPacket(PAYLOAD_TYPE &type) {
} }
} }
PAYLOAD_TYPE CommsControl::getInfoType(uint8_t *address) { PAYLOAD_TYPE CommsControl::getInfoType(uint8_t &address) {
switch (*address & PACKET_TYPE) { switch (address & PACKET_TYPE) {
case PACKET_ALARM: case PACKET_ALARM:
return PAYLOAD_TYPE::ALARM; return PAYLOAD_TYPE::ALARM;
case PACKET_CMD: case PACKET_CMD:
......
...@@ -27,7 +27,7 @@ public: ...@@ -27,7 +27,7 @@ public:
private: private:
RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *getQueue(PAYLOAD_TYPE &type); 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 sendQueue (RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *queue);
void resendPacket (RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *queue); void resendPacket (RingBuf<CommsFormat, COMMS_MAX_SIZE_RB_SENDING> *queue);
......
...@@ -10,9 +10,9 @@ UILoop::UILoop(BreathingLoop *bl, AlarmLoop *al) ...@@ -10,9 +10,9 @@ UILoop::UILoop(BreathingLoop *bl, AlarmLoop *al)
UILoop::~UILoop() 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: case CMD_TYPE::GENERAL:
cmdGeneral(cf); cmdGeneral(cf);
break; break;
...@@ -34,31 +34,31 @@ int UILoop::doCommand(cmd_format *cf) ...@@ -34,31 +34,31 @@ int UILoop::doCommand(cmd_format *cf)
return 0; return 0;
} }
void UILoop::cmdGeneral(cmd_format *cf) { void UILoop::cmdGeneral(cmd_format &cf) {
switch (cf->cmd_code) { switch (cf.cmd_code) {
case CMD_GENERAL::START : _breathing_loop->doStart(); case CMD_GENERAL::START : _breathing_loop->doStart();
break; break;
case CMD_GENERAL::STOP : _breathing_loop->doStop(); case CMD_GENERAL::STOP : _breathing_loop->doStop();
break; break;
default: default:
break; break;
} }
} }
void UILoop::cmdSetDuration(cmd_format *cf) { void UILoop::cmdSetDuration(cmd_format &cf) {
setDuration(static_cast<CMD_SET_DURATION>(cf->cmd_code), _breathing_loop->getDurations(), cf->param); 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) { void UILoop::cmdSetThresholdMin(cmd_format &cf) {
setThreshold(static_cast<ALARM_CODES>(cf->cmd_code), _alarm_loop->getThresholdsMin(), cf->param); setThreshold(static_cast<ALARM_CODES>(cf.cmd_code), _alarm_loop->getThresholdsMin(), cf.param);
} }
void UILoop::cmdSetThresholdMax(cmd_format *cf) { void UILoop::cmdSetThresholdMax(cmd_format &cf) {
setThreshold(static_cast<ALARM_CODES>(cf->cmd_code), _alarm_loop->getThresholdsMax(), cf->param); setThreshold(static_cast<ALARM_CODES>(cf.cmd_code), _alarm_loop->getThresholdsMax(), cf.param);
} }
...@@ -13,13 +13,13 @@ class UILoop ...@@ -13,13 +13,13 @@ class UILoop
public: public:
UILoop(BreathingLoop *bl, AlarmLoop *al); UILoop(BreathingLoop *bl, AlarmLoop *al);
~UILoop(); ~UILoop();
int doCommand(cmd_format *cf); int doCommand(cmd_format &cf);
private: private:
void cmdGeneral(cmd_format *cf); void cmdGeneral(cmd_format &cf);
void cmdSetDuration(cmd_format *cf); void cmdSetDuration(cmd_format &cf);
void cmdSetMode(cmd_format *cf); void cmdSetMode(cmd_format &cf);
void cmdSetThresholdMin(cmd_format *cf); void cmdSetThresholdMin(cmd_format &cf);
void cmdSetThresholdMax(cmd_format *cf); void cmdSetThresholdMax(cmd_format &cf);
BreathingLoop *_breathing_loop; BreathingLoop *_breathing_loop;
AlarmLoop *_alarm_loop ; AlarmLoop *_alarm_loop ;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
int ventilation_mode = HEV_MODE_PS; int ventilation_mode = HEV_MODE_PS;
uint8_t prev_state = LOW;
uint32_t report_timeout = 50; //ms uint32_t report_timeout = 50; //ms
uint32_t report_time = 0; uint32_t report_time = 0;
...@@ -179,8 +180,8 @@ void loop() ...@@ -179,8 +180,8 @@ void loop()
if (plReceive.getType() == PAYLOAD_TYPE::CMD) { if (plReceive.getType() == PAYLOAD_TYPE::CMD) {
// apply received cmd to ui loop // apply received cmd to ui loop
cmd_format cmd; cmd_format cmd;
memcpy(reinterpret_cast<void*>(&cmd), plReceive.getInformation(), plReceive.getSize()); plReceive.getPayload(reinterpret_cast<void*>(&cmd));
ui_loop.doCommand(&cmd); ui_loop.doCommand(cmd);
} }
// unset received type not to read it again // unset received type not to read it again
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment