opportunistic-xbee/well/main/main.ino

64 lines
1.3 KiB
Arduino
Raw Normal View History

2018-12-01 17:26:27 +00:00
#include <Wire.h>
#include "lcd.h"
#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
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;
unsigned long time;
2018-12-01 17:26:27 +00:00
void setup() {
Serial.begin(38400);
Serial.println("+ ready");
2018-12-01 17:26:27 +00:00
screen.begin();
screen.printfn(0, "[ well ready ]");
2018-12-01 17:26:27 +00:00
xbee.begin(38400);
time = millis();
2018-12-01 17:26:27 +00:00
}
void loop() {
/* [1] if DISCOVER_TTL reached -> send discover request */
if( millis() - time >= DISCOVER_TTL ){
time = millis();
// increment wave id (will overflow from 255 to 0)
2018-12-13 14:39:03 +00:00
Packet send;
send.setOpcode(OPCODE_DISCOVER);
send.setWave(++wave_id);
send.setDist(0);
screen.clear();
screen.printfn(0, "dsc[ %3d / %3d ]", send.getDist(), send.getWave());
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;
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
// log message
screen.clear();
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
}