(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. Convert.zip (Size: 2.74 KB / Downloads: 176) |
|||
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! |
|||
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.
|
|||
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. |
|||
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 ] Then I have a single column matrix with all categories that I call CCAT, so Code: "CCAT" [ 12x1 MATRIX ] 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" No .raw file as yet though. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
02-05-2018, 03:28 PM
Post: #6
|
|||
|
|||
RE: (42S, Free42) Unit Conversion Suite
Interesting idea!
|
|||
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.
|
|||
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 |
|||
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.
|
|||
10-09-2018, 01:21 PM
Post: #10
|
|||
|
|||
RE: (42S, Free42) Unit Conversion Suite
That's great Dave, thank you.
|
|||
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 |
|||
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:
These add-ons are based on Dave's version from Oct. 9, 2018 |
|||
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).
|
|||
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.
|
|||
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. |
|||
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!). |
|||
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.
|
|||
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 ;-) 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. |
|||
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 |
|||
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?
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)