Coexisting with Android

From RFO-BASIC! Manual
Jump to: navigation, search

An RFO-BASIC! program runs on an Android device that is doing many other things — such as maintaining contact with a WiFi or Bluetooth network or a cell tower — and may be running under battery power that the program should take care to conserve.

Conserving battery[edit]

Computing requires electric power; the BASIC program can use less power by spending more time in the PAUSE statement. Unfortunately, each execution of PAUSE is for a specific interval. The interval can be a variable or expression but it cannot adapt to things that happen during the interval. Moreover, PAUSE cannot be interrupted.

PAUSE totally suspends BASIC, not just execution of statements but things that happen in parallel, such as monitoring of keystrokes. A pause longer than a second (PAUSE 1000) may give the impression that the program has crashed, and may make the program lose keystrokes. For programs that do not rely on user input, PAUSE 5000 may be feasible. The program's operations are inside a main loop with the following form:

 <BASIC statements>
 PAUSE 5000
UNTIL 0  % That is, forever

Shorter pauses increase responsiveness to real-time events such as screen touches and key presses. If the main loop needs executing only every five seconds, the program can be more responsive to real-time events such as screen touches and key presses by waking every 0.1 second during the desired interval. This awakening not only permits interrupts but gives the program a chance to break out of the pause loop. For example:

 <BASIC statements>
 FOR I = 1 TO 50
  PAUSE 100
  IF <something happened that requires main-loop processing> THEN F_N.BREAK
UNTIL 0  % That is, forever

Battery usage depends on the ratio of time spent computing, to time spent pausing. A typical loop with PAUSE 100 spends almost all of its time in PAUSE. Increasing the pause time further does not change the ratio substantially.

Gaming and measurement programs may require that the main loop execute at a constant rate. In this case, the PAUSE statement should deduct time spent in the main loop (but not specify a negative amount of time):

 InitialTime = CLOCK() % Milliseconds since most recent start-up
 <BASIC statements>
 TimeSpentComputing = CLOCK() - InitialTime
 PAUSE MAX(100 - TimeSpentComputing, 1)
UNTIL 0  % That is, forever

Battery query[edit]

The SYSTEM unit of RFO-BASIC! lets programs query the power status of the Android device, using code such as this:

FN.DEF BatteryQuery$(q$)
SYSTEM.WRITE "cat /sys/class/power_supply/battery/" + q$
StartTime = CLOCK()
UNTIL r | CLOCK() - StartTime > 500
FN.RTN out$

A call to BatteryQuery$("status") returns Discharging or Not charging when the Android device is plugged into external power. A call to BatteryQuery("capacity") returns the percentage of battery charge.

When the Android device is drawing external power, there is no need to conserve power at the expense of responsiveness, as the total power used is tiny. When the device is operating from battery, there are many ways to conserve power, including longer pauses, reducing screen illumination with a statement such as GR.BRIGHTNESS 0.25, or omitting the use of user-friendly features such as confirmation with text-to-speech.


Manual contents
BASIC features Audio playerBluetoothData structuresGraphicsHTMLInterrupt routinesSocketsSQLUser-defined functions
Programming notes Coexisting with Android