HP Forums

Full Version: First python project : HP41 emulator + hints of workflow
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
While beeing disapointed due to large cpu throttle in HPPL, when python came on the Prime I could not resist ...

So I ported my HP41 emulator on the Prime in Python ... see at LINK

Some nice results :
- python is at least 10 times faster than HPPL for casual stuff
- no cpu throttling
- very deep and well done integration of python with prime firmware
- hpprime module very well done (some small bugs ...)
- python is robust on the prime, very few calc reset/reboot
- if you play too long with the emu, when you leave it the prime shut off due to auto off function not taking python execution into account

Only tested on a G2 model ... for G1 you can change some timing in nut.py (near line 790 : adjust 0.30 in hpprime.eval('WAIT(.030)'))

HP PRIME development workflow in micro-python:
  • all is at 2.1.14575 (2021 4 28) level (calc is at 2021 5 5 firmware)
  • only tested on a pure micro-python app, no use of HPPL wrapper
  • v/calc is either physical calculator (connected on the pc) or virtual calculator.
  • An important directory on the PC is 'HP Connectivy Kit/Content/app.hpappdir/' in your 'Documents' when developing the app named 'app'. This directory is important because you do not have sync problem with the v/calc. Using directly 'HP Connectivy Kit/Calaculators/app.hpappdir' can lead to sync problem if you use the v/calc and an external editor even one after the other. Try yourself and you will see ... I loose some editing due to this.
  • See this directory as a very safe scratchpad Smile and make your copies from/to v/calc yourself. If you want to be secure: always delete the destination to avoid an erasing when copying. Doing this I never had any problem. But it's only my way and opinion.
I use Calculator, Dev Kit, Emulator and a text editor (I use notepad++ and/or Vs Code) as follow:
  • Set your editor to use LF only as end of line, UTF8 and to expand TABs as spaces.
  • I never edit directly program on the v/calc with the editor in the dev kit. (It works for HPPL progs, at least in the previous releases, but for python, it is too dangerous).
  • I work with 'Content' which is local to the Pc : edit files from here either with the integrated editor or external editor from the file explorer.
  • When testing, delete the whole app in the v/calc, then copy from 'Content' to v/calc.
  • If you made some change in the v/calc, back up the previous app.hpappdir:
    - First do a 'Sync' from the contextual menu of the v/calc.
    - I use an 'archives' directory, I move the app.hpappdir from 'Content' to 'Content/archives' and replace.
    - Copy back the whole app from v/calc to 'Content' ... edit at leisure ...
  • Generally I create file.py in the PC in the 'HP Connectivy Kit/Content/app.hpappdir/' directory.
To add binary file:
You can do it in the 'HP Connectivy Kit/Content/app.hpappdir/' directory
You can also use in the devKit contextual menu 'add a file' in the v/calc. (be sure to 'Sync' before and after).

I make minor changes in v/calc without troubles, editor seems robust. I just does not succeed in using 'signets/marks'. 'goto' lines sometimes does not refresh the display, moving the cursor with arrows sometimes correct this (perhaps 1000 lines is too much ?).

I sometime edit large parts in the virtual calc directly: PC keyboard and mouse are nice at this job.
Only gripes is the lack of 'SHIFT-arrows' to select text.

Actually sometimes the dev kit takes some seconds to react ... just wait.

When in trouble (shadow apps, strange apps...):
As seen somewhere in this forum I do the following:
  • connect to the dev kit
  • make the cleaning (from contextual menu, delete or init)
  • disconnect the calc (do not shut off)
  • on the calc, make a backup (MEM/Backups,Sauvegardes)
  • still on the calc, restore it at once
Do not shut off the calc between those steps.
I tested it and all I can say is: awesome job, Olivier ! This is a good case to show that Python on the Prime is not a toy, but a pretty stable and performant implementation ! And the topic is nice too - emulating the 41 on the Prime should interest HP fans ! Smile
Hi,
I have tested too (on G2) it's SUPER, cudos !
To morrow I'll test on my 2 G1.
Many thanks Olivier.
Amazing work. Can you give any tips on your workflow when developing this software since accessing python files of the python app via the connectivity kit is one way - I can read them on my PC but cannot write them back into the prime. How did you get around this?

Perhaps you could write a tutorial or provide some tips on using Python on the Prime, since my early steps have not been promising. Simple standard python syntax corrupts the prime python app, backups can't be restored reliably, infinite reboot problems accessing prime from the connectivity kit, and as mentioned cannot edit python on the pc and put them back onto the prime. Yet you have somehow pushed through all these issues and created this amazing software and you sing the praises of Python the Prime!
Olivier that's a great app.

Beyond that, it's a great example for using Python on the Prime.
1. creating own apps, makes you independent of the Python app, thus preserving the own development now is easily possible. It was before, but we didn't know how, THANKS a lot.

Your code gives valuable examples for applying Python on the Prime.

