Article 1967 of comp.sys.handhelds:
Date:    Fri, 15 Jun 90 17:04:18 EDT
>From:    Michael Woodhams <michael@astro.princeton.edu>
Subject: HP48sx

Secondly, I have found the following programs useful. This is my first
major programing project on the HP48sx, and I have since thought of
several ways of improving the program (primarily using the port 0
memory and saving the screen and heirarchical list in variables, and
secondly spliting the program into several subprograms.) However, I am
leaving to go on holiday tomorrow for a month, and I do not have time
to make the changes before I go.

If it is too difficult for you to repost these for some reason, send
me mail, and I will do it myself after my holiday.

Michael Woodhams.

michael@astro.princeton.edu


Below are listings of two programs for the HP-48SX. The second one
acts as a full-screen menu of physical constants and units. Running
the program turns the top 5 rows of the keyboard into menu keys, and
displays the menu items in the top part of the screen, leaving the
level 1 stack entry vissable. After a key press, this menu display
disappears, and if the key was from the top 5 rows, the appropriate
constant is put on the stack or a unit is applied in the normal
manner. (unshifted - multiply by the unit, right shift - divide by the
unit, left shift - convert level 1 entry to this unit.) I assign this
program to the 'CST' key and leave my calculator in user mode, but it
could be used in other ways. (Warning - whatever is on the graphics
screen is overwritten.)

I am an astrophysics student, and this is reflected in the choice of
units and constants in my menu. Other people will have different
preferences, so the first program is one that will create a program
like the second one, with your own choice of menu items. It first
prompts for labels for the menu display, and then for the commands to
be executed by each key. There is no error checking in creating the
display, so make sure your labels are short enough to fit in an
ordinary menu box. (Except for the enter key, which has a larger box.)
If you avoid wide characters like "M" and "W", this should be 5
characters per label. The command for each key is assumed to be either
a unit or a constant. For units, enter the standard abbreviation. For
constants, enter the constant. I think the program would work if you
entered a command instead of a constant, but I haven't tried it.
Finally, note that the program refers to the "+/-" key (to the right
of the enter key) as "5,3" because it seems more intuitive to me, and
because it simplifies the program logic. The calculator, however,
thinks this is key 5,2 because it is the second key in the 5th row.

I have not make allowance for shifted keys except for units. For just
a few keys, this could be done by editing the menu program and
inserting the desired commands in the hierarchical list. If you press
a shifted key for an ordinary command, or an alpha key for a unit
command, the program will crash with several new items on the stack.
These can be dropped, and the calculator will be back in its original state.

The programs are entered by hand, as I don't have the hardware
required to dump the program to computer electronically. There may be
typos. Anything following a "%" in the listing is a comment.


The Compiler Program:

