NXTlib
Version: |
0.0.0 (August 2006, pre-release) |
Author: |
Joe Kinsella
|
Description: |
NXTlib is a collection of
Next Byte Code functions that expose the underlying
functionality of the Lego NXT
brick and sensors. The intent of this framework
is to fill a temporary gap left by the lack of high-level programming languages
for the NXT. Using Next Byte Codes and this framework, a programmer can quickly
take advantage of the core functionality of the NXT.
|
Completed Packages: |
-
Ultrasonic sensor
-
Sound sensor
-
Touch sensor
|
Packages In Progress: |
-
Motors
-
Brick display
-
Light sensor
|
Using NXTlib
A sample program called test.nbc has been provided. This program demonstrates
the proper usage of most of the functions available in the framework. To use,
just comment/uncomment the test function for the functionality you wish to
expose. It may be necessary for you to change the input/output port for your
specific configuration.
Calling Convention
To simplify the calling of a function, a corresponding structure has been
defined for each function. This structure is used to supply input
parameters and retrieve output parameters from a function call. For example,
the function touchCheck in touch.nbc allows a caller to determine if the
touch sensor has been triggered. The touch.nbc package exposes a public
variable called touchCheckArgs that provides a structure for sending
and retrieving data from this function. The below sample code shows a sample
call to this function:
   subcall touchCheck, touchCheckArgs.retval
Upon return of this function, the results of the call can be found in
touchCheckArgs.isTouched.
Multi-Threaded Programming
Since all calls to the same function must share the same structure variable
for passing data, it is necessary that no two calls to the same function
occur simultaneously. This is accomplished by a caller acquiring a mutex
prior for accessing the function or variable, and then releasing this mutex
only when all accesses to this variable are completed, or a local copy of the
data has been made.
The specific mutex required for a call should be documented in the header
of each package. Mutexes for common resources will be found in resource.nbc.
The below sample code demonstrates the use of the touch sensor in a multi-threaded
application:
   acquire touchSensorLock
   mov touchInitArgs.port, IN_1
   subcall touchInit, touchInitArgs.retval
   subcall touchCheck, touchCheckArgs.retval
   mov localVar, touchCheckArgs.isTouched
   release touchSensorLock
Known Issues
- The function ultrasonicGetConstants in ultrasonic.nbc seems to return erratic
results when called more than once (e.g. the version changes from "V1.0" to
"1.0.0" when called more than). The issue suggests some underlying firmware bug.
For now it is recommended that you call only once in an application.