2018-12-01 17:38:00 +00:00
|
|
|
#include <Wire.h>
|
2018-12-04 08:43:11 +00:00
|
|
|
#include "lcd.h"
|
2018-12-02 20:45:06 +00:00
|
|
|
#include "packet.h" // Packet
|
|
|
|
#include "xbee_wrapper.h" // WBeeWrapper
|
2018-12-01 17:38:00 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
#define SEND_TIMEOUT 10000
|
|
|
|
|
2018-12-01 17:38:00 +00:00
|
|
|
// Peripherals
|
2018-12-04 08:43:11 +00:00
|
|
|
LCDWrapper screen = LCDWrapper(0x20, 16, 2);
|
2018-12-02 20:45:06 +00:00
|
|
|
XBeeWrapper xbee = XBeeWrapper();
|
2018-12-01 17:38:00 +00:00
|
|
|
|
|
|
|
// ACTUAL DATA
|
|
|
|
uint8_t wave_id = 255; // last wave
|
|
|
|
uint8_t dist = 255; // actual distance
|
2018-12-04 10:47:19 +00:00
|
|
|
unsigned long time;
|
2018-12-04 08:43:11 +00:00
|
|
|
Packet recv, send;
|
2018-12-01 17:38:00 +00:00
|
|
|
|
|
|
|
void setup() {
|
2018-12-03 14:29:56 +00:00
|
|
|
Serial.begin(38400);
|
|
|
|
Serial.println("+ ready");
|
2018-12-01 17:38:00 +00:00
|
|
|
|
2018-12-03 14:29:56 +00:00
|
|
|
screen.begin();
|
2018-12-04 08:43:11 +00:00
|
|
|
screen.printf("[ node ready ]");
|
2018-12-03 14:29:56 +00:00
|
|
|
|
|
|
|
xbee.begin(38400);
|
2018-12-01 17:38:00 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
time = millis();
|
2018-12-01 17:38:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 20:45:06 +00:00
|
|
|
void loop() {
|
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
/* [1] If SEND_TIMEOUT reached -> send data */
|
|
|
|
if( millis() - time >= SEND_TIMEOUT ){
|
|
|
|
time = millis();
|
|
|
|
send_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Listen for incoming data */
|
2018-12-03 10:24:32 +00:00
|
|
|
if( xbee.receive(recv) != XBWRECV_OK )
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t opcode = recv.getOpcode();
|
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// 1. manage discover request
|
|
|
|
if( opcode == 0 )
|
|
|
|
return manage_discover();
|
|
|
|
|
|
|
|
// 2. manage message data
|
|
|
|
else if( opcode == 1 )
|
|
|
|
return manage_message();
|
2018-12-03 14:29:56 +00:00
|
|
|
|
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
}
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
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 ");
|
|
|
|
}
|
2018-12-03 09:54:53 +00:00
|
|
|
|
2018-12-04 08:43:11 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
void manage_discover(){
|
|
|
|
// security
|
|
|
|
if( recv.getDist() == 0 && recv.getSender() != 255 )
|
|
|
|
return;
|
2018-12-04 08:43:11 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
screen.printfn(0, "dsc[%3d] %3d / %3d", recv.getSender(), recv.getDist(), recv.getWave());
|
2018-12-04 08:43:11 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// ignore if same wave
|
|
|
|
if( recv.getWave() == wave_id ){
|
|
|
|
screen.printfn(1, "x");
|
|
|
|
return;
|
|
|
|
}
|
2018-12-03 10:24:32 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// update features
|
|
|
|
wave_id = recv.getWave();
|
|
|
|
dist = recv.getDist()+1;
|
2018-12-03 10:24:32 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// propagate wave
|
|
|
|
send.setOpcode(0);
|
|
|
|
send.setWave(wave_id);
|
|
|
|
send.setDist(dist);
|
|
|
|
send.setSender(SENDERID);
|
2018-12-03 09:54:53 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave());
|
|
|
|
xbee.broadcast(send);
|
|
|
|
}
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
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());
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// ignore
|
|
|
|
if( recv.getTTL() <= 0 || recv.getDist() > dist ){
|
|
|
|
screen.printfn(1, "x");
|
|
|
|
return;
|
2018-12-04 08:43:11 +00:00
|
|
|
}
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// 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);
|
2018-12-03 14:29:56 +00:00
|
|
|
}
|