probably asking for too much - events in lua? - LUA Scripting - CHDK Forum

probably asking for too much - events in lua?

  • 3 Replies
probably asking for too much - events in lua?
« on: 25 / December / 2009, 01:25:59 »
i find that my lua code is getting unnecessarily complex. if i was using javascript, i'd use events to simplify the code. for instance, I could fire an event after a pic is taken and write logging info, rename the file, and modify exif data.

is anything like this possible?


Offline reyalp

  • ******
  • 13947
Re: probably asking for too much - events in lua?
« Reply #1 on: 25 / December / 2009, 01:53:03 »
I'm not really clear what you have in mind. Maybe you can post a pseudo code example of what you want to do ?

Are you asking for the CHDK system to produce events that you can handle in lua ? It obviously doesn't do that now, but you could write a dispatcher in lua that was functionally equivalent (unless you need it to happen for state changes that currently aren't visible to lua, in which case you need to say which ones you need).

There are several ways you can write objectish or even functional-ish code in lua.
Don't forget what the H stands for.

Re: probably asking for too much - events in lua?
« Reply #2 on: 25 / December / 2009, 18:08:49 »
allowing camera events to trigger lua functions would be wonderful, but strikes me as a more complicated request.

I'm trying to figure out how my code can be loosely coupled. with my current timelapse code, my logging code and exif editing code ends up in my "take a pic" code -

Code: [Select]
function cam.smartShoot(default_sv,focusDistance, name, calcShotSettings)
local dirCountRename

if (name ~= nil) then
dirCountRename = disk.itemCount("A/DCIM/100CANON")

--press shutter half
  until get_shooting() == true

--get exposure
local bv=get_bv96()
local av=get_av96()

--calc camera settings
shoot_tv, shoot_sv, shoot_av = calcShotSettings(bv,av,default_sv)


--press shutter
  until get_shooting() ~= true
  --rename file
  if (name ~= nil) then
sleepCount = 0
local currentCount
  sleepCount = sleepCount + 1
  --get directory count and currnet directory count
currentCount = disk.itemCount("A/DCIM/100CANON")
  until currentCount > dirCountRename
--get from current directory
  local currentFile = disk.getNewImage("100CANON")
  local image = JPEGImage
image:SetUserComment("timestamp, " .. date.datetimestamp() .. ", shoot_tv," .. shoot_tv .. ", shoot_sv," .. shoot_sv .. ",shoot_av," .. shoot_av .. ",bv," .. bv .. ",currentFile," .. currentFile .. ",newName," ..  name .. ",optical_temp," .. get_temperature(0) .. ",ccd_temp," .. get_temperature(1) .. ", batt_temp," ..  get_temperature(2) )

  local currentFileDir = string.sub(currentFile,1,16)
  --local currentFileNumber = string.sub(currentFile, -8,-5)
  local currentFileNumber = string.sub(currentFile, -7,-5)
log.print("new file name \t",currentFile, "\t current count \t",currentCount)

  if(tonumber(currentFileNumber) > 990) then
  log.print("cam.smartShoot - restart due to file count")
  --log.print("cam.smartShoot - end")

i can see some places where my conditionals could be better, but aside from that, how could this code be improved?


Offline reyalp

  • ******
  • 13947
Re: probably asking for too much - events in lua?
« Reply #3 on: 25 / December / 2009, 19:17:34 »
Hmm, I'm not sure I see anything wrong with that code, but you could break it up into more functions and package the data into tables/objects if you wanted. The prepare, shoot, get file name/number, exif, rename, log and shutdown are all pretty independent, it would looks like it should be easy to stuff them into separate functions.

What I would do if I wanted to clean this up is make the values that need to get passed around members of cam (or some other table or object) and then break each step (set comment, rename, check for shutdown) into different functions (or methods of some objects that know how to access the values they need.

For example, calcShotSettings could become an object that calculates the settings, sets them and stores the values. The the comment update thing could just take that and query it for the last bv, tv etc. So you could end up with something like
Code: [Select]
myshotSettings=NewShotSettings{ default_sv=100, calc=mycalcsettings} -- construct a shotSettings object
... cam.smartShoot becomes like
self:WaitHalfPRess() -- does the half press + sleep until shooting ready
shotSettings:Set() -- sets tv etc as desired, saves the used settings into etc)
self:DoShot() -- does the full press, release, wait for get_shooting to change
self:UpdateCurrentFile() -- gets the latest file name and number into cam members.

obviously, you there's a lot of ways you could do this in terms of which things are objects and which are functions and how they call each other. I'm just trying to convey the general idea.

Note, I actually prefer closure based objects over the : syntax, since you don't have to worry about accidentally mixing : and . syntax, the private members are truly private, and you don't end up with a bunch of self:...
Don't forget what the H stands for.


Related Topics