Post Reply 
(42S, Free42) Unit Conversion Suite
01-10-2018, 04:08 PM (This post was last modified: 05-03-2019 03:22 PM by Dave Britten.)
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.

UPDATE Oct. 9, 2018 - I added a BYTES menu for converting between different bit/byte units with prefixes up to peta-. There's also WORD/DWORD/QWORD, with a configurable word size (default is 32 bits). If you need to work with nybbles, set the word size to 4, and just use WORD. You can choose whether the magnitude prefixes are treated as powers of 2 (the proper way) or powers of 10 (preferred by hard disk salespeople). There's also a 4th page with some rather special-purpose units I occasionally use: SQL Server pages and extents, Nintendo 3DS "blocks", and Commodore 64 1541 disk "blocks". You can trim out this page, or replace the entries with units more appropriate for your work.

UPDATE May 3, 2019 - I found one or two obscure conversion factors that were incorrect and fixed them. Also, this version takes advantage of flag 64 (which I believe is exclusive to the DM42) to provide a feature from the 19BII: if you press Shift+menu key, the input value will be added to a running total. For example, if you want to add 2 feet to 35 cm and convert the result to meters:

2 ["FEET"] 35 [SHIFT] ["CM"] ["M"]

The input value will be displayed with a "+" prefix to indicate this accumulation is taking place.


.zip  Convert.zip (Size: 2.74 KB / Downloads: 164)
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
10-09-2018, 01:18 PM
Post: #9
RE: (42S, Free42) Unit Conversion Suite
For those following along, I've uploaded a new version that includes a BYTES menu for converting between various bit/byte units. Most are pretty standard conversions, but you can toggle between powers of 2 or 10, and also choose the size of the WORD-based units.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-09-2018, 01:21 PM
Post: #10
RE: (42S, Free42) Unit Conversion Suite
That's great Dave, thank you.
Find all posts by this user
Quote this message in a reply
10-11-2018, 08:10 PM
Post: #11
RE: (42S, Free42) Unit Conversion Suite
(02-05-2018 02:00 PM)Werner Wrote:  No .raw file as yet though.

You could use this online encoder/decoder.

HTH
Thomas
Find all posts by this user
Quote this message in a reply
10-16-2018, 07:29 PM
Post: #12
RE: (42S, Free42) Unit Conversion Suite
I have created a version with dBm conversion added to the "POWER" menu and with a new menu entry "FREQ" for frequency to wavelength conversions.

Since dBm is using a LOG scale and not fitting in the regular conversions scheme, I couldn't find a clean way to implement, and this is somehow messing up the code a bit. Any ideas to handle this in a better way?

Furthermore I also liked the idea of Logan to keep the current value and unit displayed when changing menu rows, which I tried to implement as well. For this I put the print-the-value part into its own function "PRV" and added two control flags:
  • 01: a value has been entered
  • 02: "PRV" was called by menu

These add-ons are based on Dave's version from Oct. 9, 2018


Attached File(s)
.zip  Convert+dBm+freq.zip (Size: 3 KB / Downloads: 39)
Find all posts by this user
Quote this message in a reply
05-03-2019, 03:24 PM
Post: #13
RE: (42S, Free42) Unit Conversion Suite
I updated the first post with a new version that fixes a couple of wrong conversion factors (nothing major), and also adds the Shift+Unit accumulation feature from the 19BII (this requires flag 64, which is a feature specific to the DM42).
Visit this user's website Find all posts by this user
Quote this message in a reply
05-03-2019, 03:26 PM
Post: #14
RE: (42S, Free42) Unit Conversion Suite
Thanks Dave. Were you ever able to think about the idea in post 3? Not a big deal but just curious.
Find all posts by this user
Quote this message in a reply
05-03-2019, 04:08 PM
Post: #15
RE: (42S, Free42) Unit Conversion Suite
(05-03-2019 03:26 PM)Logan Wrote:  Thanks Dave. Were you ever able to think about the idea in post 3? Not a big deal but just curious.

