2018-12-01 17:26:27 +00:00
|
|
|
#include <Wire.h>
|
2018-12-04 08:43:11 +00:00
|
|
|
#include "lcd.h"
|
2018-12-03 09:54:53 +00:00
|
|
|
#include "xbee_wrapper.h"
|
|
|
|
#include "packet.h"
|
2018-12-01 17:26:27 +00:00
|
|
|
|
2018-12-13 14:39:03 +00:00
|
|
|
//Setup
|
|
|
|
Node myself = Node();
|
|
|
|
|
2018-12-01 17:26:27 +00:00
|
|
|
// Peripherals
|
2018-12-04 08:43:11 +00:00
|
|
|
LCDWrapper screen(0x20, 16, 2);
|
2018-12-13 14:39:03 +00:00
|
|
|
XBeeWrapper xbee = XBeeWrapper(myself);
|
2018-12-01 17:26:27 +00:00
|
|
|
|
|
|
|
// ACTUAL DATA
|
|
|
|
uint8_t wave_id = 250;
|
2018-12-04 08:43:11 +00:00
|
|
|
unsigned long time;
|
2018-12-01 17:26:27 +00:00
|
|
|
|
|
|
|
void setup() {
|
2018-12-03 09:54:53 +00:00
|
|
|
Serial.begin(38400);
|
|
|
|
Serial.println("+ ready");
|
2018-12-01 17:26:27 +00:00
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
screen.begin();
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn(0, "[ well ready ]");
|
2018-12-01 17:26:27 +00:00
|
|
|
|
2018-12-03 09:54:53 +00:00
|
|
|
xbee.begin(38400);
|
2018-12-04 08:43:11 +00:00
|
|
|
|
|
|
|
time = millis();
|
2018-12-01 17:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
2018-12-04 10:47:19 +00:00
|
|
|
/* [1] if DISCOVER_TTL reached -> send discover request */
|
|
|
|
if( millis() - time >= DISCOVER_TTL ){
|
2018-12-04 08:43:11 +00:00
|
|
|
time = millis();
|
|
|
|
|
|
|
|
// increment wave id (will overflow from 255 to 0)
|
2018-12-13 14:39:03 +00:00
|
|
|
Packet send;
|
2018-12-11 11:42:34 +00:00
|
|
|
send.setOpcode(OPCODE_DISCOVER);
|
2018-12-04 08:43:11 +00:00
|
|
|
send.setWave(++wave_id);
|
|
|
|
send.setDist(0);
|
|
|
|
|
|
|
|
screen.clear();
|
2018-12-11 11:42:34 +00:00
|
|
|
screen.printfn(0, "dsc[ %3d / %3d ]", send.getDist(), send.getWave());
|
2018-12-04 08:43:11 +00:00
|
|
|
|
|
|
|
if( xbee.broadcast(send) == XBWSEND_OK ) screen.printfn(1, "sent");
|
|
|
|
else screen.printfn(1, "failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] Listen for incoming data */
|
2018-12-13 14:39:03 +00:00
|
|
|
Packet recv;
|
2018-12-04 08:43:11 +00:00
|
|
|
if( xbee.receive(recv) != XBWRECV_OK )
|
|
|
|
return;
|
|
|
|
uint8_t opcode = recv.getOpcode();
|
|
|
|
|
|
|
|
// ignore DISCOVER requests
|
|
|
|
if( opcode == 0 )
|
|
|
|
return;
|
2018-12-01 17:26:27 +00:00
|
|
|
|
2018-12-04 08:43:11 +00:00
|
|
|
// log message
|
2018-12-03 09:54:53 +00:00
|
|
|
screen.clear();
|
2018-12-04 08:43:11 +00:00
|
|
|
screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
|
2018-12-11 12:34:17 +00:00
|
|
|
screen.printfn_overflow(1, "%s", (char*) recv.getData());
|
2018-12-01 17:26:27 +00:00
|
|
|
|
|
|
|
}
|