Here's the focusing routine that seems to work on the sx260 and G1X. You set the parameter, "d", to 1 for hyperfocal distance, or -1 for infinity. Anything >1 is distance in mm
With the camera zoomed all the way out at F/3.5, the hyperfocal distance is 1141. Multiplying by 1.1 adds 2 cm which then truncates down to 1167, which insures that the far limit is infinity. The multiplier of 1.1 makes sure truncation doesn't drop the focus below hyperfocal and blur distant objects too much.
With the input parameter, d, at 0, it will do an autofocus, if in autofocus mode, and set focus lock, like your current script. If in manual focus mode, it will leave the focus where it's set manually in the camera.
By the way, I gave up on this after many focus motor crashes. Philmoz suggested focusing in manual mode. I had to remove his set_aflock(1) in manual mode, though, because it crashes the sx260 frequently. Manual mode should hold focus lock anyway.
I use the trick of clicking the "left" key during half shoot to enter manual mode. This works on the sx260 and g1x cameras with manual mode. On the Elph 300, I think it sets "infinity" or "mountain" mode. If you have a camera without manual focus, I'd like to know what happens. I'll need to add a test for this, and not click "left" if there's no manual mode.
--[[
@title Focuser
@param d = Focus (mm: -1=inf 0=auto 1=hyp)
@default d 1
--]]
function restore()
set_aflock(0)
end
function press_half()
press("shoot_half")
repeat sleep(50) until get_shooting()
end
function release_half()
release("shoot_half")
while(get_shooting())do sleep(50) end
end
press_half()
if(get_focus_mode()~=1) then click("left") end -- goes into manual focus during half shoot
release_half()
if(get_focus_mode()==1) then --can only set focus in manual focus mode
if(d~=0)then
if(d==1)then d=(get_dofinfo().hyp_dist*110)/100 end
--sometimes d is truncated below hyp-dist and far limit isn't inf (-1)
--multiplying by 1.1 (or 110/100 integer math) corrects this
set_focus(d)
press_half()
release_half()
end
else set_aflock(1) end
--print for testing
dof=get_dofinfo()
print("Foc=",dof.focus,"Hyp=",dof.hyp_dist)
print("Near=",dof.near,"Far=",dof.far)
gfoc=get_focus() -- should be same as dof.focus
print(d,gfoc,(d*100)/gfoc,get_focus_mode())
--3rd number is multiplier to get focus to hyperfocal distance
shoot()
restore()