Post Reply 
(42S, Free42) Unit Conversion Suite
01-10-2018, 04:08 PM
Post: #1
(42S, Free42) Unit Conversion Suite
This is an adaptation of the UNITS menu from the 48. Obviously the 42S doesn't have unit-based calculation like the 48, so these are strictly numeric conversions. The attached program is in raw format.

Usage is very simple; run CONV, then choose a unit category from the menu. The unit menus work pretty much like any solver menu: enter a value, press a menu key, then press another menu key for the unit you want to convert to. Continue pressing menu keys to convert to other units. Note that there's no checking of unit consistency, so the program won't stop you from doing something zany like converting between cm and lb.

Conversion constants were obtained from my 48SX, by converting 1 of each unit to its corresponding value in the base unit for any category (usually the first one in the menu). Only the display precision was used; I didn't attempt to pry out the internal guard digits. The program was tested by converting through all units within a menu and comparing results while doing the same on my 48SX.

Some notable changes from the 48 units:
-Hz was removed from the Time menu, as it's really just shorthand for 1/s (and loses usefulness without true composite units).
-YrCal (365 days) was added to the Time menu, and Yr changed to YrSol (value unchanged).
-ga was removed from the Speed menu. In its place is a new Accel menu that compensates for the lack of composite units, and which contains m/s^2, ft/s^2, grav, and gal (the latter two taken from the Prime).
-Btu/h was added to the Power menu.
-cmH2O was added to the Press menu.
-Elec, Angl, Light, Rad, and Visc unit menus have been omitted, mostly for reasons of lacking a true unit calculation system.


There's a decent conversion program already available with Free42, but there are a couple of things that bother me about it: there's very little code reuse, so each unit conversion has a bunch of repeated code and the conversion factors are entered twice, and it's somewhat fragile in its use of the Y register to hold the underlying unit value. It also makes use of flag 1 in a way I can't really figure out what it's meant to do.

I set out to replicate the layout of the unit menus from my 48 in a way that only requires 6 lines of code to add a new conversion (2 for the menu entry, 4 for the conversion). If you're missing any exotic ones, it's easy to extend the program.

You're free to do whatever you like with this code.


.zip  Convert.zip (Size: 2.25 KB / Downloads: 22)
Visit this user's website Find all posts by this user
Quote this message in a reply
02-02-2018, 02:44 AM
Post: #2
RE: (42S, Free42) Unit Conversion Suite
This is great Dave, I agree with you on the need to improve this Conversion set.

I had already created my own version of the Length (since that's what I use most) and re-ordered some things, possibly because it fits my needs better. For example, I set it up to have:
mm mil cm in m ft on the first row and
km mi nmi etc on the second row, as that made a bit more logical sense to me. I see it both ways though.

I like the additional units you've added, like the ft/s in the speed portion and the acceleration conversion. Thanks!
Find all posts by this user
Quote this message in a reply
02-02-2018, 03:16 AM
Post: #3
RE: (42S, Free42) Unit Conversion Suite
Out of curiosity and before I go trying to alter too much, do you think there is a simple way to keep the current value and unit displayed in the AVIEW when you change menu rows? e.g., 4500 yd then the down arrow, keeping current unit displayed until I choose miles? I had it set up this way in my modified length program, since it's just nice to be sure I know what unit I'm working with, but I didn't get all the bugs out of it either.
Find all posts by this user
Quote this message in a reply
02-02-2018, 12:06 PM
Post: #4
RE: (42S, Free42) Unit Conversion Suite
(02-02-2018 03:16 AM)Logan Wrote:  Out of curiosity and before I go trying to alter too much, do you think there is a simple way to keep the current value and unit displayed in the AVIEW when you change menu rows? e.g., 4500 yd then the down arrow, keeping current unit displayed until I choose miles? I had it set up this way in my modified length program, since it's just nice to be sure I know what unit I'm working with, but I didn't get all the bugs out of it either.

Hmm, that's a good idea. I'll have to let that cook in my head for a bit to come up with a clean way to handle it.
Visit this user's website Find all posts by this user
Quote this message in a reply
02-05-2018, 02:00 PM (This post was last modified: 02-05-2018 03:27 PM by Werner.)
Post: #5
RE: (42S, Free42) Unit Conversion Suite
(edited twice already to fix bugs in the printout ;-)
I'm almost done rewriting this completely, just to see if it could be done.
I define each unit category as a matrix. First column is the alpha data; second is the multiplier, in case of TEMP the third column is the additive constant.
So eg. TEMP will look like

Code:
"TEMP" [ 4x3 Matrix ]
[[ "°C" 1 0 ]
 [ "°F" 5.55555555556E-1 32 ]
 [ "K" 1 273.15 ]
 [ "R" 5.55555555556E-1 491.67 ]
]

Then I have a single column matrix with all categories that I call CCAT, so

Code:
"CCAT" [ 12x1 MATRIX  ]
[[ "LENG"  ]
 [ "AREA"  ]
 [ "VOL"   ]
 [ "TIME"  ]
 [ "SPEED" ]
 [ "ACCEL" ]
 [ "MASS"  ]
 [ "FORCE" ]
 [ "ENERG" ]
 [ "POWER" ]
 [ "PRESS" ]
 [ "TEMP"  ]
]

Then all we need is a routine that will build a menu (with paging if necessary) out of the first column of a matrix, and treats the item chosen.
And so I end up with a single program that can treat as many conversions as you like - just define new matrices as you go along and add categories.
The routine is not 100% finished, but it already works as it is, so yes, it can be done ;-). The only drawback is that Free42 does not allow you to export a matrix, only programs. So I'll have to provide a one-shot program that creates the matrices. Here's the current to-be-worked-on version for the interested:

