Reflex Example 4: Digital PollingIndex

The Problem

Suppose a BrainStem must poll a digital input pin every 0.1ms and increment a counter when it detects a logic transition from 0-1 at the input pin.

The Solution

Here is some logic to implement this function:

The Details

We need to describe this logic in terms that can be directly translated into BrainStem commands.  Fortunately, the BrainStem GP 1.0 Module has a built-in digital polling loop with a period of 0.1ms.  The module can be configured to issue a reflex on a 0-1 or 1-0 transition on any of its digital input pins.  After detecting a transition, the digital input pin must be reset.  This functionality simulates a "Set-Reset" Flip-Flop.  This revised logic is shown below:

Reflex 15 Increment counter 0.
  Reset transition polling for digital pin 0.

Reflex 15 is the reflex that is activated when the digital polling loop detects a transition on digital IO pin 0.  The value 15 corresponds to the device ID for detecting transtions on digital IO pin 0.

The Commands

In the reflexive routine described above, there are really only two unique commands.  These two commands, complete with address byte and size byte, are listed below.  They are numbered from 0 to 1.

0 Increment counter 0. 2 3 43 0 1
1 Reset transition polling for digital pin 0. 2 2 28 0

The cmdCTR_CT command tells counter 0 to update its count.  The last byte of 1 in the command tells counter 0 to increment its count.  If the last byte was 0, the counter would decrement its count.  The cmdDIG_RST command resets polling for digital pin 0.  A transition usually requires some operation to be performed.  It is important to reset digital polling for a pin only after the task is completed.

These commands go in reflex command slots 0-1.  The series of cmdMSG_WR commands listed below will save the command data into the appropriate locations in the EEPROM.

Reflex CMD 0 2 12 128 0 2 3 43 0 1
Reflex CMD 1 2 12 128 1 2 2 28 0

The Vector

Now that the reflex commands are in the EEPROM, we need to define the reflex vector data for reflex 15.  This reflex only has one vector.  The binary contents of the reflex vector along with a description of what the contents mean is listed below:

Reflex Vector 15 Cmd 0
Cmd 1, (last in vector)
[ 0 | 0000000 ] [ 1 | 000 | 0000 ]
[ 1 | 0000001 ] [ 1 | 000 | 0000 ]
0 128
129 128

This vector data goes into reflex vector slot 15.  The cmdVEC_WR command listed below will save the vector data into the appropriate locations in the EEPROM.

Vector 15 2 11 128 15 0 128 129 128

The Results

The digital polling and counter increment routine is now completely defined.  A text batch file can store all the commands for downloading the reflex data.  Copy and paste the following lines of numbers to a file called "poll.txt" in your "aUser" directory.  The Console can download the file to the BrainStem.  Just enter batch "poll.txt" at the prompt.

To test the routine, connect some kind of switch to digital input 0.  Enter the following command to enable digital polling on digital pin 0:

Flip the switch on and off a few times, then enter the following command:

That command would normally set the counter, but since it does not have a new value, it tells the module to send the current count value instead.  You should see a packet in the Console with the current count.

This example demonstrates how reflexes can count events and respond to switch input.

Here is a LEAF source file for the reflex routine in this example:

ex4_poll.leaf

Download "ex4_poll.leaf" here
/* ex4_poll.leaf */

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

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

/* message ids */
#define mCOUNT 0
#define mRESET 1

module[MODULE] {

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

  message[mCOUNT] {
    MODULE, cmdCTR_CT, 0, 1
  }

  message[mRESET] {
    MODULE, cmdDIG_RST, 0
  }

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

  vector[aGP_RFX_DTRANS_0] {
    mCOUNT,
    mRESET
  }
}

Here is the BAG output from the LEAF compiler:

ex4_poll.bag

Download "ex4_poll.bag" here

// module 2 (2 messages, 1 vectors)

// message 0
2 12 128 0 2 3 43 0 1
// message 1
2 12 128 1 2 2 28 0
// vector 15
2 11 128 15 0 128 129 128

This batch file contains the same commands for downloading the reflex that were listed for the "poll.txt" file, but in a different order.


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