Flashing My User LED Using C++

Overview

BrainStem modules have a dedicated blue colored LED available for user manipulation. The User LED is designated on the module  with a stick figure "man" icon.  A host machine can manipulate the User LED using the BrainStem C++ API.  The API can discover and create a link to an attached BrainStem module. After the  the link is created, the host can use the System class in the API to toggle the LED.

BrainStem application examples require the BrainStem Support package. Acroname provides simulated BrainStem modules within this distribution that can be used in the absence of hardware using the Symonym application.

The Setup

There are several hardware components needed for this example:

  • 40-Pin EtherStem 1.0 Module
  • 40-Pin Breakout Board
  • Ethernet Cable
  • Computer
  • Power Supply
  • BrainStem Support Package

-OR-

  • aSymonym Software Package

Example Setup

Synchronous LED enable using the C++ API

The BrainStem C++ API enables a host machine to interact with a BrainStem module.  A link must be established before any interaction occurs.  The aLink class can discover and create a link to a BrainStem module.  Once a link is established the code then uses the LED entity of the System class to toggle the User LED.  The Sleep entity is then used to adjust the length of time the LED is on or off.  The Sleep entity delays code execution in millisecond increments for the C++ API.

LED Flashing using C++ API

#include "a40PinModule.h"

int
main(const int argc,
     const char* argv[])
{
  // Create a link to the first BrainStem found on the specified
  // transport linktype
  aLink link(aLink::specifier::IP4);
  
  printf("connecting to stem...");
  
  if (!link.isConnected(3000)) {
    printf("timed out\n");
    return aErrTimeout;
  }
  printf("connected\n");

  // Need a link and I2C module address to create a BrainStem object
  a40PinModule stem(link, a40PINSTEM_MODULE);

  uint8_t bLEDState = 0;
  
  for (uint8_t i = 0; i < 10; i++) {
    stem.system.setLED(bLEDState);
    stem.system.setSleep(500);
    bLEDState = !bLEDState;
  } // end of for loop
  
  // Turn the LED off before exiting
  stem.system.setLED(0);
  printf("goodbye\n");

  return 0;
} // main

Line 7 -  This constructor creates a link to the first discovered BrainStem on the specified transport linktype
Line 16 - Creation of a BrainStem object requires a link and I2C module address