| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Files spread between Dropbox, Google Drive, Gmail, Slack, and more? Dokkio, a new product from the PBworks team, integrates and organizes them for you. Try it for free today.

View
 

IXM Network LED

Page history last edited by Chris 9 years, 5 months ago

Networking demo that toggles a remote LED:

  • Defines service 'l' for LED toggling (red vs blue).
  • Long button press toggles whether board is an 'LED provider'
  • Short button press sends a 'toggle LED' request to nearest LED provider.
  • 'Toggle reply' causes matching flash on the button-pressed board

 

Initialize Sketch Directory

Pick a name for your sketch. Note that this name will become the name of the directory holding the sketch code. 

$ mkdir ~/sketches

$ mkdir ~/sketches/netled

$ cd ~/IXM/SFB

$ make SKETCH_DIR=~/sketches/blinky sketchinit

 

Sketch

Edit ~/sketches/netled/sketch.pde and paste:

 /* Networking demo that toggles a remote LED:

 *  - Defines service 'l' for LED toggling (red vs blue).

 *  - Long button press toggles whether board is an 'LED provider'

 *  - Short button press sends a 'toggle LED' request to nearest LED provider.

 *  - 'Toggle reply' causes matching flash on the button-pressed board

 */

bool iProvideLight = false;     // Initially, I'm not an 'LED provider'

bool ledRed = false;            // When I am, remember what color I'm showing

void handleToggleRequest(u8 * packet) { // This function runs on the service provider

  ledRed = !ledRed;             // Toggle our color

  facePrintf(packetSource(packet),"r%d\n",ledRed); // And reply back to the requester

}

void handleToggleReply(u8 * packet) {  // This function runs on the service requester

  u32 isRed;

  if (packetScanf(packet,"r%d\n",&isRed) != 3)   // Analyze packet

    return;                                      // ignore bogus ones

  if (isRed) ledOn(BODY_RGB_RED_PIN);   // Match the provider's color

  else ledOn(BODY_RGB_BLUE_PIN);

  delay(50);                     // Brief flash.. mustn't delay long in packet handler!

}

void setup() {

  Body.reflex('t',handleToggleRequest); // For when I'm a provider

  Body.reflex('r',handleToggleReply);   // For when I'm a requester

  Net.begin();                          // Start the net!

}

void loop() {

  u32 start = millis();                 // Capture current time

  while (buttonDown()) delay(10);       // Wait for button up

  u32 pressTime = millis()-start;       // Compute how long the button was down

  if (pressTime > 500) {                // If over half second: Long click

    iProvideLight = !iProvideLight;     // Toggle my status

    if (iProvideLight) Net.offer('l');  // And update network advertising

    else Net.withhold('l');

  } else if (pressTime > 50)            // If >50ms and <500ms: Short click

    Net.printf('l',"t\n");              // So send toggle request to service 'l'

  // Set our LED

  ledOff(BODY_RGB_RED_PIN);

  ledOff(BODY_RGB_BLUE_PIN);

  if (iProvideLight) {

    if (ledRed) ledOn(BODY_RGB_RED_PIN);

    else ledOn(BODY_RGB_BLUE_PIN);

  }

}

 

Build the sketch

$ cd ~/sketches/netled

$ make

 

Upload the sketch

$ cd ~/IXM/SFB

$ ./targets/host/bin/sfbdl -f ~/sketches/netled/sketch.hex -d /dev/ttyUSB0

 

 

 

 

Comments (0)

You don't have permission to comment on this page.