Lens info in Lua - page 2 - General Discussion and Assistance - CHDK Forum supplierdeeply

Lens info in Lua

  • 16 Replies
  • 470 Views
*

Offline reyalp

  • ******
  • 11998
Re: Lens info in Lua
« Reply #10 on: 31 / July / 2019, 02:40:59 »
Advertisements
Great, glad it worked. I would like to support ILCs better in CHDK script, so it's good to identify stuff like this that could be added.
Don't forget what the H stands for.

Re: Lens info in Lua
« Reply #11 on: 31 / July / 2019, 07:23:06 »


@reyalp

I don't have access to my camera at the moment, but have taken your script and stripped it down to a bare bones function, ie for my use.

Here it is:

Code: [Select]
function test_lens()

-- add in if [not M3 129f] then return false end

local get_lens_name_fn=0xfc2f3fd5 -- EOS M3 120F value from ShowEFLensInfo
local ppname=call_event_proc('AllocateMemory',8)
local plen=ppname+4
poke(ppname,0)
poke(plen,0)

local status=call_func_ptr(get_lens_name_fn,ppname,plen)

local pname=peek(ppname)
local len=peek(plen)
call_event_proc('FreeMemory',ppname)

local i=0
local t={}
while i < len do
   local c=peek(pname + i,1)
   if c == 0 then break end
   table.insert(t,string.char(c))
   i = i + 1
end
local name = table.concat(t)

return name

end

What I would welcome is some education regarding the poking and peeking etc going on. I know what poke and peek mean, but I'm only using them by copying you, not because I know what I'm doing  ;)

Cheers

Garry

*

Offline reyalp

  • ******
  • 11998
Re: Lens info in Lua
« Reply #12 on: 02 / August / 2019, 02:42:14 »
What I would welcome is some education regarding the poking and peeking etc going on. I know what poke and peek mean, but I'm only using them by copying you, not because I know what I'm doing  ;)
The 'AllocateMemory' eventproc is used because the fc2f3fd5 function that returns the name expects pointers to put the length value and the address of the name string in, and Lua doesn't have a native way to pass pointers.
8 bytes because it needs two 32 bit values. ppname is set to the first, plen to the second.
The initial poke just sets each to 0, mostly to be able to tell if the function actually changed the value.

After the call, the values are read out using peek. len is the actual length value, while pname is the address of the start of the string.

CHDK Lua doesn't have a way to directly get a C string from memory into a lua string, so the while loop reads it out byte by byte. Since I wasn't sure how the function actually worked, it checks for both the returned length and a possible terminating null. The Canon code uses a printf-like function, so it should be pretty safe to assume the name is null terminated, but I wanted to be sure it stopped fairly quickly even if I misunderstood the function.

Other notes:
In
Code: [Select]
local status=call_func_ptr(get_lens_name_fn,ppname,plen)
status was another thing I put in just in case I didn't understand the function, you shouldn't need it.

I would suggest keeping status checks for event proc calls like
Code: [Select]
if ppname == -1 or ppname == 0 then
error('AllocateMemory failed')
end
When everything is working normally they are harmless, and if something isn't it will be a lot easier to see the error than debug a crash.
Don't forget what the H stands for.

Re: Lens info in Lua
« Reply #13 on: 02 / August / 2019, 09:20:28 »

@reyalp


Many thanks for taking the time to give me that insight.


I'm pleased to say I understood it  :)


Cheers


Garry


Re: Lens info in Lua
« Reply #14 on: 06 / August / 2019, 07:35:17 »

@reyalp

Can you help me understand what the following call actually does.

Code: [Select]
call_event_proc('System.Create')
Cheers


Garry

*

Offline reyalp

  • ******
  • 11998
Re: Lens info in Lua
« Reply #15 on: 06 / August / 2019, 12:06:05 »
Can you help me understand what the following call actually does.
Code: [Select]
call_event_proc('System.Create')
It registers other eventprocs, including AllocateMemory which is needed by the script.

edit:
System.Create only needs to be called once per camera boot, but calling it more than once is probably harmless.
Don't forget what the H stands for.

Re: Lens info in Lua
« Reply #16 on: 06 / August / 2019, 12:39:42 »
@reyalp


Many thanks.

 

Related Topics