Pages: [1]   Go Down
  Print  
Author Topic: Lua Scripting  (Read 1585 times)
0 Members and 1 Guest are viewing this topic.
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« on: 23 / April / 2008, 20:53:50 »

Like the title says: I wish for Lua scripting instead of BASIC. I think it is the ideal language for this task.
The BASIC restrictions are really annoying.

I wonder if this was ever considered?

Jörg

Logged
PhyrePhoX
Global Moderator
Hero Member
*****

Karma: +139/-37
Offline Offline

Posts: 1818


Coders Humiliate DSLR Kiddies


« Reply #1 on: 23 / April / 2008, 21:00:07 »

what is lua? why is it the ideal language? what restrictions in BASIC are you referring to?
why am i asking this? why is your post lacking information? because you didnt provide any.
please answer my questions, then maybe other people can answer your questions Smiley
Logged

fbonomi
Sr. Member
****

Karma: +35/-1
Offline Offline

Posts: 320

A570IS SD1100/Ixus80


« Reply #2 on: 23 / April / 2008, 21:57:26 »

Basic was chosen because a very small implementation (uBasic) was readily available and was extremely "lightweight" in terms of memory etc. Consider that all the source code of Ubasic is well under 1.000 lines (25k of source). The requirement in terms of other libraries is extremely small (IIRC only printf() and exit() are required!)

By contrast Lua has 437 Kbytes of source code for over 15.000 lines of code. Moreover, it strongly relies on the C standard libraries...

too much power required! After all out cameras are not PCs :-)
Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #3 on: 25 / April / 2008, 22:20:17 »

I was a little frustrated at my first tries to bring my scripts to live. Had to fight that if/then/else doesn't seem to work right with then-blocks longer than one line.
One character variable names or the inability to have functions was also not very helpfull.
I programmed in quite a lot of languages. And I must say that uBasic is at the end of my favorite list.

But back to Lua. I still think that it is the right language for such tasks, considered that it is even used in much smaller devices.
Do you know about Lua: technical note 2 ?
This describes how to create a minimalist Lua that compiles to 25kb. Well you know that this depends on the architecture.

Well if no one steps forward, I think I'll embed Lua myself, thanks to Open Source. And if its only for me to keep sane while writing scripts.

Jörg

Logged
PhyrePhoX
Global Moderator
Hero Member
*****

Karma: +139/-37
Offline Offline

Posts: 1818


Coders Humiliate DSLR Kiddies


« Reply #4 on: 26 / April / 2008, 00:19:39 »

hm good luck then Smiley
after reading this technical note, i think that by reducing the size of the lua core, you also lose the ability to parse scripts, thus you have to feed it precompiled bytecode, eh? that would render the concept of "scripts" kind of useless. correct me if i'm wrong.
you are more than welcome to try to implement this Smiley
but maybe you can invest some time & brains into coding a editing function that uses the camera keyboard before? Smiley this way one would not have to rely on a computer to write one's scripts on. makes in-camera-debugging way easier and subsequently the use of ubasic.
whatever you do, have fun while doing it and let us know what you are up to Smiley
Logged

fbonomi
Sr. Member
****

Karma: +35/-1
Offline Offline

Posts: 320

A570IS SD1100/Ixus80


« Reply #5 on: 26 / April / 2008, 07:52:59 »

but maybe you can invest some time & brains into coding a editing function that uses the camera keyboard before? Smiley

I would LOVE this :-)
Logged
fe50
Hero Member
*****

Karma: +65/-1
Offline Offline

Posts: 734


IXUS50 & 860, SX10


« Reply #6 on: 26 / April / 2008, 11:23:51 »

but maybe you can invest some time & brains into coding a editing function that uses the camera keyboard before? Smiley

I would LOVE this :-)


I would LOVE this too - an editing feature (for scrips) would be phenomenal !
Logged

Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #7 on: 26 / April / 2008, 17:15:14 »

whatever you do, have fun while doing it and let us know what you are up to Smiley

Thanks for the motivation. I'm at the point that Lua compiles fine and is linked into the FIR-file. It grows about 43kb. That is with parser and bytecode compiler. But without most standard libraries. But there is not much use for them anyway, I guess.

The biggest problem so far is the absence of setjmp/longjmp. But this is only used for error handling. So the first version will only run with error-less scripts. Smiley
Perhaps anyone knows how to make them work? Last resort will be to implement them in asm. But I hope to skip that step.

Now I'll start to replace the uBasic binding with Lua. After what I've seen so far this should go without much problems.
I'll keep you informed.

Jörg
Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #8 on: 26 / April / 2008, 22:16:15 »

Well here it is: the first Lua script running on my IXUS 70.

Code:
for i = 1,10 do
  print( "Picture "..i )
  shoot()
  sleep( 2000 )
end

I am already in the process to port more complex scripts to Lua. It is mostly straight forward. And they get much better to read and understand.

Still missing is error handling and script parameters.

New features include:
- variable names longer than 1 char
- no 10 ms delay between different code lines (only for commands that do something with the camera like "press" or "shoot")
- bug free scripting language
- functions
- data structures (Lua tables can be used as arrays or associative containers)
- strings
- garbage collection
- coroutines
- (and more I possibly forgot about)

Logged
fbonomi
Sr. Member
****

Karma: +35/-1
Offline Offline

Posts: 320

A570IS SD1100/Ixus80


« Reply #9 on: 26 / April / 2008, 23:02:56 »

Well, I must say I am impressed!
keep up the good work!
Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #10 on: 27 / April / 2008, 11:11:35 »

Good news. With newlib's setjmp/longjmp implementation error handling works now like a charm.

I think script parameters is the last feature that is missing compared to uBASIC.
Well, shouldn't take too long. Smiley

Logged
nshepperd
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 7


« Reply #11 on: 29 / April / 2008, 12:35:53 »

Impressive. I started on that but gave up almost as soon as I started. Shows how persistent I am. Tongue
Next step: Implement an offline script compiler (the parser bits) so one doesn't have to compile them on the PC, (possibly) risking breakage if the bytecode format changes and you forget to recompile. Please. Smiley
This depends, of course, on how much program memory space the cameras have. And other stuff.
Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #12 on: 29 / April / 2008, 17:54:05 »

You don't have to compile them on the PC. My current version compiles it in the cam.

But I'm thinking about how to reduce the compiled image size. To kick the parser would be the first thing to do. But than you loose the ability to compile Lua source in the cam.

But I have the idea to use a Lua compiler that is written in Lua. So you only load the compiler when you need it.
This idea can be extended to nearly all of chdk, I guess.

A little core with Lua runtime and all cam-function bound to Lua. And a rich set of Lua scripts implementing the logic of CHDK. But this idea is far in the future.

Logged
fbonomi
Sr. Member
****

Karma: +35/-1
Offline Offline

Posts: 320

A570IS SD1100/Ixus80


« Reply #13 on: 29 / April / 2008, 20:43:29 »

actually having a cross-compiler would be a GREAT bonus.
At least the syntax erros could be found without switching the card to the camera and back....
Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #14 on: 29 / April / 2008, 21:05:19 »

Well this is easy. Just install Lua on you PC and run 'lua' on the scripts.
You can even write a stub script with all available cam-functions and let the script run.

uBasic seems to have the same feature. At least I've seen this in the source tree. But never tried it.

Logged
Pages: [1]   Go Up
  Print  
 
Jump to: