Detecting focus mode from within a script - Script Writing - CHDK Forum

Detecting focus mode from within a script

  • 4 Replies
  • 3875 Views
Detecting focus mode from within a script
« on: 06 / November / 2019, 05:28:43 »
Advertisements
I am writing a script to run with a ND1000 filter, because of this it is unlikely that the autofocus will work.


Ideally I want to be able to check if manual focus has been selected and exit the script if manual focusing has not been selected.


Despite many searches here and on Google I have not managed to find anything that tells me if it is possible to detect the focus mode and if it is how to detect it.


The code I am writing is in Lua, and the camera it is initially being written for is a SX40 HS


Ant comments and or sample code greatly appreciated


Re: Detecting focus mode from within a script
« Reply #1 on: 06 / November / 2019, 09:18:50 »
Typical, after posting this another search has revealed something which hopefully will solve the problem

*

Offline reyalp

  • ******
  • 14079
Re: Detecting focus mode from within a script
« Reply #2 on: 06 / November / 2019, 13:14:34 »
FWIW, I use the following focus module in chdkptp and some of my scripts
Code: [Select]
-- focus override module
focus={
mode_names={'AF','AFL','MF'},
valid_modes={}, -- table of name=true
modes={}, -- array of usable mode names
}
-- initialize valid modes for sd over
function focus:init()
-- bits: 1 = AF, 2 = AFL, 3 = MF
local modes=get_sd_over_modes()
self.modes={}
for i=1,3 do
if bitand(1,modes) == 1 then
table.insert(self.modes,self.mode_names[i])
end
modes = bitshru(modes,1)
end
for i,m in ipairs(self.modes) do
self.valid_modes[m]=true
end
end
-- get current AF/AFL/MF state
function focus:get_mode()
if get_prop(require'propcase'.AF_LOCK) == 1 then
return 'AFL'
end
if get_prop(require'propcase'.FOCUS_MODE) == 1 then
return 'MF'
end
return 'AF'
end
--[[
set AF/AFL/MF state
mode is one of 'AF','MF', 'AFL'
unless force is true, does not call set functions if already in desired state
]]
function focus:set_mode(mode,force)
local cur_mode = self:get_mode()
if not force and cur_mode == mode then
return
end
if mode == 'AF' then
if cur_mode == 'MF' then
set_mf(false)
elseif cur_mode == 'AFL' then
set_aflock(false)
end
elseif mode == 'AFL' then
if cur_mode == 'MF' then
set_mf(false)
end
set_aflock(true)
elseif mode == 'MF' then
if cur_mode == 'AFL' then
set_aflock(false)
end
set_mf(true)
end
end
--[[
set to a mode that allows override, defaulting to prefmode, or the current mode if not set
]]
function focus:enable_override(prefmode)
-- override not supported or in playback
if #self.modes == 0 or not get_mode() then
return false
end
-- no pref, default to overriding in current mode if possible
if not prefmode then
prefmode=self:get_mode()
end
local usemode
if self.valid_modes[prefmode] then
usemode = prefmode
else
-- if pref is MF or AFL, prefer locked if available
if prefmode == 'MF' and self.valid_modes['AFL'] then
usemode = 'AFL'
elseif prefmode == 'AFL' and self.valid_modes['MF'] then
usemode = 'MF'
elseif self.valid_modes[self:get_mode()] then
usemode = self:get_mode()
else
  -- no pref, use first available
usemode = self.modes[1]
end
end
self:set_mode(usemode)
return true
end
function focus:set(dist)
set_focus(dist)
end
This detects which override modes the port supports, and also has code to check which mode is currently set.
Don't forget what the H stands for.

*

Offline c_joerg

  • *****
  • 1248
Re: Detecting focus mode from within a script
« Reply #3 on: 07 / November / 2019, 04:03:52 »
I am writing a script to run with a ND1000 filter, because of this it is unlikely that the autofocus will work.

I would not rule that out in principle. My G1X also focuses with an ND1000 in good light and contrast. I would build an if statement (get_focus_ok / get_focus_state) and cancel it in case the autofocus does not work (or change to MF).
M100 100a, M3 121a, G9x II (1.00c), 2*G1x (101a,100e), S110 (103a), SX50 (100c), SX230 (101a), S45,
Flickr https://www.flickr.com/photos/136329431@N06/albums
YouTube https://www.youtube.com/channel/UCrTH0tHy9OYTVDzWIvXEMlw/videos?shelf_id=0&view=0&sort=dd


Re: Detecting focus mode from within a script
« Reply #4 on: 13 / November / 2019, 04:04:40 »
Thank you both for the feedback

 

Related Topics