Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Topic started by: garry23 on November 01, 2016, 11:39:19 PM

Title: Modifying Keys/Buttons
Post by: garry23 on November 01, 2016, 11:39:19 PM
For some time I have been trying to extend the functionality of the Canon keys in Lua scripting. In other words trying to get more out of a key, which could be useful to those with cameras with limited key access.

The idea I am sharing is as follows:
 - Select a key to have a modified role, ie KEY.RATE in the demo script below
 - Choose a second key to act as a modifier, ie KEY.PLAY in the demo below
 - Insert the Lua modified code, ie move the lens to the HFD point in the demo below
 - To use simply press any key as normal
 - Press KEY.RATE twice to use KEY.RATE in normal mode
 - Press KEY.RATE followed by KEY.PLAY to use the KEY.RATE button in modified mode

The script could be refined further, and I may well do that ;-)

Also the script is easy to extend, eg add other modified buttons.

As usual I welcome feedback of any kind.

Code: [Select]
--[[
Script to illustrate the use of key modification
Version 0.3 (should work on all ML enabled cameras with the Lua module)
Shows the use of a modified key approach using an AF lens auto positioned to the HFD
Garry George Oct 2016
http://photography.grayheron.net/
For rhe HFD functionality must be in LV and in AF mode, and lens must report distance
Also make sure ML DoF set up as you require it, eg diffraction on
Also for the fastest move position lens at infinity end, ie macro end moves are slower
This version of the script does no error checking ;-)
--]]

-- Declare globals

mod_button_1 = KEY.RATE -- key to modify
mod_button_2 = KEY.PLAY -- key to indicate mod mode requested
last_key_pressed = 999 -- used to reset mod_button pairing

-- Choose buttons of your choice, eg KEY.RATE, KEY.UP, KEY.INFO or KEY.PLAY.
-- See http://davidmilligan.github.io/ml-lua/modules/constants.html for key constants

function move()
if lens.focus_distance < lens.hyperfocal then
repeat
lens.focus(-1,1,false)
until lens.focus_distance >= lens.hyperfocal
else
repeat
lens.focus(1,1,false)
until lens.focus_distance <= lens.hyperfocal
repeat -- ensures lens is positioned with a bias towards INF
lens.focus(-1,1,false)
until lens.focus_distance >= lens.hyperfocal
end
end

function test4mod(key)
if keymenu.submenu["Turn On/Off"].value == "Off" then
return true -- all keys work as normal
elseif key == mod_button_1 and last_key_pressed == mod_button_1 then -- use moded key as normal
last_key_pressed = 999 -- reset
return true
    elseif key == mod_button_2 and last_key_pressed == mod_button_1 then -- use moded key in modified way
-- ****************** Place modified code here ************
last_key_pressed = 999 -- reset
move() -- to HFD position
-- ****************** Place modified code here ************
return false -- steal key press
elseif key == mod_button_1 then -- first press of modified key in new sequence
last_key_pressed = mod_button_1
return false -- steal key press
else
return true -- use all non-moded buttons in an unmodified way
end
end

event.keypress = test4mod

keymenu = menu.new
{
parent = "Shoot",
name = "HFD",
help = "Moves lens to HFD focus distance",
depends_on = DEPENDS_ON.LIVEVIEW,
submenu =
{
{
name = "Turn On/Off",
help = "Switches the script on/off",
choices = {"On","Off"},
}
}
}