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

probably asking for too much - events in lua?

  • 3 Replies
  • 2915 Views
probably asking for too much - events in lua?
« on: 25 / December / 2009, 01:25:59 »
Advertisements
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

  • ******
  • 11924
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")
end

--press shutter half
press("shoot_half")
  repeat
    sleep(1)
  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)

  set_prop(propcase.TV,shoot_tv)
  set_prop(propcase.SV,shoot_sv)
  set_prop(propcase.AV,shoot_av)
  set_focus(65535)

--press shutter
press("shoot_full")
  sleep(1)
  release("shoot_full")
  sleep(1)
  release("shoot_half")
  repeat
    sleep(1)
  until get_shooting() ~= true
 
  --rename file
  if (name ~= nil) then
sleepCount = 0
local currentCount
  repeat
  sleep(1)
  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:new(currentFile)
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)
  os.rename(currentFile,currentFileDir..name)
print(currentFileNumber)
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")
  shut_down()
  end
 
  end
  --log.print("cam.smartShoot - end")
end

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

*

Offline reyalp

  • ******
  • 11924
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
cam:smartShoot(focusDistance,name,shotSettings)
self:WaitHalfPRess() -- does the half press + sleep until shooting ready
shotSettings:Set() -- sets tv etc as desired, saves the used settings into shotSettings.tv 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.
UpdateComment(self.currentFile,shotSettings)
...

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