Magic Lantern Forum

Developing Magic Lantern => General Development => Topic started by: knoodrake on October 28, 2016, 09:59:14 AM

Title: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 28, 2016, 09:59:14 AM
Hello all.  :)

Ability to register AF focus distances and set them to buttons. ..is what I need.

The goal is quite simple: having a quick way to switch to an acceptable "hyperfocal" focus distance for that lens/aperture/..

I have a 5dIII. This is not a feature request since I need it quickly ( in 20 days ) but couple of question:


Thanks for any tips or directions !

Sorry if I miss obvious posts that already answered my questions. I gave a look in the forum but didn't find.

Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on October 28, 2016, 11:34:21 AM
1. Yes; I prefer hooking it to a menu (many buttons can be model-dependent, so it's harder to make it portable that way)
2. I'd say it's best to start with a Lua script; the DOF and focus APIs are present.
3. See also http://www.magiclantern.fm/forum/index.php?topic=4997 ; the module you linked should be converted to a script IMO.
4. As long as you don't require changes to core features, no.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 28, 2016, 11:56:38 AM
Thanks a lot a1ex, I will look into all that :)
( and I completely forgot the support for lua scripts !  )

( by the way, also thank you for everything else you do arround here / ML  :) )
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on October 28, 2016, 02:53:54 PM
@knoodrake

You may be interested in looking at my humble attempts at using Lua to access lens control, including DoF positioning: http://photography.grayheron.net and https://gist.github.com/pigeonhill/e008f051681885bc6ff84de637cb1d8e

Also others are making progress to control lens positioning to a specified point, rather than my approach of just stopping the lens motor when a position is reached.

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 28, 2016, 03:56:52 PM
Thank you very much garry.

Right now, I made my very first baby steps: updating ML to a new nighly that have lua scripting: Done.

And now I'm struggling to understand anything  :'(
First, I don't know lua. Maybe I should look at some tutos first.. But That's not my biggest problem.

Mostly, I don't yet get how the whole thing works. Don't understand when the console is supposed to be shown (I saw it some time)
I tried to debug and set some breakpoint (those red marks on the left are breakpoints right ?) from within the lua text editor, but.. no. nope. I don't understand anything :) I just am under the impression that I do some writting in (main) loop in the console, console that I cannot even get how to see !


My first goal was to do a hello world while showing lens infos instead of hello ( lens.focal_length for instance ).

I'm also struggling between coding stuff on computer, then popping the CF out and in the camera, and that is tedious, and trying to do some stuff with the build-in lua editor but I struggle to remember the buttons/shortcuts.

Since I'm only trying for less than an hour, I'm not yet desperate, but I would love to be able to at least grab a sense of the whole workflow, and be able to do my hello world :)

I'm used to code on java, so, all is nice, JVM, easy debug, easy IDE, everything documented, and here, I feel like I'm in the jungle, trying to find infos in various forum posts dating from 2015 I don't know if up to date, ...

If you please, have any insight for me to "kickstart" the whole stuff about making a module or a lua script. Please share :)


Anyway, thank you for your link, I'll get a look and maybe that'll help me understand some stuff.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on October 30, 2016, 09:16:47 AM
I'm more than happy to help, but first have you read through these postings: http://www.magiclantern.fm/forum/index.php?topic=14828.0

I was in a similar position to you a short while ago :-)

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on October 30, 2016, 09:39:31 AM
@knoodrake

After you have scanned the postings, you may wish to read through this: https://www.lua.org/manual/5.1/