Quite a lot to chew on, THANKS.

Günter
Fantastic App!

Would you consider swap the function of these 2 keys?
() : x<>y
, : RDN

In Prime RPN mode the " ," key functions as x<>y (its symbolised as the crossarrows symbol on right of key).
(05-18-2021 04:30 PM)Stevetuc Wrote: [ -> ]Fantastic App!

Would you consider swap the function of these 2 keys?
() : x<>y
, : RDN

In Prime RPN mode the " ," key functions as x<>y (its symbolised as the crossarrows symbol on right of key).

To swap the keys, just swap the lines 33 and 34 in the key.py file Smile

P.S. v2 is progressing, with .MOD support and a config.py file to allow
full control of configuration (system, loading of modules in page or in ports, ram, ...)

But it seems that python code
Code:
a=[]
a.append='test {:#x}'.format([1,2,3])
can crash python and then make the prime to reboot
(05-16-2021 01:14 PM)Oulan Wrote: [ -> ]HP PRIME development workflow in micro-python:

> An important directory on the PC is 'HP Connectivy Kit/Content/app.hpappdir/' in your 'Documents'

The 'C:\Users\Andy\Documents\HP Connectivity Kit\Content' directory has no .hpappdir directories in it, and nothing called 'app.hpappdir', only the directories 'Exam Modes' and 'Results'. If you mean 'app.hpappdir' to refer to any app being developed, then ok, so I copy a random .hpappdir from 'C:\Users\Andy\Documents\HP Connectivity Kit\Calculators\MYVIRTCALC1' into 'Content' - what meaning or effect does that actually have? - I see no effect in the virtual calculator or in the conn kit, which suggests 'Content' is an arbitrary location and any other directory on the PC could be used as a temporary work area, right?

Copying a .hpappdir e.g. Py41b_copy.hpappdir into 'C:\Users\Andy\Documents\HP Connectivity Kit\Calculators\MYVIRTCALC1' whilst the virtual calculator is running then hitting 'Refresh' from the r. click menu of the calculator listed in the conn. kit does not update the virtual calculator - in fact 'Refresh' actually DELETES the new .hpappdir directory I just copied in.

> First do a 'Sync' from the contextual menu of the v/calc.

I cannot find a 'Sync' menu item in the virtual calc, all I see when I r.click is the regular 'Calculator/Skins/Edit/Help' menu structure. Inside the 'Calculator' menu there is only the menu item to 'Reset' not 'Sync'?
I was to force the the app to the calc/emu by right clicking the app and do send to class and that worked
This app is phenomenal.
(05-19-2021 01:14 AM)Dougggg Wrote: [ -> ]I was to force the the app to the calc/emu by right clicking the app and do send to class and that worked

That worked - thanks! To be clear, in order to edit a Python file on your PC:
  • Double click on a python file in conn.kit and vscode (or whatever) will open.
  • Edit the file and save, which saves to the 'C:\Users\Andy\Documents\HP Connectivity Kit\Calculators\SOMECALC1' directory but not to the calc itself.
  • Right click on the calc in the conn.kit and select 'send to class' to save the file back into the calculator.
Perhaps I missed these instructions elsewhere, but as far as I know, this is the first bit of documentation on how to edit a Prime Python file via the PC. Not sure why this really useful piece of information has not been communicated to the Prime Python community of beta testers - perhaps its obvious? It's not obvious to me, as I've never needed to use the 'send to class' feature before and am not a teacher.

(05-22-2021 09:47 PM)Eddie W. Shore Wrote: [ -> ]This app is phenomenal.

I agree - this HP41C emulator running on the HP Prime written in Python - is truly astonishing. Pressing and holding a key even shows 'NULL' after a second, etc. just like the real HP41C.
I had to press all the keys and write down what each one does - we might need an overlay ;-) Some useful keys are:
- To enter in LBL press Units
- To enter in RTN press SHIFT EEX

I was even able to assign programs to keys in USER mode and XEQ them, with the traditional little bird flying on the screen as they are executed!
Hi, new release (V.1) see.
  • Now a config.py script for configuration Wink (see current config when running with 'Help' key)
  • Better Timer support
  • .MOD modules format support added for ALL stuff (even system)
  • Some .MODs in the apps (see the list with 'shift' Help)
  • Hepax support added BUT load the rom in an ODD page and use only ONE 8kword ram module (see examples in config.py). Ram is relocated at load (you can change the port at will)
  • ...
The keymap is a real problem, but very difficult to solve ... as Prime and 41 key do not have the same primary, secondary and alpha mapping ... converting on the fly is already done but taking all case into account is NOT possible:
Ie. when 'GETKEY' is used in a program, depending of the displayed message or program, the meaning is either a digit entry or an alpha entry ... (ie Y/N ... or 0..9). The keymapping is different but I can not guess which one to use Wink
Same problem arise in system rom ... it can be sometime guessed but with all the different roms existing ...

