Omnimaga

Calculator Community => Other Calc-Related Projects and Ideas => TI-Nspire => Topic started by: Vogtinator on October 28, 2014, 06:48:08 am

Title: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 06:48:08 am
Yeah, it's been quite a while since the first release and I haven't posted here as it wasn't very stable at all, but now all bugs I could find are gone.
It's a port of micropython (http://micropython.org/), specifically for microcontrollers and due to the working integration of newlib and a "real" nio_use_stdio with the help of libsyscalls it could be ported quite easily. I added a small set of 2d functions to test the performance with mandelbrot and now it seems to be quite useful, and it's actually used by students already!
You can use a text editor to write .py.tns files and execute them directly (due to the file association) or use micropython in interactive mode.
It's using a heavily modified version of nspire-io for line history and I also changed the input method to be more PC-like (hold shift for upper case, for example). tab inserts 4 spaces for easier intendation.

(http://img.ourl.ca/micropython-1.png)
(http://img.ourl.ca/micropython_mandel1.png)

More in-depth information on ti-planet: http://tiplanet.org/forum/viewtopic.php?f=43&t=15140 and http://tiplanet.org/forum/viewtopic.php?f=43&t=15309
Title: Re: micropython - Python for Nspire calculators
Post by: Sorunome on October 28, 2014, 08:19:13 am
Wow, that is pretty awesome!
Python is a pretty powerful language after all :)
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 28, 2014, 08:52:03 am
Is there documentation for the nsp module? (Also, how do you exit an infinate loop?)
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 12:57:47 pm
Quote
Is there documentation for the nsp module?
here (https://github.com/Vogtinator/micropython/blob/nspire/nspire/texture.c#L18)

Quote
(Also, how do you exit an infinate loop?)
You can't, it runs single-threaded. It should be possible however to register an IRQ handler for the On-button.
Title: Re: micropython - Python for Nspire calculators
Post by: Xeda112358 on October 28, 2014, 01:28:17 pm
Wow, this is cool! Will it work for grayscale models?
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 01:34:12 pm
Quote
Wow, this is cool! Will it work for grayscale models?
Yes, on all models! Mandelbrot doesn't look that pretty, though :)
All python programs are entirely platform independant, the nsp module handles everything.
(There may be some keys missing, I didn't look at the clickpad and touchpad keys)
Title: Re: micropython - Python for Nspire calculators
Post by: Art_of_camelot on October 28, 2014, 03:37:44 pm
Pretty cool stuff! :D
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 28, 2014, 03:41:27 pm
Quote
Is there documentation for the nsp module?
here (https://github.com/Vogtinator/micropython/blob/nspire/nspire/texture.c#L18)


I wouldn't exactly call that documentation...
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 03:54:18 pm
Quote
Is there documentation for the nsp module?
here (https://github.com/Vogtinator/micropython/blob/nspire/nspire/texture.c#L18)


I wouldn't exactly call that documentation...
I would. Is something crucial missing? Functions like setPx should be fairly simple to use.
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 28, 2014, 03:55:56 pm
isn't that only for nsp.texture?
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 03:57:13 pm
Yes, there isn't anything else in nsp yet (except for nsp.readRTC(), but that's not really useful and not documented anywhere).
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 28, 2014, 04:02:51 pm
Ah, I see. and are you going to make the on button be ctrl-c? Other wise an infinite loop requires a reset, correct?
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 04:16:01 pm
Quote
Ah, I see. and are you going to make the on button be ctrl-c? Other wise an infinite loop requires a reset, correct?
Exactly. Shouldn't be hard to do, it has a dedicated IRQ (power management).
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on October 28, 2014, 04:48:48 pm
This is really promising. I am curious how fast this will run compared to Lua and C? It would definitively be a great alternative for those who prefer Python and I know that at one point or another, Omnimaga always had its share of Python users.

Glad this is still being updated, by the way. I wish you good luck on this. :)
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 05:02:29 pm
This is really promising. I am curious how fast this will run compared to Lua and C?
There are some graphs on ti-planet, although it changed a bit. There are three modes in micropython:
-Bytecode-Interpreter: A bit slower than Lua
-Native (generates ARM code): A lot faster than lua (dependant on code)
-Viper (generates optimized ARM code): Almost like native, although variables must have types assigned to them

Quote
It would definitively be a great alternative for those who prefer Python and I know that at one point or another, Omnimaga always had its share of Python users.

Glad this is still being updated, by the way. I wish you good luck on this. :)
Yeah, with a lot of other stuff to do work will be slow, but present :)
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on October 28, 2014, 05:05:37 pm
I assume that native and viper will not require Micropython to run and that Bytecode-interpreter is mainly for quick testing, right?
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 28, 2014, 05:10:14 pm
Quote
I assume that native and viper will not require Micropython to run and that Bytecode-interpreter is mainly for quick testing, right?
Nope, all of them are done at runtime, so you will always need micropython as it containes the needed runtime functions (print, for instance).
Compiling doesn't take long though.

There's a post by Damien on kickstarter:
https://www.kickstarter.com/projects/214379695/micro-python-python-for-microcontrollers/posts/664832
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on October 28, 2014, 05:14:21 pm
Aaah I see now. Thanks for the info. MY main  concern about needing the main program to run something is when the software takes like 40% of the calculator memory but that is only really an issue with the regular 83+ and older Nspire models I think and sometimes the file size or dependency is necessary.
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 30, 2014, 10:36:06 am
On my Nspire CX CAS it take about 5 seconds for python to start, it didn't happen before...
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on October 30, 2014, 02:34:52 pm
Do you have more files than before? I noticed that when I have a lot of files on my calculator, the documents menu takes much longer to open and starting stuff takes longer too.
Title: Re: micropython - Python for Nspire calculators
Post by: aeTIos on October 30, 2014, 02:48:19 pm
awesome.
Then. the most important question for game programmers:

Does It Have All Keys Repeat?
if so, I might make some programs using this (and learn python)

Also, how hard is it to make an on-calc editor/compiler?
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 30, 2014, 03:07:49 pm
Also, how hard is it to make an on-calc editor/compiler?


Vogtinator is already working on that: http://www.omnimaga.org/ti-nspire-projects/pywrite-python-script-editor/
Title: Re: micropython - Python for Nspire calculators
Post by: aeTIos on October 30, 2014, 03:14:43 pm
Aah good. I see that that's only a script editor though (which is already awesome). I reckon that a compiler wouldn't be too hard, either.
If everything goes on-calc, I could see this become a kind of Axe.
(We'd still need some way to create graphics, though :P)
Title: Re: micropython - Python for Nspire calculators
Post by: Adriweb on October 30, 2014, 03:20:40 pm
Well, there's already some kind of "JIT" available, btw.
(See the examples)

(Edit : It's native code emission, IIRC - look at "@micropython.native")
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 30, 2014, 03:38:24 pm
Do you have more files than before? I noticed that when I have a lot of files on my calculator, the documents menu takes much longer to open and starting stuff takes longer too.


I'm talking about when I launch micropython, the screen is black for about 5 seconds before opening the prompt.
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on October 30, 2014, 03:51:34 pm
Quote
On my Nspire CX CAS it take about 5 seconds for python to start, it didn't happen before...
Yeah, I know that happens sometimes. I'm not sure why and where this happens, but it's not really a big issue.

Quote
Aah good. I see that that's only a script editor though (which is already awesome). I reckon that a compiler wouldn't be too hard, either.
If everything goes on-calc, I could see this become a kind of Axe.
Compiling isn't possible, but that shouldn't be an issue as micropython translates fast enough.

Quote
(We'd still need some way to create graphics, though )
ConvertImg? (http://github.com/Vogtinator/ConvertImg)

Quote
Do you have more files than before? I noticed that when I have a lot of files on my calculator, the documents menu takes much longer to open and starting stuff takes longer too.
It's not even fragmentation, it's just that the NAND is really f*cking slow and the filesystem as well. I would understand if it was writing being slow, as it has to be reliable, even if you pull the battery or reset during saving.

Quote
awesome.
Then. the most important question for game programmers:

Does It Have All Keys Repeat?
if so, I might make some programs using this (and learn python)
Apart from a gets like "input()" function there isn't any input handling right now. I'm not sure how to do that, probably "isKeyPressed".
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on October 31, 2014, 12:02:21 am
It's not even fragmentation, it's just that the NAND is really f*cking slow and the filesystem as well. I would understand if it was writing being slow, as it has to be reliable, even if you pull the battery or reset during saving.
Thankfully, at least it's nowhere as brutal as back when we were stuck with MirageOS 1.1 on Z80 calcs before 1.2 came out. When you had 150+ programs, it took 40 minutes to open the program list. O.O
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on October 31, 2014, 07:05:35 am
Aah good. I see that that's only a script editor though (which is already awesome). I reckon that a compiler wouldn't be too hard, either.
If everything goes on-calc, I could see this become a kind of Axe.
(We'd still need some way to create graphics, though :P )


Python is an interpreted language, there is no compiler for it.
Title: Re: micropython - Python for Nspire calculators
Post by: Jim Bauwens on November 01, 2014, 05:22:42 pm
Python is an interpreted language, there is no compiler for it.

There are JIT (Just-in-Time) compilers out there for Python (Micro Python has a JIT too IIRC).  Also you can compile a python script to python bytecode to improve loading time.
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on November 01, 2014, 05:39:10 pm
Not to mention in the case of Micropython, I think Vogtinator mentionned that there's a compiled mode or two. It really depends of the language and platform I guess.
Title: Re: micropython - Python for Nspire calculators
Post by: Adriweb on November 01, 2014, 07:22:35 pm
which is ... what I've written above: http://www.omnimaga.org/ti-nspire-projects/micropython-python-for-nspire-calculators/msg395678/#msg395678
Title: Re: micropython - Python for Nspire calculators
Post by: xlibman on November 01, 2014, 08:02:03 pm
I know, I was just mentionning since he seemed to have missed your post or found it unclear.
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on February 12, 2015, 03:17:28 pm
Are you going to submit a pull request to the main micropython repo?
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on February 12, 2015, 04:12:14 pm
Maybe, depends on the progress and integration I make. They integrated several new features (including modules!) in micropython which aren't usable on the nspire yet, but I'm working on it.
Title: Re: micropython - Python for Nspire calculators
Post by: Ivoah on February 13, 2015, 01:04:57 pm
I hope you do, it would result in much more publicity for the nspire port.
Title: Re: micropython - Python for Nspire calculators
Post by: Chirlian on February 28, 2017, 07:39:34 pm
Hi,
have some questions about micropython (just trying to ask vogtinator, but it seems he is busy now):
-use nspire cx cas
-os 4.4.0.532
-nspire 4.4 installed
-micropython 146 started and terminated as told (also tried 134)
-get no .py.tns file started (also such with i=1 and print(i) as 2 lines Content)
-nassembler also doesnt start, gives the 'file format not supported' error
-how to enter the ' sign? Didnt find it. Also the graphic at TI planet shows several special keys but not this sign
-can't use pyWrite. It starts, I can write my 2 lines (i=1, print(i)), can save it with adding .py.tns. Can see them by open function in pyWrite, also
 can load, edit and save again. But RUN doesnt do anything
-starting such a saved .py.tns file also doesn't work, same file error
-is there a list of the micropython commands? Trying several commands from tutorials giving mostly File Error
Hope to find some help.
thanks
klaus
Title: Re: micropython - Python for Nspire calculators
Post by: Vogtinator on March 02, 2017, 09:13:56 am
Hi,
have some questions about micropython (just trying to ask vogtinator, but it seems he is busy now):
-use nspire cx cas
-os 4.4.0.532
-nspire 4.4 installed
-micropython 146 started and terminated as told (also tried 134)
-get no .py.tns file started (also such with i=1 and print(i) as 2 lines Content)

Check your ndless.cfg.tns.

Quote
-nassembler also doesnt start, gives the 'file format not supported' error

Hm, no idea.

Quote
-how to enter the ' sign? Didnt find it. Also the graphic at TI planet shows several special keys but not this sign
Indeed, it's not available on CX...

Quote
-can't use pyWrite. It starts, I can write my 2 lines (i=1, print(i)), can save it with adding .py.tns. Can see them by open function in pyWrite, also
 can load, edit and save again. But RUN doesnt do anything
You need to configure the path to micropython.

Quote
-starting such a saved .py.tns file also doesn't work, same file error
-is there a list of the micropython commands? Trying several commands from tutorials giving mostly File Error
It's a basic subset of python.

Quote
Hope to find some help.
thanks
klaus
Title: Re: micropython - Python for Nspire calculators
Post by: Chirlian on March 03, 2017, 12:06:25 pm
Hi,
have some questions about micropython (just trying to ask vogtinator, but it seems he is busy now):
-use nspire cx cas
-os 4.4.0.532
-nspire 4.4 installed
-micropython 146 started and terminated as told (also tried 134)
-get no .py.tns file started (also such with i=1 and print(i) as 2 lines Content)
Quote
Check your ndless.cfg.tns.
ext.nes=nespire
ext.gb=gbc4nspire
ext.gbc=gbc4nspire
ext.gba=gpsp_launcher
ext.nvid=nPlayer
ext.cvid=nPlayer
ext.bmp=mviewer_cx
ext.png=mviewer_cx
ext.jpg=mviewer_cx
ext.wad=ndoom

Should I add
ext.py=micropython ?
Quote
-how to enter the ' sign? Didnt find it. Also the graphic at TI planet shows several special keys but not this sign
Indeed, it's not available on CX...

Quote
-can't use pyWrite. It starts, I can write my 2 lines (i=1, print(i)), can save it with adding .py.tns. Can see them by open function in pyWrite, also
 can load, edit and save again. But RUN doesnt do anything
You need to configure the path to micropython.

Quote
-starting such a saved .py.tns file also doesn't work, same file error
-is there a list of the micropython commands? Trying several commands from tutorials giving mostly File Error
It's a basic subset of python.
Now I found the error, a simple mistake I made: for testing both micropythen versions (134/146) I added this numbers to their names. So the .py files and nassembler couldn't find it. Just had to rename one and now everthing works:-)
Thanks for your help!
thanks
klaus



Edit (Eeems): Fixed quotes
Title: Re: micropython - Python for Nspire calculators
Post by: superloach on February 22, 2018, 04:19:31 pm
I'm not too familiar with how µPy works in the background (or much C at all), but I ran into an issue with lambdas and default arguments (https://github.com/micropython/micropython/issues/1628), and found that somebody had already fixed it (https://github.com/micropython/micropython/commit/2c838942574a4970c922d1550f04e4b7b4d865a6). Could this be added to µPy for nspire?
I'd like to see further development and bug fixing on this project, but, as I mentioned before, I'm not currently able to help directly, although I'm working on learning C.  :3
Also, basic key events and a time module would be great, perhaps even clearing the screen?