Writing Interactive Programs

Introduction

Writing interactive programs can be challenging because the BASIC program can block the execution of other parts in the system. For example, the INPUT functions will hold the BASIC program at that line. If an interrupt occurs (sensor alarms, PIN code requests, etc.) the request does not get fulfilled until the INPUT times out or finishes.
The programmer should keep in mind:

  • Avoid using endless loops
  • Use one of the schedulers to run interactive functions
  • Always use TIMEOUT during input

Schedulers

There are 5 different schedulers that can be used to schedule operations:

  1. @IDLE, slave
  2. @ALARM
  3. @SENSOR, nextsns
  4. @INQUIRY, inquiry
  5. @PIO_IRQ (external trigger)

Example

The slave function opens the Bluetooth SPP port for incoming connections for a specified time. The function itself returns immediately allowing other operations to continue while the system accepts incoming connections. After this time, the @IDLE routine will be started again. The @IDLE must return before that happens. The following example allows the reading of a command from the UART while the system is idle (or accepting incoming connections). Note: timeout expires before slave expires to restart @IDLE.

@IDLE 10
10 A = slave 10
11 TIMEOUTU 8
12 INPUTU $0
13 IF $0[0] <> 0 THEN 20
14 RETURN

0 REM check for 'A' 'B' and 'C'
20 IF $0[0] = 65 THEN 24
21 IF $0[0] = 66 THEN 30
22 IF $0[0] = 67 THEN
23 RETURN

24 PRINTU "> "
0 REM real stop
25 INPUTU A
26 IF A <> 0 THEN ok:
27 RETURN

30 PRINTU "Returning"
32 RETURN

35 PRINTU"ALARM has been
36 PRINTU" scheduled
37 ALARM 5
38 RETURN

The alarm scheduler can be used the same way, as in this example:

@ALARM 50
50 ALARM 6
51 TIMEOUTU 5
52 INPUTU A
53 RETURN

This is the approach we had taken in our Command Line software.

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