--[[ ****************************************** Script version: v1.07 beta Requires CHDK >= 1.3 USB high speed mode requires CHDK >= v3442 Licence: GPL (c) Werner_O 2016/11/01 Thx to msl ****************************************** @title USB Rem 1.07v4 @chdk_version 1.3 @param m Shoot-Mode @default m 0 @values m PHOTO VIDEO AUTO INTERV @param e TEST: Used Shoot Command @default e 0 @values e SHOOT HALFULL @param v TEST: Verify Shooting @default v 1 @values v NO YES @param t Time Limit AUTO (100ms) @default t 5 @range t 1 15 @param f Foc-Mode (HYP Av/M only!) @default f 0 @values f AF MAN HYP INF @param d MAN Focus Distance (mm) @default d 2000 @range d 0 65000 @param c # Pics Interval @default c 10 @range c 2 1000 @param i Interval Time (sec) @default i 2 @range i 0 60 @param j Interval Time (1/10 sec) @default j 0 @range j 0 9 @param a Auto-Mode USB-Port @default a 1 @range a 0 1 @param s Save-Mode: P,Tv,Av,M only @default s 1 @range s 0 1 ]] --Definition of the used variables: shoot_mode = m --Definition shoot-mode: PHOTO, VIDEO, AUTO or INTERVAL foc_mode = f --Definition focus-mode: AF, MAN, HYP or INF if foc_mode == 2 then --True with choosen hyperfocal distance last_av96 = get_user_av_id() --Any found changes of the aperture value will cause new MF-settings in focus-mode HYP end usb_time_limit = t * 10 --Predefinition USB time limit; will changed later at found possible usb high speed mode usb_auto = a --Automatically enable/disable remote-mode for USB-Port if wanted save_mode = s --Allow only programs P, Tv, Av or M if activated mf_mode = 0 --Predefinition MF-mode; might be changed later afl_mode = 0 --Predefinition AFL-mode; might be changed later last_zoom = get_zoom() --Store current zoom position interval_time = ((i * 1000) + (j * 100)) --Define interval time in ms interval_limit = c --Define interval limit (number of pictures) interval_counter = 0 --Predefinition of a variable; might be changed later mstring1 = "" --Predefinition of a variable; might be changed later mstring2 = "" --Predefinition of a variable; might be changed later pic_count = get_exp_count() --Predefinition of a variable; might be changed later pic_first = (get_tick_count) --Predefinition of a variable; might be changed later pic_last = (get_tick_count) --Predefinition of a variable; might be changed later --TEST: shoot_command = e verify_shooting = v --Definition console-layout: set_console_layout(0, 0, 33, 6) --Subfunction restore(), used at script aborting via keys SHOOT or MENU: function restore() cls() if get_movie_status() == 4 then --True at running video record click("video") --Stop video record repeat sleep(100) until get_movie_status() <= 1 --Wait until videoprocessing has finished print("Video recording stopped") end if usb_auto == 1 then set_config_value(121,0) --Disable USB remote function print("USB remote disabled") end if high_speed then --Disable USB high speed mode if enabled if type(set_remote_timing) == "function" then set_remote_timing(0) elseif type(enable_remote_hp_timer) == "function" then enable_remote_hp_timer(0) elseif type(enable_highspeed_usb) == "function" then enable_highspeed_usb(0) end print("USB High speed mode disabled") end if interval_counter >= 2 then x = interval_time message = "Interval Set = " print_time(x, message) message = "Interval Real = " x = (pic_last - pic_first) / (interval_counter - 1) print_time(x, message) print(string.format("# Pics Interval: %d of %d", interval_counter, interval_limit)) end if foc_mode > 0 then if mf_mode == 1 then set_mf(0) --Disable MF-mode print("Auto-MF has been disabled") elseif afl_mode == 1 then set_aflock(0) --Disable AFL-mode print("Auto-AFL has been disabled") else --True with native activated MF-mode print("Cam left in native MF-mode") end print(string.format("Used MF-type: %s", fix_mode)) if foc_mode == 1 then message = "Wanted focus:" x = distance x_string = tostring(distance) print_focus() end message = "Focus set to:" x = focus x_string = tostring(focus) print_focus() sleep(3000) console_redraw() end end --Subfunction print_time() to show interval length: function print_time(val, message) if message == nil then message = "" end local tt = os.date("*t", val/1000) local dd = val/86400000 print(string.format("%s%02dh%02dm%02d,%02ds", message, tt.hour+dd*24, tt.min, tt.sec, (val%1000)/10)) end --Subfunction set_fixfoc() to set and show the results: function set_fixfoc() if foc_mode == 1 then --True at setting manual focus distance = d --Set focus distance in mm fix_mode = "Manual focus" elseif foc_mode == 2 then --True at setting hyperfocal distance last_av96 = get_user_av_id() distance = get_dofinfo().hyp_dist --Get and set value for hyperfocal distance fix_mode = "Hyperfocal distance" else --True at setting infinite distance distance = -1 fix_mode = "Infinite distance" end local foc_count = 0 local timeout = false set_focus(distance) press("shoot_half") repeat sleep(50) foc_count = foc_count + 1 if foc_count > 40 then timeout = true end until get_shooting() or timeout release("shoot_half") focus = get_focus() print(string.format("Used MF-type: %s", fix_mode)) if foc_mode == 1 then message = "Wanted focus:" x = distance x_string = tostring(distance) print_focus() end message = "Focus set to:" x = focus x_string = tostring(focus) print_focus() last_zoom = get_zoom() print("Waiting for USB-impulse...") print("(Click MENU to exit)") sleep(3000) console_redraw() sleep(3000) console_redraw() end --Subfunction print_focus() to show wanted/realized distances: function print_focus() if (x < 0) then --True at distance infinite print(string.format("%s infinite", message)) elseif (x < 1000) then --True at distances < 1 m print(string.format("%s %03d mm", message, focus)) elseif (x < 10000) then --True at distances >= 1 m to < 10 m local string1 = string.sub(x_string, 1, 1) local string2 = string.sub(x_string, 2, 4) print(string.format("%s %s,%s m", message, string1, string2)) elseif (x < 100000) then --True at distances >= 10 m to < 100 m local string1 = string.sub(x_string, 1, 2) local string2 = string.sub(x_string, 3, 4) print(string.format("%s %s,%s m", message, string1, string2)) else --True at distances > 100 m local string1 = string.sub(x_string, 1, 3) local string2 = string.sub(x_string, 4, 4) print(string.format("%s %s,%s m", message, string1, string2)) end end --Subfunction start_stop_video(): function start_stop_video() if get_movie_status() == 4 then --True at currently running video recording click("video") --Stop video record repeat sleep(100) until ((get_movie_status() <= 1) or (get_movie_status() == 6)) --Wait until videoprocessing has finished cls() print("Video recording stopped") else click("video") --Start video record cls() print("Video recording started") end print("Waiting for USB-impulse...") print("(Click MENU to exit)") end --Subfunction take_photo(): function take_photo() pic_count = get_exp_count() if shoot_command == 0 then shoot() if verify_shooting == 1 then repeat sleep(10) until (get_exp_count() ~= pic_count) end else press("shoot_half") repeat sleep(10) until get_shooting() == true --Wait for valid exposure and/or AF press("shoot_full_only") if verify_shooting == 1 then repeat sleep(10) until get_exp_count() ~= pic_count else sleep(50) end release("shoot_full_only") sleep(50) release("shoot_half") sleep(50) end if (shoot_mode == 0) or (shoot_mode == 2) then --True at single shot cls() print("Shoot successful") print("Waiting for USB-impulse...") print("(Click MENU to exit)") end end --Subfunction interval_timer(): function interval_timer() interval_counter = 0 repeat interval_time_counter = get_tick_count() if interval_counter == 0 then pic_first = get_tick_count() elseif interval_counter == (interval_limit - 1) then pic_last = get_tick_count() end take_photo() interval_counter = (interval_counter + 1) sleep_time = (interval_time - (get_tick_count() - interval_time_counter)) if (sleep_time > 0) and (interval_counter < interval_limit) then sleep(sleep_time) end until interval_counter == interval_limit end --Get information to CHDK version/build and other values needed to run this script successfully: if get_mode() == false then --True in playback mode print("Enable shoot mode...") set_record(1) --Enable shoot mode sleep(1000) while get_mode() == false do sleep(10) end print("Shoot mode active!") end chdk_version = tonumber(string.sub(string.gsub(get_buildinfo().build_number, "%p", ""), 1, 3)) --Get CHDK version as integer number chdk_build = tonumber(get_buildinfo().build_revision) --Get CHDK build as integer number capmode = require("capmode") --Include lua-library capmode.lua from folder A/CHDK/LUALIB cmode = capmode.get_name() --Get name of the currently activated program video_button = get_video_button() --Is a separate video button available (1 = Yes; 0 = No)? rec,vid = get_mode() --Get current video recording state (variable "vid": true or false) --1) Abort, if CHDK version is < 1.30 err = {} if chdk_version < 130 then table.insert(err, "Error: This script") table.insert(err, "requires CHDK >= 1.3!") end --2) Abort, if video recording is currently active if vid == true then table.insert(err, "Error: Stop video recording") table.insert(err, "before running this script!") end --3) Abort in save-mode, if another program as P, Tv, Av or M is active if save_mode == 1 then if not (cmode == "P" or cmode == "TV" or cmode == "AV" or cmode == "M") then table.insert(err, "Error: Save-mode is active,") table.insert(err, "select program P, Tv, Av or M!") end end --4) Abort at choosen focus distance HYP, if programs Av or M are not active if foc_mode == 2 then if not (cmode == "AV" or cmode == "M") then table.insert(err, "Error: Hyperfocal distance") table.insert(err, "requires program Av or M!") end end --5) Abort at shoot modes VIDEO or AUTO, if no separate video button is available if ((shoot_mode == 1) or (shoot_mode == 2)) and (video_button == 0) then table.insert(err, "Error: No video button found at") table.insert(err, "this cam - select mode PHOTO!") end --Start main script if no errors were found: if #err == 0 then if usb_auto == 1 then set_config_value(121,1) --Automatically enable USB remote function if wanted print("USB remote enabled") end --Enable USB high speed mode if possible: high_speed = false if type(set_remote_timing) == "function" then high_speed = set_remote_timing(1000) elseif type(enable_remote_hp_timer) == "function" then high_speed = enable_remote_hp_timer(1000) elseif type(enable_highspeed_usb) == "function" then enable_highspeed_usb(1000) high_speed = true end if high_speed then print("USB high speed mode enabled") usb_time_limit = t * 100 else print("USB high speed mode n/a") end if foc_mode > 0 then --True at focus-settings MAN, HYP or INF if not (get_focus_state() < 0) then --True if cam is not in manual MF-mode x = set_mf(1) --Try to enable MF-mode if x == 1 then --At cams with available MF-mode x will be 1 mf_mode = 1 print("MF-mode enabled automatically") else set_aflock(1) --Enable AFL-mode instead MF-mode afl_mode = 1 print("AFL-mode enabled automatically") end else print("CAM is already in MF-mode") end set_fixfoc() --Set a fixed focus setting via this subfunction else --True for focus setting AF print("Waiting for USB-impulse...") print("(Click MENU to exit)") end --Start main loop: repeat if foc_mode == 2 then --True with choosen hyperfocal distance current_av96 = get_user_av_id() mstring1 = "" if (last_av96 ~= current_av96) then --True if Av value has been changed mstring1 = "Found changed Av value!" last_av96 = current_av96 end end mstring2 = "" if ((get_zoom() ~= last_zoom) and (foc_mode > 0)) then --True if zoom position has been changed mstring2 = "Found changed zoom position!" end if (((mstring1 ~= "") or (mstring2 ~= "")) and (get_movie_status() ~= 4)) then cls() if (mstring1 ~= "") then print(mstring1) end if (mstring2 ~= "") then print(mstring2) end print("The fixed focus settings will") print("be actualized in 2 sec:") sleep(2000) cls() set_fixfoc() --Force a new fixed focus setting after a changed zoom position with MF or a changed Av value in hyperfocal mode end usb = get_usb_power() --Wait for an USB impulse if usb > 0 then --True after a recognized USB-impulse if shoot_mode == 0 then --True at activated photo shooting take_photo() elseif shoot_mode == 1 then --True at activated video recording start_stop_video() elseif shoot_mode == 2 then --True at activated AUTO for Shoot-Mode if (usb >= usb_time_limit) and (get_movie_status() <= 1) then --True at stopped/paused video recording and USB-impulse >= usb_time_limit start_stop_video() elseif get_movie_status() == 4 then --True while currently running video recording start_stop_video() else take_photo() end else --True in interval mode interval_timer() cls() print("Interval successful") print(string.format("# Pics Interval: %d of %d", interval_counter, interval_limit)) print("Waiting for USB-impulse...") print("(Click MENU to exit)") end end until is_pressed("menu") --Abort script after pressing key MENU restore() --Run this subfunction to reset the cam to the settings before script start else --True if table "err" is not empty cls() for i=1, #err do print(err[i]) end play_sound(6) --Play error sound sleep(3000) console_redraw() end