Return to the Index
<==

Example programs

This chapter contains example programs written in OPL. The programs are not intended to demonstrate all the features of OPL, but they should give you a few hints. To find out more about a particular command or function, refer to the alphabetic listing.

There are some other example programs in the `Advanced topics' chapter.

When you're typing in

Errors

The following programs do not include full error handling code. This means that they are shorter and easier to understand, but may fail if, for example, you enter the wrong type of input to a variable.

If you want to develop other programs from these example programs, it is recommended that you add some error handling code to them. See the chapter on error handling for further details.


Countdown Timer


Dice

When the program is run, a message is displayed saying that the dice is rolling. You then press a key to stop it. A random number from one to six is displayed and you choose whether to roll again or not.

Random numbers: in this example, the RND function returns a random floating-point number, between 0 and 0.9999999... It is then multiplied by 6, and 1 is added, to give a number from 1 to 6.9999999... This is rounded down to a whole number (from 1 to 6) by assigning to the integer "dice%".


Birthdays

This procedure finds out on which day of the week people were born.

The DOW function works out what day of the week, from 1 to 7, a date is. The DAYNAME$ function then converts this to MON, TUE and so on. MON is 1 and SUN is 7.


Data files

The following module works on a data file called "DATA", containing names, addresses, post codes and telephone numbers. It assumes this file has already been created with a statement like this:

CREATE "DATA",A,nm$,ad1$,ad2$,ad3$,ad4$,tel$
To use the "DATA" file which the Database application uses, you need to open ""\DAT\DATA.DBF"".

The first procedure is the controlling, calling procedure, offering you choices. The next two let you add or edit records.


Re-order

When you use the Database application and enter or change an entry, it goes to the end of the database file. However, if, in your address book, each entry begins with a person's second name for example, "Tate, Hazel" you can use this program to re-order all of the entries. This doesn't change the way you find an entry, but after running it you can step through it like a paper address book, or print it out neatly ordered.

This procedure can be used as required for any data file in internal memory or on Ram SSDs. If used on a data file held on a Flash SSD it would use up disk space each time you run it. The dialog it shows is set to show data files used by the Database.

You can adapt this procedure to sort other types of data files in other ways.

If you try to reorder a file which is already open (ie shown in bold on the System screen) you will see a `File or device in use' error. Move the highlight onto the file's name in the System screen, use the Delete key to exit the file, then try again.


Stopwatch

Here is a simple stopwatch with lap times. Note that the Series 3a switches off automatically after a time if no keys are pressed; you may want to disable this feature (with the `Auto switch off' option in the System screen) before running this program.

Each timing is only accurate to within one second, as the procedure is based on the SECOND function.


Inserting a new line in a database

If you insert a new label in a database, the entries will no longer match up with the labels. Rather than using the `Update' option on every entry, to insert a suitable blank line in each one, you can use this program to do this for the entire data file.

The Database allows you to use as many lines (fields) as you want in an entry (record); OPL can only access 32 fields. This program only lets you insert a new field in the first 16 fields, although you can adapt the program simply to check up to 31 fields.

If, in the Database, you enter a line longer than 255 characters, it is stored as two fields, with a character of code 20 at the start of the second field. This program correctly handles any such fields.

The program checks that the 17th field is blank, as it will be overwritten by what was the 16th field. If a long entry has a 17th field, and it contains text, the program skips this entry. The rest of longer entries even if there are more than 32 fields will be unchanged.

If you insert a new field at a position below the last label, the Database will not show it, even when using `Update'.

The maximum record length in OPL is 1022 characters. The OPEN command will display a `Record too large' error if the file contains a record longer than this.


Bouncing Ball


Circles

Here are two example programs for drawing circles the first hollow, the second filled:

If you use "gUPDATE OFF" after the "IF DIALOG" line, and "gUPDATE ON" before the "ENDIF", the procedure will run a little faster. However, all but the smaller circles will be drawn rather jerkily, piece by piece.


Zooming

For each of the three types of status window, this program changes the font to implement zooming.

Press Psion-Z to cycle between small, medium and large fonts, and Shift-Psion-Z to cycle in the other direction. Esc changes to the next status window.

As well as changing the font and style for the text window (for PRINT etc.), the FONT command automatically changes the default graphics window size (ID=1) and the text window size to fit exactly in the space left by any status window. (A special feature not used here is that "FONT -$3fff,0" just changes the window sizes without changing font).

The procedure "dispinfo:" uses the command SCREENINFO to display the margin sizes in pixels between the default window and the text window, the text screen size in character units, and the text screen's character width and line height in pixels.


Animation example

This program requires five bitmap files "one.pic" to "five.pic". Each of these would differ slightly they might, for example, be five `snapshots' of a running human figure, each with the legs at a different point in their cycle.

The program copies each bitmap into a window of its own, then makes each window visible in turn, each time slightly further across the screen.

To make bitmap files, first draw the pattern you want with any of the graphics drawing commands. (Use "gLINEBY 0,0" to draw single dots.) When the pattern is complete, use gSAVEBIT to make the bitmap file. For advanced animation, you could use a sprite as described in the `Advanced Topics' chapter.


Two-voice "ice-cream van" sound

The following program plays a rising and falling scale. It uses the amplifier-driven loudspeaker device (with device driver SND:) which allows you to play tunes using two-note chords ie it has two voices.

This program uses I/O keywords as described in the `Advanced topics' section. Take care to enter them exactly as shown here.

notes1%() and notes2%() are set up to hold "len1%" and "len2%" notes to be played on voice 1 and voice 2 respectively. The number of notes to each voice must not exceed 16384.

Each note is composed of two consecutive integers in the array with the first of each pair giving the frequency in Hz (middle A is 440Hz) and the second giving the note duration in quarter-beats per minute.


<== Return to the Index