As to taking the card in and out, I also have to live with this approach :-(

However, one help is to use this App: https://studio.zerobrane.com/

I also use this text editor: https://notepad-plus-plus.org/

As for your need, my coding could get you started, ie you could adapt from there. But remember we can't command the lens to move to a specific position, not yet, although others are working on this. Thus my approach is to twist the lens to, say, the macro end and move the lens until the HFD position is satisfied.

Bottom line: Lua is a great way to get started with extending ML.

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on October 30, 2016, 04:40:14 PM
@knoodrake

As I had a few minutes today I hacked out a demo script for your use case.

I tested it on my 5D3 and a 24-105 F4/L.

Lens must report focus distance and be in AF. You must also be in LV.

Ensure DoF set up, ie diffraction on.

Note, until we are able to position the lens to a stated position (some are working this), this iterative approach is the best you can do. aving said that you may be able to optimize the move algorithm, ie I simply used single steps.

See what you think and experiment and report your findings :-)

--[[
Still photography HFD script
Version 0.1 (should work on all ML enabled cameras with the Lua module)
Garry George Oct 2016
http://photography.grayheron.net/
Must be in LV and lens must report distance
--]]

-- Declare globals

hfd = KEY.RATE

-- Change HFD value to your choice of button, eg KEY.RATE, KEY.UP, KEY.INFO or KEY.PLAY.
-- See http://davidmilligan.github.io/ml-lua/modules/constants.html for key constants
-- Default is KEY.RATE

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

event.keypress = function(key)
if keymenu.submenu["Turn On/Off"].value == "Off" then
return true -- key works as normal
elseif key == hfd and  lv.enabled -- user asking for a hfd move
then
move()
return false -- steal the button push
end
end


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"},
}
}
}
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 30, 2016, 06:23:19 PM
@garry23 Awesome !
Thank you very much for the huge help.

Many things I didn't know like

Anyway, I'll dig into the links you provided me.
Meanwhile, I tried you script, and it's awesome because:

That's a very very helpfull starting (and already working) point, thanks again.

One last question thou, I don't get where does the API's lens.hyperfocal value comes from ?
since it depends on many factors, does that mean that ML already actually does the maths for me (CoC, current aperture, ..) ?

( BTW, the lens I'm using it with is a new Tamron 15-30 2.8 vc.  )
( BTW2, np++ is already my everyday text editor when not bothering with an IDE :) )
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 30, 2016, 06:27:08 PM
Quoteyou may be able to optimize the move algorithm, ie I simply used single steps.
Good point. And I'm sure I can start by borrowing ideas from sorting algorithms for instance  :)
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on October 30, 2016, 06:40:09 PM
@knoodrale

Yes ML has the difraction correction built in, in fact I helped put the code in, see http://photography.grayheron.net/2015/04/customising-magic-lantern.html

Look under focus menu.

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 30, 2016, 06:50:47 PM
Thanks @garry23,
I was also just about to post that I found DoF calculation code http://a1ex.bitbucket.io/ML/5D/lens_8c_source.html#l00132
And I'm slowly getting a general sense of what ML does and does not :)
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 30, 2016, 07:22:04 PM
Random (but kinda related) thought :

Could it be interesting/possible (probably possible at least) to estimate the SNR and correlate that to the CoC ?

What I mean is, the detail resolution in the final image is also heavily dependant on the noise. So let's say I'm at 12k ISO, is that still relevant to compute the DoF with the real CoC since anyway, the details won't be there. So couln't it be interesting to "adapt"  the maths with a fake, larger CoC than the real one when in high iso ?
That may be of little use for landscape photographers and crip D810 images, but when shooting in low light, 6400 ISO, I am not sure the blur will be visible as it would be in base iso.

Is that a stupid Idea ? And was that understandable ? (I struggle expressing myself in english sometimes :) )
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 30, 2016, 08:48:54 PM
Please excuse me for the many posts, do you know if there is any way to know/test/log/whatever the length of an AF step ?
After some reading, I learned that those steps vary greatly from one lens to another.

So Can I "measure" (or find) that length for my lens ?

And Is there ways trough lua scripting to store that info associated with my lens ID/model other than doing dirty stuff with writing in some files ?
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on October 30, 2016, 09:49:41 PM
@knoodrake

I don't think SNR and DoF are linked in any real sense.

One thing you soon learn on the ML forum is patience :-)

Have a read of the ground breaking work going on here: http://www.magiclantern.fm/forum/index.php?topic=4997.msg171205#msg171205

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: dmilligan on October 31, 2016, 01:05:38 AM
Quote from: knoodrake on October 30, 2016, 08:48:54 PM
And Is there ways trough lua scripting to store that info associated with my lens ID/model other than doing dirty stuff with writing in some files ?
The only way to store data is in files, but there's a nice wrapper for you: http://davidmilligan.github.io/ml-lua/modules/config.html
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on October 31, 2016, 04:29:16 PM
@garry23
QuoteHave a read of the ground breaking work going on here: http://www.magiclantern.fm/forum/index.php?topic=4997.msg171205#msg171205
Yes, I have read this thoughtfully, and I do not understand yet very well the whole things with the lenses motors/steps but yes, and it looks very interesting and very promising ! Also still slowly learning the precise meaning of some of the domain-specific vocabulary used there !

QuoteI don't think SNR and DoF are linked in any real sense.
not directly, absolutely, I just meant that at the end, in the final picture, if the image is very very noisy, you won't be able to separate details, whether or not it should be optically acceptably sharp.
My thinking was that when relating to hyperfocal, if you can loose 5cm on the near end while going from 40m to infinity, and not being able to actually measure a difference in details in thoses 5cm, well, that can be usefull.

Not sure I made myself understandable :) ..Also, I may be completly wrong. But in such case I don't know where !

@dmilligan
Quotebut there's a nice wrapper for you
thank you ! perfect ! As long as the I/O part is abstracted, there is no problem using files from my side as user of the API !
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 05:15:29 PM
Hello,

