From 9a7d7bb37c4c1f600dd218429e338c56014eb045 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 4 Dec 2018 11:47:19 +0100 Subject: [PATCH] make node routine#0 to be non-blocking before each MESSAGE_TIMEOUT | add node routine#1 to print and propagate/ignore discover requests and messages data --- node/main/main.ino | 122 +++++++++++++++++++++++++++++---------------- protocol.h | 4 +- well/main/main.ino | 8 ++- 3 files changed, 85 insertions(+), 49 deletions(-) diff --git a/node/main/main.ino b/node/main/main.ino index a371ae2..85fbfda 100644 --- a/node/main/main.ino +++ b/node/main/main.ino @@ -3,6 +3,8 @@ #include "packet.h" // Packet #include "xbee_wrapper.h" // WBeeWrapper +#define SEND_TIMEOUT 10000 + // Peripherals LCDWrapper screen = LCDWrapper(0x20, 16, 2); XBeeWrapper xbee = XBeeWrapper(); @@ -10,6 +12,7 @@ XBeeWrapper xbee = XBeeWrapper(); // ACTUAL DATA uint8_t wave_id = 255; // last wave uint8_t dist = 255; // actual distance +unsigned long time; Packet recv, send; void setup() { @@ -21,68 +24,101 @@ void setup() { xbee.begin(38400); + time = millis(); } void loop() { - // 1. ignore if no received data + /* [1] If SEND_TIMEOUT reached -> send data */ + if( millis() - time >= SEND_TIMEOUT ){ + time = millis(); + send_data(); + } + + + /* [2] Listen for incoming data */ if( xbee.receive(recv) != XBWRECV_OK ) return; - uint8_t opcode = recv.getOpcode(); - // DISCOVER REQUEST - if( opcode == 0 ) { + // 1. manage discover request + if( opcode == 0 ) + return manage_discover(); - // security - if( recv.getDist() == 0 && recv.getSender() != 255 ) - return; + // 2. manage message data + else if( opcode == 1 ) + return manage_message(); - screen.printfn(0, "dsc[%3d] %3d / %3d", recv.getSender(), recv.getDist(), recv.getWave()); - // ignore if same wave - if( recv.getWave() == wave_id ){ - screen.printfn(1, "x"); - return; - } +} - // update features - wave_id = recv.getWave(); - dist = recv.getDist()+1; +void send_data(){ + // 1. prepare message + send.setOpcode(1); + send.setDist(dist); + send.setTTL(MESSAGE_TTL); + uint8_t data[19] = {0}; + memcpy(data, "https://git.xdrm.io", 19); + send.setData(data, 19); + send.setSender(SENDERID); - // propagate wave - send.setOpcode(0); - send.setWave(wave_id); - send.setDist(dist); - send.setSender(42); + // 2. debug + screen.clear(); + screen.printfn(0, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize()); + screen.printfn_overflow(1, "%s", send.getData()); - screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave()); - xbee.broadcast(send); + // 3. send + xbee.broadcast(send); + screen.printfn(1, "sent "); +} - // MESSAGE DATA - } else { - screen.clear(); - screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize()); - delay(500); - screen.printfn_overflow(0, "%s", recv.getData()); +void manage_discover(){ + // security + if( recv.getDist() == 0 && recv.getSender() != 255 ) + return; - // ignore - if( recv.getTTL() <= 0 || recv.getDist() > dist ){ - screen.printfn(1, "x"); - return; - } - - // propagation - send.setOpcode(1); - send.setTTL(recv.getTTL()-1); - send.setDist(dist); - send.setData(recv.getData(), recv.getSize()); - - screen.printfn(1, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize()); - xbee.broadcast(send); + screen.printfn(0, "dsc[%3d] %3d / %3d", recv.getSender(), recv.getDist(), recv.getWave()); + // ignore if same wave + if( recv.getWave() == wave_id ){ + screen.printfn(1, "x"); + return; } + // update features + wave_id = recv.getWave(); + dist = recv.getDist()+1; + + // propagate wave + send.setOpcode(0); + send.setWave(wave_id); + send.setDist(dist); + send.setSender(SENDERID); + + screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave()); + xbee.broadcast(send); +} + +void manage_message(){ + screen.clear(); + screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize()); + delay(500); + screen.printfn_overflow(0, "%s", recv.getData()); + + // ignore + if( recv.getTTL() <= 0 || recv.getDist() > dist ){ + screen.printfn(1, "x"); + return; + } + + // propagation + send.setOpcode(1); + send.setTTL(recv.getTTL()-1); + send.setDist(dist); + send.setData(recv.getData(), recv.getSize()); + + screen.printfn(1, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize()); + xbee.broadcast(send); } \ No newline at end of file diff --git a/protocol.h b/protocol.h index 7713558..8669eaa 100644 --- a/protocol.h +++ b/protocol.h @@ -3,7 +3,9 @@ #include - #define DISCOVER_TTL 10000 + #define DISCOVER_TTL 5000 + #define SENDERID 42 + #define MESSAGE_TTL 10 // discover request (c.f. class node) #define DISCOVER_SIZE sizeof(uint8_t)*4 diff --git a/well/main/main.ino b/well/main/main.ino index d318e75..88c060b 100644 --- a/well/main/main.ino +++ b/well/main/main.ino @@ -3,8 +3,6 @@ #include "xbee_wrapper.h" #include "packet.h" -#define WAVE_TIMEOUT 5000 - // Peripherals LCDWrapper screen(0x20, 16, 2); XBeeWrapper xbee = XBeeWrapper(); @@ -28,15 +26,15 @@ void setup() { void loop() { - /* [1] if WAVE_TIMEOUT reached */ - if( millis() - time >= WAVE_TIMEOUT ){ + /* [1] if DISCOVER_TTL reached -> send discover request */ + if( millis() - time >= DISCOVER_TTL ){ time = millis(); // increment wave id (will overflow from 255 to 0) send.setOpcode(0); send.setWave(++wave_id); send.setDist(0); - send.setSender(42); + send.setSender(SENDERID); screen.clear(); screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave());