Porting Python (specifically p14p) to CHDK - General Discussion and Assistance - CHDK Forum

Porting Python (specifically p14p) to CHDK

  • 5 Replies
  • 4368 Views
Porting Python (specifically p14p) to CHDK
« on: 18 / November / 2013, 12:07:22 »
Advertisements
I've decided (more for fun than anything else) to port Python as a 3rd scripting language to CHDK.

I've recently got a SX150IS and stuck CHDK on it, and now I need to do more hacking! I started making Klondike solitaire game module, but got a bit bored with that, so decided to do something more useful.

As I'm very (very) new to the platform, is this a bad (or impossible) idea? Do we need another interpreter language? I've already got the source code and the tools for building CHDK, so I'm going to get cracking now.

*

Offline reyalp

  • ******
  • 14082
Re: Porting Python (specifically p14p) to CHDK
« Reply #1 on: 18 / November / 2013, 16:15:37 »
I don't much about python (something I really should remedy one of these days :-[) or p14p, but here are some things to keep in mind

1) CHDK doesn't have a full ANSI runtime library. Functions need to either be found in the firmware, or implemented from scratch. The functions we do use may not confirm exactly to standards.
2) CHDK code has relatively little memory available. Typically, ports have a few hundred kb to a 1 mb available to play with. From the looks of it, p14p should should be fine with this.
3) In the current scripting setup, a scripting engine needs to be explicitly aware of and play nicely with task scheduling. The script interpreters run in kbd_task. kbd_task needs to run it's non-script related code regularly, and various calls in the script code (such as waiting for a key press) need to yield back from the scripting engine. You should look at the 'action stack' code to get a feel for how this works. The script engine should also be able to yield periodically so a tight loop in script doesn't lock up the camera UI, see the debug hook usage in the lua implementation for this.
4) A scripting implementation will likely need a significant amount of "glue" to access useful camera functionality (see luascript.c as an example).

As to whether we need an additional interpreter: From my point of view, it seems like more maintenance work without a lot of obvious benefit. Lua is a sufficiently capable language for the kinds of things you can do in a scripting environment. The only reason we have both ubasic and lua is that ubasic was there first, and is NOT capable enough to do a lot of the things you would want to do in a scripting language.

However, if enough people prefer python over Lua, it might prove valuable.
Don't forget what the H stands for.

Re: Porting Python (specifically p14p) to CHDK
« Reply #2 on: 18 / November / 2013, 16:27:12 »
Python is actually a VM based language and has intermediate byte code.

1) This version of python is specially designed for environments without ANSI, it runs on system-on-chip platforms for example, so is ideally suited for CHDK.
2) p14p needs a minimum of 8KB to be practical. The fact we have at least 10 times this makes it viable.
3) Thanks for the pointers :) I'm using the Lua implementation as a base, so will look at the action stack and the debug hooks in it.
4) I am aware that I'll have to create a CHDK module (an include for python) that will implement all of the built in functions. I'll use the Lua implementation as a base.

The main benefits over Lua would be that it is very widely used and included by default. We could even create extensions for PTP in desktop python that would let the computer run the same code as the camera for rapid testing/prototyping, but I won't get too ahead of myself.

*

Offline reyalp

  • ******
  • 14082
Re: Porting Python (specifically p14p) to CHDK
« Reply #3 on: 18 / November / 2013, 16:51:08 »
Python is actually a VM based language and has intermediate byte code.
So is Lua.
Quote
2) p14p needs a minimum of 8KB to be practical. The fact we have at least 10 times this makes it viable.
According the homepage, it also needs ~55k for the code. In CHDK, this counts against RAM. Still likely to come out better than Lua. The Lua .flt module comes in at 130k, although this does include the standard libraries and the CHDK specific code.
Don't forget what the H stands for.


Re: Porting Python (specifically p14p) to CHDK
« Reply #4 on: 19 / November / 2013, 07:23:09 »
Initial compilation is yielding about 125KB, including standard libs, but not the CHDK specific functions yet. Looks like I might have to add it as an alternative to Lua, rather than a 3rd option.

I'm going to carry on anyway, because it'll help me understand the CHDK code :)

*

Offline reyalp

  • ******
  • 14082
Re: Porting Python (specifically p14p) to CHDK
« Reply #5 on: 19 / November / 2013, 16:06:01 »
Initial compilation is yielding about 125KB, including standard libs, but not the CHDK specific functions yet. Looks like I might have to add it as an alternative to Lua, rather than a 3rd option.
I don't understand what you mean, what's the difference between an alternative and a 3rd option? I would not consider replacing Lua with python at this point, it would be a great disservice to all the people who have existing Lua code.

The script system uses loadable .flt modules for each language (thanks philmoz!), so there should be no problem having both python and lua as options.
Quote
I'm going to carry on anyway, because it'll help me understand the CHDK code :)
There's nothing wrong with that :)
Don't forget what the H stands for.

 

Related Topics