CHDK Wiki
(updated yaaintrlmtr)
Line 18: Line 18:
   
 
'''A first LUA sample script:'''
 
'''A first LUA sample script:'''
 
<pre>
<source lang="lua">
 
 
for i = 1,10 do
 
for i = 1,10 do
 
print( "Picture "..i )
 
print( "Picture "..i )
Line 24: Line 24:
 
sleep( 2000 )
 
sleep( 2000 )
 
end
 
end
</source>
+
</pre>
   
 
===Running LUA scripts===
 
===Running LUA scripts===
Line 41: Line 41:
   
 
===LUA sample scripts===
 
===LUA sample scripts===
  +
====yet another accurate intervalometer====
''Note: to enabled the syntax highlighting feature in scripts the following tags should be used:''
 
<pre>
 
<source lang="lua">
 
..... text of the code .....
 
</source>
 
</pre>
 
 
====Yet Another Accurate Intervalometer====
 
 
<source lang="lua">
 
<source lang="lua">
 
--[[
 
--[[
  +
rem 20080805
 
@title yet another accurate intervalometer
 
@title yet another accurate intervalometer
 
@param a Duration (min)/-1 disable
 
@param a Duration (min)/-1 disable
Line 74: Line 68:
 
]]
 
]]
   
  +
drivemode_continuous = 1
--[[
 
different propcases for DIGIC II/III
 
S(hooting in progress-propcase):
 
for DIGIC II: 205, DIGIC III: 206
 
]]
 
prop_shooting = 205
 
 
--[[
 
D(rive mode -property case) - II:6, III:102?
 
C(ontinuous-value for drive mode) - II:1, III:?
 
]]
 
prop_drivemode = 6
 
prop_continuous = 1
 
   
 
if a < -1 then a = -1 end
 
if a < -1 then a = -1 end
Line 101: Line 83:
 
if ( i == -1 and j < 1 ) then j = 1 end
 
if ( i == -1 and j < 1 ) then j = 1 end
 
if (( i == -1 and j > 1 ) or i > -1 ) then
 
if (( i == -1 and j > 1 ) or i > -1 ) then
  +
if get_drive_mode() ~= drivemode_continuous then
prop_temp = get_prop( prop_drivemode )
 
 
print( "set drive mode" )
if prop_temp ~= prop_continuous then
 
print( "set drive mode" )
 
 
print( "to continuous" )
 
print( "to continuous" )
sleep(1500)
+
sleep(1500)
cannot_continue = true
+
cannot_continue = true
 
end
 
end
 
end
 
end
Line 119: Line 100:
 
sequence_current = 0
 
sequence_current = 0
 
repeat
 
repeat
 
tick_target = get_tick_count() + delay
sleep(0)
 
 
sequence_current = sequence_current + 1
tick_target = get_tick_count() + delay
 
sequence_current = sequence_current + 1
+
print( "sequence " .. sequence_current .. " of " .. sequence_target )
 
if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
print( "sequence " .. sequence_current .. " of " .. sequence_target )
 
 
while ( get_tick_count() < tick_target and sequence_current < sequence_target ) do
if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
 
 
end
while ( get_tick_count() < tick_target and sequence_current < sequence_target ) do
 
sleep(0)
 
end
 
 
until sequence_current >= sequence_target
 
until sequence_current >= sequence_target
 
end
 
end
Line 133: Line 112:
 
duration_target = get_tick_count() + duration
 
duration_target = get_tick_count() + duration
 
repeat
 
repeat
 
tick_target = get_tick_count() + delay
sleep(0)
 
 
if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
tick_target = get_tick_count() + delay
 
  +
print( (duration_target-get_tick_count())/1000 .. " sec to go" )
if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
 
print( (duration_target-get_tick_count())/1000 .. " sec to go" )
+
while ( get_tick_count() < tick_target and get_tick_count() < duration_target ) do
 
end
while ( get_tick_count() < tick_target and get_tick_count() < duration_target ) do
+
until get_tick_count() >= duration_target
sleep(0)
 
