Musings and comments about our common interest
The latest HP Prime firmware has introduced several important features, which were lacking in previous releases and that allow to better use the machine resources.
Here we will discuss the drop-down menus and the screen tab menus. This makes communicating with your program much more seamless, GUI-oriented, than the typical question/answer and input field method (that we had to use if we didn’t have access to a Prime SDK (software development kit)
Both need some event-management concepts beforehand. An event is either a key click, or a mouse event. Mouse events can be of several types: click, long click, drag, etc., but we will now concentrate on the simple mouse click, in order to get up and running a short application (this program has been modified from one published by Eddie B. Shore, as many here). We have included some comments so that you can see what’s happening.
We will now focus on the screen tab menus and leave the dropdown menus for tomorrow:
// CAS Custom Menu
// EWS 2014–04–20
LOCAL m,m1,mx,my; //variables containing the mouse event and coordinates
WHILE MOUSE(1)≥0 DO END;
TEXTOUT_P(“Choose the function.”,1,1,4);
TEXTOUT_P(“Here you could put a description of the menu functions below”,1,18,4);
UNTIL SIZE(m1)>0; //Waiting until the Mouse variable is filled with a mouse event
mx:=m1(1); //Getting the coordinates of such event - first X
my:=m1(2); //Then y
IF my≥220 AND my≤239 THEN //Only if the mouse event in in the lower part of the screen
IF mx≥0 AND mx≤51 THEN
IF mx≥53 AND mx≤104 THEN
RETURN; //your code here;
IF mx≥106 AND mx≤157 THEN
RETURN ; //your code here;
IF mx≥159 AND mx≤210 THEN
RETURN; //your code here;
IF mx≥212 AND mx≤263 THEN
RETURN; //your code here;
IF mx≥265 AND mx≤319 THEN
RETURN; //your code here;
I have just seen a HP Prime version of the NPV, IRR and cashflow applications made by Salvomic, a user in HPmuseum.org. I found it much longer than my own limited programs, which in reality were just functions to be used out of the keyboard, without any procedural or data entry instructions. But then I saw a screen shot with menu items. That was interesting! I had heard that many users wanted to be able to use the menus in the system, but there was no direct access to them with the common HP Prime functions. That got me interested and I started to search the program code for the menu functions information. It ended up being more low-level functions that I initially expected!
Let’s take a look at them. First, you need to know that something has happened, either on the screen as screen touch (or mouse in the case of the emulator), or on the keyboard.
// Detect keyboard or mouse input (keyboard has priority)
eTYP := “”;
kP := GETKEY;
IF kP <> –1 THEN
eTYP := “K”;
m := MOUSE;
m1 := m(1);
IF SIZE(m1) > 0 THEN
eTYP := “M”;
Then, he defines the procedure PUTMENU(mTXT), where mTXT is a list with 6 text elements. The real procedure that draws the menu is DRAWMENU() with all the text elements. Some of them may be “”.
// Draw the menu using the list passed in
He also defines the procedure GETMENU(mx,my,mTXT), where mx and my are the coordinates of the mouse click, using the convention of starting in the upper left corner of the screen, counting pixels. He uses a simple CASE function, after determining that the mouse click is in the lower part of the screen (line 4 of the procedure)
// Get the number of the menu item selected (1–6) by checking mouse position
// Menu items with empty/blank text will return a zero
mSEL := 0;
IF my≥220 AND my≤239 THEN
IF mx≥0 AND mx≤51 AND mTXT(1)>”” THEN mSEL := 1; END; IF mx≥53 AND mx≤104 AND mTXT(2)>”” THEN mSEL := 2; END; IF mx≥106 AND mx≤157 AND mTXT(3)>”” THEN mSEL := 3; END; IF mx≥159 AND mx≤210AND mTXT(4)>”” THEN mSEL := 4; END; IF mx≥212 AND mx≤263 AND mTXT(5)>”” THEN mSEL := 5; END; IF mx≥265 AND mx≤319 AND mTXT(6)>”” THEN mSEL := 6; END;
I have installed in my Mac the latest version of Xcode. Not that I am doing any programming; and I will be very much limited in testing the output of it, since I am not paying the Apple developer fees; but I just wanted to know all the fuss about Swift, the programming language that is taking the It world by storm. I wanted to know how it feels, what it looks like, and whether HP should have used it instead of the sort of Basic that is now being used in the HP Prime.
Well, the initial impression is very satisfactory. But the best thing of all is the simultaneous interpreter, that shows you the result of whatever you’re programming in real time (not when you run the program, but as you write it: it tries to make sense of the current sentence meaning, and shows you any possible error along the way. It is something I have never seen before, and it speeds up programming like never before. If, in addition, you read in the included documentation the file called “The Swift Programming Language - a Swift Tour”, and paste in the development windows the different examples, and do the exercises as proposed in said document, you’ll probably get the same feeling as I did.
As a first observation, it is less stricter in syntax than C+ or any of its siblings; but also much more powerful, it that you can say many things in a small space; the inferences the interpreter makes about what you’re doing are very intelligent, and sufficient most of the time; and the by-side result and clever error warning as you write, helps a lot in developing fast.
Of course, such power is very much above what is required for a calculator like the HP Prime - but it is a pity that we cannot have the leading programming language in the HP Prime. Someone willing to write an interpreter for it?
In the www.hpmuseum.org site, which is probably the website most popular when it comes to HP calculators, Han has published a couple of very interesting tutorials for Prim programming.
You can find the first here: HP Prime Programming - An introduction
It shows the answer to many questions I have faced when starting to program. Questions that imply that Han has a deep knowledge, not only about the manual, but about the behaviour of the machine. There are many things that do not appear in the manual but are the result of testing and failing or succeeding with the calculator.
A comment that can be made is that most of the explorations have been made with the calculator emulator on the computer. There are many things that can be done quite easily using your fingers on the calculator screen, like bringing variables to screen, executing programs, etc. For example, when he speaks of the several methods or running a program, he does not cite the one I use most: locate the program with the fingers on the toolbox menus! That said, I have learned a lot of things about the Prime when reading it, and it is for me probably the best introduction to programming the HP Prime.
The new software used for the forum in hpmuseum.org allows to easily include code snippets, that do not occupy substantial space but scroll in the mini-window allocated for them. This is quite convenient to copy programs to illustrate a point, without interfering with the reasoning behind. This is used by Han to very good effect.
There is already a continuation in HP Prime Programming: Variable types and their priorities
Here is another simple program that I use in my HP-41CL. (It’s an advantage not to be concerned with memory usage, and even being able to switch different environments!). It was written in very much the same manner as the currency translator.
Our company has factories in both the European Union and USA. All shipments in Europe are denominated in Tons, while all shipments in the US are in pounds. Similarly, while all surface painting is measured in square meters in Europe, it is in square feet in the USA. As well, there is a production item that is measured in meters in Europe and feet in USA. And while we don’t sell anything in volume, I just added gallons to liter and miles to km for the sake of using the whole upper row and be prepared for my next trip to the USA.
The criteria used is the same: press the function key to translate the value TO international system; press shift-function key when translating FROM international system.
1 LBL “EXCHN”
2 “Lb ft 2 G Mi”
5 LBL A
6 RCL 29
9 LBL a
10 RCL 29
13 LBL B
14 RCL 30
17 LBL b
18 RCL 30
21 LBL C
22 RCL 30
26 LBL c
27 RCL 30
31 LBL D
32 RCL 31
35 LBL d
36 RCL 31
39 LBL E
40 RCL 32
43 LBL e
44 RCL 32
Of course, we use one register less than in the previous program, because we only need one for feet and square feet!
I assign it to a shifted key in the user manual. If I ever don’t remember where a conversion was, just press that shifted key again.
Despite its simplicity, it is very convenient to perform fast translations - actually faster than the HP48 family of calculators using their unit menu (there is far more navigation to reach the page where you can actually perform conversions. Let’s not talk about the prime in RPN mode!). And in my job, I can use it several times per day.