Basic Language

Variables

The available variables include:

  • Integer
  • String
  • String Index

Integer Variables

Integer Variables are indicated by single capital letters A through Y. Twenty-five variables (A=1, B=2, … Y=25) can be used. The variable named Z is reserved for specialized debugging, and is described later. Variables are not initialized when a program starts. They are initialized when the AIRcable OS reboots. 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 $1023 (or $2047)
$0 is a special variable. It is volatile and not stored permanently. $0 is used for delivering results to and from 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 longer 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:
$0[0] = 65
Write "A" in the first character of the string.
Expressions
The AIRcable's central execution engine is programmed in an embedded and enhanced BASIC language. The program can be uploaded and downloaded via OBEX FTP over air.
The BASIC program contains BASIC lines. Each BASIC line starts with a line number from 1 to 1023 (or 2047, depending on the configuration) 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. Except for line 0 which is special and non transient that can take up to 80 chars. Each BASIC line contains one BASIC statement.

Expressions

The AIRcable's central execution engine is programmed in an embedded and enhanced BASIC language. The program can be uploaded and downloaded via OBEX FTP over air.
The BASIC program contains BASIC lines. Each BASIC line starts with a line number from 1 to 1023 (or 2047, depending on the configuration) 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. Except for line 0 which is special and non transient that can take up to 80 chars. Each BASIC line contains one BASIC statement.
The embedded BASIC language has the following elements:

  • Variables
  • Expressions
  • Statements
  • Built-in Functions
  • Interrupt Routines

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-Y
  • string index variables: $expr[expr]
  • unary minus (negative values): -expr
  • brackets: [expr]
  • parentheses: (expr)
  • multiplication, division: expr * expr or expr / expr
  • addition, subtraction: expr + expr or expr - expr

Boolean Expressions

Note: Integer variables can be used for Boolean expressions as well. A zero is treated as false; a non-zero value is true.

Expression Variable
Compare expr = expr
expr <> expr
Less than or greater than expr < expr
expr > expr
expr >= expr
expr <= expr
Boolean AND expr * expr
Boolean OR expr + expr
Bitwise NAND * expr & expr
Bitwise XOR * expr ^ expr

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).

  • Included since OS version 30.

Statements

BASIC program statements must start with a line number following a space. Line numbers go from 1 to 1023 (or 2047).
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 actually a delay of 100ms between the lines. This gives the other parts of the system CPU time, such as the Bluetooth baseband 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 at that time.
All statements are indicated by capital letters only.

Example Description
'expr' used to indicate an expression
'var' used to indicate an integer variable or a string index variable, such as 'A' or '$1[2]'
'svar' 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:
WAIT expr
In this case expr has to be an integer value, or WAIT will return immediately.
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

~ Assignment ~ Description
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 F.
FOR var=expr TO expr
NEXT var

Subroutines

Subroutines are called by the start line number. To return from a subroutine, use RETURN.
GOSUB expr
To jump to another line number, use GOTO.
GOTO expr

Execution Control

IF expr THEN expr
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.
If you need to make a IF - THEUse GOTO to complete this part.

Output

Output can write on 4 different 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 into the string $0

Examples on how to send data to the UART

Description Example
This sequence will send the ASCII value of "5" A = 5
PRINTU A
This sequence will output the integer representation of the second character to the UART. In this case it will print "65" $5 = "HALLO"
PRINTU $5 [1]
Will send the string "HALLO" to the UART PRINTU $5
Sends the string "ME" to the UART. PRINTU "ME"
To output a string or a variable to the incoming SPP channel PRINTS expr
PRINTS svar
PRINTS $expr[expr]
PRINTS "immediate string"
To output a string or a variable to the outgoing SPP channel 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 REM initialize string, start at the null terminator
$0[0] = 0
PRINTV expr
PRINTV svar
PRINTV $expr[expr]
PRINTV "immediate string"
To create escape sequences in the immediate string, these special characters are supported
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 three channels:

  1. A number terminated with CR (Carriage Return) and
  2. A string terminated with CR. For the UART channel, a defined number of bytes can be input as well.

Note: 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.

INPUTU var INPUTS var INPUTM var
INPUTU svar INPUTS svar INPUTM svar
UART var * GETU var * GETS var * GETM 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 on termination. The UART command puts the number of bytes read back into the variable given as the parameter. You can read as much as 80 chars which each call to UART.

Note: * Since version OS version 30 two new commands had been added to the OS, those are GETU, GETS. GETU is exactly like UART while GETS is like UART for the Slave Port. GETM will be available soon.

By default only INPUTS will process input characters. It will echo incoming bytes and will honor back space to delete the previously typed character. But each channel can be configured to change default behaviour by using STTYU STTYS or STTYM.
By default the various INPUT commands will wait indefinitely until the terminating condition is reached. During this time, no interrupt routines are processed. You should make sure that interrupts such as ALARM, inquiry results and PIN code responds should not happen while you wait for input from the user.
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

Input/Output configuration

STTYU expr STTYS expr STTYM expr

The values for STTY are:
<COMING SOON>

Capture Input

The CAPTURE statement is a way to log data from the UART to the file system directly. This command is available on AIRcable OS with file system. 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.
0 REM this is a test

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License