[ Use program attached below not this one -- Wayne ]
<<
  " << PICT LCD-> {#0 #0}"	% enter the first part of the menu
				% program onto the stack as a string
  GROB 21 7 FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1
  -> block			% store a 27 by 7 black block
    <<
      ERASE			% clear screen
      0 32 FOR J		% vertical loop (step 8)
        0 110 FOR I		% horizontal loop (step 22)
          PICT
          I R->B J R->B 2 ->LIST	% turn I, J into co-ordinates
          block
          REPL			% draw block at I, J
          PICT			% put PICT on stack for latter use
          J 8 / 1 + ->STR	% turn J (position in pixels) into a 
				% keyboard position
          "," +
          I 22 / 1 + ->STR +	% same for I
          " Label?" +		% construct prompt string
          { \alpha }
          INPUT			% input label
          1 ->GROB		% turn label into small lettering GROB
          DUP SIZE		% extract the size of the label
          #2 /			% divide height by 2
          J 4 + R->B		% find the middle of the box vertically
          SWAP -		% take difference to center string in box
          SWAP			% swap to get the horizontal size
          #2 /			% divide width by 2
          I
          IF 'I!=0 OR J!=32' ->NUM 	% special case: enter key
          THEN
            11			% normal key is 21 pixels wide, half
				% this is 11.
          ELSE
            22 'I' STO+		% enter key. Increment I.
            PICT
            I R->B J R_B 2 ->LIST
            block		% draw next box over
            REPL
            {#21 #32} {#21 #38} LINE	% fill in the pixels between
				% the boxes to make one big box.
            22			% half of the big box is size 22
          END
          SWAP -		% take the difference to center horizontally
          SWAP			% swap to get the order of co-ords right
          2 ->LIST		% turn them into a coordinate list.
				% Now we have the coords to draw the
				% label centered in the box.
          SWAP			% swap co-ords with the label GROB
          GXOR			% draw the label (using PICT from
				% the stack at level 3.)
        22 STEP			% step in the horizontal direction
      8 STEP			% step in the vertical direction
    >>
  PICT				% extract the menu display from the
  {#0 #0} {#130 #39}		% graphics screen
  SUB				
  ->STR +			% turn it into a string and concatenate
				% to the program segment above.
  " REPL {#0 #0} SWAP REPL	% the next program segment
    {#0 #0} PVIEW 0 WAIT
    IF DUP 60 > THEN DROP ELSE "
  +				% concatenate with the rest of the program
  { }				% Start the outer-most (menu) level of
				% the commands list
  1 5 FOR row
    { }				% start a row-level commands list
    1 6 FOR col
      IF 'row==5 AND col==2' ->NUM
      THEN			% special case for "enter" again
        3 'col' STO
      END
      "(C) M Woodhams.		% a random item to be put on the stack
       May be copied		% and dropped immediately inside the 
       May not be sold."	% loop.
      DO
        DROP			% drop top of stack (junk or previous
				% incorrect keystroke)
        "UNIT (Y/N)?" 3 DISP	
        0 WAIT			% get keystroke
      UNTIL
        DUP DUP 32.1 ==
        SWAP 52.1 == OR		% keep looping until "Y" or "N"
      END			% leave loop with either 32.1 ("N")
				% or 52.1 ("Y") on stack.
      row ->STR			% start building prompt string
      "," +
      col ->STR +
      SWAP			% swap Y or N to top of stack
      IF
        32.1 ==			% if "N" then constant
      THEN
        " Const" +		% finish prompt string
        { V } INPUT
        ->OBJ 1 ->LIST		% turn the result into a one item list
      ELSE			% a unit has been requested
        " Unit" +		% finish prompt string
        { \alpha } INPUT
        -> unit			% store in unit temporarily.
          <<			% build a list of three programs to
				% perform the unshifted and shifted
				% functions of a unit menu key.
            " << 1_" unit + " * >>" +
            OBJ->		% make unit multiply program
            " << 1_" unit + " CONVERT >>" +
            OBJ->		% make unit convert program
            " << 1_" unit + " / >>" +
            OBJ->		% make unit divide program
            3 ->LIST		% turn them into a list.
          >>
      END
      1 ->LIST +		% add the newly formed list (which
				% contains the commands for one key)
				% to the list of commands for keys
				% for the whole row. (This is a list
				% of lists.)
    NEXT			% next row
    1 ->LIST +			% add the newly formed row list
				% (with the commands for all the keys
				% in one row) to the master list. (This
				% is a list of lists of lists.)
  NEXT	
  -> STR +			% turn into a string, and concatenate
				% with the program so far.
  " OVER 10 / IP GET		% the final menu program segment.
    OVER 10 / FP 10 * IP GET
    SWAP FP 10 * GET
    EVAL END
    >> "
  +				% concatenate with program so far
  ->OBJ				% convert from a string to a program
>>				% exit with the menu program on the stack.


Sample menu program:

<<
  PICT				% PICT on stack for latter use
  LCD->				% make copy of stack display
  {#0 #0}
				% this is the display made by the
				% compile program. I have not typed it
				% out in full.
  GROB 131 40 FFFFFDFFFF7FFFFFDFFFF7FFFFFDFFFF70
              FF3FFDFF8F7FF5FFDA3737FF1FFDF7BF70
              FFDFFDF7FF7FF5FFDABAA6FFDFFDF7DF70
              FFDFFDF79F7FF1FFD83327FF1FFDF7EF70
              etc.
  REPL				% put menu display over stack display
  {#0 #0}
  SWAP
  REPL				% put stack + menu in PICT
  {#0 #0} PVIEW			% display PICT
  0 WAIT			% wait for keystroke
  IF				% if in row 6 or greater, quit
    DUP 60 >
  THEN
    DROP
  ELSE				% otherwise, put hierarchical list
				% onto the stack.
    {				% start of main list
      {				% start of first row sub-list
        {'299792458_m/s'}	% first row, first key command sub-sub-list
        {'6.672E-11_N*m^2/kg^2'}% etc. First row is all constants.
        {'6.62618E-34_J*s'}
        {'1.054589E-34_J*s'}
        {'1.602189E-19_C'}
        {'1.38066E-23_J/K'}
      }				% end of first row sub-list
      {				% start of second row sub-list
        {'.000000056703_W/(m^2*K^4)'}
        {'1.672649E-27_kg'}	% all constants again
        {'9.010953E-31_kg'}
        {'1.989E30_kg'}
        {'695990000_m'}
        {'3.826E26_W'}
      }				% end of second row sub-list
      {				% start of third row sub-list
        { << '1_s' * >> << '1_s' CONVERT >> << '1_s' / >> }
				% a sub-sub-list of three programs for
				% the first key of the third row. These
				% execute the normal unit commands.
        { << '1_m' * >> << '1_m' CONVERT >> << '1_m' / >> }
				% all the third and remaining rows are
				% unit keys.
        { << '1_kg' * >> << '1_kg' CONVERT >> << '1_kg' / >> }
        { << '1_m/s' * >> << '1_m/s' CONVERT >> << '1_m/s' / >> }
        { << '1_J' * >> << '1_J' CONVERT >> << '1_J' / >> }
        { << '1_K' * >> << '1_K' CONVERT >> << '1_K' / >> }
      }				% end of third row sub-list
      {				% start of fourth row sub-list
        { << '1_yr' * >> << '1_yr' CONVERT >> << '1_yr' / >> }
        { << '1_cm' * >> << '1_cm' CONVERT >> << '1_cm' / >> }
        { << '1_g' * >> << '1_g' CONVERT >> << '1_g' / >> }
        { << '1_cm/s' * >> << '1_cm/s' CONVERT >> << '1_cm/s' / >> }
        { << '1_erg' * >> << '1_erg' CONVERT >> << '1_erg' / >> }
        { << '1_W' * >> << '1_W' CONVERT >> << '1_W' / >> }
      }				% end of fourth row sub-list
      {				% start of fifth row sub-list
				% contains only 5 items, as fifth row
				% has only 5 keys.
        { << '1_pc' * >> << '1_pc' CONVERT >> << '1_pc' / >> }
        { << '1_\angstm' * >> << '1_\angstm' CONVERT >> << '1_\angstm' / >> }
        { << '1_c' * >> << '1_c' CONVERT >> << '1_c' / >> }
        { << '1_eV' * >> << '1_eV' CONVERT >> << '1_eV' / >> }
        { << '1_b' * >> << '1_b' CONVERT >> << '1_b' / >> }
      }				% end of fifth row sub-list
    }				% end of main list
    OVER 10 / IP GET		% get key co-ords, extract tens digit,
				% and use this to get the correct row
				% sub-list
    OVER 10 / FP 10 * IP GET	% get the key co-ord, extract ones digit,
				% and use this to get the correct key
				% sub-sub-list form the row sub-list.
    SWAP FP 10 * GET		% get the tenths digit, and use it
				% to extract the correct item from the
				% key sub-sub-list
    EVAL			% execute/evaluate this item.
  END
>>

[ This is a downloadable version I recieved. -- Wayne]

I don't have a working version of the sample menu supplied, but here is the 
compiler program:
%%HP: T(3)A(R)F(.);
\<<
" \<< PICT LCD\-> {#0 #0}"
GROB 21 7 FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1
\-> block
  \<< ERASE 0 32
    FOR J 0 110
      FOR I PICT I
R\->B J R\->B 2 \->LIST
block REPL PICT J 8
/ 1 + \->STR "," + I
22 / 1 + \->STR +
" Label?" + { \Ga }
INPUT 1 \->GROB DUP
SIZE # 2h / J 4 +
R\->B SWAP - SWAP
# 2h / I
        IF 'I\=/0 OR
J\=/32' \->NUM
        THEN 11
        ELSE 22 'I'
STO+ PICT I R\->B J
R\->B 2 \->LIST block
REPL { # 21h # 32h
} { # 21h # 38h }
LINE 22
        END + R\->B
SWAP - SWAP 2 \->LIST
SWAP GXOR 22
      STEP 8
    STEP
  \>> PICT { # 0h
# 0h } { # 130h
# 39h } SUB \->STR +
" REPL {#0 #0} SWAP REPL	
    {#0 #0} PVIEW 0 WAIT
    IF DUP 60 > THEN DROP ELSE "
+ { } 1 5
  FOR row { } 1 6
    FOR col
      IF 'row==5
AND col==2' \->NUM
      THEN 3 'col'
STO
      END
"(C) M Woodhams.		
       May be copied		
       May not be sold."
      DO DROP
"UNIT (Y/N)?" 3
DISP 0 WAIT
      UNTIL DUP DUP
32.1 == SWAP 52.1
== OR
      END row \->STR
"," + col \->STR +
SWAP
      IF 32.1 ==
      THEN " Const"
+ { V } INPUT OBJ\->
1 \->LIST
      ELSE " Unit"
+ { \Ga } INPUT \->
unit
        \<< " \<< 1_"
unit + " * \>>" +
OBJ\-> " \<< 1_" unit +
" CONVERT \>>" + OBJ\->
" \<< 1_" unit +
" / \>>" + OBJ\-> 3
\->LIST
        \>>
      END 1 \->LIST +
    NEXT 1 \->LIST +
  NEXT \->STR +
" OVER 10 / IP GET		
    OVER 10 / FP 10 * IP GET
    SWAP FP 10 * GET
    EVAL END
    \>> "
+ OBJ\->
\>>

That ought to work.  If you're interested, here is a modified version which uses eight rows.  It has a couple of "quirks."  Rows 6, 7, and 8 are treated as the
second key (i.e. number keys 7, 4, and 1) as being the third keys (as you did 
with the +/- key in the labeling part of the program.  Also, in the list making
subroutine, the program asks for input for keys (6,2), (7,2), and (8,2), but
the final menu does nothing with the information.  Key inputs on rows 6, 7, and
8 actually begin with (6,3), (7,3), and (8,3).  Although these "quirks" can 
confuse someone that doesn't know about them, they do not affect the menu 
program (which seems to work just fine).  Also, if you want to use the menus to
run programs, I've found that input such as { HOME poly RT} works well as a key
assignment.  Oops, put an EVAL command after the { directory path}.  Anyways,
here is the modified program:
%%HP: T(3)A(D)F(.);
DIR
  COMPILE
    \<< " \<< PICT "
GROB 21 7 FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1FFFFF1
\-> block
      \<< ERASE 0 56
        FOR J 0 110
          FOR I
            IF 'J==
40 AND I==0' \->NUM
            THEN 44
'I' STO+
            END
            IF 'J==
48 AND I==0' \->NUM
            THEN 44
'I' STO+
            END
            IF 'J==
56 AND I==0' \->NUM
            THEN 44
'I' STO+
            END
PICT I R\->B J R\->B 2
\->LIST block REPL
PICT J 8 / 1 + \->STR
"," + I 22 / 1 +
\->STR + " Label?" +
{ \Ga } INPUT 1 \->GROB
DUP SIZE # 2h / J 4
+ R\->B SWAP - SWAP
# 2h / I
            IF 'I\=/0
OR J\=/32' \->NUM
            THEN 11
            ELSE 22
'I' STO+ PICT I R\->B
J R\->B 2 \->LIST block
REPL { # 15h # 20h
} { # 15h # 26h }
LINE 22
            END +
R\->B SWAP - SWAP 2
\->LIST SWAP GXOR 22
          STEP 8
        STEP
      \>> PICT { # 0h
# 0h } { # 82h
# 3Fh } SUB \->STR +
" {#0 #0} SWAP REPL	
    {#0 #0} PVIEW 0 WAIT
    IF DUP 90 > THEN DROP ELSE "
+ { } 1 8
      FOR row { } 1
6
        FOR col
          IF 'row==
5 AND col==2' \->NUM
          THEN 3
'col' STO
          END
          IF 'row==
6 AND col==1' \->NUM
          THEN 2
'col' STO
          END
          IF 'row==
7 AND col==1' \->NUM
          THEN 2
'col' STO
          END
          IF 'row==
8 AND col==1' \->NUM
          THEN 2
'col' STO
          END
"(C) M Woodhams.		
       May be copied		
       May not be sold."
          DO DROP
"UNIT (Y/N)?" 3
DISP 0 WAIT
          UNTIL DUP
DUP 32.1 == SWAP
52.1 == OR
          END row
\->STR "," + col \->STR
+ SWAP
          IF 32.1
==
          THEN
" Const" + { V }
INPUT OBJ\-> 1 \->LIST
          ELSE
" Unit" + { \Ga }
INPUT \-> unit
            \<<
" \<< 1_" unit +
" * \>>" + OBJ\->
" \<< 1_" unit +
" CONVERT \>>" + OBJ\->
" \<< 1_" unit +
" / \>>" + OBJ\-> 3
\->LIST
            \>>
          END 1
\->LIST +
        NEXT 1
\->LIST +
      NEXT \->STR +
" OVER 10 / IP GET		
    OVER 10 / FP 10 * IP GET
    SWAP FP 10 * GET
    EVAL END
    \>> "
+ OBJ\->
    \>>
END

P.S.  Check over the first program above and compare it to yours.  I had to
make a number of typo corrections to the original PHYCONST programs on Wayne
Scott's mailserver.  I even had to put in my best guess for a line of code which seemed to be missing.  I haven't found any bugs with the program, so I probably guessed right.  If the first program is correct, it should be reposted to
Wayne Scott's mailserver.

					Later,
					Brent A. Nelson
					University of Florida
					brent@Elm.CIRCA.UFL.EDU


