C enviroment that can read python occasionally, is micropython what I want?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
microPyforMy
Posts: 7
Joined: Wed Mar 22, 2017 4:09 pm

C enviroment that can read python occasionally, is micropython what I want?

Post by microPyforMy » Wed Mar 22, 2017 4:17 pm

I have a microconctroller project that absolutely must be done in C for reasons.

However, I have functions this micro will need to perform that will come from a server. I'd LIKE to feed it python commands and have them execute locally. This means a python interpreter inside of a C program on a micro. The alternative is either a domain specific language that could get messy quickly. Or, to write the commands in C, compile in a stand alone enviroment, cut them out, paste them to the server and send them to the device - risky.

It appears MicroPython's main focus is a whole environment for the micro. I only want some of this functionality. If micropy isn't what I want, does anyone make it?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by deshipu » Wed Mar 22, 2017 5:12 pm

You can always put your whole program in a function in a module written in C, and then in main.py only have one line that imports and runs that. Then, in that C code, you have access to all the MicroPython tooling, including functions for calling and executing Python code. Not the most straightforward way to do it, but that's the easiest I can think of without delving too deep into MicroPyhton. Of course that Python code will have full control over the microcontroller once called, so this is a pretty bad idea from the security point of view.

What are those snippets of code supposed to do, exactly? Are you sure you can't replace them with data?

An alternative, and much lighter, approach would be to write (or use a ready-written) a very small Forth interpreter...

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by stijn » Wed Mar 22, 2017 7:47 pm

deshipu wrote:that's the easiest I can think of without delving too deep into MicroPyhton
Or if you are ready for some playing around (hard to tell which way is more work - also don't forget the build process etc) have a look at https://github.com/micropython/micropyt ... lo-embed.c, that's about the minimum of code needed to be able to run a string of uPy code. Same principle but more extensive: https://github.com/stinos/micropython/b ... d/py/run.c
Oh and if you don't want/need all of uPy's functionality look in mpconfigport.h: there's a whole lot of things which can be disabled.

microPyforMy
Posts: 7
Joined: Wed Mar 22, 2017 4:09 pm

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by microPyforMy » Wed Mar 22, 2017 11:11 pm

[quote="deshipu"]What are those snippets of code supposed to do, exactly? Are you sure you can't replace them with data?
An alternative, and much lighter, approach would be to write (or use a ready-written) a very small Forth interpreter...[/quote]

Simple things. Execute some pre-made C functions with data from the server, do a couple if / else type things. Fairly simple. I'm not really up to speed on forth except that it looks confusing AF to read, reads to me like something from the 1970s (which it is), I'm not super duper keen on it. Part of the reason I want to move to a scripting language is so that other people could write these "test" or remote code executions, that could happen with Py. Not with Forth. I agree it would be smaller and lighter, but defeat the purpose.



[quote="stijn"][quote="deshipu"]that's the easiest I can think of without delving too deep into MicroPyhton[/quote]

Or if you are ready for some playing around (hard to tell which way is more work - also don't forget the build process etc) have a look at [url]https://github.com/micropython/micropyt ... lo-embed.c[/url], that's about the minimum of code needed to be able to run a string of uPy code. [/quote]

So that's actually pretty close. Two questions though,

1. If I were to compile remotely, that would save some bandwidth during the transfer and the steps of having to compile on the remote machine, right? Although I suppose that in this case compile would be working on that systems memory and limitations, something where a pre-compiled code might not be portable to another instance of that module. Not sure on this.

2. I haven't looked yet to see how mPy in that case of executing small scripts would be able to access system memory. Also how return codes from Py back to C would go. I do like how you can specify the stack right there though. Seems easy enough but could get out of hand quickly.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by deshipu » Wed Mar 22, 2017 11:33 pm

The precompiled files can actually be larger than the original source, so you are unlikely to get much savings there.

The Python functions you are calling can return a value, set a global variable, or call a method on an object they get to change the state.

microPyforMy
Posts: 7
Joined: Wed Mar 22, 2017 4:09 pm

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by microPyforMy » Thu Mar 23, 2017 4:23 pm

deshipu wrote:The precompiled files can actually be larger than the original source, so you are unlikely to get much savings there.

The Python functions you are calling can return a value, set a global variable, or call a method on an object they get to change the state.
Well, that's annoying on compiled size because I'm looking at a LOT of bytes with scripting over efficient bytecode. But fine.

Ok, makes sense on Python functions returning, but what about C functions from Python? Possible? Libraries?

For example:

In C: function fooC (uint8_t a) { return a; }

Can I call fooC(value) from a Python script?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by dhylands » Thu Mar 23, 2017 4:39 pm

microPyforMy wrote:Ok, makes sense on Python functions returning, but what about C functions from Python? Possible? Libraries?

For example:

In C: function fooC (uint8_t a) { return a; }

Can I call fooC(value) from a Python script?
Not directly.

C functions which are callable from python have to be registered with the interpreter and have to have very particular signatures. In particular:
https://github.com/micropython/micropyt ... #L402-L409

I wrote up some examples of calling C functions from python here:
http://forum.micropython.org/viewtopic. ... g+c+sample
and the latest code example is here:
https://github.com/dhylands/micropython ... c_sample.c
The individual commits (which show all of the little changes) are here:
https://github.com/dhylands/micropython ... 055abb5fef
https://github.com/dhylands/micropython ... 8997508622

microPyforMy
Posts: 7
Joined: Wed Mar 22, 2017 4:09 pm

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by microPyforMy » Thu Mar 23, 2017 6:14 pm

dhylands wrote: Not directly.

C functions which are callable from python have to be registered with the interpreter and have to have very particular signatures. In particular:
https://github.com/micropython/micropyt ... #L402-L409
Dave,

Thanks for that. A little more complicated than I'd like as every command I send is basically a C command and it's args, that's a lot of what are almost like work-arounds to get what I want.

What I really need is scripting with fast and clear access to C. Combined with the python overhead and all the super-cool python things I have zero need for (like importing anything). I'm a little bummed, but at least I know this isn't what I need just yet.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by stijn » Thu Mar 23, 2017 7:12 pm

microPyforMy wrote:What I really need is scripting with fast and clear access to C.
I'm not sure such a thing really exists (unless you consider C compiled on the fly a scripting language). There will always be a layer in between, mostly converting types back and forth between the script and C. Lua is more mature so the C API is a probably a bit more polished and clear than uPy. I have no clue whether it's faster though.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: C enviroment that can read python occasionally, is micropython what I want?

Post by deshipu » Thu Mar 23, 2017 7:14 pm

You can also take a look at LUA (nodemcu) and JavaScript (espruino) -- they are implemented for similar platforms to those on which MicroPython works, and might fit your use case better. And of course there is always that Forth ;-).

Post Reply