zformat3.cpp
This sketch performs the same functions as
zformat2::cpp, but uses recursive printf/scanf calls inside the custom handlers, to reduce the number of lines of code we have to write, and just to show we can. (Though convenient, note that this version consumes more stack space that the previous.)
struct TwoD {
s16 x;
s16 y;
};
void TwoDZPrinter(u8 face, void * arg, bool alt, int width, bool zerofill) {
API_ASSERT_NONNULL(arg);
TwoD t = * (TwoD*) arg;
facePrintf(face,"(%d,%d)",t.x,t.y);
}
bool TwoDZScanner(u8 * packet, void * arg, bool alt, int width) {
int x, y;
if (packetScanf(packet,"(%d,%d)", &x, &y) != 5)
return false;
if (arg) {
TwoD * tp = (TwoD*) arg;
tp->x = x;
tp->y = y;
}
return true;
}
void negateCoord(u8 * packet) {
TwoD c;
if (packetScanf(packet,"%Zn%z\n", TwoDZScanner, &c) != 3) {
logNormal("Failed at %d\n",packetCursor(packet));
return;
}
c.x = -c.x;
c.y = -c.y;
facePrintf(packetSource(packet),"n%Z%z\n", TwoDZPrinter, &c);
}
void setup() {
Body.reflex('n',negateCoord);
}
void loop() {
}