The text of a manual · part 3 of 6
QTERM-R55 User's Manual: Chapter 2.
Pages 15 to 26 of 42, from QTERM-R55 User's Manual
The original document is the PDF (1.6 MB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
For the QSI Corp QTerm R55 and Beijer QTerm R55
Page 15
CHAPTER 2. QABASIC The QTERM-R55 is a user programmable operator interface terminal for industrial applications. The terminal is programmed using qaBASIC, a dialect of the common and simple BASIC programming language. The BASIC program can be written using any computer text editor and then downloaded into the terminal and stored in non-volatile memory. This program is executed after power is applied to the system. The program may be updated through the Power-On Setup menu; see section 1.1.
2.1.1.2 qaBASIC is a modified version of YABASIC, a BASIC compiler and runtime execution engine written by Marc Oliver Ulm. Much of this chapter was derived from the documentation that is included with the YABASIC distribution. YABASIC is distributed under the terms of the GNU Public License which allows modification and redistribution of the software provided that the same license applies to the modified version. Source code for the YABASIC modification may be obtained from QSI upon request. Contact QSI for details. Refer to the LICENSE file included on the distribution disk for more information on the GNU Public License.
a, c3po or ThisIsAnEvenLongerName are examples of numerical variables and can contain real numbers like 1.3, 2, 15.3e44 or 0.
The QTERM-R55 also uses uC/OS, the Real Time Kernel, by Jean Labrosse.
2.2
2.1 2.1.1
Syntax Case
Commands can be entered in any case. input is the same as INPUT and even as InPUt. This applies to every command in qaBASIC. 2.1.1.1
Variables
Variable names are case sensitive (i.e. types of variables: a$ and A$ are different) and can be of any length. There are two sorts of variables:
String Variables
String variables always have a dollar sign ($) as the last character of their names, e.g., a$, b12$ or VeryLongName$ and may contain strings of any length. String variables can contain any characters except the NULL character (ASCII 0). 2.1.1.3
Numerical Variables
Variables (with the exception of arrays) need not be declared; their initial values are "" (for string variables) and 0.0 (for numerical variables). 2.1.1.4
Multiple Commands on One Line
More than one command can appear on one line. The commands must be separated with colons.
Introduction to qaBASIC
Example 1: REM this is the first R55 BASIC program input "Enter two numbers:" a,b print a,"+",b,"=",a+b print "Please enter your Name:"; INPUT a$ print "Hello ",a$," !"
This program produces the following output (user input is displayed using this typeface): Enter two numbers: 2 3 2+3=5 Please enter your Name: Bill Hello Bill !
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 16
6
QTERM-R55 User's Manual
2.2.1
REM
The REM statement introduces comments. Everything after REM up to the end of the line is ignored. The ’(apostrophe) is an abbreviated replacement for REM. 2.2.2
Input
This program produces the following output:
This statement reads one or more variables from the user. The syntax for this command is input [prompt] var1[,var2,...]
The optional [prompt] string after the input statement ("Enter two numbers:") is printed on the terminal prior to reading any input. If the prompt string is omitted, the terminal uses the “?” character to prompt the user. Note that there is no semicolon or comma after this prompt string. If multiple variables are requested, they are listed after the prompt string, separated by commas. The default input source is the keypad. This command may also be used to read input from a serial port or the non-volatile file store. See Serial Communication (section 2.8.4) and Nonvolatile Storage of Data (section 2.10) for details. 2.2.3 print atan(2),atan(1,2) print exp(1),log(2),log(euler) print int(2.34),frac(2.34) mod(11,4) print min(2,3),max(2,3) print sqrt(2)
3 6 2 83 0.841471 -1 -0.142547 0.523599 0.795399 1.10715 0.463648 2.71828 0.693147 1 2 0.34 3 2 3 1.41421
2.3.1 qaBASIC has five arithmetic operators: + (addition), (subtraction), * (multiplication), / (division) and ^ (power); they all behave as expected, as in Example 2. Note that the power operator (^) handles fractional powers: 8^(1/3) returns 2 as a result.
Print 2.3.2
The print statement writes all its arguments to the display. After writing its last argument, print advances the cursor to the next line (as in print "Hello ",a$," !"). To do this, the QTERM-R55 inserts a carriage return character (0x0D) and a linefeed character (0x0A) after the string. To avoid this behavior, place a semicolon after the last argument (as in print "Please enter your Name:";). Note that print can be abbreviated with a single question mark (?). If you want to print (or input) at a specific location, refer to Printing and Controlling the Screen (section 2.9). The default output destination is the display. This command may also be used to send output to a serial port or the non-volatile file store. See Serial Communication (section 2.8.4) and Nonvolatile Storage of Data (section 2.10) for details.
2.3
Operations
Arithmetic
Example 2:
This section demonstrates and explains the arithmetic functions of qaBASIC. 2.3.2.1
Trigonometric Functions
Example 2 illustrates the six supported trigonometric functions. qaBASIC can calculate sine, cosine, tangent and their inverses. All these functions expect their argument in radians; to facilitate the transformation from degrees to radians (radian=degree*pi/180), there is a predefined variable named pi (or PI) which has an initial value of 3.14159. The atan() function has two forms. Called with a single argument (e.g. atan(2)) atan()returns a value between -pi/2 ... +pi/2. Called with two arguments (e.g. atan(2,1)) atan() returns a value between -pi and +pi. (This can be useful when transforming from cartesian to polar coordinates). 2.3.2.2 print 1+2,2*3,4/2,2^3 print sin(1.0),cos(pi),tan(3) print asin(0.5),acos(0.7)
Functions
Exponentiation
The exp() functions comes with its inverse: the log() function. log() and exp() operate with the base e
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 17
QTERM-R55 User's Manual (2.71828) which comes as a predefined variable named euler. The exponentiation functions are illustrated by the second qaBASIC example. 2.3.2.3
Integer and Fractional Parts
The functions int() and frac() split their argument at the decimal point (see Example 2). int drops the fractional portion of the argument and returns an integer. frac returns just the fractional portion of the argument. 2.3.2.4
Remainder
To get the remainder of a division use the mod() function. As Example 2 shows, mod(11,4) produces 3, because 11/4 = 2 with a remainder of 3. NOTE: Arithmetic operations using large numbers may cause fractional inconsistencies in your answer. For example, 12000000 / 300000 may yield 40.00000001. This is inherent in floating point arithmetic. 2.3.2.5
Minimum and Maximum
The functions min() and max() return the lower and higher value of their two arguments respectively (see Example 2). 2.3.2.6
Square Root
The square root is calculated by the sqrt() function (see Example 2).
2.4
String Operations
BASIC has always been simple and strong in string processing. qaBASIC also maintains this feature. Example 3: a$="123456" print len(a$)
print left$(a$,2),"-"; print mid$(a$,2,3),"-"; print right$(a$,3) left$(a$,2)="abcd":print a$ print str$(12) print str$(12.123455,"%08.5f") print 2+val("23") print val("e2") instr("Hallo","al") lower$("aBcD12fG") ltrim$(" foo ") print asc("e")
7 This program produces the following output: 6 12-234-456 ab3456 12 12.12346 25 0 2 abcd12fg "foo " 101
2.4.1
Length of a String
The len() function returns the length of the string (see Example 3). 2.4.2
Extracting Parts of a String
There are three functions which return parts of a string: left$(<string>,<length>) right$(<string>,<length>) mid$(<string>,<position>,<length>)
left$() returns the leftmost <length> characters of <string>. right$() returns the rightmost <length> characters of <string>, and mid$() cuts in the middle, returning <length> number of characters starting at <position> characters from the left end of <string>. The first character of a string is at position 1. Furthermore, left$() and its associated functions can even be used to selectively change parts of a string by assigning a string to the function. Example 3 shows that only the two leftmost characters are changed (even though the string "abcd" contains four characters). The same can be done with mid$() or right$(). 2.4.3
Strings to Numbers (and Numbers to Strings)
The str$ ( ) converts its numeric argument to a string (see conversion of 12 in Example 3). Formatting of the number is optionally specified by a second argument ("08.5f" in Example 3). The second argument is essentially a format string as used by the printf() function in the C programming language. A subset of this function is supported by the QTERM-R55. Format: str$ (value, "%Flagfieldwidth. PrecisionArgument")
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 18
8
QTERM-R55 User's Manual
Value is the variable to be converted Flag OPTIONAL – One or more flags can be used to modify the result of the output, as follows – Left adjustment of the output in the specified field width + Number will be printed with a sign space If the first character does not contain a sign, a space will be added 0 Leading zeros will pad the field # A decimal point will always be added. For g and G formats (see below), trailing zeros will be removed Fieldwidth REQUIRED – A number that specifies the minimum width of the field. The output will be printed in a field at least this wide, wider if necessary. The fewer characters than specified. Padding on the right will occur if left alignment (– ) was used. . (period) OPTIONAL (required if precision is used) – Separates the Fieldwidth from the Precision. Precision OPTIONAL – Maximum number of characters to be printed after the decimal point for e, E, and f output. For g and G, it represents the number of significant digits. Argument f f Floating point ouput: (–)xxx.yyy The number of y digits is specified by the Precision 6 is the default precision, 0 will suppress the decimal point unless the # flag is used e, E Floating point ouput: (–)xx.yyyye zz or(–)xx.yyyyE zz The number of y digits is specified by the Precision 6 is the default precision, 0 will suppress the decimal point g, G Floating point ouput: %e or %E are used if the exponent is less than -4 or greater than or equal to the Precision, otherwise use %f. Trailing zeros and decimals are not printed. Value
More examples are listed in Table 2-0. Further information can be found in any textbook on the C programming language. The val()function converts its string argument to a number (see conversion of "23" in the Example 3). Note that "e2" is converted to 0 because this string is not a valid number. Table 2-0. More Examples Using the str$ Function. Print Statements
Output Produced print "=" str$(12.12345,"%08.3f"); print "="
=0012.123= print "=", str$(12.12345,"%8.2f"); print "="
= print "=", str$(12.12345,"%-6.2f"); print "="
=12.12 =
2.4.4
12.12=
Finding Strings in Strings
The instr()function returns the position of its second string argument within the first. instr()returns zero if the string cannot be found. instr("Hallo","al") in Example 3 returns 2 because "al" appears at position 2 within "Hallo". instr("Hallo","Al") returns 0, because "Al" is not contained in "Hallo" (the case doesn't match). 2.4.5
Changing the Case of Strings lower$() and its counterpart upper$() convert their string argument to all lower or all upper case characters respectively. lower$("aBcD12fG") returns "abcd12fg" shown in Example 3. 2.4.6 as
Removing Spaces ltrim$() and rtrim$() are two functions to remove leading or trailing spaces from a string. trim$() removes both leading and trailing spaces. ltrim$(" foo ") returns "foo " (see Example 3) and rtrim$(" foo ") returns " foo". trim$(“ foo “) returns “foo”.
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 19
QTERM-R55 User's Manual 2.4.7
9
The ASCII Character Set Functions qaBASIC offers two functions to work with the ASCII character set. asc() converts a specific character to its ASCII value. print asc("e") returns 101 as a result (see Example 3), because the character "e" has position 101 within the ASCII character set. Appendix A contains a complete 7-bit ASCII chart. Likewise the function chr$() returns the ASCII character for a given position within the character set, e.g. chr$(98) returns "b". 2.4.8
Escape Sequences print "Your number is bigger than 10" else print "Byebye "; print "Your number is less or equal 10" endif
This program produces the following output: Please enter a number 2 Byebye Your number is less or equal 10
Alternatively: Please enter a number 11 Hello Your number is bigger than 10
Example 5:
The most important non-printable characters can be constructed using escape sequences with the \ character: The sequence \n might be used instead of chr$(10) for the newline character. Table 2-1 lists all escape sequences of qaBASIC (these are similar to the sequences used by the C-language).
input "Please enter a number" a if a>10 and a<20 then rem parentheses are optional print "bigger than 10 "; print "but less than 20" fi
This program produces the following output: Table 2-1. Escape Sequences for qaBASIC. Escape Sequence
Resulting Character
ASCII Value
\n newline
10
\t tabulator
9
\b vertical tabulator
11
\v backspace
8
\r carriage return
13
\f form feed
12
\a alert
7
\\ backslash
92
\‘ single quote
39
\” double quote
34
These escape sequences are replaced within every pair of double quotes (""), i.e. within literal strings. User input read with the input statement is not affected in any way.
2.5
Conditions and Flow Control
Example 4: input "Please enter a number" a if (a>10) then print "Hello ";
Please enter a number 11 bigger than 10 but less than 20
Alternatively: Please enter a number 10
Example 6: label loop ? "Enter a string containing \"R55\"" input a$ if (instr(upper$(a$),"R55")<>0) then gosub thanx else print "No, please try again !" endif goto loop label thanx print "Thanks a lot !" return
This program produces the following output: Enter a string containing "R55" ?thequickbrownfox No, please try again ! Enter a string containing "R55" ?jumpedR55overthelazydog Thanks a lot !
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 20
10
QTERM-R55 User's Manual
2.5.1
The if Statement
The if-then statement is necessary for making decisions. The syntax is as follows: if [(]<condition>[)] then <instructions> [else <alternative instructions>] endif
As shown, the parentheses around <condition> are optional. The else <alternative instructions> is also optional. Note that endif can be written as fi. <condition> is described below in Conditions (section 2.5.2). <instructions> and <alternative instructions> can be be any series of BASIC statements. If <condition> evaluates to TRUE, then <instructions> is executed. If <condition> evaluates to FALSE, <alternative instructions> (if included) is executed. 2.5.2
Conditions
Numbers or arithmetic expressions can be compared with the usual relational operators: = (equal), <> (not equal), < (less than), <= (less or equal), > (greater than) and >= (greater or equal). Strings can be compared with the same set of operators, where characters are ordered according to the ASCII character set. For example,("a"<"b") is true because "a" precedes "b" within the ASCII character set. Likewise ("a"="b") is false. More than one comparison can be combined with parentheses () and these keywords: or, and, not. not has higher precedence than and, which in turn has higher precedence than or (in the same way as * precedes + within arithmetic expressions). This means that not a>b or a==c and b>0 is the same as ((not a>b) or (a==c)) and (b>0)
Finally, the enclosing parentheses can be omitted, i.e. if a<10 then ... is a valid statement. 2.5.3
Marking Locations in a Program
The first line in the Example 6 program (label loop) is a label.
qaBASIC does not require line numbers, thus labels are necessary to mark a specific location within your program (however, see the following paragraph). You can compose labels out of letters and digits. The keyword label is required, and the label itself should be unique within your program. qaBASIC allows for line numbering. This feature makes qaBASIC more compatible with traditional versions of BASIC. Line numbers are just special types of labels with the following properties:
• Line numbers can appear only at the beginning of a line.
• Not every line needs a number and line numbers need not be consecutive. 2.5.4
Jumping Around in a Program
A label by itself causes no special action. Only in conjunction with the goto statement (or gosub or restore) does a label have any function. If qaBASIC encounters a goto statement (in the Example 6 goto loop), then it searches for the matching label (label loop) and proceeds to execute at the position of the label. Note that you can even leave (and enter) a for-next loop (see section 2.6) with goto. Closely related to the goto command is the gosub command. If qaBASIC encounters a gosub statement, it searches for the matching label (label thanx in Example 6), and proceeds with execution at the position of the label until it finds a return statement. return makes qaBASIC return to the position of the original gosub and proceed from there. Note that both goto and gosub can be used as on goto and on gosub (see on gosub, on goto (section 2.5.5). 2.5.5 on gosub, on goto
The on gosub statement is followed by a list of labels (sorry,one,two,...) in Example 9 (below). Depending on the value of the expression (number+1), the corresponding label in the list is chosen: for example, if number+1 evaluates to 3, the third label (three) is selected and a gosub to this label is performed. A gosub is always performed, regardless of the value of the expression. More specifically, if number+1 gives anything less or equal to 1, then the first label (sorry) is chosen. If number+1 evaluates to anything greater or equal to the number of elements in the list (which is 7 in Example 9), then the last label (sorry) is chosen. Therefore, the
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 21
QTERM-R55 User's Manual label sorry is chosen whenever the program cannot convert the given number. Note that the on construct can also be used as on goto. 2.5.6
End of Program
The end statement ends your program immediately (see Example 9 below). Note that this will cause the QTERMR55 to enter download mode and wait for a new application program to be sent via the serial port.
2.6
Loops
Example 7: input "Please enter a word" a$ for a=len(a$) to 1 step -1 print mid$(a$,a,1); next a print " is ",a$," reversed !"
If you try this program, you will get this output: Please enter a word: hello olleh is hello reversed !
In the above program, everything from for to next is repeated, while the variable (a) goes from its initial value len(a$) to its final value 1. Note the step clause. The number after step (here: -1) is added to a after every repetition. The step clause makes a go down with every iteration. If the step clause 3 is omitted, step 1 is assumed.
11 endif print "Sorry, can't convert ",num label names data 9,"one","two","three" data "four", "five","six" data "seven","eight","nine"
The following program (Example 9) produces similar output but is written using totally different language constructs. Example 9: label loop input "Please enter a number: " number number=int(number) on number+1 gosub sorry,one,two,three, four,five,sorry goto loop label sorry print "Sorry, can't convert ",number:end label one:print "1=one":return label two:print "2=two":return label three:print "3=three":return label four:print "4=four":return label five:print "5=five":return
These programs produces the following output: Please enter a number: 2 2=two Please enter a number: 3 3=three Please enter a number: 12 Sorry, can't convert 12
2.7.1 Within the for-next loop above the string functions, len() and mid$() are applied (see section 2.4).
2.7
Data and Arrays
Occassionally the need arises to supply a program with initial data. Example 8 program converts numbers to strings. Example 8: restore names read maxnum dim names$(maxnum) for a=1 to maxnum:read names$(a):next a label loop input "Please enter a number: " num num=int(num) if (num>=1 and num<=maxnum) then print num,"=",names$(num) goto loop
Reading Data
Example 8 program converts numbers to their textual representation. For this purpose it needs to know the numbers from 1 to 9 as text. This information is stored with the data lines at the bottom of the program. The program uses the read command to get one piece of data after the other. If you want to deviate from the linear ordering while reading the data statements, you may use the restore statement. In the example above, restore names makes sure that the next read statement reads its data after the label names. 2.7.2
Arrays
In Example 8, the words "one" ... "nine" are stored within the string array names$().
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 22
12
QTERM-R55 User's Manual
LED 1
LED 2
LED 3
LED 4
LED 5
LED 1
LED 2
LED 3
LED 4
SHIFT SHIFT
Figure 2-1. QTERM-R55 Keypad LEDs. Arrays can be used to process large quantities of data. There are numerical arrays as well as string arrays. Both need to be declared prior to their first use. This is necessary because qaBASIC needs to know how much memory has to be allocated for the array. The dim keyword is used to declare arrays. Example 8 uses dim names$(maxnum) to declare a string array. Another example would be dim numbers(200) to create a numerical array with 200 elements numbered 0 to 199. More complex tasks may even require multidimensional arrays with more than one index. dim matrix(10,10) defines a two dimensional array. Up to 10-dimensional arrays are supported by qaBASIC.
2.8
Interaction with System Hardware
Example 10: print date$," ",time$ cursor blink cursor at(2,3) a$="12345":a=4 send #COM1 a$ send #COM1 a a$ = recv$ #COM1 a = recv #COM1 print #COM1 a$,a led 3 on led 4 off backlight on bell pause 10
This program produces the following output (phrases in parentheses describes actions taken by the QTERM-R55, not output to the display): 5 28 08 98 13:51.53 (cursor set to blink) (cursor moves to column 2, row 3) (str$ "12345" sent out COM1) (num 4 sent out COM1) (COM1 input stored into str$ a$) (COM1 input stored into num a) (string in str$ a$ and number in num a is sent out COM1) (LED 3 is turned on) (LED 4 is turned off) (backlight is turned on) (a speaker beep is made) (execution stops for 10 seconds)
2.8.1
Date and Time
NOTE: The date$ and time$ commands are only supported for QTERM-R55 terminals with the optional realtime clock installed. The date$ output string has four fields (see Example 10): 5 is the day of the week (1-7, 1 is Sunday, 7 is Saturday), 28 is the day of the month (01-31), 08 is the month (01-12) and 98 is the year.
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 23
QTERM-R55 User's Manual The time$ output string has three fields: 13 is the hour (00-23), 51 is the minute (00-59) and 53 is the second (0059).
13 The numeric return value contains the requested information. 2.8.4
All fields of date$ and time$ (except the last field within time$) are fixed length; thus it is easy to extract fields with the mid$ function [see Extracting Parts of a String (section 2.4.2)]. 2.8.2
Cursor Appearance
The following cursor commands control the appearance of the cursor: cursor normal cursor blink cursor off
Normal is the standard underscore cursor (default). The cursor may be positioned with the at() command as follows:
Serial Communication
The program may send characters or strings to the serial port with these commands: send #COM1 string$ send #COM1 num string$ = recv$ #COM1 num = recv #COM1
In a string context, send outputs string string$ to the port. In a numeric context, send outputs a character whose ASCII value is contained in num (truncated to an integer, modulo 255). In a string context, recv will grab everything in the serial input buffer (up to 100 characters) and put it into string string$. If no characters are available, this function returns an empty string.
cursor at(X,Y)
where X and Y are the column and row of the desired cursor location, respectively. The at()command can also be used with the print command (see section 2.9.1). These commands are illustrated in Example 10. 2.8.3
Peek
The peek() function is handy for obtaining certain information about the QTERM-R55 hardware. This command can be used to get the number of character rows and columns on the QTERM-R55 display, the number of keys on the keypad and the current cursor position (X or Y). Peeking at COM1 or COM2 returns the number of characters available in the receive buffer for that interface. Peeking at ostick returns the number of operating system time ticks since the unit was powered on. The time interval between ticks is 20 milliseconds. The syntax is: peek(<“keyword”>)
where <keyword> is one of the following: columns rows numkeys cursorX cursorY COM1 COM2 ostick
NOTE: As explained in section 2.1.1.2, string variables may not contain NULL characters. Therefore, if the serial stream might contain NULL characters, recv must be used in a numeric context. In a numeric context, recv pulls one character from the serial port and puts its ASCII value into variable num. If no character is available, this function returns the value 256. print and input also work with the serial port by inserting the serial port identifier: print #COM1 string$,num input #COM1 a$
Example 10 illustrates the serial communications commands. Programming serial communications for the EIA-485 interface requires special attention. See section 3.4.3 for more information. 2.8.5
Keypad LEDs
The LEDs may be turned on and off with the following commands: led <lednum> on led <lednum> off
<lednum> is an expression yielding an integer from 1 to 6. See Figure 2-1.
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 24
14
QTERM-R55 User's Manual
Example 10 program illustrates the led commands.
2.8.11 Pause Execution
2.8.6
The following identical commands delay program execution (see Example 7):
Display Contrast
The display contrast may be adjusted with the contrast commands (see Example 10): contrast up contrast down
The new contrast setting persists until the terminal is powered down. To permanently adjust the contrast of the display, use the Power-On Setup facility (see section 1.1). 2.8.7
Display Backlight
The display backlight may be turned on and off with the backlight commands (see Example 10): backlight on backlight off
2.8.8
Key Click
If enabled, the Key Click feature causes the terminal speaker to emit a short beep whenever a key on the keypad is pressed. This feature may be turned on and off with the following commands: keyclick on keyclick off
The Key Click setting persists until the terminal is powered down. The default setting (when the terminal is powered on) can be selected in the Power-On Setup facility (see section 1.1). 2.8.9
Keypad Backlight
If the QTERM-R55 was purchased with the keypad backlight option, the keypad backlight may be turned on and off with the keypad commands: keyback on keyback off 2.8.10 Speaker The internal speaker beeps in response to either of the following commands (see Example 10): beep bell pause <time> wait <time> <time> is the amount of time execution is delayed in seconds. 2.8.12 Defining the Keyboard Input from the keyboard can be done in two ways. The first method is to check periodically if there has been a key pressed by using the iskeypressed$ string function. If there has been a key pressed, the resulting string from this function will be the value of the key that was pressed. Otherwise, the string will be empty. Here is an example of how this function can be used. inChar$ = iskeypressed$ if (len(inChar$) <> 0) then send #COM1 inChar$ endif
The other method of getting input from the keypad is to use the inkey$ string function. This function will stop execu tion of the BASIC program until the user of the terminal presses a key on the keypad. When he does, the value of the key is returned as the result. print inkey$
The keyboard has a default definition for characters that are returned to the input functions. This definition contains the characters that are printed on the standard legend; see Figure 2-1. However, as the QTERM-R55 legends are completely customizable, a qaBASIC facility has been created to allow for redefinition of the characters that are returned by input functions when keys are pressed. The command to accomplish this is described below. keydef[release][shift] <col>,<row>,<string$>
This command redefines the key at location (col, row), where col and row are the one-based column and row of the key on the keypad. For example, the upper left key on the 40-key keypad is key 1,1, and the lower right key is key 5,8. The string$ is sent to the BASIC input functions when the key is pressed. The optional release and shift modifiers change the definition for a key release event and/ or a shifted key event.
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 25
QTERM-R55 User's Manual
1
2
Columns 3
15 This program produces a screen resembling the following layout:
4
5
1
1,1
2,1
3,1
4,1
5,1
2
1,2
2,2
3,2
4,2
5,2
3
1,3
2,3
3,3
4,3
5,3
4
1,4
2,4
3,4
4,4
5,4
5
1,5
2,5
3,5
4,5
5,5
6
1,6
2,6
3,6
4,6
5,6
7
1,7
2,7
3,7
4,7
5,7
8
1,8
2,8
3,8
4,8
5,8
1 -- Setup 2 -- Save 3 -- Quit Your choice:
Rows
The clear screen command erases the screen.
Figure 2-2. Key Locations Key release events can be enabled with the keyrelease on command. The keyrelease off command disables them. The default is disabled. 2.8.13 Shift State The state of the shift key (on or off) may be altered with the shiftstate commands: shiftstate on shiftstate off The shift state is toggled automatically when the shift key is pressed (See Figure 2-1). When the shift state is on, the shifted key definition is used to determine the key input (seeDefining the Keyboard (section 2.8.12).
The at() clause in print or input statements allows you to move to any location (specified by the two arguments of the at() clause) on your screen. This is illustrated by Example 8. Note that at() can be written as @() also, and that the row and column addresses are zero-based (i.e. the first row is row 0, etc.). 2.9.2
Autowrap and Autoscroll autowrap and autoscroll are screen display commands that allow the qaBASIC program to control how the display responds when printing and reaching the end of a line or the bottom of the screen. If the autowrap is on when the cursor reaches the end of the screen, then the cursor will be placed at the beginning of the next line (unless the line is the bottom line and autoscroll is off, in which case the cursor will be placed at the beginning of the bottom line). If autoscroll is on when the cursor reaches the bottom line of the display and receives a newline character, it will scroll the screen up (leaving the cursor in the same horizontal position). The following syntax is used for autowrap and autoscroll statements. autowrap on autowrap off autoscroll on autoscroll off
2.9
Printing and Controlling the Screen
2.10
2.9.1
Printing Output
The following commands were added to allow for a limited “file system.” Although one may read and append, a file can’t be overwritten because of the nature of flash memory. The entire file space must be erased in order to rewrite a file. The command to erase is given as a system command (described below). Up to nine files may be used for storage of data.
Printing output at specific locations is helpful for interactive programs. Example 11: clear screen print at(4,0) "1 -- Setup" print at(4,1) "2 -- Save" print at(4,2) "3 -- Quit" input at(0,3) "Your choice: " a$
Nonvolatile Storage of Data open #<1-9> [, “r” or “a”] opens a file for reading (“r”) or appending (“a” -- append is default). Example: open #1, “r”
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 26
16
QTERM-R55 User's Manual print #<1-9> <str$> prints str$ (writes) to the end of a currently open file. input #<1-9> <str$> reads from a currently open file until it reaches a newline character. eof (#<1-9>) determines if the end of a file has been reached when reading. close #<1-9> closes a currently open file. system (“erase filespace”) or system$ (“erase filespace”) erases the filespace. system (“avail filespace”) or system$ (“avail filespace”) is a pessimistic indication of how much free area exists for writing more data. seek #<1-9> to line <expression> allows random access to an open file in the filespace. This command will evaluate <expression> to a positive integer n (dropping fractions) and will position a file pointer at the beginning of the nth line in the specified file. Subsequent input command will read the file at this location. A line is defined as any sequence of bytes ending in the newline character. seek #<1-9> <expression> lines is similar to the seek command above, except that it positions the file pointer relative to the current file pointer location rather than to an absolute line number. This command evaluates <expression> to a positive or negative integer n (dropping fractions) and positions the file pointer to the beginning of the nth following line (for positive n) relative to the current file pointer location in the specified file. Subsequent input command will read the file at this location. Print command can only be used to write to a file if it has been opened for appending and the current file pointer location is at the end of the file.
Note that the print and input commands automatically position the file pointer to the beginning of the following line after reading/writing the file.
2.11
Error Handling
There are two types of error handling statements that can be used: on error goint <label> on lowbat goint <label> The first statement sets the terminal so that if it encounters a non-fatal error, it will begin execution of a special subroutine starting at the location given by the label rather than sending the error description to the primary serial port and aborting the program. Any subsequent errors encountered while handling that error, however, will not call the error handler but a description will be sent to the primary serial port. The error that caused the interrupt may be found in the error handling routine by using the command peek$(“error”). The resulting string of this function will contain the error information. The lowbat handling routine will also call a subroutine starting at the location given by the label, assuming the unit is designed for battery operation. If there is no error handler, and the unit’s battery is getting low, it will beep periodically instead. IMPORTANT NOTE: While in these interrupt handlers, one should not attempt to perform any type of input processing from the keypad or communications port that suspends program flow. Interrupt handlers should be simple and quick and should return with a gosub at the end of the routine (subroutine calls are allowed inside the handler and will be handled appropriately).
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770