fifthdirection.cpp

Here's a riddle, of a sort. When embedded in a grid, how many IXMs can an IXM talk to directly?

The answer, of course, is five: Its four neighbors -- to the north, south, east, and west -- and it can talk to itself as well.

While this is not a great riddle, it does raise a point: Since a sketch can simply do:

  facePrintf(NORTH,"you are north of me\n");
  facePrintf(SOUTH,"you are south of me\n");
  facePrintf(EAST,"you are east of me\n");
  facePrintf(WEST,"you are west of me\n");

to send packets to the four points of the compass, why can't a sketch send a packet to itself -- or more properly, to its future self -- in a similar way?

The answer is that a sketch can do exactly that, using a special 'working memory' face named WMEM:

  facePrintf(WMEM,"you ARE me (in the future)\n");

It is also possible to do this via the Memory object:

  Memory.printf("you ARE me (in the future)\n");

Packets printed to WMEM aren't sent offboard at all; they are stored locally in RAM, and reflex-triggered some time in the (usually near) future, as seen in this tiny sketch, which toggles an LED whenever a 't' packet is received, or when the button is pushed -- using the talk-to-self power of WMEM in the latter case.

// Print to WMEM to make a packet in 'working memory'

void toggle(u8 *) {
  ledToggle(BODY_RGB_GREEN_PIN);
}

void setup() {
  Body.reflex('t',toggle);     // Set up reflex
}

void loop() {
  if (buttonDown()) {           // If button is pushed
    facePrintf(WMEM,"t\n");     // Send a blink packet to ourselves
    while (buttonDown()) delay(1); // And wait for button up
  }
}

Generated on Fri Apr 22 06:54:10 2011 for SFB by doxygen 1.5.9