--[[ *************************** Script version: v1.01 beta Requires CHDK 1.3 or higher Licence: GPL (c) Werner_O 2015/11/15 Thx msl *************************** @title Test focus v1.01 @chdk_version 1.3 @param d Start Distance (mm) @default d 5000 @range d 0 65000 @param s Step-size at Start (mm) @default s 3 @values s 1 10 100 1000 10000 100000 @param a Max. Digits AutoInfTest @default a 3 @range a 1 5 @param t StepTime AutoInfTest (s) @default t 2 @range t 1 3 @param l Create logfile @default l 0 @range l 0 1 @param v Value for infinite @default v 0 @values v -1 65535 @param m Test inf-value only @default m 0 @range m 0 1 @param u Console offset vertical @default u 1 @range u 0 6 @param h Console offset horizontal @default h 0 @range h 0 9 ]] --Define console layout: x1 = h y1 = u x2 = h + 32 y2 = u + 12 set_console_layout(x1, y1, x2, y2) --Initialize used variables: distance = d start_distance = distance digits = a steps = math.pow(10, s) start_steps = steps mf_mode = 0 afl_mode = 0 show_meter = false script_mode = m log_file = l if v == 0 then inf = -1 else inf = 65535 end step_time = t * 1000 hyperfoc = get_dofinfo().hyp_dist current_inf = -2 current_maxfoc = -2 max_z = get_zoom_steps() max_zoom = max_z - 1 error_beep = true ready = true --Subfunction set_fixfoc() to set a focus and show the results: function set_fixfoc() local count = 0 local timeout = false set_focus(distance) press("shoot_half") repeat sleep(100) count = count + 1 if count > 50 then timeout = true end until get_shooting() or timeout release("shoot_half") focus = get_focus() if show_meter == true then message = "Wanted focus: " x = distance print_focus() message = "Focus set to: " x = focus print_focus() end end --Subfunction print_focus() to show wanted/realized distances: function print_focus() local x2 = x + 10000000 local x_string = tostring(x2) if (x <= -2) then --True at missing valid value print(string.format("%s NOT TESTED", message)) elseif (x == -1) then --True at distance infinite print(string.format("%s INF (-1)", message)) elseif ((inf == 65535) and (x == 65535)) then --dito print(string.format("%s INF (65535)", message)) else local meter = string.sub(x_string, 2, 5) local mm = string.sub(x_string, 6, 8) print(string.format("%s %s,%s m", message, meter, mm)) end end --Subfunction print_steps() to show current step size: function print_steps() local steps1 = steps + 10000000 local steps_string = tostring(steps1) local meter = string.sub(steps_string, 2, 5) local mm = string.sub(steps_string, 6, 8) print(string.format("Current steps: %s,%s m", meter, mm)) end --Subfunction show_state() to show current script state: function show_state() cls() print_steps() message = "Wanted focus: " x = distance print_focus() message = "Focus set to: " x = focus print_focus() if current_inf == -3 then print("AutoInfTest has been aborted!") elseif current_inf ~= -2 then print(string.format("AutoInfTest results (%d digits):", digits)) message = ("Hyperfoc. distance: ") x = hyperfoc print_focus() message = ("Max. focus < INF: ") x = current_maxfoc print_focus() message = ("Focus for INF: ") x = current_inf print_focus() end print("Press L/R/UP/DOWN/SET/DISP/MENU") end --Subfunction print_keys() to show available keys: function print_keys() print("Press L/R/UP/DOWN/SET/DISP/MENU") end --Subfunction increase_steps(): function increase_steps() if steps < 100000 then local s_factor = tonumber(string.sub(tostring(steps), 1, 1)) if s_factor == 1 then steps = steps * 5 else steps = steps * 2 end end end --Subfunction decrease_steps(): function decrease_steps() if steps > 1 then local s_factor = tonumber(string.sub(tostring(steps), 1, 1)) if s_factor == 1 then steps = steps / 2 else steps = steps / 5 end end end --Subfunction test_inf_value() to get value for infinite at max. zoom position: function test_inf_value() cls() print("TEST VALUE FOR INFINITE:") print("Press SET to run test") print("or MENU to exit") inf_test = true --current_zoom = get_zoom() repeat wait_click(2000) if is_pressed("set") then cls() print("Searching value for inf:") while get_zoom() < max_zoom do --Set max. zoom position press("zoom_in") sleep(2000) release("zoom_in") sleep(500) end distance = 9000000 set_fixfoc() if focus > 65535 then print("FATAL ERROR:") print("THE CAM IS NOT COMPATIBLE WITH") print("THIS SCRIPT - DON'T USE IT !!!") play_sound(6) sleep(3000) play_sound(6) else if focus == -1 then inf_string = "-1" else inf_string = "65535" end print("Value for infinite must be") print(string.format("set to: %s", inf_string)) sleep(3000) end while get_zoom() > 0 do --Set min. zoom position press("zoom_out") sleep(2000) release("zoom_out") sleep(500) end inf_test = false elseif is_pressed("menu") then inf_test = false print("Test for infinite") print("has been aborted!") sleep(3000) end console_redraw() until inf_test == false error_beep = false end --Subfunction auto_inf_search() to auto-search infinite distance and max. focal distance below infinite: function auto_inf_search() abort = false actual_distance = distance actual_steps = steps cls() current_z = (get_zoom() + 1) print("Auto-Inf-Mode activated,") print("searching for max. focus") print(string.format("at zoom-position %d of %d:", current_z, max_z)) sleep(3000) if inf == -1 then steps = 1000000 --Will be changed the first time 7 lines below else steps = 100000 --Will be changed the first time 5 lines below end c = 0 hyperfoc = get_dofinfo().hyp_dist current_zoom = get_zoom() zoom_ratio = ((current_zoom * 100) / max_zoom) if zoom_ratio <= 25 then near_distance = 1500 elseif zoom_ratio <= 50 then near_distance = 5000 else near_distance = 10000 end test_distance = 0 repeat distance = near_distance set_fixfoc() --Reset focus to a near and always valid start value steps = steps / 10 --Change to next lower digit auto_steps() --Subfunction auto_steps() will automatically generate new values for "distance" until ((c == digits) or (steps == 0)) or (abort == true) distance = actual_distance steps = actual_steps set_fixfoc() --Reset focus to former value if abort == true then current_inf = -3 current_maxfoc = -2 end end --Subfunction auto_steps() to search max. value for the current digit (new version which can be aborted): function auto_steps() local n = 1 repeat distance = (test_distance + (steps * n)) local finish = false j = c + 1 set_fixfoc() cls() key_timeout = (get_tick_count() + step_time) print("AutoInfSearch running...") print(string.format("steps:%07dmm digit:%dof%d i:%d", steps, j, digits, n)) message = "Testing: " x = distance print_focus() message = "Result: " x = focus print_focus() print("(Press SET to abort test)") repeat console_redraw() if is_pressed("set") then abort = true end key_interval = (key_timeout - get_tick_count()) until (key_interval <= 0) or (abort == true) n = n + 1 if ((n == 11) and (j > 1)) then finish = true elseif ((n == 11) and (j == 1)) then c = c + 1 end until ((focus == inf) or (finish == true) or (abort == true)) if not ((n == 2) and (c == 0)) then --Start counting digits (c) at first found valid value c = c + 1 end current_inf = distance test_distance = (distance - steps) current_maxfoc = test_distance end --Subfunction restore(), used at script aborting via keys SHOOT or MENU: function restore() cls() print("Exit script...") if mf_mode == 1 then set_mf(0) --Disable MF-mode print("MF disabled now") elseif afl_mode == 1 then set_aflock(0) --Disable AFL-mode print("AFL disabled now") else --True with native activated MF-mode print("Native MF kept") end sleep(3000) console_redraw() end --Script start: Check CHDK version, used program and video mode: errors = {} if tonumber(string.sub(string.gsub(get_buildinfo().build_number, "%p", ""), 1, 3)) < 130 then table.insert(errors, "Error: This script") table.insert(errors, "requires CHDK 1.3 or higher!") end rec,vid = get_mode() if vid == true then table.insert(errors, "Error: Video record is active,") table.insert(errors, "please stop first!") end if #errors == 0 then if get_mode() == false then --True in play mode print("Activate record mode...") set_record(1) sleep(1500) while get_mode() == false do sleep(10) end print("Done!") end capmode = require("capmode") --Include lua-library capmode.lua from folder A/CHDK/LUALIB cmode = capmode.get_name() --Get name of the used program if not ((cmode == "AV") or (cmode == "M")) then table.insert(errors, "Error: This script") table.insert(errors, "requires program Av or M!") end end --Start main script: if log_file == l then print_screen(get_day_seconds()/10) end if #errors == 0 then print("Script started...") if not (get_focus_state() < 0) then --True if cam is not in manual MF-mode mf = set_mf(1) --Try to enable MF-mode if mf == 1 then --At cams with available MF-mode mf will be 1 mf_mode = 1 print("MF enabled now") else set_aflock(1) --Enable AFL-mode instead MF-mode afl_mode = 1 print("AFL enabled now") end else print("Native MF found") end sleep(2000) if script_mode == 1 then test_inf_value() restore() else distance = start_distance set_fixfoc() repeat wait_click(2000) show_state() if is_pressed("up") then increase_steps() elseif is_pressed("down") then decrease_steps() elseif is_pressed("right") then distance = distance + steps if distance > 9000000 then distance = 9000000 end set_fixfoc() elseif is_pressed("left") then distance = distance - steps if distance < 0 then distance = 0 end set_fixfoc() elseif is_pressed("set") then auto_test = true cls() print("Auto-Infinite-Test:") print("Press SET to start test") print("Press MENU to abort test") print("(DOWN to clear current results)") repeat wait_click(2000) if is_pressed("set") then auto_inf_search() auto_test = false elseif is_pressed("menu") then auto_test = false click("shoot_half") cls() print("Auto-Infinite-Test") print("has been aborted!") sleep(2000) elseif is_pressed("down") then auto_test = false current_inf = -2 current_maxfoc = -2 cls() print("Auto-Infinite-Test results") print("have been deleted!") sleep(2000) end console_redraw() until auto_test == false elseif is_pressed("display") then show_state() end until is_pressed("menu") restore() if log_file == l then print_screen(0) end end else print("SCRIPT HAS BEEN ABORTED:") for i=1, #errors do print(errors[i]) end play_sound(6) sleep(3000) console_redraw() end