I tried to auto switch LV on before running the move() function and then auto switch it back to off (if it wasn't  already on) but I cannot..

basically, I check if lv.enabled, if not I lv.start().
My problem is that I cannot find a way to properly wait for LV to be really started before calling move().
For instance, if I do
lv.start()
lv.wait(1)

The console clearly tells me that the lv.wait fails because lv is stopped.

I tried the msleep way (with a repeat loop, and even with just a huge value of 10s) but msleep apparently blocks the execution of the lv.start().

I searched the API doc, looking for something like an "LV ready" event, but couldn't find.

Any hints ?  :o

Thanks.

EDIT: Please correct me if I'm wrong, but in the lua script, I'm single threaded, but the calls to ML/Canon API are asynchronous. I got this right ?
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 03, 2016, 05:22:11 PM
lv_start() should return when LiveView is up and running (it already includes a msleep, but no closed-loop confirmation).

Have a minimal example that I could debug?
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 06:12:33 PM

event.keypress = function(key)
if key == KEY.RATE then
   if not lv.enabled then
lv.start()
lv.wait(1)
  end
return false
end
end



and

http://imgur.com/a/e0E64
(http://imgur.com/a/e0E64)
I did something wrong ?
Need more code/infos ?
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 03, 2016, 06:13:34 PM
@knoodrake

I looked into your lv.start strangeness, and I have it as well :-(

Here is my demo code snippet, used with LV off.

lv.start()
repeat
msleep(200)
until lv.enabled
move() -- to HFD position
lv.stop()


The camera freezes and I need to remove the battery.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 06:21:15 PM
@garry23

yep, I also had many strange behavior like the camera freeze or, semi-freeze (like I can still AF for instance) or other strange buggy behaviors, depending on how I tried things with code. But it was too hard to remember the steps to reproduce, and the exact camera behavior.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 03, 2016, 06:27:37 PM
I wonder if the problem is related to it being called inside event.keypress call?

I'm sure A1ex or David will give us guidance.

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 06:27:56 PM
I remember however having tried to do something like you have

lv.start()
repeat
  msleep(200)
  until lv.enabled


but with the "fail-safe" counter ( something like )


local slept = 0
local threeshold = 3000
lv.start()
repeat
  slept = slept + 200
  msleep(200)
  until lv.enabled or slept >= threeshold


and the loop just stopped at he 3s threeshold without further bugs. Camera would work normaly after that (except the issue we'r discussing).
So it's just that lv.enabled was never updated or lv.start() did not start.

Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 06:31:45 PM
@garry23

maybe something to do with this sentence I read on the lua event api doc https://davidmilligan.github.io/ml-lua/modules/event.html (https://davidmilligan.github.io/ml-lua/modules/event.html)
QuoteEvent handlers will not run if there's already a script or another event handler actively executing at the same time.
?

But yes, I hope A1ex or David will give us guidance.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 03, 2016, 07:27:19 PM
I think you are right and believe David told be about that a while ago  ;D
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 03, 2016, 07:58:21 PM
Quote from: garry23 on November 03, 2016, 06:27:37 PM
I wonder if the problem is related to it being called inside event.keypress call?

Correct. It's running under Canon's GUI task, so a msleep there will prevent Canon code from switching the GUI mode. Therefore, the event handler must be short (similar to an interrupt in embedded systems, or to a button handler in GUI frameworks - you can't use delays there either without locking up the entire thing while waiting).

To my knowledge, the way to implement this is to send a message to some other task from the event handler. Canon code uses semaphores (for binary messages), message queues (for more complex messages) or event flags (e.g. for waiting for a number of events which may happen in any order). There's no API exposed in Lua for those things yet.

Dirty way (usually good enough if you don't need real-time response): global variable and polling.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 08:10:22 PM
Thanks @a1ex !

Dirty way (usually good enough if you don't need real-time response): global variable and polling.


I'm absolutely not used to dev on real small hardware such as a dslr (java dev). Pooling won't do much harm in term of CPU (battery life) ? if it's like 500ms ? I guess not ?..
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 03, 2016, 08:16:29 PM
I think you can use 100ms or 200ms with very little impact on battery power and overall performance. Even 50 should give good results when you need it.

You can estimate the power draw from the Debug menu (not very exact, but still useful).
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 03, 2016, 09:32:31 PM
@a1ex

Sorry to be stupid, but by polling do you mean like this?

lv.start()
repeat
dummy = 0
msleep(100)
until lv.enabled
move() -- to HFD position
lv.stop()


If so, the above doesn't work  :(
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 09:54:11 PM
I think he means that the lv.start() should be done outside the event handler ( which I see as a "UI Thread" from my java mind :) )
So the idea would be that the keypress event just set a flag to says we want to start LiveView.

Somewhere else, we have a loop that checks every Xms if the flag is set. If so, we can start LV because we are within another "thread" (or whatever is called this Canon GUI Context ).

I hope I'm right up to this point :)

One thing I don't get, is how to start the "pooling" ? From my understanding I can't just write startPooling() at the end of the file, it won't be executed. On the other end, the only other "starting point" I have are camera events.. ( lens event, button event, ... )
I don't know how to "pool" automatically from the script loading ?
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 10:04:34 PM
I think I may have understood but the exact opposite: start LV in the event handler context. Works ok, but we have to "wait" by checking (polling) if LV is on outside of the event handler (the opposite of what I said in my previous post).

Anyway, that doesn't change the problem: how to start the "pooling" elsewhere ?
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 03, 2016, 10:09:56 PM
I'll 'just' stick to the script only working IF liveview is on  ;)

--[[
Script to illustrate the use of key modification
Version 0.4 (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 Nov 2016
http://photography.grayheron.net/
For the 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.MENU -- key to modify
mod_button_2 = KEY.INFO -- 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 ~= 0 and lens.af then -- lens is OK
if lens.dof_far ~= lens.dof_near then -- ok to 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
else
beep (3, 200 , 500)  -- warning message
display.notify_box("Check aperture", 3000)
end
else
beep (3, 200 , 500)  -- warning message
display.notify_box("Check lens settings", 3000)
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"},
}
}
}
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 03, 2016, 10:12:01 PM
https://en.wikipedia.org/wiki/Polling_(computer_science)
https://en.wikipedia.org/wiki/Pooling_(resource_management)
https://en.wikipedia.org/wiki/Busy_waiting

;)

What you write at the end of the file will be executed during initialization. So, if you place your main loop there, it may block other scripts from loading.

You can start a background task (task.create), or you can use the shoot_task hook (called pretty often, with no guarantees about the timing, but it's a ML task, so you can use it for this purpose).

Tasks are a scarce resource on some cameras, so the second method may give less headache when running on other cameras.

Note that moving the focus ring is also an activity that takes a while, so you shouldn't do it from the GUI event handler either.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 10:25:33 PM
Quotehttps://en.wikipedia.org/wiki/Polling_(computer_science)
https://en.wikipedia.org/wiki/Pooling_(resource_management)
Sorry :D

Thanks for the infos and advice. I'll look into tasks and that hook.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 03, 2016, 10:44:29 PM
@knoodrake

I looked at using the seconds_clock (arg) event, called every second, but it didn't seem to work.

I looked for the event module in the Lua code, but couldn't find any reference to event handling: so maybe these bits of Lua aren't enabled yet.

Keep us all plugged into you experiments  ;)
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 11:23:26 PM
@garry23 I just happened to do something that seems to work, and with seconds_clock as well ! It takes at worst 0.99999s to kick in, but that seems to do the trick.
If already in LV, stays in LV, if not, goes back in LV off.

HFDRequested = false
LVWasAlreadyOn = true

event.seconds_clock = function (arg)
if HFDRequested and lv.enabled then
HFDRequested = false
move()
if not LVWasAlreadyOn then
lv.stop()
end
end
return true
end

event.keypress = function(key)
if keymenu.submenu["Turn On/Off"].value == "Off" then
  return true -- key works as normal
elseif key == hfd then
HFDRequested = true
  if not lv.enabled then
LVWasAlreadyOn = false
lv.start()
  end
return false -- steal the button push
end
end

Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 03, 2016, 11:26:38 PM
Note: I did not tried yet with shoot_task because I have yet to understand what it is (is that a generic event that is fired for various unrelated things ? )
EDIT: Also, not yet investigated the tasks
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 09:05:11 AM
@knoodrake

I realised my idea of using event.seconds_clock would work, after a saw I had coded in an error.

Anyway, here is my latest version of the HFD script, which uses the modified key approach, so I don't 'lose' a key, and some additional error checking:

--[[
Script to illustrate the use of key modification
Version 0.5 (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 Nov 2016
http://photography.grayheron.net/
For the HFD functionality must be in AF mode and lens must report distance
Script handles LV, ie LV doesn't need to be on'
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 some globals

mod_button_1 = KEY.MENU -- key to modify
mod_button_2 = KEY.INFO -- key to indicate mod mode requested
last_key_pressed = 999 -- used to reset mod_button pairing
hfd_move_requested = false
lv_already_on = false

-- 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 check_lens_ready() -- just in case
if hfd_move_menu.submenu["Turn On/Off"].value == "On" then-- check lens is ready to be moved
if lens.focus_distance ~= 0 and lens.af then -- lens is OK
repeat
msleep(100)
until lens.focus(0,1,false)
end
end
end

function move()
if lens.focus_distance ~= 0 and lens.af then -- lens is OK
if lens.dof_far ~= lens.dof_near then -- ok to 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
else
beep (3, 200 , 500)  -- warning message
display.notify_box("Check aperture", 3000)
end
else
beep (3, 200 , 500)  -- warning message
display.notify_box("Check lens settings", 3000)
end
end

function test4mod(key)
if hfd_move_menu.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 flag requests here ************
last_key_pressed = 999 -- reset
hfd_move_requested = true -- flag HFD move requested
if lv.enabled then lv_already_on = true else lv_already_on = false end
-- ****************** Place modified flag requests 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

function check_requests(arg)
if hfd_move_requested == true then -- handle lv and lens move
hfd_move_requested = false -- reset flag
if not lv_already_on then lv.start() end
check_lens_ready() -- just in case it isn't'
move() -- lens to HFD position
if not lv_already_on then lv.stop() end
end
end

event.keypress = test4mod
event.seconds_clock = check_requests

hfd_move_menu = menu.new
{
parent = "Shoot",
name = "HFD",
help = "Moves lens to HFD focus distance",
submenu =
{
{
name = "Turn On/Off",
help = "Switches the script on/off",
choices = {"On","Off"},
}
}
}
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 04, 2016, 09:21:40 AM
Nice.

The only problem that may appear is if your seconds_clock handler takes more than one second to run - this will cause other handlers not to get called at every second. Currently the side effects are minor (probably throwing off the battery level estimations if it happens a lot), but others might rely on this hook getting called once per second.

Therefore, I recommend using the shoot_task hook instead. The rest of the code can stay unchanged.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 09:30:09 AM
@A1ex

One thought I had was asking you or David to put in a variable seconds_clock event handler, or one, at say, 0.5 seconds.

I'm afraid I'm stumped on using the shoot_hook approach, would you be so kind to show me how I would do it.

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 04, 2016, 09:35:51 AM
Simply find/replace: s/seconds_clock/shoot_task/.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 09:42:40 AM
Of course: how stupid of me.

Just tried it and all OK - Thank you.

Here is the final script: https://gist.github.com/pigeonhill/92df2d0ae7b69b4223cd802eedee3db1
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 09:56:56 AM
@A1ex

I forgot to ask, if you don't return a bool when calling a lua event handler, does it have an consequences.

In my code I don't return anything.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: a1ex on November 04, 2016, 10:54:02 AM
The return value is used to allow (true) or block (false) other routines (from other modules/scripts) that might run after this event. Their order is currently somewhat hardcoded (it depends on the order in which the modules are loaded - alphabetically - and then, the Lua module dispatches the event to the scripts - I think it's in the order they are started).

If you don't return anything, Lua assumes nil, which evaluates to true. So, it should do the right thing.

Blocking other CBRs makes sense for keys, for example (you handled some key and you don't want other feature, or Canon code, to reuse the same key).
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 11:23:27 AM
@A1ex

Thanks: fully understood.

I'll insert a 'return false' IF my HFD move is used.

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 04, 2016, 01:04:43 PM
@garry23 @a1ex

Cool, thanks a lot to you both  :)
Now that I discovered all the potential and easiness of scripting my camera, I want to do so many things (well, my last Idea was to play GoT theme song in camera beeps, maybe not the best nor the most useful idea )

A last question @a1ex if I may; Are there many interesting things the picoc modules API allows that are not exposed/possible in Lua ? And most importantly, is there a nice API doc for the module api somewhere, something as nice as this lua one https://davidmilligan.github.io/ml-lua/ (https://davidmilligan.github.io/ml-lua/)  ?

I just found the doxygen doc, some chaotic wiki pages, and pieces of advice scattered around the forum.
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 01:17:06 PM
@knoodrake

I'm looking forward to seeing/using your work.

I share all my Lua scripts, but I note that not many others seem to  :o

All my working scripts are here: https://gist.github.com/pigeonhill

Maybe you should share yours via github?

Cheers

Garry
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: knoodrake on November 04, 2016, 01:27:23 PM
@garry23 I already checked your scripts  :P ( LE Simulation interested me a lot for instance, since my new lens doesn't take my ND1000 )

As for me, I love ML ever since I discovered DualISO, but it was here the very first time I tried to do anything :)
I'll make sure to share if (more likely when) I do anything else !
Title: Re: set a button to a focus distance (for hyperfocal scenaris)
Post by: garry23 on November 04, 2016, 01:41:47 PM
@knoodrake

My all singing script is: https://gist.github.com/pigeonhill/e008f051681885bc6ff84de637cb1d8e

This includes LE mode.

I may well add the multi-mode key approach to that and embedded HFD positioning  ;)