I've thought about it a bit, but hadn't spent tons of time with it. The main problem is that it's not as simple as just redisplaying the alpha register, since it gets clobbered by setting up the menu pages, and so far I haven't come up with an implementation I'm totally happy with.

A few possible approaches:

- Save the alpha register by chopping it up into a matrix so it can be restored after MENU is called. This requires tracking if anything has been entered, and maybe canceled if the user starts doing other things (e.g. flag 22 gets set).
- Save the label number of the last-entered unit, and call that indirectly to fire the "convert to" routine, redisplaying the value. This would require 2 extra steps in each unit label to store that value, and you'd need to be careful of flag 22 causing unintended unit input.
- Just toss a unit label like "cm:" into stack Y, as a crude imitation of RPL tags.

All of these have their warts: needing more global labels, complicating the individual unit conversions, chewing up a stack level, or needing complete alpha save/restore routines (which, in my current implementation, would add about 50 more steps).

It's too bad the 42 doesn't have all the X-MEM features of the 41C, because then you could probably just write the whole alpha register to an ASCII file, and subsequently restore it, making the alpha save/restore much simpler and more appetizing. (Note: I haven't done much with 41C X-MEM, so perhaps it's actually a bit more complicated than that.)

Conceptually, this is probably how I would solve it:

1. Replace all the calls to MENU with XEQ "UMENU".
2. Create a "UMENU" routine that checks for a preserved alpha register (probably by using one of the flags above 81), restores it, calls MENU, then returns. "UMENU" should probably also clear the flag to cancel redisplaying the unit if it detects flags 22 or 23 are now set, under the assumption the user is doing something else now.
3. Modify the "CVT" routine to save alpha and set the appropriate flag.
4. Have each individual unit type program clear the saved alpha on entry, so you're not seeing length measurements in the temperature menu, for instance.

This would all be quite a lot of extra code for a very small feature, so I wouldn't waste bytes and execution time on it if I were running this on a real 42S. Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
05-03-2019, 04:20 PM
Post: #16
RE: (42S, Free42) Unit Conversion Suite
Thanks, that's a good writeup.

I had a version on my real 42s where I think I just re-generated the call to the AVIEW after keys 7 or 8 were pressed, but given your structure with the CVT function, that's more tricky. There was also some problem with that method if I remember correctly (just don't remember what that problem was!).
Find all posts by this user
Quote this message in a reply
05-16-2019, 10:22 PM
Post: #17
RE: (42S, Free42) Unit Conversion Suite
As of release 2.2.1, Free42 has the flag 64 shift-indicator behavior as well.
Visit this user's website Find all posts by this user
Quote this message in a reply
09-05-2020, 01:05 PM
Post: #18
RE: (42S, Free42) Unit Conversion Suite
(02-05-2018 02:00 PM)Werner Wrote:  (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.
...
Then I have a single column matrix with all categories that I call CCAT, so
..
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 ;-).

Cheers, Werner

Hello, Werner. I am new to this forum as well as to the 42 ecosystem (I've had a DM42 for just over a month, after decades without a programmable calculator).

I have been looking for a unit-conversion program that is user-modifiable and extensible, and your method make sense even to me... that is, I understand the format of the matrices even though I don't at all understand the code that follows.

Did you complete this project? It is an elegant and clear solution to a problem encountered by many.
Find all posts by this user
Quote this message in a reply
09-07-2020, 09:20 AM
Post: #19
RE: (42S, Free42) Unit Conversion Suite
Yes I did: CONV post
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
10-27-2022, 12:57 AM
Post: #20
RE: (42S, Free42) Unit Conversion Suite
Why do I get "Label Not Found" when changing units on the DM42 while it works fine on Free42?
Find all posts by this user
Quote this message in a reply
Post Reply 




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