getting started c++ api

Getting started with a BrainStem using C++

Introduction

This guide describes the necessary steps to using the BrainStem C++ API along with providing a basic example for flashing the User LED.

Required Items

This guide was designed using the following items:

  • BrainStem Module
  • BrainStem Development Board
  • Link transport cable
  • BrainStem Support Package

A BrainStem Module and BrainStem Development board can be purchased from the Acroname Store. The latest version of the BrainStem Support Package can be downloaded from the Acroname Download page. Extract the package to the location of your choice when the download has finished.

About C++ API

The BrainStem C++ API provides a cross platform way to interface between a host machine and a BrainStem module.  The API provides a user with virtually unconstrained computing power in a familiar development environment when working with a BrainStem.

Build environment requirements

The following items are required to develop using the BrainStem C++ API:

  • C++11 compliant compiler
  • Update Header Search Path to include aBrainStem and module header files (aInclude by default)
  • Update Library Search Path to include aBrainStem library (aBinary by default)

Most recent compilers are C++11 compliant. Some examples on various platforms are:

  • MacOS X: Xcode 4 or newer
  • Windows: Visual Studio 2012 or newer
  • Linux: gcc 4.7 or newer

Example Code

The aBrainStemExample is included in the examples folder of the BrainStem Support Package and shows how to toggle the User LED on a BrainStem using the C++ API.  Included with the example are IDE projects for Windows, Mac OS X and Linux.  The following code shows how to toggle the User LED.  Please visit the Flashing My User LED Using C++ example page for a detailed explanation.

Toggling the LED

#include "a40PinModule.h"

// 1 second delay
#define DELAY 1000

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(DELAY);
    bLEDState = !bLEDState;
  } // end of for loop

  // Turn the LED off before exiting
  stem.system.setLED(0);
  printf("goodbye\n");

  return 0;
} // main

If the User LED turns on and off 5 times after the example code is run then the code worked successfully.  Now try writing an embedded Reflex program to perform this same action on the BrainStem.

Getting Started with Reflexes