Anyway, happy testing Smile

Btw this is a TRUE hardware emulator, so the goose is included Wink (try synthetic programing if you want to be sure)
Fantastic program! Is there a way to save the speed mode (Fast or Slow) when exiting?
Home doesn't do it for me.
(05-23-2021 04:21 PM)toml_12953 Wrote: [ -> ]Fantastic program! Is there a way to save the speed mode (Fast or Slow) when exiting?
Home doesn't do it for me.

The file p41.py has the entry (line 125) cpu=nut.nut(speed=1). Changing speed=1 to speed=0 starts the 41 in fast-mode. But this is also semi permanent.

You have to tell the calculator to use the changed file of course Wink

Günter
41b is working fine for me on g1. However 41c does not. After pressing clear I get to the main screen. But now pressing any key takes me to Python numeric view showing :
Import phi
...
...
Import config
I can press clear again to get back to main screen as above, with same issue.
I have firmware 2.1.14588 (2021 05 05)
CK build 2.1.14575 (2021 4 28)
Any ideas what's wrong?
(05-24-2021 08:28 AM)Stevetuc Wrote: [ -> ]41b is working fine for me on g1. However 41c does not. After pressing clear I get to the main screen. But now pressing any key takes me to Python numeric view showing :
Import phi
...
...
Import config
I can press clear again to get back to main screen as above, with same issue.
I have firmware 2.1.14588 (2021 05 05)
CK build 2.1.14575 (2021 4 28)
Any ideas what's wrong?

Hi,

No ideas, I don't have a G1.
BUT there are some bugs in micropython. Some code can work in the virtual calc and make the G2 crash ...
I found one on a G2 with string.format function ... ok in virtual, crash in real.

You have to look at the 41b and 41c code to see what kind of new function/syntax is used in 41c
Then try to avoid that by rewriting code differently, and try ...

Sorry, that is all I can do
(05-24-2021 10:03 AM)Oulan Wrote: [ -> ]
(05-24-2021 08:28 AM)Stevetuc Wrote: [ -> ]41b is working fine for me on g1. However 41c does not. After pressing clear I get to the main screen. But now pressing any key takes me to Python numeric view showing :
Import phi
...
...
Import config
I can press clear again to get back to main screen as above, with same issue.
I have firmware 2.1.14588 (2021 05 05)
CK build 2.1.14575 (2021 4 28)
Any ideas what's wrong?

Hi,

No ideas, I don't have a G1.
BUT there are some bugs in micropython. Some code can work in the virtual calc and make the G2 crash ...
I found one on a G2 with string.format function ... ok in virtual, crash in real.

You have to look at the 41b and 41c code to see what kind of new function/syntax is used in 41c
Then try to avoid that by rewriting code differently, and try ...

Sorry, that is all I can do
Thanks. I think that might be quite a challenge for me!
Is anyone running 41c on g1 without issues?
if so what version firmware and CK are you using?

Thanks
Steve
(05-24-2021 10:48 AM)Stevetuc Wrote: [ -> ]
(05-24-2021 10:03 AM)Oulan Wrote: [ -> ]Hi,

No ideas, I don't have a G1.
BUT there are some bugs in micropython. Some code can work in the virtual calc and make the G2 crash ...
I found one on a G2 with string.format function ... ok in virtual, crash in real.

You have to look at the 41b and 41c code to see what kind of new function/syntax is used in 41c
Then try to avoid that by rewriting code differently, and try ...

Sorry, that is all I can do
Thanks. I think that might be quite a challenge for me!
Is anyone running 41c on g1 without issues?
if so what version firmware and CK are you using?

Thanks
Steve

Just to be sure, what are your settings in Plot/setting for python ?
On my G2 I have Heap:1024 and Stack:40000 (and Memory is 233MB). py41c is fine with those settings.
'Files will auto load when changed' is ticked and 'ask before resetting python' is not.

Olivier
(05-25-2021 07:00 AM)Oulan Wrote: [ -> ]
(05-24-2021 10:48 AM)Stevetuc Wrote: [ -> ]Thanks. I think that might be quite a challenge for me!
Is anyone running 41c on g1 without issues?
if so what version firmware and CK are you using?

Thanks
Steve

Just to be sure, what are your settings in Plot/setting for python ?
On my G2 I have Heap:1024 and Stack:40000 (and Memory is 233MB). py41c is fine with those settings.
'Files will auto load when changed' is ticked and 'ask before resetting python' is not.

Olivier

Hi Olivier I have the same settings for Heap and Stack, and same ticked options, but of course the free memory is dramatically less
at 11.26MB. I guess this is the issue? I could try reducing the number of modules loaded.
What are the red hex numbers (circled in blue)?

[Image: HP-41C.jpg?raw=1]
Pages: 1 2 3
Reference URL's