Lua Scripting Integration - page 2 - General Discussion and Assistance - CHDK Forum supplierdeeply

Lua Scripting Integration

  • 46 Replies
  • 34877 Views
*

Offline Velo

  • *
  • 30
Re: Lua Scripting Integration
« Reply #10 on: 28 / April / 2008, 12:51:35 »
Advertisements
Hm. What is the output of 'uname -s' on MinGW?
You can revert both files if you are not building on Darwin (Mac), like I do.

For a simple Lua script you can try:

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

Save it as test.lua in the scripts folder an run it.


*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
Re: Lua Scripting Integration
« Reply #11 on: 28 / April / 2008, 14:28:17 »
Sorry, my bad. Doesn't need "end" at the end of the script, that prevented my hello-script from working... :]

*

Offline Velo

  • *
  • 30
Re: Lua Scripting Integration
« Reply #12 on: 01 / May / 2008, 04:59:37 »
I finally got around implementing script parameters.
@Jucifer: The build problems on MinGW should be fixed too.

zSHARE - chdk-lua-2.patch.zip

To be compatible with basic, parameters have the same restrictions as in basic. This means parameters must have names of a,b,c,... or z. But you still can use longer variable names in your script.

Like so:
Code: [Select]
--[[
@title Intervalometer
@param a Interval (Seconds)
@default a 2
@param b Interval (10th Seconds)
@default b 0
]]

timeout = a*1000+b*100
nr = 0

while true do
  nr = nr + 1
  print( "Picture "..nr )
  shoot()
  sleep( timeout )
end

Note that parameters must be enclosed in a Lua-comment block to be recognized by the params parser but ignored from Lua.

Lacking more feedback I haven't changed anything else. Please try my patch and report back. Critics and comments are always welcome.


*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
Re: Lua Scripting Integration
« Reply #13 on: 02 / May / 2008, 04:06:10 »
I'm trying to convert an existing script, and now have a major problem with press(). The code I'm trying to get working is basically

Code: [Select]
C=get_exp_count
press "shoot_half"
press "shoot_full"
do
D=get_exp_count
until (D-C)>=j
release "shoot_full"

so it could convert to something like

Code: [Select]
count_initial = get_exp_count()
press( "shoot_half" )
press( "shoot_full" )
repeat
until ( get_exp_count() - count_initial ) >= count_target
release( "shoot_full" )
.

Here's the problem:
uBASIC version does what it's supposed to: presses shoot_half, (actually goes to loop already), presses shoot_full when shoot_half completed...
Lua version presses shoot_half and goes to loop, but shoot_full never gets pressed.
Why?
I have no idea. Could this have something to do with stack manipulation?


*

Offline Velo

  • *
  • 30
Re: Lua Scripting Integration
« Reply #14 on: 02 / May / 2008, 07:07:31 »
Hi Jucifer

Code: [Select]
repeat
until ( get_exp_count() - count_initial ) >= count_target

Could be that you enter a loop without sleep(). Try

Code: [Select]
repeat
  sleep(10)
until ( get_exp_count() - count_initial ) >= count_target

I made this bug myself two or three times. I'll have to think of a solution than works without sleep.

*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
Re: Lua Scripting Integration
« Reply #15 on: 02 / May / 2008, 07:31:11 »
Indeed works with sleep. Any sleep. Even sleep(0). :]

*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
Re: Lua Scripting Integration
« Reply #16 on: 03 / May / 2008, 05:35:22 »
I hope you don't mind that I put this in my build already... :]

*

Offline Velo

  • *
  • 30
Re: Lua Scripting Integration
« Reply #17 on: 04 / May / 2008, 09: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.



*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
Re: Lua Scripting Integration
« Reply #18 on: 10 / May / 2008, 11:31:18 »
Can e.g. random from mathlib be called easily?

*

Offline Velo

  • *
  • 30
Re: Lua Scripting Integration
« Reply #19 on: 11 / May / 2008, 05: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.


 

Related Topics