The HP48 Homepage  
 

 
News My Programs Software Programming Articles Links Sitemap Search Feedback    
   
 

Programming in User RPL - Getting input

The most simple and obvious way of getting input for HP programs is the stack. The user should put the arguments on the stack, run the program, and the results are returned on the stack. However, sometimes this is not the best way. When there are many arguments, it may be difficult for the user to remember the order, the type, and so on. So, there are better ways of getting the input. And so there are better ways of displaying output, which you will see on next page.

INPUT: Enter Input by the Command Line

The first command to get input is INPUT (obvious, isn't it?). INPUT allows you to display a message and then edit a string using the command line. It takes two arguments: on level 2, the message to be displayed on the top of the screen. I may be empty, and may contain newline characters for multi-line messages.

On level 1, there are two options. You can enter a string with the initial text, which the user will then modify (it may be blank) or a list in the form:

    { "string" { row column } mode(s) }

The arguments may be in any order, and you don't have to specify all the arguments above, just one if necessary.

  • "string" is the initial text that will be displayed.
  • row and column specify where the cursor will appear (the default is in the end of the string) and wheter it is on insert or overwrite mode (the default is insert). Row numbers start at 1 for the topmost row, and column numbers start at 1 for the leftmost character. Row 0 means the bottom row, and column 0 means the last character. Instead of specifying the row and column, you may substitute the list for a single real value, the caracter position starting at the first character. To make the cursor start in overwrite mode, make the first number negative.
  • mode(s) are zero or more of the following:
    • A greek alpha symbol (alpha left-shift a) will start the editor with the alphabetical keyboard on. This is very useful if the user is supposed to enter a string.
    • ALG will cause the editor to start with ALG mode on.
    • V will check the syntax of the entire command line when ENTER is finally pressed, in the same way that the command line editor normally does, disallowing an edit if there is a RPL syntax error.

INPUT returns what the user entered as a string. Normally, you would use OBJ-> to make it the way you need, like a number.

When the user presses CANCEL the first time during INPUT, the input line is cleared. If the user presses CANCEL again, then the rest of the program is cancelled.

An example of use in a program:

    << "Enter A, B, C"
       { ":A:
       :B:
       :C:" { 1 0 } V }
       INPUT
    >>
    

(insert newlines between the three lines of the input string.)

When executed, the program will show:

When run, the program will return a string

    ":A:1
    :B:2
    :C:3"
    

you can then run OBJ-> an you'll get:

    4:
    3:      A: 1
    2:      B: 2
    1:      C: 3
    

CHOOSE: Presenting Options

Another command related to input is CHOOSE. You may see a example of CHOOSE by pressing right-shift 7. Yes, it is that box with several options. They are very easy to create.

To use CHOOSE, you just need three arguments. On level 3, a string with the title. If it's an empty string, the choose box will have no title. On level two, a list containing the options. And on level 1, a number corresponding to the initial position where the hightlight should be.

If the user chose an item, then CHOOSE will return the chosen item on level 2 and 1 (true) on level 1. If he cancelled, only 0 (false) will be returned to level 1. The chosen item will be returned as it was entered on the list. However, you can change this behavoir. You may substitute every item on the list of option by a list with two objects: the first one will be shown, and the second will be returned if it that item was selected.

Let's see an example of CHOOSE. The following program will display a choose box which lets you to select your angle mode.

    << "ANGLE MODE"               @ Title
       { { " Degrees" DEG }
         { " Radians" RAD }
         { " Gradians" GRAD } }   @ Items
       1                          @ Initial position
       IF CHOOSE THEN EVAL END
    >>
    

INFORM: Forms

And now, we will see the command for getting input: INFORM. You may see an example of INFORM by pressing right-shift 7 ENTER. Yes, INFORM creates that menus that you have seen many times. And it is not so difficult as it seems.

INFORM takes five arguments: on level 5, the title, which will be displayed in the top of the screen. On level 4, the specification of the fiels. On level 3, the format. On level 2, the values that will be used if one or all of the fiels are reset. And on level 1 the initial values.

The field specifications is a list of the form { field1 field2 ... fieldn}, where each field is one of the following:

  • "label"
  • { "label" "help" }
  • { "label" "help" type1 type2 ... typen }
  • { }

"label" is a string which will serve as the title of the label. "help" is the text that will be displayed on the bottom line when the field is specified. The type specifications are zero or more real numbers representing the types of objects allowed on the field. If unespecified, then all object types are valid. You can find a list of object types on appendix H of the manual, under command TYPE. If a field specification is an empty list, then the immediate left field (if present) is expanded to occupy the unespecified field space.

Format may be one of the following objects:

  • { }
  • columns
  • { columns }
  • { columns width }

Where columns is the number of columns that the display has. The default is one. width is the tab width between the left edge of each title and each field. This allows vertical alignment of the fields. The default is three.

The reset and initial values are either empty lists or lists containing exactly one object foer each field. This values will be used when one or all fields are reset, or when the display is shown. If you wanta field to be left blank, specify NOVAL as its value.

If the user filled the form, then a list with the values will be returned to level 2, and 1 (true) to level 1. If he cancelled, then 0 (false) will be returned to level 1. A field that wasn't specified will be returned as NOVAL.

Let's see an example of INFORM, because I'm sure you didn't understant a thing of the above. But you will understand if you see an example. This program will display the following dialog box

and will then calculate the distance of the points if the user doesn't cancel the form.

    << "DISTANCE OF TWO POINTS"    @ Title
       { { "x1:" "" 0 }           @ Title, help and only real numbers
         { "y1:" "" 0 }
         { "x2:" "" 0 }
         { "y2:" "" 0 } }
       { }                        @ The default format is good for us
       DUP DUP                    @ No reset or initial values
       IF
          INFORM            @ Calculate only if the user didn't cancel
       THEN
          OBJ-> DROP              @ Split the values
          ROT - SQ 3 ROLLD - SQ + v/   @ Calculate
       END
    >>
    

Now, let's change a little our program, so that it displays with two columns, like the following:

    << "DISTANCE OF TWO POINTS"   @ Title
       { { }                      @ Blank line at top
         { "x1:" "" 0 }
         { "y1:" "" 0 }
         { "x2:" "" 0 }
         { "y2:" "" 0 } }
       2                          @ Two columns
       { } DUP                    @ No reset or initial values
       IF
       .
       .                          @ The rest is equal and
       .                          @ I'm not typing it again
    >>
    

How do you display checkboxes in INFORM?
Using only INFORM, it is impossible. However, there is a library that allows you to create input forms with checkboxes, choose boxes, etc. Click here to download it.

Getting Keypresses

To get keys, we use a special form of the command WAIT. Normally, this command takes the number of seconds from the stack and delays program execution for that lenght of time. However, if you give 0 as input for it, it will wait until a key is pressed, and will return the code of that key.

Key codes are in the form:

   rc.p
   ^^ ^
   || |
   || +--- Plane (see below)
   |+----- Comlumn
   +------ Row

row and column correspond to the physical location of the key, starting at the top left. plane is a number from one to six representing the key's modifiers.

planeMeaning
1Mo modifier
2Left-shift
3Right-shift
4Alpha
5Alpha and left-shift
6Alpha and right-shift

This way, MTH is key 21.1; RCL is 32.3; x2 is 44.2; and so on.

The following code will wait for a key, and exit if ENTER is pressed.

    << 0 WAIT
       IF
          51.1 ==
       THEN
          KILL
       END
       .
       .     @ Rest of program
       .  
    >>
    

Another command related to keys is KEY (no comments). It is used in loops, when you want a loop to stop. The use is in the form:

    << DO
          .
          .     @ Your code here
          .
       UNTIL
          KEY
       END
    >>
    

The loop will run until a key is pressed. The key pressed will be returned, but only the row and column, because the modifiers keys will also stop the loop. Thus, if you press alpha, the loop will be stopped and it will return 61.

Exercises

  1. Usign three INPUTs, write a program to calculate the roots of quadratic equation, displaying the results on the stack.
  2. Write a progrm that uses INFORM to let the user enter a, b and c, calculates the roots of the quadratic equation and uses INFORM again to display them. The screens should be like the following:

     
  3. Write a program that creates a screen like the following

     


    and then solves the system



    and returns x and y on the stack.
  4. Write a program that creates a screen like the following



    and then calculates the inverse matrix. After the calculations are done, use a screen like the above the give the results.


Answers to exercises




Previous Page Next Page
 
 

This page was created by Eduardo M Kalinowski