Code:
>LBL "CONV"
 CLX
>LBL 12
 INDEX "CCAT"
 XEQ 13
 RCL ".I"
 X>=0?
 GTO 00
 CLV ".I"
 CLV ".C"
 CLV ".B"
 EXITALL
 RTN
>LBL 00
 STO ".C"
 RCLEL
 INDEX IND ST X
 CLX
 CF 22
>LBL 11
 XEQ 13
 RCL ".I"
 X>=0?
 GTO 00
 RCL ".C"
 GTO 12
>LBL 00
 Rv
 RCLEL
 J+
 RCLEL
 J+
 RCLEL
 FS? 76
 CLX
 FC?C 22
 GTO 00
 +/-
 RCL+ ST T
 x
 STO ".B"
 X<> ST T
 GTO 01
>LBL 00
 X<>Y
 RCL ".B"
 X<>Y
 /
 +
>LBL 01
 CLA
 ARCL ST X
 +" "
 ARCL ST Y
 AVIEW
 RCL ".I"
 GTO 11
>LBL 13        -- Matrix Menu
 STO ".I"
 CLMENU
 1
 STO+ ST Y
 STOIJ
 -
 KEY 9 GTO 09
 XEQ 14
 KEY 1 GTO 01
 FS? 76
 GTO 00
 XEQ 14
 KEY 2 GTO 02
 FS? 76
 GTO 00
 XEQ 14
 KEY 3 GTO 03
 FS? 76
 GTO 00
 XEQ 14
 KEY 4 GTO 04
 FS? 76
 GTO 00
 XEQ 14
 KEY 5 GTO 05
 FS? 76
 GTO 00
 XEQ 14
 KEY 6 GTO 06
-- key 7 and 8 only when 7 or more items
 FC? 76
 GTO 07
>LBL 00
-- we reached the end. We have more than 6 items when index>0
 X=0?
 GTO 10
>LBL 07
 KEY 7 GTO 07
 KEY 8 GTO 08
>LBL 10
 MENU
 STOP
 GTO 10
>LBL 01
 1
 GTO 00
>LBL 02
 2
 GTO 00
>LBL 03
 3
 GTO 00
>LBL 04
 4
 GTO 00
>LBL 05
 5
 GTO 00
>LBL 06
 6
>LBL 00
 RCL+ ".I"
 1
 STOIJ
 Rv
 Rv
 RTN
>LBL 07        -- previous page
 RCL ".I"
 6
 -
 X>=0?
 GTO 13
         -- last page
 1
 ENTER
 STOIJ
 I-
 RCLIJ
 SIGN
 -
 STO ST Y
 6
 MOD
 -
 GTO 13 
>LBL 08        -- next page
 6
 RCL+ ".I"
 FS? 76
 CLX
 GTO 13        -- first page
>LBL 09
 -1
 STO ".I"
 RTN

>LBL 14
 CLA
 RCLEL
 Rv
 ARCL ST T
 I+
 END


No .raw file as yet though.

Cheers, Werner
Find all posts by this user
Quote this message in a reply
02-05-2018, 03:28 PM
Post: #6
RE: (42S, Free42) Unit Conversion Suite
Interesting idea!
Find all posts by this user
Quote this message in a reply
02-05-2018, 08:31 PM
Post: #7
RE: (42S, Free42) Unit Conversion Suite
The Matrix idea crossed my mind too, but the current lack of an import/export mechanism is what kept me from going down that road. That definitely makes it easiest to maintain the catalog of units, though.
Visit this user's website Find all posts by this user
Quote this message in a reply
02-06-2018, 07:01 AM
Post: #8
RE: (42S, Free42) Unit Conversion Suite
I just found out Free42 does support matrix copy/paste from the stack, since version 2.0. I was still running a very old one on my PC. So I can copy/paste from text files.
But how would that work on my phone?

Werner
Find all posts by this user
Quote this message in a reply
Post Reply 




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