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 "packet.h" // Packet
#include "xbee_wrapper.h" // WBeeWrapper #include "xbee_wrapper.h" // WBeeWrapper
#define SEND_TIMEOUT 10000
// Peripherals // Peripherals
LCDWrapper screen = LCDWrapper(0x20, 16, 2); LCDWrapper screen = LCDWrapper(0x20, 16, 2);
XBeeWrapper xbee = XBeeWrapper(); XBeeWrapper xbee = XBeeWrapper();
@ -10,6 +12,7 @@ XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA // ACTUAL DATA
uint8_t wave_id = 255; // last wave uint8_t wave_id = 255; // last wave
uint8_t dist = 255; // actual distance uint8_t dist = 255; // actual distance
unsigned long time;
Packet recv, send; Packet recv, send;
void setup() { void setup() {
@ -21,20 +24,57 @@ void setup() {
xbee.begin(38400); xbee.begin(38400);
time = millis();
} }
void loop() { 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 ) if( xbee.receive(recv) != XBWRECV_OK )
return; return;
uint8_t opcode = recv.getOpcode(); uint8_t opcode = recv.getOpcode();
// DISCOVER REQUEST // 1. manage discover request
if( opcode == 0 ) { if( opcode == 0 )
return manage_discover();
// 2. manage message data
else if( opcode == 1 )
return manage_message();
}
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);
// 2. debug
screen.clear();
screen.printfn(0, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize());
screen.printfn_overflow(1, "%s", send.getData());
// 3. send
xbee.broadcast(send);
screen.printfn(1, "sent ");
}
void manage_discover(){
// security // security
if( recv.getDist() == 0 && recv.getSender() != 255 ) if( recv.getDist() == 0 && recv.getSender() != 255 )
return; return;
@ -55,14 +95,13 @@ void loop() {
send.setOpcode(0); send.setOpcode(0);
send.setWave(wave_id); send.setWave(wave_id);
send.setDist(dist); send.setDist(dist);
send.setSender(42); send.setSender(SENDERID);
screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave()); screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave());
xbee.broadcast(send); xbee.broadcast(send);
}
// MESSAGE DATA void manage_message(){
} else {
screen.clear(); screen.clear();
screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize()); screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
delay(500); delay(500);
@ -82,7 +121,4 @@ void loop() {
screen.printfn(1, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize()); screen.printfn(1, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize());
xbee.broadcast(send); xbee.broadcast(send);
}
} }

View File

@ -3,7 +3,9 @@
#include <Wire.h> #include <Wire.h>
#define DISCOVER_TTL 10000 #define DISCOVER_TTL 5000
#define SENDERID 42
#define MESSAGE_TTL 10
// discover request (c.f. class node) // discover request (c.f. class node)
#define DISCOVER_SIZE sizeof(uint8_t)*4 #define DISCOVER_SIZE sizeof(uint8_t)*4

View File

@ -3,8 +3,6 @@
#include "xbee_wrapper.h" #include "xbee_wrapper.h"
#include "packet.h" #include "packet.h"
#define WAVE_TIMEOUT 5000
// Peripherals // Peripherals
LCDWrapper screen(0x20, 16, 2); LCDWrapper screen(0x20, 16, 2);
XBeeWrapper xbee = XBeeWrapper(); XBeeWrapper xbee = XBeeWrapper();
@ -28,15 +26,15 @@ void setup() {
void loop() { void loop() {
/* [1] if WAVE_TIMEOUT reached */ /* [1] if DISCOVER_TTL reached -> send discover request */
if( millis() - time >= WAVE_TIMEOUT ){ if( millis() - time >= DISCOVER_TTL ){
time = millis(); time = millis();
// increment wave id (will overflow from 255 to 0) // increment wave id (will overflow from 255 to 0)
send.setOpcode(0); send.setOpcode(0);
send.setWave(++wave_id); send.setWave(++wave_id);
send.setDist(0); send.setDist(0);
send.setSender(42); send.setSender(SENDERID);
screen.clear(); screen.clear();
screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave()); screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave());