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

This commit is contained in:
Adrien Marquès 2018-12-04 11:47:19 +01:00
parent cbe48ceebc
commit 9a7d7bb37c
3 changed files with 85 additions and 49 deletions

View File

@ -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);
}

View File

@ -3,7 +3,9 @@
#include <Wire.h>
#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

View File

@ -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());