From d1ecf570dbb479370d6d367cf5fdd7290b0164a4 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 11 Dec 2018 14:45:23 +0100 Subject: [PATCH] update protocol (add Node object) | update node.ino accordingly | fix packet (construct/destruct) + *.ino --- node/main/main.ino | 33 ++++++++++++++++++--------------- packet.cpp | 3 +++ packet.h | 1 + protocol.h | 26 ++++++++++++++++++++++++++ well/main/main.ino | 3 ++- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/node/main/main.ino b/node/main/main.ino index b23b2ae..3ad3ba8 100644 --- a/node/main/main.ino +++ b/node/main/main.ino @@ -2,6 +2,7 @@ #include "lcd.h" #include "packet.h" // Packet #include "xbee_wrapper.h" // WBeeWrapper +#include "protocol.h" #define SEND_TIMEOUT 10000 @@ -10,10 +11,10 @@ LCDWrapper screen(0x20, 16, 2); XBeeWrapper xbee = XBeeWrapper(); // ACTUAL DATA -uint8_t wave_id = 255; // last wave -uint8_t dist = 255; // actual distance unsigned long time; -Packet recv, send; +Node myself = Node(); +Packet recv = Packet(); +Packet send = Packet(); void setup() { Serial.begin(38400); @@ -26,6 +27,10 @@ void setup() { time = millis(); } +void send_data(); +void manage_discover(); +void manage_message(); + void loop() { /* [1] If SEND_TIMEOUT reached -> send data */ @@ -56,10 +61,10 @@ void send_data(){ // 1. prepare message send.setOpcode(OPCODE_MESSAGE); send.setSender(SENDERID); - send.setDist(dist); + send.setDist(myself.getDist()); send.setTTL(MESSAGE_TTL); char data[64] = {0}; - sprintf(data, "%d has: dist %d / wave %d", SENDERID, dist, wave_id); + sprintf(data, "%d has: dist %d / wave %d", SENDERID, myself.getDist(), myself.getWave()); send.setData(data); // 2. broadcast @@ -83,20 +88,16 @@ void manage_discover(){ screen.clear(); screen.printfn(0, "dsc[ %3d / %3d ]", recv.getDist(), recv.getWave()); - // ignore if same wave - if( recv.getWave() == wave_id ){ + // update features + if( !myself.update(recv.getWave(), recv.getDist()) ){ screen.printfn(1, "x "); return; } - // update features - wave_id = recv.getWave(); - dist = recv.getDist()+1; - // propagate wave send.setOpcode(OPCODE_DISCOVER); - send.setWave(wave_id); - send.setDist(dist); + send.setWave(myself.getWave()); + send.setDist(myself.getDist()); screen.printfn(1, "dsc[ %3d / %3d ]", send.getDist(), send.getWave()); xbee.broadcast(send); @@ -122,7 +123,7 @@ void manage_message(){ delay(1000); // ignore - if( recv.getDist() > dist ){ + if( recv.getDist() > myself.getDist() ){ screen.printfn(1, "x "); return; } @@ -130,9 +131,11 @@ void manage_message(){ // propagation // send.setOpcode(OPCODE_MESSAGE); recv.setTTL(recv.getTTL()-1); - recv.setDist(dist); + recv.setDist(myself.getDist()); // send.setData(recv.getData()); screen.printfn(1, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize()); + delay(500); + screen.printfn(1, "%s", (char*) recv.getData()); xbee.broadcast(recv); } diff --git a/packet.cpp b/packet.cpp index 33cf332..fc22a52 100644 --- a/packet.cpp +++ b/packet.cpp @@ -3,6 +3,9 @@ Packet::Packet(){ msg.data = malloc(1 * sizeof(char)); } +Packet::~Packet(){ + free(msg.data); +} /* PUBLIC ----------------------------------------*/ // builds a packet from raw data and returns the error code diff --git a/packet.h b/packet.h index 3ca01e6..a71bcc8 100644 --- a/packet.h +++ b/packet.h @@ -28,6 +28,7 @@ public: Packet(); + ~Packet(); // builds a packet from raw data and returns the status code uint8_t read(uint8_t* buf, const size_t size); diff --git a/protocol.h b/protocol.h index b72c7c0..653f8c5 100644 --- a/protocol.h +++ b/protocol.h @@ -32,4 +32,30 @@ #define PROTO_SIZE MESSAGE_MAX_SIZE + + class Node{ + private: + uint8_t wave = 0; + uint8_t dist = 255; + + public: + getWave(){ return wave; } + getDist(){ return dist; } + + bool update(uint8_t new_dist, uint8_t new_wave){ + // ignore if already setup for this wave + if( new_wave == wave ) + return false; + + // update with new wave + // + // Note: + // - We consider that we will receive lowest dist(ance) + // in the first receive of each wave because of the XBee stack + wave = new_wave; + dist = new_dist+1; + return true; + } + }; + #endif \ No newline at end of file diff --git a/well/main/main.ino b/well/main/main.ino index 0394961..65cea59 100644 --- a/well/main/main.ino +++ b/well/main/main.ino @@ -9,7 +9,8 @@ XBeeWrapper xbee = XBeeWrapper(); // ACTUAL DATA uint8_t wave_id = 250; -Packet recv, send; +Packet recv = Packet(); +Packet send = Packet(); unsigned long time; void setup() {