Reflex Example 1: Debug CommandIndex

The Problem

How do I make a reflex?

The Solution

The simplest example is a reflex that sends a debug message to the host when it receives an input.  Connect your BrainStem to the host computer and apply power.  Run the Console program.  Your BrainStem should have an address of 2 for this example. (This is the default address for a new GP 1.0 module.) Enter these three lines at the prompt:

The BrainStem should respond by sending the following debug packet (with values in hexadecimal) up to the host:

Congratulations!  You just downloaded and executed your first reflexive routine.

The Details

So what did those commands do?  The first command loaded the data for a debug command into the EEPROM locations reserved for reflex command 0.  The second command loaded vector data into the EEPROM locations reserved for reflex vector 100.

The third command is a cmdRAW_INPUT packet that initiates the reflex propagation routine.  This routine retrieves reflex vector 100 from the EEPROM.  The first entry in the vector points to reflex command 0.  The reflex propagation routine retrieves that command and stuffs it onto the command queue.  Then it exits because there are no more entries in the vector.  Finally the module pulls the debug command off the queue and executes it.

The Command

In the reflexive routine described above, there is a single command.  This cmdDEBUG command, complete with address byte and size byte, is described in the table below.  The command goes in slot 0 in the reflex command section of the EEPROM.  The cmdMSG_WR command shown previously and in the table below writes it into the appropriate location.

Cmd Slot Description Packet Data cmdMSG_WR Data
0 Send a debug packet with data "1 2" back to host. 2 3 23 1 2 2 12 128 0 2 3 23 1 2

The Vector

In the reflexive routine described above, there is a single vector.  A reflex vector is an array of eight 2-byte records.  Each record contains a command index and control fields for executing that command.  Since this reflex only has one command, the vector only has a single entry.  This vector is described in the table below.  It goes in slot 100 of the reflex vector section of the EEPROM.  The cmdVEC_WR command shown previously and in the table below writes it into the appropriate location.

Vector Slot Description
Control Fields Data cmdVEC_WR Data
100 Get Cmd 0, (last in vector)
[ 1 | 0000000 ] [ 1 | 000 | 0000 ]
128 128
2 11 128 100 128 128

The Results

This example demonstrated the basics of reflex operations.  Inputs initiate reflexes, which in turn can propagate commands or initiate other reflexes.  One of the most powerful attributes of reflexive routines is that they can run concurrently with other reflexes and VM processes.

Reflexes can be defined by writing a LEAF program.  The LEAF compiler interprets the command and vector statements and translates them into their binary format.  Here is a LEAF source file for the reflex routine in this example:

ex1_dbg.leaf

Download "ex1_dbg.leaf" here
/* file: ex1_dbg.leaf */

#include <aCmd.tea>
#include <aGPReflexes.tea>

/* assume the BrainStem has address 2 */
#define MODULE 2

/* message ids */
#define mDEBUG 0

/* user-defined vectors */
#define vGO 100

module[MODULE] {

  /************* REFLEX COMMANDS *************/

  /* send message back to host */
  message[mDEBUG] {
    MODULE, cmdDEBUG, 1, 2
  }

  /************* REFLEX VECTORS *************/

  vector[vGO] {
    mDEBUG
  }
}

The output of the LEAF compiler is a bag file with commands for loading the reflex into the BrainStem:

ex1_dbg.bag

Download "ex1_dbg.bag" here

// module 2 (1 messages, 1 vectors)

// message 0
2 12 128 0 2 3 23 1 2
// vector 100
2 11 128 100 128 128

This batch file contains the same commands for downloading the reflex that were listed earlier in this document.


version: 1.0, build 80903
© Copyright 1994-2008 Acroname, Inc., Boulder, Colorado.  All rights reserved.