The ScratchPad

2016 August 10

What is the ScratchPad?

The BrainStem scratchpad is a shared memory on a brainstem module which is accessible from both the host and Reflex code. It serves as a simple interprocess communication channel for separate Reflex routines, as well as a communication channel between the host and reflex code running on the BrainStem module.

The Scratchpad is accessed from the host and from the BrainStem module in two separate ways, this can be slightly more complex to work with at first, but leads to a couple of powerfull access patterns for reflex/host applications. The scratchpad is a volatile memory store, and is not preserved across a reboot or reset of the Brainstem module.


Reflexes and the ScratchPad

From the reflex side the scratchpad is accessed through a declaration style syntax. The scratchpad is a memory byte buffer, with no inherent structure. the Reflex pad access syntax allows the developer to overlay simple structures over the contents of the pad and access components as named characters, shorts and integers.  On most brainstem modules, the scratchpad is 300 bytes long.

The pad declaration syntax looks something like this;

pad[0:3] unsigned int timing;

This declaration reserves the first 4 bytes of the pad as an integer literal with the name timing. The figure below shows the relationship of the byte array nature of the scratchpad to the overlay of structure that the named pad variables provide. Be aware that pad declarations can overlap indices, which may cause issues if it is not an intended overlap.



The developer can then access this value as an integer in a reflex routine. For example;

timing = 500;


timeout_in_micro = timing * 1000; 
note: The BrainStem module stores values in little-endian format... if you read out multi-byte value as a series of bytes, keep this in mind.


The host and the Scratchpad

The host application interacts with the Scratchpad via a BrainStem entity called a Pointer.

What is a Pointer?

Pointers act like an open file handle into the BrainStem scratchpad. Each pointer starts out pointing at the beginning of the Scratchpad, index 0. The developer can set the pointer offset from the start of the pad, and control the mode of the pointer.

Initially all pointers are by default static. That means that reading or writing a character, short, or int from the pointer position will not result in changing the pointer position. The developer can change this behavior by setting the pointer mode to auto increment, after which a read or write to the pointer will cause the pointer position to advance by either 1, 2 or 4 bytes depending on whether the read/write was of a char, short, or int.

The developer has access of up to 4 pointers within the application, which all initially point to the starting location of the Scratchpad. Accessing data in the pad via a pointer in c++ looks like;



Note: The pointer position and mode are stored on the BrainStem module in volatile memory. This is important to remember, as destroying and recreating a module object via the host API, will not change set positions and modes on the module. Also if the BrainStem module is reset during the execution of the application, these pointer positions and modes will be reset to initial values.
Tip: The reflex language can use pointers too, to access a Scratchpad on another module connected via the BrainStem network. Reflex pointer use looks similar to the C++ above

reflex mapEnable() {
	char charVar;


An Example, Setting a bank of digitals on the 40pin USBStem.

The following example shows a reflex file which sets up the BrainStem digital pins to be used in a bank with a set of pad values as state inputs. See the Working with Reflex files section of the brainstem reference for more information about loading and working with Reflex files.

Once the reflex is loaded onto the 40pin module and enabled. A python script can be used to change the state of the digital outputs. The python script is given below, followed by the Reflex code.

the Python script:


import brainstem
from time import sleep

# Create a brainstem module, 
# and connect to the first one found.
stem = brainstem.stem.USBStem()

# The 40pin Stem has 15 Digitals, so a Short can be used 
# to set all 15 digitals.
for i in range(0, 10000):
	if (i % 2):
	sleep(0.1) # sleep for 100 milliseconds

The Reflex file:

#include <a40pinmodule.reflex>

// Create a reference to this module.
a40PinModule stem;

// 10 millisecond update delay.
#define DELAY 10000

// Create a shared memory location for,
// holding the digital state.
pad[0:0] unsigned char bank0;
pad[1:1] unsigned char bank1;

// mapEnable executes when the reflex is enabled.
// we set upd the counter, set the timer mode to
// repeat, and set the timer to expire after delay.

reflex mapEnable() {
    bank0 = 0;
    bank1 = 0;[0].setConfiguration(digitalConfigurationOutput);[1].setConfiguration(digitalConfigurationOutput);[2].setConfiguration(digitalConfigurationOutput);[3].setConfiguration(digitalConfigurationOutput);[4].setConfiguration(digitalConfigurationOutput);[5].setConfiguration(digitalConfigurationOutput);[6].setConfiguration(digitalConfigurationOutput);[7].setConfiguration(digitalConfigurationOutput);[8].setConfiguration(digitalConfigurationOutput);[9].setConfiguration(digitalConfigurationOutput);[10].setConfiguration(digitalConfigurationOutput);[11].setConfiguration(digitalConfigurationOutput);[12].setConfiguration(digitalConfigurationOutput);[13].setConfiguration(digitalConfigurationOutput);[14].setConfiguration(digitalConfigurationOutput);
    // Prime the timer to expire in 50 ms. 

reflex timer[0].expiration(char val) {
    if (bank0 & 0x01) {[0].setState(1);
    } else {[0].setState(0);
    if (bank0 & 0x02) {[1].setState(1);
    } else {[1].setState(0);
    if (bank0 & 0x04) {[2].setState(1);
    } else {[2].setState(0);
    if (bank0 & 0x08) {[3].setState(1);
    } else {[3].setState(0);

    if (bank0 & 0x10) {[4].setState(1);
    } else {[4].setState(0);

    if (bank0 & 0x20) {[5].setState(1);
    } else {[5].setState(0);

    if (bank0 & 0x40) {[6].setState(1);
    } else {[6].setState(0);

    if (bank0 & 0x80) {[7].setState(1);
    } else {[7].setState(0);

    if (bank1 & 0x01) {[8].setState(1);
    } else {[8].setState(0);

    if (bank1 & 0x02) {[9].setState(1);
    } else {[9].setState(0);

    if (bank1 & 0x04) {[10].setState(1);
    } else {[10].setState(0);

    if (bank1 & 0x08) {[11].setState(1);
    } else {[11].setState(0);

    if (bank1 & 0x10) {[12].setState(1);
    } else {[12].setState(0);

    if (bank1 & 0x20) {[13].setState(1);
    } else {[13].setState(0);

    if (bank1 & 0x40) {[14].setState(1);
    } else {[14].setState(0);


reflex mapDisable() {