2018-12-01 17:38:00 +00:00
|
|
|
#include <Wire.h>
|
|
|
|
#include <LiquidCrystal_I2C.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
|
|
|
|
|
|
|
// Peripherals
|
2018-12-03 14:29:56 +00:00
|
|
|
LiquidCrystal_I2C screen(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-03 09:54:53 +00:00
|
|
|
Packet recv;
|
|
|
|
Packet 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();
|
|
|
|
screen.backlight();
|
|
|
|
screen.home();
|
|
|
|
screen.setCursor(0, 0);
|
|
|
|
screen.print("ready");
|
|
|
|
|
|
|
|
xbee.begin(38400);
|
2018-12-01 17:38:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-02 20:45:06 +00:00
|
|
|
void loop() {
|
|
|
|
|
|
|
|
// 1. ignore if no received data
|
2018-12-03 10:24:32 +00:00
|
|
|
if( xbee.receive(recv) != XBWRECV_OK )
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t opcode = recv.getOpcode();
|
|
|
|
|
|
|
|
// DISCOVER REQUEST
|
|
|
|
if( opcode == 0 ) {
|
2018-12-03 14:29:56 +00:00
|
|
|
|
|
|
|
// security
|
|
|
|
if( recv.getDist() == 0 && recv.getSender() != 255 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
status(wave_id, dist);
|
|
|
|
screen.print("dsc");
|
|
|
|
screen.print("[");
|
|
|
|
screen.print(recv.getSender());
|
|
|
|
screen.print("] -> ");
|
|
|
|
screen.print(recv.getDist());
|
|
|
|
screen.print("/");
|
|
|
|
screen.print(recv.getWave());
|
2018-12-03 10:24:32 +00:00
|
|
|
|
|
|
|
if( recv.getWave() != wave_id ){
|
|
|
|
wave_id = recv.getWave();
|
|
|
|
dist = recv.getDist()+1;
|
2018-12-03 14:29:56 +00:00
|
|
|
|
|
|
|
status(wave_id, dist);
|
|
|
|
screen.print("propagated");
|
|
|
|
delay(100);
|
2018-12-03 10:24:32 +00:00
|
|
|
|
|
|
|
send.setOpcode(0);
|
|
|
|
send.setWave(wave_id);
|
|
|
|
send.setDist(dist);
|
|
|
|
send.setSender(42);
|
|
|
|
xbee.broadcast(send);
|
2018-12-03 14:29:56 +00:00
|
|
|
Serial.print(" + send discover[");
|
2018-12-03 10:24:32 +00:00
|
|
|
Serial.print(send.getSender());
|
2018-12-03 14:29:56 +00:00
|
|
|
Serial.print("] -> ");
|
2018-12-03 10:24:32 +00:00
|
|
|
Serial.print(send.getDist());
|
2018-12-03 14:29:56 +00:00
|
|
|
Serial.print(" / ");
|
|
|
|
Serial.println(send.getWave());
|
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 10:24:32 +00:00
|
|
|
// MESSAGE DATA
|
|
|
|
} else {
|
|
|
|
|
2018-12-03 14:29:56 +00:00
|
|
|
screen.clear();
|
|
|
|
screen.setCursor(0,0);
|
|
|
|
screen.print("message(");
|
|
|
|
screen.print(recv.getDist());
|
|
|
|
screen.print(",");
|
|
|
|
screen.print(recv.getTTL());
|
|
|
|
screen.print(",");
|
|
|
|
screen.print(recv.getSize());
|
|
|
|
screen.print(")");
|
|
|
|
screen.setCursor(0,1);
|
|
|
|
screen.print( (char*)recv.getData());
|
|
|
|
delay(600);
|
2018-12-03 10:24:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
// propagation
|
|
|
|
if( recv.getTTL() > 0 && recv.getDist() <= dist ){
|
|
|
|
send.setOpcode(1);
|
|
|
|
send.setTTL(recv.getTTL()-1);
|
|
|
|
send.setDist(dist);
|
|
|
|
send.setData(recv.getData(), recv.getSize());
|
|
|
|
xbee.broadcast(send);
|
|
|
|
Serial.print(" + send message(");
|
|
|
|
Serial.print(recv.getDist());
|
|
|
|
Serial.print(", ");
|
|
|
|
Serial.print(recv.getTTL());
|
|
|
|
Serial.print(", ");
|
|
|
|
Serial.print(recv.getSize());
|
|
|
|
Serial.println(")");
|
|
|
|
}
|
2018-12-03 09:54:53 +00:00
|
|
|
|
|
|
|
}
|
2018-12-02 20:45:06 +00:00
|
|
|
|
|
|
|
}
|
2018-12-03 14:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void status(uint8_t wave, uint8_t dist){
|
|
|
|
screen.clear();
|
|
|
|
screen.setCursor(0,0);
|
|
|
|
screen.print(dist);
|
|
|
|
screen.print(" / ");
|
|
|
|
screen.print(wave);
|
|
|
|
screen.setCursor(0,1);
|
|
|
|
}
|