People have been asking for this source code. Here it is. I am going to make this easier to copy for CHDK programmers and easier to skip over for wind turbine people soon.
--[[
@title security cam 5
rem Based on work done by MLuna - based om MX3 sample script
rem and based on work done by Matthew Kime in the area of automatic file deletion
rem Tested on A590
rem Shot without auto-focus/with auto-focus/continuously (need to put in continuous mode manually)
rem T implies test mode with MD cells drawing and no shots taken
@param a Shot (0=nf/1=f/2=c/3=t)
@default a 1
rem How long the shutter button will be pressed in continuous mode
@param b Continuos shoot (secs)
@default b 10
@param c Threshold (0-255)
@default c 5
@param d Compare Interval (msecs)
@default d 20
@param e Compare Interval (secs)
@default e 0
rem If this value is too small, the camera goes continuously shooting after the 1st shot.
rem Experiment with this value to find one fitted to your needs
@param f Begin Delay (secs)
@default f 5
@param g Pix step(speed/accuracy adj)
@default g 5
@param h Columns
@default h 6
@param i Rows
@default i 6
rem Frame width in which no MD is performed (in cell units)
@param j Dead frame
@default j 0
]]
-- set eyefi to zero without eyefi card
eyefi = 1
-- filemax is the number of pictures to leave on the sd card when cleaning up old pictures
filemax=5
Dirname = "A/DCIM/102CANON"
-- function definitions
function getCurrentDir ()
local dir = os.listdir("A/DCIM", false)
if(dir) then
local count = table.getn(dir)
--if(dir[count] ~= customDir) then
print("getCurrentDir: ",dir[count])
return "A/DCIM/"..dir[count]
--else
--print("getCurrentDir:A")
--return nil
--end
else
print("getCurrentDir:B")
return nil
end
end
function getNumberOfFiles ()
local dir = os.listdir( Dirname, false)
if(dir) then
local count = table.getn(dir)
return count
else
return 0
end
end
function getNewFile()
local dir = os.listdir(Dirname, false)
if(dir) then
local count = table.getn(dir)
if(dir[count] ~= 0) then
local itemCount = 0
repeat
itemCount = itemCount + 1
if (string.sub(dir[itemCount],1,3) == "IMG") then
io.write("getNewFile loop count: "..itemCount.."\r\n")
return dir[itemCount]
--return "A/DCIM/100CANON/"..dir[itemCount]
end
until itemCount == count
end
end
end
function deleteImgFiles()
local dir = os.listdir(Dirname, false)
if(dir) then
local count = table.getn(dir)
if(count ~= 0) then
local itemCount = 0
repeat
itemCount = itemCount + 1
if (string.sub(dir[itemCount],1,3) == "IMG") then
io.write("delete: "..dir[itemCount].."\r\n")
os.remove(Dirname.."/"..dir[itemCount])
end
until itemCount == count
end
end
end
function deleteOldFiles()
local dir = os.listdir(Dirname, false)
if(dir) then
local count = table.getn(dir)
if(dir[count] ~= 0) then
local itemCount = 0
repeat
itemCount = itemCount + 1
if (string.sub(dir[itemCount],1,3) == "IMG") then
io.write("delete: "..dir[itemCount].."\n")
os.remove(Dirname..dir[itemCount])
end
until itemCount == count
end
end
end
log = io.open("A/intlog.txt","a+")
io.output(log)
deleteImgFiles()
if a<0 then
a=0
end
if a>3 then
a=3
end
if c<0 then
c=0
end
if d<0 then
d=0
end
if e<0 then
e=0
end
if g<1 then
g=1
end
if h<1 then
h=1
end
if i<1 then
i=1
end
if j<0 then
j=0
end
-- Conversions secs to msecs
b=b*1000
e=e*1000
f=f*1000
d=d+e
-- This is the timeout in msecs. After this period, the motion trap is rearmed.
T=600000
-- Parameters for the Dead Frame
J=j+1
H=h-j
I=i-j
t=0
print( "press Shutter to Stop")
while ( 1 ) do
rectangles = 0
rectangles = md_detect_motion( h, i, 1, T, d, c, 1, t, 1, J, J, H, I, 0, g, f)
t=rectangles
if a==0 and t>0 then click( "shoot_full") end
if a==1 and t>0 then shoot() end
--if ( a>0 and a < 3) then
-- local currentFile = getNewFile()
-- -- currentfile was nil once so I added a test for this
-- if( currentfile ) then
-- local fileCreatedDate = os.date("%m%d%H%M%S")
-- os.rename("A/DCIM/100CANON/"..currentFile,"A/DCIM/100CANON/"..fileCreatedDate..".JPG")
-- print(currentFile.."|"..fileCreatedDate..".JPG")
-- io.write(currentFile.."|"..fileCreatedDate..".JPG\r\n")
-- end
-- end
-- continuous mode shooting
if a==2 and t>0 then
X=get_tick_count()
press("shoot_full")
while (V
U=get_tick_count()
V=(U-X)
end
release( "shoot_full" )
end
-- Test Mode Output
if a==3 then
if t>0 then
print( "Detected cells: ",t)
else
print( "No detection in 10 min!" )
end
t=0
end
-- delete old files because eyefi card will fill up over time
-- This code can be removed when eye-fi adds an autodelete option
if ( eyefi ) then
while(getNumberOfFiles() > filemax) do
--delete image until we have room for 50(or filemax) new ones
local filecounter = getNumberOfFiles()
local dir = os.listdir(Dirname, false)
os.remove(Dirname.."/"..dir[1])
io.write("deleting: "..dir[1].."\r\n")
end
end
-- end of main while loop
end
print("END")

