Pages: Prev 1 [2] 3 4 Next   Go Down
  Print  
Author Topic: Lua Scripting Integration  (Read 5317 times)
0 Members and 1 Guest are viewing this topic.
Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #15 on: 02 / May / 2008, 12:31:11 »

Indeed works with sleep. Any sleep. Even sleep(0). :]
Logged

Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #16 on: 03 / May / 2008, 10:35:22 »

I hope you don't mind that I put this in my build already... :]
Logged

Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #17 on: 04 / May / 2008, 14:28:58 »

I hope you don't mind that I put this in my build already... :]

Of course not!

I hope you like my third iteration of the patch:
zSHARE - chdk-lua-3.patch.zip

Changes to the last one:
- Added support for print_screen function (bool parameter). Moved the print_screen functionality out of ubasic.c to script.c
- Fixed a race condition that would lead to a Lua script interpreted as Basic. I have this only seen with script-autostarting.
- Loops without sleep escape every now and then back to the cam mainloop. The cam can't be blocked anymore so easily.
- When a compile error happens the error is now displayed in the script console window.

Logged
Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #18 on: 10 / May / 2008, 16:31:18 »

Can e.g. random from mathlib be called easily?
Logged

Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #19 on: 11 / May / 2008, 10:23:02 »

Can e.g. random from mathlib be called easily?

You mean math.random?

I disabled the math library, because most of it would not compile/work anyway.
I compiled Lua with lua_Number defined as int. The default is double. So things like sin or exp make no sense.

But math.random and math.randomseed can also be provided with integers. But math.random without arguments would not be allowed.
I can enable all functions, that are reasonable with ints.

This would include math.min, math.max, math.abs, math.pow and the two functions above.

Logged
Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #20 on: 11 / May / 2008, 11:34:00 »

Yes, pretty much the math.random.
Would adding something like

static int luaCB_random( lua_State* L )
{
  math.random( L, shooting_get_prop( luaL_checknumber( L, 1 ), luaL_checknumber( L, 2 ) ) );
  return 1;
}

to luascript.c suffice?
And what would be the correct lines to add? :]

Also, could a function for executing camera commands directly (exec(camera_function_to_execute(parameters, for, the, function))) be easily added?
Logged

Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #21 on: 11 / May / 2008, 11:51:11 »

Hi Jucifer,

math.random will be defined in lmathlib.c. No need to define it in luascript.c
Just now I finished the mentioned math functions. Still untested, but I don't expect any problems.
Before publishing a new patch I want to include 'require' support to load arbitrary Lua Sub-Scripts.
This will really rock.

Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #22 on: 11 / May / 2008, 12:04:16 »

Also, could a function for executing camera commands directly (exec(camera_function_to_execute(parameters, for, the, function))) be easily added?

camera_function_to_execute must be bound to lua (luascript.c).

But the Lua philosophy is to bind only the most basic function to Lua and define new convenience functions within Lua itself.

Like binding get_prop and defining a get_mode function.
Something like this (for my IXUS 70/untested)

Code:
function get_mode()
  local pg = getprop(49)
  if pg == -32764 then
    return "manual"
  elseif pg == -32768 then
    return "auto"
  elseif
  ...
end

This functions can be defined in a submodule like chdk.lua and all scripts can use this functions after calling require:

Code:
require "chdk"

if get_mode() == "manual" then
  print "good choice"
end

Well, this will only work with the next patch.

Logged
Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #23 on: 11 / May / 2008, 12:59:46 »

math.random will be defined in lmathlib.c.
I noticed that, when searching if random function existed already...

Quote
include 'require' support to load arbitrary Lua Sub-Scripts.
This will really rock.
Sounds really good. Rock on! :]

If there was a camera function, that would call another function with the parameters supplied, and then the former is bound in luascript.c..? Or ...something. :D (I might like the ability to call any camera function from a Lua script. Would it make any sense?)

Lua to the trunk!
Logged

Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #24 on: 11 / May / 2008, 13:36:02 »

(I might like the ability to call any camera function from a Lua script. Would it make any sense?)

I think you are looking for something like C/Invoke. Where you can call any C function from Lua without binding it.
But this will not work as-is on the cam. You don't have something like dlsym to find a function by its name. So you still have to register all callable function somewhere.

Lua to the trunk!

You have my vote. Smiley

Logged
Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #25 on: 11 / May / 2008, 13:59:56 »

Patch#4 is ready for take off:

zSHARE - chdk-lua-4.patch.zip

New features:
- New math functions (abs,min,max,pow,random,randomseed)
  They are in table 'math'. Must be used like so: math.max( 2, 3 )

- require function (and dofile/loadfile)
  require loads a file out of the scripts folder. This means one can now create a library of functions and recycle this lib in scripts with a call to require "libname".
  (libname is the filename without '.lua' suffix)

Logged
fudgey
Global Moderator
Hero Member
*****

Karma: +74/-1
Offline Offline

Posts: 842

a570is


« Reply #26 on: 11 / May / 2008, 16:47:12 »

- require function (and dofile/loadfile)
  require loads a file out of the scripts folder. This means one can now create a library of functions and recycle this lib in scripts with a call to require "libname".
  (libname is the filename without '.lua' suffix)

Should we consider using some other directory than chdk/script for loading libraries (such as chdk/lib chdk/scripts/lib or chdk/lua/lib or or chdk/scripts/lua/lib) or giving the user an option to change the lua library load path?

The "load script" file browser gets crowded easily, I already have subdirectories for different types of scripts and only have a few scripts in the actual chdk/scrips directory. Forcing all libraries (which may not even work as standalone scripts) to exist in that directory clutters the script load browser making it more cumbersome to select scripts.

(I haven't had the time to test the lua builds, but for what I've read it sounds awesome and needs to find it's way into trunk someday soon if you think there won't be any large modifications which would likely break compatibility with old and new lua scripts).
Logged
Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #27 on: 11 / May / 2008, 17:28:03 »

You can put libname.lua to chdk/script/libs and
require "libs/libname"
.
Would chdk/lib or chdk/lualib be ok? Less scrolling, when selecting scripts. :]
Logged

Velo
Newbie
*

Karma: +9/-0
Offline Offline

Posts: 30


« Reply #28 on: 11 / May / 2008, 17:57:31 »

Would chdk/lib or chdk/lualib be ok? Less scrolling, when selecting scripts. :]

You mean lib not within scripts folder? Or do you mean scripts/lib?

I would prefer chdk/scripts/lib

Normally require would honor the package.path variable to find the script. But this funtionality requires some string manipulation functions I have not compiled in.
Instead I hardcoded the path A/CHDK/SCRIPTS/?.lua where ? is the argument to require.

This means that scripts from subfolders will be found with require "folder/script" as Jucifer pointed out. And I find it not too bad.
Logged
Jucifer
Global Moderator
Full Member
*****

Karma: +42/-0
Offline Offline

Posts: 224


[A710IS]


« Reply #29 on: 11 / May / 2008, 18:10:27 »

I was thinking of not putting anything but scripts to the chdk/scripts folder. If (when) the path is hardcoded to chdk/scripts, would it be possible to load libs from e.g. chdk/lib? I think I tried
require "../lib/libname"
but no avail.
Logged

Pages: Prev 1 [2] 3 4 Next   Go Up
  Print  
 
Jump to: