|
Support : Industrial : AIRcable AIRmotes Programming Handbook : PART 2
PART 2: BASIC Language
The AIRmote's central execution engine is programmed in an embedded and enhanced BASIC language. The program can be uploaded and downloaded via OBEX FTP over the air.
The BASIC program contains BASIC lines. Each BASIC line starts with a line number from 1 to 511 followed by a single space. No spaces are allowed before the line number. A BASIC line can be up to 31 characters in length—including line number, spaces, commands and all punctuation. Each BASIC line contains one BASIC statement.
The embedded BASIC language has the following elements:
Variables
The available variables include:
Integer Variables
Integer Variables are indicated by single capital letters A thru T. Twenty variables (A=1, B=2, . . . T=20) can be used. Variable names beyond the letter "T" should not be used. Variables are not initialized when a program starts. Integer variables are 16-bit signed numbers with a range of -32768 to 32767.
String Variables
String variables are indicated by $ and a number, such as "$5". These string variables are actually the BASIC command lines, meaning that "$10" is the 10th line of the BASIC program. String variables are persistent. All changes to the string variables are written back to the memory into the BASIC program itself. This way an application can have initialized strings stored in BASIC lines.
It is also possible to write a BASIC program that can change itself dynamically, such as download programs or self-configuration programs.
String variables range from $0 to $511.
$0 is a special variable. It is volatile and not stored permanently. $0 is used for delivering results from and to built-in functions and interrupt routines. For example, writing to a file in the file system will write the content of $0 into the specified file.
$0 variables are longer strings. In this version, $0 has a length of 80 characters. See the use of the PRINTV functions to create strings.
An immediate string is put in quotation marks, such as "hello", as a value to initialize a string variable. Immediate strings have a limit of 31 characters.
String Index Variables
Strings can be indexed by using square brackets. For example, $0[0] indicates the first character of the string "$0". String index variables are treated as integer variables with a range of 0 to 255. To write characters to a string index variable, use the decimal representation of the ASCII code. For example, to write an "A" in the first position, use its decimal representation, 65:
Write "A" in the first character of the string.
Expressions
The expression parser in the BASIC interpreter is recursive. Be careful when using complex expressions. Stack space is very limited. The following expressions are evaluated in the ranking below. "expr" is used to indicate an expression.
- number range: 0 - 32768 (or 32767 when using unary minus)
- variables: A-T
- string index variables: $expr[expr]
- unary minus (negative values): -expr
- brackets: [expr]
- multiplication, division: expr * expr or expr / expr
- addition, subtraction: expr + expr or expr - expr
Boolean Expressions
Integer variables can be used for Boolean expressions as well. A zero is treated as false; a non-zero value is true.
- compare
- less than or greater than
- expr < expr
- expr > expr
- expr >= expr
- expr <= expr
- Boolean AND
- Boolean OR
Note: string variables cannot be used in expressions. Instead, string index variables should be used at this time. String variables cannot be compared using this mechanism. There are built-in functions available to do that (e.g., strcmp).
Statements
BASIC program statements must start with a line number following a space. Line numbers go from 1 to 511.
The execution is faster when the line numbers are adjacent, with no empty lines in between. The BASIC interpreter executes lines with a default duty cycle of 10 lines per second or a delay of 100ms between the lines. This gives the other parts of the system CPU time, such as the Bluetooth functions or the sensor-read functions.
If a statement ends with a ';' the execution of the next line continues without the delay. This way, a speed of about 200 lines per second can be reached, but no other functions (such as Bluetooth operations) can be executed.
All statements are indicated by capital letters only.
'expr' is used to indicate an expression
'var' is used to indicate an integer variable or a string index variable, such as 'A' or '$1[2]'
'svar' is used for string variables, such as $0
Delay
The wait statement increases the default wait time of 100ms to the number of seconds evaluated from the expression as an argument:
The wait time may not be accurate. It is possible that a WAIT is interrupted and that execution will commence earlier. To write applications with a time-controlled repeated execution, you can use either the sensor-reading interrupt routine or the alarm interrupt routine and configure the time between readings (see Part 3).
Assignments
- Variable assignment: var = expr
- String assignment: svar = string or svar = svar
- Substring assignment: svar = $expr[expr]
The substring assignment will copy the string from the position given with the substring argument into the assigned string.
Control Statements
For-loops are indicated in this syntax. Six loops can be nested. The variable controlling the number of iterations must be an integer variable between A and T.
FOR var=expr TO expr
NEXT var
Subroutines
Subroutines are called by the start line number. To return from a subroutine, use RETURN.
To jump to another line number, use GOTO.
Execution Control
If the first expression evaluates to non-zero, the execution continues at the line number that is the result of the second expression. Otherwise the execution continues on the next line number. Use GOTO to complete this part.
Output
Input and output can be performed on 3 channels:
- UART serial port, 'U'
- SPP incoming slave serial link, 'S'
- Second SPP outgoing master serial link, 'M'
- Virtual string output, 'V', which will write the string $0
To output a string or a variable to the UART, use:
PRINTU expr
PRINTU svar
PRINTU $expr[expr]
PRINTU "immediate string"
To output a string or a variable to the incoming SPP channel, use:
PRINTS expr
PRINTS svar
PRINTS $expr[expr]
PRINTS "immediate string"
To output a string or a variable to the outgoing SPP channel, use:
PRINTM expr
PRINTM svar
PRINTM $expr[expr]
PRINTM "immediate string"
To append a string to the global variable $0, use the virtual string channel. The function searches for a null terminator in $0 and then prints the string beginning at this position.
$0[0] = 0 (initialize string, start at the null terminator)
PRINTV expr
PRINTV svar
PRINTV $expr[expr]
PRINTV "immediate string"
To create escape sequences in the immediate string, use this sequence:
| Escape Sequence |
Description |
| \\ |
Backslash |
| \0 |
Null character |
| \n |
Newline |
| \r |
Carriage return |
| \xnn |
Character as hexadecimal number (nn) |
Input and Timeout
Data input can be in two formats from any of the three channels: a number terminated with CR (Carriage Return) and a string terminated with CR. For the UART channel, in addition a defined number of bytes can be input as well.
Input into a variable reads a number of digits from the input channel until a CR (Carriage Return) terminates the input. An input into a string variable reads in a string from the channel until a CR (Carriage Return) terminates the input or until the number of characters typed in reaches 32 characters. If $0 is used as the string variable, the limit is 80 bytes.
All the INPUT commands will echo the input characters back to the channel it came from. It also processes backspace (delete last character).
| INPUTU var |
INPUTS var |
INPUTM var |
| INPUTU svar |
INPUTS svar |
INPUTM svar |
| UART var |
|
|
The UART command reads a certain number of characters from the UART channel into the global string $0. The variable given to the UART command contains the number of characters. This command will not echo or process any input.
Only INPUTS will process input characters. It will echo incoming bytes and will honor back space to delete the previously typed character. The UART and Master channels will not do that.
The various INPUT commands will wait indefinitely until the terminating condition is reached. During this time, no interrupt routines are processed.
Before calling an INPUT (or UART) command, a TIMEOUT can be scheduled. The TIMEOUT (for each of the three channels separately) takes as an argument an expression. This is the number of seconds before the following INPUT command will terminate:
| TIMEOUTU expr |
TIMEOUTS expr |
TIMEOUTM expr |
Example:
10 PRINTS "type 0 to terminate"
20 TIMEOUTS 10
30 INTPUTS A
40 IF A <> 0 THEN 10
50 END
Capture Input
The CAPTURE statement is a way to log data from the UART to the file system directly. The expr parameter specifies the number of bytes to be logged. A file has to be open for this function to work. CAPTURE does not close the file.
This example logs 100 bytes from the UART into a file with a 60-second timeout.
100 A = open "log.txt"
101 B = 100
102 TIMEOUTU 60
102 CAPTURE B
105 PRINTS "done, size "
106 C = size
107 PRINTS C
108 A = close
Comments
A line beginning with REM is treated as a comment and not executed. The line is indeed stored in the BASIC program occupying program space. A possible practice is to start the line number with 0 to have the line not stored in memory but still have comments in the original BASIC file.
www.aircables.net/support-ind-program-manual-pt2.html
|