pingpong.cpp

Here's a complete sketch showing how to respond to the arrival of a packet. When this sketch sees a "ping" packet, it responds with "pong".

Compared to Traditional serial communications, note two differences here:

  1. In setup(), there are no begin calls on faces specifying baud rates, and
  2. There is no sketch code to do any serial reading at all.

The first difference arises because in the packet system, you are not allowed to specify an explicit baud rate. Instead, SFB's (and other SFB-aware programs, such as sfbdl) automatically perform baud rate negotiation (BRN) to determine a mutually agreeable baud rate.

And the second difference arises because the core software automatically collects input bytes as they arrive and assembles them into packets. Once a packet is complete, some appropriate packet handler function -- if any -- is called to respond to the packet.

A sketch registers interest in certain packets by establishing a reflex (or several of them), associating a 'packet type' with a 'packet handler'. The type of a packet is just the value of its first byte -- so the type of a packet containing the four bytes "ping" is 'p'. In this example, the setup() function establishes a reflex between type 'p' and the packet handler function 'doPingPong'.

Note that this particular sketch responds to, but never actually prints, a ping packet. So if a bunch of IXMs were plugged together all running this sketch, they would all just sit there mute, waiting for somebody or something else to ping them.

// Ping pong echo sketch: Get ping, send pong

void doPingPong(u8 * packet) {
  if (packetScanf(packet,"ping\n") != 5)  // Must be what we want
    return;                               // If not, ignore it
  facePrintln(packetSource(packet),"pong");  // Respond to who pinged us
}

void setup() {
  Body.reflex('p', doPingPong);       // set up reflex
}

void loop() { /* Nothing to do! */ }

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