I got the new display/backlight off technique working as a C function using event_procs from C as reyalp described.
extern unsigned _ExecuteEventProcedure(const char *name,...);
//new set backlight function
// 0 off | 1 on | -1 toggle
static void backlight(int newbl)
{
static int bl=1; //current backlight state starts out ON
static int eproc=2; //0=use TurnOffBackLight() / 1=use event proc / 2=initial call
if(newbl<0)newbl=bl^1;
newbl&=1;
if(newbl==bl)return;
bl=newbl;
if(eproc)
{
unsigned args[1];
if(eproc==2) //first call
{
eproc=1;
args[0]=(unsigned)"DispDev_EnableEventProc";
if (call_func_ptr(_ExecuteEventProcedure,args,1)==-1)
{
args[0]=(unsigned)"DispDev.Create";
if (call_func_ptr(_ExecuteEventProcedure,args,1) == -1)eproc=0;
}
}
if(eproc)
{
if(bl)args[0]=(unsigned)"DispCon_TurnOnDisplay";
else args[0]=(unsigned)"DispCon_TurnOffDisplay";
call_func_ptr(_ExecuteEventProcedure,args,1);
return;
}
if(bl)TurnOnBackLight();
else TurnOffBackLight();
}
}
I was able to add it entirely in luascript.c, but it should probably be somewhere else to work with ubasic too. You need to call backlight(1) if the script ends, is aborted by shoot_full, or error aborts. Attached is a diff file of the luascript.c changes.
A nice addition is set_backlight(-1) which toggles the backlight. That's usually what a script wants to do, so this makes it a lot easier. I need to keep track of the current backlight state anyway, so I can skip turning on the backlight when it's already on, in case this could cause problems on some cameras.
Here's a simple test script:
--[[
@title Backlight Test
--]]
press("shoot_full");
repeat wait_click(10)
if(is_key("set"))then set_backlight(-1) end
if(is_key("video"))then Force_Error() end
until is_key("menu")
Start in record mode, continuous drive to take pictures while the script is running. You have to set the review time to 0 or the screen will still flash. In continuous mode, the review time >0 is apparently ignored, except to turn on the display between pictures.
Press "set" to toggle the backlight
"menu" to exit normally
"video" to force a script error abort
"shoot_full" to abort,
<ALT> to exit alt mode with the script running
This tests all possible ways to exit a script. Pressing <alt> leaves the backlight off with the script running. So you still toggle the backlight pressing set, as well as go through the Canon menus with <ALT> off. This is really messed up, and one reason I think <ALT> should replace "shoot_full" as the script abort key like I did in my other patch.
I'm not sure how to add the new functions to CHDK as opposed to calling them as event procs. If reyalp or phil would be kind enough to show how it's done specifically for this case, I can modify the changes to work that way.
I think it's worth putting into the trunk, as a modification to set_backlight() instead of a new function. That way, all the old scripts will work without flashing as well, without any changes. I don't think it matters to the script what Canon calls the backlight (display) off function. For clarity, we can include a comment in the C code that makes it clear the the script set_backlight() is actually using the display off Canon firmware code.
I do think I notice that the screen takes a little longer to light up again when turning the "backlight" back on this way. Also, my battery tests showed a 14% to 19% increase in run time with the new method. waterwingz tests only showed about 10% with the old method as I recall. It looks like it's turning the power to the display off, instead of just the light.