end
 
until tick_current >= duration_target
 
 
end
 
end
   
Line 147: Line 124:
 
sequence_current = 0
 
sequence_current = 0
 
repeat
 
repeat
 
tick_target = get_tick_count() + delay
sleep(0)
 
  +
sequence_current = sequence_current + 1
tick_target = get_tick_count() + delay
 
 
print( "sequence " .. sequence_current )
sequence_current = sequence_current + 1
 
print( "sequence " .. sequence_current )
+
if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
  +
print ( (get_tick_count()-tick_initial)/1000 .. " sec elapsed")
if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
 
print ( (get_tick_count()-tick_initial)/1000 .. " sec elapsed")
+
while ( get_tick_count() < tick_target ) do
 
end
while ( get_tick_count() < tick_target ) do
 
sleep(0)
 
end
 
 
until false
 
until false
 
end
 
end
   
function shoot_count( count_target )
+
function shoot_count( count_inc )
count_initial = get_exp_count()
+
count_target = get_exp_count() + count_inc
  +
if count_target > 9999 then count_target = count_target - 9999 end
 
press( "shoot_half" )
 
press( "shoot_half" )
 
press( "shoot_full" )
 
press( "shoot_full" )
 
repeat
 
repeat
 
until get_exp_count() == count_target
sleep(0)
 
until ( get_exp_count() - count_initial ) >= count_target
 
 
release( "shoot_full" )
 
release( "shoot_full" )
 
repeat
 
repeat
  +
until get_shooting() == false
sleep(0)
 
until get_prop( prop_shooting ) == 0
 
 
end
 
end
   
Line 177: Line 151:
 
press( "shoot_full" )
 
press( "shoot_full" )
 
while ( get_tick_count() < tick_target ) do
 
while ( get_tick_count() < tick_target ) do
sleep(0)
 
 
end
 
end
 
release( "shoot_full" )
 
release( "shoot_full" )
 
repeat
 
repeat
  +
until get_shooting() == false
sleep(0)
 
until get_prop( prop_shooting ) == 0
 
 
end
 
end
   
 
if not cannot_continue then
 
if not cannot_continue then
tick_target = get_tick_count() + delay_first
+
tick_target = get_tick_count() + delay_first
print( "waiting " .. delay_first/1000 .. " sec" )
+
print( "waiting " .. delay_first/1000 .. " sec" )
while ( get_tick_count() < tick_target ) do
+
while ( get_tick_count() < tick_target ) do
tick_current = get_tick_count()
+
tick_current = get_tick_count()
end
+
end
if h == 1 then shoot_forever() end
+
if h == 1 then shoot_forever() end
if a > -1 then shoot_by_duration( duration ) else shoot_by_numbers( duration ) end
+
if a > -1 then shoot_by_duration( duration ) else shoot_by_numbers( duration ) end
 
end
 
end
  +
 
</source>
 
</source>
   

Revision as of 08:44, 5 August 2008

LUA Scripting language for CHDK

...this is work in process...

The CHDK allows you to automate your camera by running "scripts", small and simple programs written in a short-hand version of BASIC, named "UBASIC".

Additionally to this an alternative scripting language named "LUA" is available in some of the latest experimental builds [NEW - Exceptional Builds from the CHDK Forum].

So what is Lua?

Lua is a powerful, fast, light-weight, embeddable scripting language. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping. Lua is Open Source software, its licenses are compatible with GPL. (quoted from www.lua.org)

For Developers: LUA is brought to CHDK by "Velo" in the forum in April 2008: Lua Scripting Integration

A first LUA sample script:

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

Running LUA scripts

  • A CHDK version with build-in LUA support is needed, e.g. the "the collaborative CHDK build"
  • The script should be saved with the filename extension .lua in the CHDK script folder
  • Loading, starting, pausing & stopping works the same way as on UBASIC scripts
  • UBASIC scipts are also working as usual, the filename extension (.lua or .bas) tell CHDK how to run the script


