Skip to content

Commit

Permalink
Refactored to single static object (DW1000) + infrastructure for call…
Browse files Browse the repository at this point in the history
…back handlers
  • Loading branch information
thotro committed May 27, 2015
1 parent b2c9234 commit ec43df3
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,29 @@

// reset line to the chip
int RST = 9;
// chip driver instances with chip select and reset
DW1000 dw = DW1000(SS, RST);

void setup() {
// DEBUG monitoring
Serial.begin(9600);
// initialize the driver
dw.initialize();
DW1000.begin();
DW1000.init(SS, RST, 0);
Serial.println("DW1000 initialized ...");
// general configuration
dw.newConfiguration();
dw.setDeviceAddress(5);
dw.setNetworkId(10);
dw.commitConfiguration();
DW1000.newConfiguration();
DW1000.setDeviceAddress(5);
DW1000.setNetworkId(10);
DW1000.commitConfiguration();
Serial.println("Committed configuration ...");
}

void loop() {
// wait a bit
delay(1000);
// DEBUG chip info and registers pretty printed
Serial.print("Device ID: "); Serial.println(dw.getPrintableDeviceIdentifier());
Serial.print("Unique ID: "); Serial.println(dw.getPrintableExtendedUniqueIdentifier());
Serial.print("Network ID & Device Address: "); Serial.println(dw.getPrintableNetworkIdAndShortAddress());
// DEBUG print device tuning results
Serial.println(dw.getPrettyBytes(AGC_TUNE, AGC_TUNE1_SUB, LEN_AGC_TUNE1));
Serial.println(dw.getPrettyBytes(AGC_TUNE, AGC_TUNE2_SUB, LEN_AGC_TUNE2));
Serial.print("Device ID: "); Serial.println(DW1000.getPrintableDeviceIdentifier());
Serial.print("Unique ID: "); Serial.println(DW1000.getPrintableExtendedUniqueIdentifier());
Serial.print("Network ID & Device Address: "); Serial.println(DW1000.getPrintableNetworkIdAndShortAddress());
// wait a bit
delay(10000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,57 @@
// DEBUG packet sent status and count
volatile boolean received = false;
volatile int numReceived = 0;
String message;
// reset line to the chip
int RST = 9;
// chip driver instances with chip select and reset
DW1000 dw = DW1000(SS, RST);

void setup() {
// DEBUG monitoring
Serial.begin(9600);
Serial.println("### DW1000-arduino-receiver-test ###");
// initialize the driver
dw.initialize();
DW1000.begin();
DW1000.init(SS, RST, 0);
Serial.println("DW1000 initialized ...");
// general configuration
dw.newConfiguration();
dw.setDefaults();
dw.setDeviceAddress(6);
dw.setNetworkId(10);
dw.setFrameFilter(false);
dw.interruptOnReceived(true);
dw.commitConfiguration();
DW1000.newConfiguration();
DW1000.setDefaults();
DW1000.setDeviceAddress(6);
DW1000.setNetworkId(10);
DW1000.setFrameFilter(false);
DW1000.commitConfiguration();
Serial.println("Committed configuration ...");
// DEBUG chip info and registers pretty printed
Serial.print("Device ID: "); Serial.println(dw.getPrintableDeviceIdentifier());
Serial.print("Unique ID: "); Serial.println(dw.getPrintableExtendedUniqueIdentifier());
Serial.print("Network ID & Device Address: "); Serial.println(dw.getPrintableNetworkIdAndShortAddress());
Serial.println(dw.getPrettyBytes(SYS_CFG, NO_SUB, LEN_SYS_CFG));
Serial.println(dw.getPrettyBytes(PANADR, NO_SUB, LEN_PANADR));
Serial.println(dw.getPrettyBytes(SYS_MASK, NO_SUB, LEN_SYS_MASK));
// attach interrupt and ISR
pinMode(INT0, INPUT);
attachInterrupt(0, serviceIRQ, FALLING);
Serial.println("Interrupt attached ...");
// configure as permanent receiver
dw.newReceive();
dw.setDefaults();
dw.startReceive();
Serial.print("Device ID: "); Serial.println(DW1000.getPrintableDeviceIdentifier());
Serial.print("Unique ID: "); Serial.println(DW1000.getPrintableExtendedUniqueIdentifier());
Serial.print("Network ID & Device Address: "); Serial.println(DW1000.getPrintableNetworkIdAndShortAddress());
// attach callback for (successfully) received messages
DW1000.attachReceivedHandler(handleReceived);
// start reception
receiver();
}

void serviceIRQ() {
if(received) {
return;
}
// "NOP" ISR
void handleReceived() {
// status change on reception success
received = true;
numReceived++;
// get data as string
DW1000.getData(message);
}

void receiver() {
DW1000.newReceive();
DW1000.setDefaults();
DW1000.startReceive();
}

void loop() {
// enter on confirmation of ISR status change (successfully received)
if(received) {
numReceived++;
Serial.print("Received packet ... #"); Serial.println(numReceived);
Serial.print("Bytes available ... "); Serial.println(dw.getDataLength());
// get data as String
String recv;
dw.getData(recv);
Serial.print("Data is ... "); Serial.println(recv);

Serial.print("Data is ... "); Serial.println(message);
// restart the receiver
received = false;
dw.newReceive();
dw.setDefaults();
dw.startReceive();
receiver();
}
//Serial.println(dw.getPrettyBytes(SYS_STATUS, NO_SUB, LEN_SYS_STATUS));
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,73 @@
#include <DW1000.h>

// DEBUG packet sent status and count
volatile boolean sent = false;
boolean sent = false;
volatile boolean sentAck = false;
volatile unsigned long delaySent = 0;
int sentNum = 0;
unsigned long sentTime = 0;
// reset line to the chip
int RST = 9;
// chip driver instances with chip select and reset
DW1000 dw = DW1000(SS, RST);

void setup() {
// DEBUG monitoring
Serial.begin(9600);
Serial.println("### DW1000-arduino-sender-test ###");
// initialize the driver
dw.initialize();
DW1000.begin();
DW1000.init(SS, RST, 0);
Serial.println("DW1000 initialized ...");
// general configuration
dw.newConfiguration();
dw.setDefaults();
dw.setDeviceAddress(5);
dw.setNetworkId(10);
dw.setFrameFilter(false);
dw.interruptOnSent(true);
dw.commitConfiguration();
DW1000.newConfiguration();
DW1000.setDefaults();
DW1000.setDeviceAddress(5);
DW1000.setNetworkId(10);
DW1000.setFrameFilter(false);
DW1000.commitConfiguration();
Serial.println("Committed configuration ...");
// DEBUG chip info and registers pretty printed
Serial.print("Device ID: "); Serial.println(dw.getPrintableDeviceIdentifier());
Serial.print("Unique ID: "); Serial.println(dw.getPrintableExtendedUniqueIdentifier());
Serial.print("Network ID & Device Address: "); Serial.println(dw.getPrintableNetworkIdAndShortAddress());
Serial.println(dw.getPrettyBytes(SYS_CFG, NO_SUB, LEN_SYS_CFG));
Serial.println(dw.getPrettyBytes(PANADR, NO_SUB, LEN_PANADR));
Serial.println(dw.getPrettyBytes(SYS_MASK, NO_SUB, LEN_SYS_MASK));
// attach interrupt and ISR
pinMode(INT0, INPUT);
attachInterrupt(0, serviceIRQ, FALLING);
Serial.println("Interrupt attached ...");
Serial.print("Device ID: "); Serial.println(DW1000.getPrintableDeviceIdentifier());
Serial.print("Unique ID: "); Serial.println(DW1000.getPrintableExtendedUniqueIdentifier());
Serial.print("Network ID & Device Address: "); Serial.println(DW1000.getPrintableNetworkIdAndShortAddress());
// attach callback for (successfully) sent messages
DW1000.attachSentHandler(handleSent);
// start a transmission
transmitter();
}

void serviceIRQ() {
if(sent) {
return;
}
// "NOP" ISR
sent = true;
void handleSent() {
// status change on sent success
sentAck = true;
}

void transmitter() {
// transmit some data
Serial.print("Transmitting packet ... #"); Serial.println(sentNum);
DW1000.newTransmit();
DW1000.setDefaults();
String msg = "Hello DW1000, it's #"; msg += sentNum;
DW1000.setData(msg);
//DW1000.delayedTransceive(500, DW1000::MILLISECONDS);
DW1000.startTransmit();
delaySent = millis();
}

void loop() {
if(sent) {
// process confirmation of ISR status change (successfully sent)
sent = false;
if(!dw.isTransmitDone()) {
return;
}
// update and print some information about the sent message
unsigned long newSentTime = dw.getTransmitTimestamp();
Serial.print("Processed packet ... #"); Serial.println(sentNum);
Serial.print("Sent timestamp ... "); Serial.println(newSentTime);
// NOTE: delta is just for simple demo as not correct on system time counter wrap-around
Serial.print("Delta send time [s] ... "); Serial.println((newSentTime - sentTime) * 1e-9 * 8.01282);
sentTime = newSentTime;
sentNum++;
} else {
// transmit some data
Serial.print("Transmitting packet ... #"); Serial.println(sentNum);
dw.newTransmit();
dw.setDefaults();
String msg = "Hello DW1000";
dw.setData(msg);
dw.startTransmit();
// wait a bit
delay(500);
if(!sentAck) {
return;
}
// continue on success confirmation
sentAck = false;
// update and print some information about the sent message
Serial.print("Delay sent [ms] ... "); Serial.println(millis() - delaySent);
unsigned long newSentTime = DW1000.getTransmitTimestamp();
Serial.print("Processed packet ... #"); Serial.println(sentNum);
Serial.print("Sent timestamp ... "); Serial.println(newSentTime);
// NOTE: delta is just for simple demo as not correct on system time counter wrap-around
Serial.print("Delta send time [s] ... "); Serial.println((newSentTime - sentTime) * 1.0e-9 * 8.01282);
sentTime = newSentTime;
sentNum++;
// again, transmit some data
transmitter();
delay(10);
}
Loading

0 comments on commit ec43df3

Please sign in to comment.