I stumbled upon an issue that is hindering my progress on my game so I hope someone out there can provide some advice.
It seems there is some timing issue related to sending datagrams. Consider the program below:
#include <blinklib.h>
#include <shared/blinkbios_shared_functions.h>
#include <string.h>
#define TRANSFER_LEN 16
#define PROCESSING_DELAY 0
void setup() {}
byte datagram[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
Timer wait_timer;
void Send(byte f) { sendDatagramOnFace(datagram, TRANSFER_LEN, f); }
void loop() {
if (buttonSingleClicked()) {
FOREACH_FACE(f) { Send(f); }
}
if (wait_timer.isExpired()) {
FOREACH_FACE(f) {
if (isValueReceivedOnFaceExpired(f)) continue;
if (!canSendDatagramOnFace(f)) continue;
if (getDatagramLengthOnFace(f) == 0) continue;
const byte* data = getDatagramOnFace(f);
if (memcmp(data, datagram, TRANSFER_LEN) != 0) {
BLINKBIOS_ABEND_VECTOR(f);
} else {
setColorOnFace(GREEN, f);
}
markDatagramReadOnFace(f);
Send(f);
wait_timer.set(PROCESSING_DELAY);
}
} else {
setColor(OFF);
}
}
(Keep in mind this is juts a hacked up testing case.)
What the program was supposed to do:
If you load it in 2 blinks and connect them, clicking on the button on one would start a datagram message to be sent back and forth between them.
What actually happens is that, after a while it stops. The reason it stops is because one side sends a message that never reaches the other side.
Now, the interesting part is that if you increase the value of PROCESSING_DELAY, you will see it will take longer for the messages to stop. If the delay is big enough, the messages do not seem to stop at all.
I looked at the blinklib code but could not find anything obviously wrong but it might simply mean that the actual bug is inside BlinkBIOS itself.
Now, the even more interesting thing can be seem if you connect several blinks (say, 7, clicking on the center one). In this case the messages will stop again even with the delay so, whatever it is, it is affected by the number of faces you are trying to send data to in a single loop iteration.