Links

NEWS
Reference Manuals
License


LUA sample scripts

yet another accurate intervalometer

--[[
rem 20080805
@title yet another accurate intervalometer
@param a Duration (min)/-1 disable
@default a -1
@param b Duration (sec)/n of seqs
@default b 5
@param c Delay 1st sequence (min)
@default c 0
@param d Delay 1st sequence (sec)
@default d 3
@param e Trigger every n min
@default e 0
@param f ...every n sec
@default f 3
@param g ...every .n sec
@default g 0
@param h Endless?
@default h 0
@param i Seq dur (m)/-1
@default i -1
@param j Seq dur (s)/n of shots/seq
@default j 1
]]

drivemode_continuous = 1

if a < -1 then a = -1 end
if ( a > -1 and b < 0 ) then b = 0 end
if ( a == -1 and b < 1 ) then b = 1 end
if c < 0 then c = 0 end
if d < 0 then d = 0 end
if e < 0 then e = 0 end
if f < 0 then f = 0 end
if g < 0 then g = 0 end
if ( h < 0 or h > 1 ) then h = 0 end
if i < -1 then i = -1 end
if ( i > -1 and j < 0 ) then j = 0 end
if ( i == -1 and j < 1 ) then j = 1 end
if (( i == -1 and j > 1 ) or i > -1 ) then
  if get_drive_mode() ~= drivemode_continuous then
    print( "set drive mode" )
    print( "to continuous" )
    sleep(1500)
    cannot_continue = true
  end
end

if a > -1 then duration = a*60000 + b*1000 else duration = b end
delay_first= c*60000 + d*1000
if g < 0 then delay = e*60000 + f*1000 + g*10 else delay = e*60000 + f*1000 + g*100 end
if i > -1 then sequence = i*60000 + j*1000 else sequence = j end


function shoot_by_numbers( sequence_target )
sequence_current = 0
repeat
  tick_target = get_tick_count() + delay
  sequence_current = sequence_current + 1
  print( "sequence " .. sequence_current .. " of " .. sequence_target )
  if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
  while ( get_tick_count() < tick_target and sequence_current < sequence_target ) do
  end
until sequence_current >= sequence_target
end

function shoot_by_duration( duration )
duration_target = get_tick_count() + duration
repeat
  tick_target = get_tick_count() + delay
  if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
  print( (duration_target-get_tick_count())/1000 .. " sec to go" )
  while ( get_tick_count() < tick_target and get_tick_count() < duration_target ) do
  end
until get_tick_count() >= duration_target
end

function shoot_forever()
tick_initial = get_tick_count()
sequence_current = 0
repeat
  tick_target = get_tick_count() + delay
  sequence_current = sequence_current + 1
  print( "sequence " .. sequence_current )
  if i == -1 then shoot_count( sequence ) else shoot_tick( sequence ) end
  print ( (get_tick_count()-tick_initial)/1000 .. " sec elapsed")
  while ( get_tick_count() < tick_target ) do
  end
until false
end

function shoot_count( count_inc )
count_target = get_exp_count() + count_inc
if count_target > 9999 then count_target = count_target - 9999 end
press( "shoot_half" )
press( "shoot_full" )
repeat
until get_exp_count() == count_target
release( "shoot_full" )
repeat
until get_shooting() == false
end

function shoot_tick( tick_duration )
tick_target = get_tick_count() + tick_duration
press( "shoot_half" )
press( "shoot_full" )
while ( get_tick_count() < tick_target ) do
end
release( "shoot_full" )
repeat
until get_shooting() == false
end

if not cannot_continue then
  tick_target = get_tick_count() + delay_first
  print( "waiting " .. delay_first/1000 .. " sec" )
  while ( get_tick_count() < tick_target ) do
    tick_current = get_tick_count()
  end
  if h == 1 then shoot_forever() end
  if a > -1 then shoot_by_duration( duration ) else shoot_by_numbers( duration ) end
end