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-11 13:45:23 +00:00
|
|
|
#include "protocol.h"
|
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
|
|
|
// ACTUAL DATA
|
2018-12-04 10:47:19 +00:00
|
|
|
unsigned long time;
|
2018-12-11 13:45:23 +00:00
|
|
|
Node myself = Node();
|
2018-12-01 17:38:00 +00:00
|
|
|
|
2018-12-13 14:39:03 +00:00
|
|
|
// Peripherals
|
|
|
|
LCDWrapper screen(0x20, 16, 2);
|
|
|
|
XBeeWrapper xbee = XBeeWrapper(myself);
|
|
|
|
|
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-11 11:42:34 +00:00
|
|
|
screen.printfn(0, "[ node ready ]");
|
2018-12-03 14:29:56 +00:00
|
|
|
|
|
|
|
xbee.begin(38400);
|
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-13 13:23:23 +00:00
|
|
|
Packet recv;
|
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
|
2018-12-11 11:42:34 +00:00
|
|
|
if( opcode == OPCODE_DISCOVER )
|
2018-12-13 13:23:23 +00:00
|
|
|
return manage_discover(recv);
|
2018-12-04 10:47:19 +00:00
|
|
|
|
|
|
|
// 2. manage message data
|
2018-12-11 11:42:34 +00:00
|
|
|
else if( opcode == OPCODE_MESSAGE )
|
2018-12-13 13:23:23 +00:00
|
|
|
return manage_message(recv);
|
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(){
|
2018-12-09 14:21:15 +00:00
|
|
|
Serial.println(" -> message");
|
2018-12-04 10:47:19 +00:00
|
|
|
// 1. prepare message
|
2018-12-13 13:23:23 +00:00
|
|
|
Packet send;
|
2018-12-11 11:42:34 +00:00
|
|
|
send.setOpcode(OPCODE_MESSAGE);
|
|
|
|
send.setSender(SENDERID);
|
2018-12-11 13:45:23 +00:00
|
|
|
send.setDist(myself.getDist());
|
2018-12-04 10:47:19 +00:00
|
|
|
send.setTTL(MESSAGE_TTL);
|
2018-12-11 11:42:34 +00:00
|
|
|
char data[64] = {0};
|
2018-12-11 13:45:23 +00:00
|
|
|
sprintf(data, "%d has: dist %d / wave %d", SENDERID, myself.getDist(), myself.getWave());
|
2018-12-11 11:42:34 +00:00
|
|
|
send.setData(data);
|
2018-12-04 10:47:19 +00:00
|
|
|
|
2018-12-11 12:33:51 +00:00
|
|
|
// 2. broadcast
|
2018-12-13 14:39:03 +00:00
|
|
|
xbee.sendToNearest(send);
|
2018-12-11 12:33:51 +00:00
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
// 2. debug
|
|
|
|
screen.clear();
|
|
|
|
screen.printfn(0, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize());
|
|
|
|
screen.printfn_overflow(1, "%s", (char*) send.getData());
|
|
|
|
delay(500);
|
2018-12-04 10:47:19 +00:00
|
|
|
}
|
2018-12-03 09:54:53 +00:00
|
|
|
|
2018-12-04 08:43:11 +00:00
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
void manage_discover(Packet recv){
|
2018-12-11 11:42:34 +00:00
|
|
|
Serial.print(" <- discover[ ");
|
|
|
|
Serial.print(recv.getDist());
|
|
|
|
Serial.print(" / ");
|
|
|
|
Serial.print(recv.getWave());
|
|
|
|
Serial.println(" ]");
|
2018-12-04 08:43:11 +00:00
|
|
|
|
2018-12-09 14:21:15 +00:00
|
|
|
screen.clear();
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn(0, "dsc[ %3d / %3d ]", recv.getDist(), recv.getWave());
|
2018-12-04 08:43:11 +00:00
|
|
|
|
2018-12-11 13:45:23 +00:00
|
|
|
// update features
|
2018-12-11 13:48:07 +00:00
|
|
|
if( !myself.update(recv.getDist(), recv.getWave()) ){
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn(1, "x ");
|
2018-12-04 10:47:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-12-03 10:24:32 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// propagate wave
|
2018-12-13 13:23:23 +00:00
|
|
|
Packet send;
|
2018-12-11 11:42:34 +00:00
|
|
|
send.setOpcode(OPCODE_DISCOVER);
|
2018-12-11 13:45:23 +00:00
|
|
|
send.setWave(myself.getWave());
|
|
|
|
send.setDist(myself.getDist());
|
2018-12-03 09:54:53 +00:00
|
|
|
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn(1, "dsc[ %3d / %3d ]", send.getDist(), send.getWave());
|
2018-12-04 10:47:19 +00:00
|
|
|
xbee.broadcast(send);
|
|
|
|
}
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-13 13:23:23 +00:00
|
|
|
void manage_message(Packet recv){
|
2018-12-11 11:42:34 +00:00
|
|
|
if( recv.getTTL() <= 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Serial.print(" <- message[");
|
|
|
|
Serial.print(recv.getDist());
|
|
|
|
Serial.print("/");
|
|
|
|
Serial.print(recv.getTTL());
|
|
|
|
Serial.print("] ");
|
|
|
|
Serial.print(recv.getSize());
|
|
|
|
Serial.print(" '");
|
|
|
|
Serial.print((char*) recv.getData());
|
|
|
|
Serial.println("'");
|
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
screen.clear();
|
|
|
|
screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn_overflow(1, "%s", (char*) recv.getData());
|
|
|
|
delay(1000);
|
2018-12-03 14:29:56 +00:00
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
// ignore
|
2018-12-11 13:45:23 +00:00
|
|
|
if( recv.getDist() > myself.getDist() ){
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn(1, "x ");
|
2018-12-04 10:47:19 +00:00
|
|
|
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
|
2018-12-11 12:33:51 +00:00
|
|
|
recv.setTTL(recv.getTTL()-1);
|
2018-12-11 13:45:23 +00:00
|
|
|
recv.setDist(myself.getDist());
|
2018-12-04 10:47:19 +00:00
|
|
|
|
2018-12-11 12:33:51 +00:00
|
|
|
screen.printfn(1, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
|
2018-12-11 13:45:23 +00:00
|
|
|
delay(500);
|
|
|
|
screen.printfn(1, "%s", (char*) recv.getData());
|
2018-12-13 14:39:03 +00:00
|
|
|
xbee.sendToNearest(recv);
|
2018-12-09 14:21:15 +00:00
|
|
|
}
|