Post Reply 
24-MAR-2021 Simple Units
03-24-2021, 02:17 AM (This post was last modified: 03-24-2021 06:33 PM by Han.)
Post: #1
24-MAR-2021 Simple Units
User mode is required. This program basically takes over the [Shift][_] key and provides a super-simple units menu for facilitating unit conversion. When the user types the _ symbol via [Shift][_], a units menu is automatically shown (and if applicable, the last menu used will be displayed). The first menu is the category, and the second menu is the list of units within that category. So if one were to convert 5 kilometers to miles, the keystrokes are:

5
[Shift][_]
(select distance)
(select km)
(press the Sto> menu button on the screen)
[Shift][_]
(select mile)
[Enter]

Press [Esc] to traverse back a menu (i.e. from units to category). Escaping out of the category menu will simply place the _ symbol onto the command line (in case you need to type the underscore character inside a string, for example). Pressing [On] will escape all menus (i.e. to avoid pressing [Esc] twice while inside a menu of unit symbols.

Adding one's own custom units can be easily done by simply adding to the existing lists named catlist (category) and symlist (unit symbols), which should be fairly obvious to do when looking at the source code (toward the very top). Just make sure to keep the lists ordered properly.

To install, simply create a new program called UNITS (or whatever name you want) and copy and paste the code below into your new program (easiest if done via the connectivity kit). Then drag/drop to your calculator. If you already have custom keys set up, then just add this to your existing custom keys program.

If you find any typos in the unit symbols, please let me know and I'll update here accordingly. Maybe in the (far?) future I'll focus on creating a custom menu that takes advantage of the large screen so that all categories and unit symbols are visible at once (with keyboard shortcuts) to remove the need to scroll the menu and improve efficiency.

Code:
#pragma mode( separator(.,;) integer(h32) )
// simple units conversion by Han Duong 24-MAR-2021


lastunit:={0,0};
catlist:={
  "Length",
  "Area",
  "Volume",
  "Time",
  "Speed",
  "Mass",
  "Force",
  "Acceleration",
  "Energy",
  "Power",
  "Pressure",
  "Temperature",
  "Electrical",
  "Angle",
  "Light",
  "Radiation",
  "Viscosity",
  "Torque"
};
symlist:={
  {"m","cm","mm","yd","ft","in","mpc","pc","lyr","au","km","mile","nmi","miUS","chain","rod","fath","ftUS","mil","Å","fermi"},
  {"m^2","cm^2","b","yd^2","ft^2","in^2","km^2","ha","a","mile^2","miUS^2","acre"},
  {"m^3","st","cm^3","yd^3","ft^3","in^3","l","galUK","galUS","qt","liqpt","ptUK","ml","cu","ozfl","ozUK","tbsp","tsp","bbl","buUS", "bu","pk","fbm"},
  {"yr","d","h","min","s","Hz"},
  {"m/s","cm/s","ft/s","kph","km/h","mph","mile/h","knot","rad/s","tr/min","tr/s"},
  {"kg","g","lb","oz","slug","lbt","tonUS","tonUK","t","ozt","ct","grain","u","mol"},
  {"N","dyn","gf","kip","lbf","pdl","kg*m/s^2"},
  {"m/s^2","ft/s^2","grav","Gal","rad/sec^2"},
  {"J","erg","kcal","cal","Btu","ft*lbf","thermEC","thermUS","thermUK","eV","MeV","toe","tec","lep","boe","Wh","kWh","kg*m^2/s^2"},
  {"W","MW","hp","kg*m^2/s^3","ft*lbf/s"},
  {"Pa","atm","bar","psi","torr","mmHg","inHg","inH2O","kg/m*s^2"},
  {"°C","°F","K","°R"},
  {"V", "A", "C", "Ohm", "F", "W","Fdy","H","mho","S","T","Wb","A*h"},
  {"deg","rad","grad","arcmin","arcs","gon","tr"},
  {"flam","cd"},
  {"Gy","rd","rem","Sv","Bq","Ci","R"},
  {"P","St","kg/(m*s)","lb/(ft*h)","m^2/S","ft^2/S"},
  {"N*m","in*lbf","ft*lbf"}
};


KEY KS_Space()
BEGIN

  local c1,c2;
  if lastunit(1) OR lastunit(2) then
    c1:=lastunit(1); c2:=lastunit(2);
    if NOT(choose(c2,catlist(c1),symlist(c1))) then
      c2:=0;
    end;
  else
    c1:=0; c2:=0;
  end;

  if c2==0 then
    repeat
      if choose(c1,"Units",catlist) then
        if c1 then
          choose(c2,catlist(c1),symlist(c1));
        end;
      else
        c1:=0; c2:=0;
      end;
    until c1==0 OR c2;
    lastunit:={c1,c2};
  end;

  if c2 then return("_("+symlist(c1,c2)+")"); else return("_"); end;
END;


Attached File(s)
.hpprgm  UNITS.hpprgm (Size: 9.03 KB / Downloads: 19)

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
03-24-2021, 02:58 AM
Post: #2
RE: 23-MAR-2021 Simple Units
(03-24-2021 02:17 AM)Han Wrote:  User mode is required. This program basically takes over the [Shift][_] key and provides a super-simple units menu for facilitating unit conversion. When the user types the _ symbol via [Shift][_], a units menu is automatically shown (and if applicable, the last menu used will be displayed). The first menu is the category, and the second menu is the list of units within that category. So if one were to convert 5 kilometers to miles, the keystrokes are:

5
[Shift][_]
(select distance)
(select km)
(press the Sto> menu button on the screen)
[Shift][_]
(select mile)
[Enter]

Press [Esc] to traverse back a menu (i.e. from units to category). Escaping out of the category menu will simply place the _ symbol onto the command line (in case you need to type the underscore character inside a string, for example). Pressing [On] will escape all menus (i.e. to avoid pressing [Esc] twice while inside a menu of unit symbols.

Adding one's own custom units can be easily done by simply adding to the existing lists named catlist (category) and symlist (unit symbols), which should be fairly obvious to do when looking at the source code (toward the very top). Just make sure to keep the lists ordered properly.

To install, simply create a new program called UNITS (or whatever name you want) and copy and paste the code below into your new program (easiest if done via the connectivity kit). Then drag/drop to your calculator. If you already have custom keys set up, then just add this to your existing custom keys program.

If you find any typos in the unit symbols, please let me know and I'll update here accordingly. Maybe in the (far?) future I'll focus on creating a custom menu that takes advantage of the large screen so that all categories and unit symbols are visible at once (with keyboard shortcuts) to remove the need to scroll the menu and improve efficiency.

Code:
#pragma mode( separator(.,;) integer(h32) )
// simple units conversion by Han Duong 23-MAR-2021


lastunit:={0,0};
catlist:={
  "Length",
  "Area",
  "Volume",
  "Time",
  "Speed",
  "Mass",
  "Force",
  "Acceleration",
  "Energy",
  "Power",
  "Pressure",
  "Temperature",
  "Electrical",
  "Angle",
  "Light",
  "Radiation",
  "Viscosity"
};
symlist:={
  {"m","cm","mm","yd","ft","in","mpc","pc","lyr","au","km","mile","nmi","miUS","chain","rod","fath","ftUS","mil","Å","fermi"},
  {"m^2","cm^2","b","yd^2","ft^2","in^2","km^2","ha","a","mile^2","miUS^2","acre"},
  {"m^3","st","cm^3","yd^3","ft^3","in^3","l","galUK","galUS","qt","liqpt","ptUK","ml","cu","ozfl","ozUK","tbsp","tsp","bbl","buUS", "bu","pk","fbm"},
  {"yr","d","h","min","s","Hz"},
  {"m/s","cm/s","ft/s","kph","km/h","mph","mile/h","knot","rad/s","tr/min","tr/s"},
  {"kg","g","lb","oz","slug","lbt","tonUS","tonUK","t","ozt","ct","grain","u","mol"},
  {"N","dyn","gf","kip","lbf","pdl","kg*m/s^2"},
  {"m/s^2","ft/s^2","grav","Gal","rad/sec^2"},
  {"J","erg","kcal","cal","Btu","ft*lbf","thermEC","thermUS","thermUK","eV","MeV","toe","tec","lep","boe","Wh","kWh","kg*m^2/s^2"},
  {"W","MW","hp","kg*m^2/s^3","ft*lbf/s"},
  {"Pa","atm","bar","psi","torr","mmHg","inHg","inH2O","kg/m*s^2"},
  {"°C","°F","K","°R"},
  {"V", "A", "C", "Ohm", "F", "W","Fdy","H","mho","S","T","Wb","A*h"},
  {"deg","rad","grad","arcmin","arcs","gon","tr"},
  {"flam","cd"},
  {"Gy","rd","rem","Sv","Bq","Ci","R"},
  {"P","St","kg/(m*s)","lb/(ft*h)","m^2/S","ft^2/S"}
};


KEY KS_Space()
BEGIN

  local c1,c2;
  if lastunit(1) OR lastunit(2) then
    c1:=lastunit(1); c2:=lastunit(2);
    if NOT(choose(c2,catlist(c1),symlist(c1))) then
      c2:=0;
    end;
  else
    c1:=0; c2:=0;
  end;

  if c2==0 then
    repeat
      if choose(c1,"Units",catlist) then
        if c1 then
          choose(c2,catlist(c1),symlist(c1));
        end;
      else
        c1:=0; c2:=0;
      end;
    until c1==0 OR c2;
    lastunit:={c1,c2};
  end;

  if c2 then return("_"+symlist(c1,c2)); else return("_"); end;
END;

I will check this out later. thanks!
Find all posts by this user
Quote this message in a reply
03-24-2021, 03:48 AM
Post: #3
RE: 23-MAR-2021 Simple Units
I just tried this out, but I have a few questions. In order to create a unit like Newton Meters, would it require adding "N*m" in the list in the code (actually creating a new group called "Torque" and placing that under that entry)?

Also, does the conversion only work when in Textbook/Algebraic mode? There is no "Sto>" button on screen when in RPN mode (which i am using 99% of the time).
Find all posts by this user
Quote this message in a reply
03-24-2021, 04:04 AM
Post: #4
RE: 23-MAR-2021 Simple Units
(03-24-2021 03:48 AM)spiff72 Wrote:  I just tried this out, but I have a few questions. In order to create a unit like Newton Meters, would it require adding "N*m" in the list in the code (actually creating a new group called "Torque" and placing that under that entry)?

Also, does the conversion only work when in Textbook/Algebraic mode? There is no "Sto>" button on screen when in RPN mode (which i am using 99% of the time).

Yes, so create a new entry in the catlist variable called "Torque" and add in all your torque units in the symlist variable. (Don't forget to add a comma at the end of the previous entry!)

Unfortunately you lose a lot of the efficiency in RPN mode (which to this day seems very rudimentary in my opinion). In RPN mode the keystrokes are a little more drawn out:

5
[Shift][_]
(pick units category)
(pick unit)
[Enter]
1
[Shift][_]
(pick target unit)
[Enter]
[Shift][C]
(Tools -> Convert; if Tools was previously selected then it automatically shows this menu)
[Enter]

So basically you still benefit from the "previous" category being saved when pressing [Shift][_] and don't have to re-navigate through the menus in RPN mode but it is not as convenient as in non-RPN mode. I personally don't use this mode on the Prime (I have other HP calculators I prefer to use for RPN/RPL.)

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
03-24-2021, 04:02 PM
Post: #5
RE: 23-MAR-2021 Simple Units
(03-24-2021 04:04 AM)Han Wrote:  
(03-24-2021 03:48 AM)spiff72 Wrote:  I just tried this out, but I have a few questions. In order to create a unit like Newton Meters, would it require adding "N*m" in the list in the code (actually creating a new group called "Torque" and placing that under that entry)?

Also, does the conversion only work when in Textbook/Algebraic mode? There is no "Sto>" button on screen when in RPN mode (which i am using 99% of the time).

Yes, so create a new entry in the catlist variable called "Torque" and add in all your torque units in the symlist variable. (Don't forget to add a comma at the end of the previous entry!)

Unfortunately you lose a lot of the efficiency in RPN mode (which to this day seems very rudimentary in my opinion). In RPN mode the keystrokes are a little more drawn out:

5
[Shift][_]
(pick units category)
(pick unit)
[Enter]
1
[Shift][_]
(pick target unit)
[Enter]
[Shift][C]
(Tools -> Convert; if Tools was previously selected then it automatically shows this menu)
[Enter]

So basically you still benefit from the "previous" category being saved when pressing [Shift][_] and don't have to re-navigate through the menus in RPN mode but it is not as convenient as in non-RPN mode. I personally don't use this mode on the Prime (I have other HP calculators I prefer to use for RPN/RPL.)

I took a shot at adding those elements but I ran into some syntax errors when I tried to use the Sto> softkey to convert. I need to revisit this later.
Find all posts by this user
Quote this message in a reply
03-24-2021, 05:39 PM
Post: #6
RE: 23-MAR-2021 Simple Units
I still can't get this to work with torque units (Force x Length)...

It works fine to "manually" create units in the Units48 app that I originally posted about, and the syntax it shows there would be:

10_(N*m)
1_(in*lbf) (this can be reversed too and entered as lbf-in, but this is not typical for US units)

Using the CONV softkey in Units48 then outputs:
88.507..._(in*lbf)

I initially tried setting up the list with a new "Torque" entry, and the line for the contents of that list was:
{"N*m","in*lbf","ft*lbf"},

With this method, following your instructions would create the following syntax when using the Sto> button:
10_(N)*m>1_(in)*lbf
and then pressing Enter yields a popup message stating:
Error: Syntax Error

So after seeing how the Units48 program formatted the (N*m) in parenthesis, I edited the line in the code to:
{"(N*m)","(in*lbf)","(ft*lbf)"},

I then ended up with the following syntax on the entry line:
10_(N*m)>1_(in*lbf)
and then pressing enter yields an error message (not a popup this time) stating:
Error: Bad Argument Type

So I am not sure what to do in this case.
Find all posts by this user
Quote this message in a reply
03-24-2021, 05:50 PM
Post: #7
RE: 23-MAR-2021 Simple Units
One more observation:

If I use the same arguments in the CONVERT() function, it converts correctly:
CONVERT(10_(N*m),1_(in*lbf))
I get 88.507..._in*lbf

It seems like maybe the Sto> shortcut for conversion doesn't work in this application?
Find all posts by this user
Quote this message in a reply
03-24-2021, 06:30 PM (This post was last modified: 03-24-2021 06:34 PM by Han.)
Post: #8
RE: 23-MAR-2021 Simple Units
(03-24-2021 05:50 PM)spiff72 Wrote:  One more observation:

If I use the same arguments in the CONVERT() function, it converts correctly:
CONVERT(10_(N*m),1_(in*lbf))
I get 88.507..._in*lbf

It seems like maybe the Sto> shortcut for conversion doesn't work in this application?

Type it without the 1 in the front so that it looks like: 10_(N*m)▶_(in*lbf)

I've adjusted the code to account for non-atomic units. You can simply change the last if statement to:

Code:
if c2 then return("_("+symlist(c1,c2)+")"); else return("_"); end;

so that you don't have to encapsulate all your units in parentheses (should look nicer in the menus, too, without them). I've updated the first post to reflect these changes.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
03-25-2021, 02:33 AM
Post: #9
RE: 24-MAR-2021 Simple Units
(03-24-2021 06:30 PM)Han Wrote:  
(03-24-2021 05:50 PM)spiff72 Wrote:  One more observation:

If I use the same arguments in the CONVERT() function, it converts correctly:
CONVERT(10_(N*m),1_(in*lbf))
I get 88.507..._in*lbf

It seems like maybe the Sto> shortcut for conversion doesn't work in this application?

Type it without the 1 in the front so that it looks like: 10_(N*m)▶_(in*lbf)

I've adjusted the code to account for non-atomic units. You can simply change the last if statement to:

Code:
if c2 then return("_("+symlist(c1,c2)+")"); else return("_"); end;

so that you don't have to encapsulate all your units in parentheses (should look nicer in the menus, too, without them). I've updated the first post to reflect these changes.

Thanks! I just came here to post that I just found the same after doing some more testing here (leaving off the 1).
Find all posts by this user
Quote this message in a reply
03-25-2021, 02:42 AM
Post: #10
RE: 24-MAR-2021 Simple Units
(03-24-2021 06:30 PM)Han Wrote:  
(03-24-2021 05:50 PM)spiff72 Wrote:  One more observation:

If I use the same arguments in the CONVERT() function, it converts correctly:
CONVERT(10_(N*m),1_(in*lbf))
I get 88.507..._in*lbf

It seems like maybe the Sto> shortcut for conversion doesn't work in this application?

Type it without the 1 in the front so that it looks like: 10_(N*m)▶_(in*lbf)

I've adjusted the code to account for non-atomic units. You can simply change the last if statement to:

Code:
if c2 then return("_("+symlist(c1,c2)+")"); else return("_"); end;

so that you don't have to encapsulate all your units in parentheses (should look nicer in the menus, too, without them). I've updated the first post to reflect these changes.

One additional question...

Is there any reason (or disadvantage) to assigning this program to the template key (the one that would normally be the blue-shift UNITS button?

So instead of:
KEY KS_Space()
use:
KEY K_Templ()

I think that this would be easier for me to remember (and one less key press to activate it). I am relatively new to the Prime (a few months in, and I came from the HP50g), and I am still getting used to things on this calculator. I REALLY miss being able to assing softkeys to a program!
Find all posts by this user
Quote this message in a reply
03-25-2021, 03:44 AM
Post: #11
RE: 24-MAR-2021 Simple Units
(03-25-2021 02:42 AM)spiff72 Wrote:  KEY KS_Space()
use:
KEY K_Templ()

I think that this would be easier for me to remember (and one less key press to activate it). I am relatively new to the Prime (a few months in, and I came from the HP50g), and I am still getting used to things on this calculator. I REALLY miss being able to assing softkeys to a program!

You can assign it to any key you wish. I assigned it to KS_Space() since that is where most people would naturally use units (since typing _ suggests they are about to type a unit). Perhaps KS_Templ() might be more natural (unless you never use the templates interface). But again, it's completely up to you to decide.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
03-25-2021, 01:40 PM
Post: #12
RE: 24-MAR-2021 Simple Units
(03-25-2021 03:44 AM)Han Wrote:  
(03-25-2021 02:42 AM)spiff72 Wrote:  KEY KS_Space()
use:
KEY K_Templ()

I think that this would be easier for me to remember (and one less key press to activate it). I am relatively new to the Prime (a few months in, and I came from the HP50g), and I am still getting used to things on this calculator. I REALLY miss being able to assing softkeys to a program!

You can assign it to any key you wish. I assigned it to KS_Space() since that is where most people would naturally use units (since typing _ suggests they are about to type a unit). Perhaps KS_Templ() might be more natural (unless you never use the templates interface). But again, it's completely up to you to decide.

OK - thanks. I have changed that in my program to try it for a while. I figure that since it only affects the user keyboard it won't really affect normal usage in my case.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)