hide_osd() script function - page 10 - General Discussion and Assistance - CHDK Forum

hide_osd() script function

  • 98 Replies
  • 16552 Views
*

Offline lapser

  • *****
  • 1093
Re: hide_osd() script function
« Reply #90 on: 16 / October / 2013, 18:28:08 »
Advertisements
This is the trunk so nothing is set in stone, we can change it if someone has a convincing argument or better code, but after 9 pages of discussion we might as well have something people can actually use...
Sounds good. I spent some time today working on my patch, and figured out a lot about hiding and showing the title line, console, and osd. I have it working pretty well, without messing up the help menu and console timeouts, or the file browser, textbox or touchscreen.

I also fixed a bug that causes a lot of flashing of the script console, especially if the console is expanded. This flashing happens when the help menu timeout is set to 0, and the help menu is disabled with the checkbox. This causes the screen to be cleared before every gui_redraw(). I'll post more details tomorrow, if I have enough time.

I've decided to implement the script interface as a single function called "on_off()", with a single string parameter. The function returns true for "on" and false for "off". The string is prefixed with "+", "-", or "~" for ON, OFF, or TOGGLE. The status is not changed if there is no prefix, but the current ON/OFF status is still returned.

I haven't written the on_off() code in luascript.c yet, but I did write a test script to use when I do write it. It will give you the idea of how it will work:
Code: [Select]
--[[
@title On_Off 1 Lua
@param d Dummy
@default d 1
--]]
set_console_layout(10,0,45,5)
print("<SET> backlight | <DISP> error")
print("<LEFT> osd | <RIGHT> console")
print("<UP> title line | <DOWN> all")
print("<MENU> exit")
stype={"osd","console","title_line","all","backlight"}
repeat
  sp=string.format("a=%s t=%s c=%s o=%s", tostring(on_off("all")), tostring(on_off("title_line")), tostring(on_off("console")), tostring(on_off("osd")))
  draw_string(0,0,sp,264,256)
  print(sp)
  wait_click()
  if(is_key("display") or is_key("video"))then force_error() end -- call nil function
  if(is_key("set"))then  on_off("~backlight") end
  if(is_key("left"))then  on_off("~osd") end
  if(is_key("right"))then
    on_off("~console")
    set_console_layout(10,0,45,5) -- moves console if title_line is off
  end
  if(is_key("up"))then on_off("~title_line") end
  if(is_key("down"))then on_off("~all") end
until is_key("menu")
--flash on off individually
for i=1,5 do
  on_off("-"..stype[i])
  sleep(1000)
  on_off("+"..stype[i])
  sleep(1000)
end
Although the discussion may have seemed unproductive, I've appreciated all the ideas, and tried to take them into account in developing this solution. New comments are welcome.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline lapser

  • *****
  • 1093
Re: hide_osd() script function
« Reply #91 on: 18 / October / 2013, 10:08:40 »
Making progress on the generalized patch, but the weather's been good lately, so I haven't wanted to be inside to work on it very much.

I was able to implement waterwingz idea of moving the script console to the bottom line when the <alt> title line is hidden. It's a little more complicated than it looks like at first.

The console stays in the normal, line 1, position until the script calls:

set_layout(x1,0,x2,y2)

That is, the script moves the console to line 0. After doing this, if the script then hides the <alt> title line, the console moves down to the bottom, line 0. When the script shows the <alt> line again, the console moves back up to line 1 so it doesn't cover the <alt> line.

Any scripts that don't hide the <alt> line, which includes all current scripts, won't be affected.

Now that all the actions are implemented, I'm ready to write the osd_off() script function described above__ in between sunsets, of course.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline reyalp

  • ******
  • 12942
Re: hide_osd() script function
« Reply #92 on: 18 / October / 2013, 15:57:27 »
I've decided to implement the script interface as a single function called "on_off()", with a single string parameter. The function returns true for "on" and false for "off". The string is prefixed with "+", "-", or "~" for ON, OFF, or TOGGLE. The status is not changed if there is no prefix, but the current ON/OFF status is still returned.
Some comments on this:

on_off is not a descriptive name. There are lots of things on a camera you can turn on and off.

The concept isn't consistent with the existing API. This isn't necessarily a reason to reject it (some of the existing patterns suck, and there are already plenty inconsistencies) but where possible having consistent interfaces is preferable.

String parameters limit the possibility of making the interface compatible between ubasic and lua. You can use literal strings in ubasic, but you can't put them in variables or manipulate them. I'm not necessarily against leaving features out of ubasic, but all else being equal it's preferable to support both. Lua centric interfaces makes sense for things that can't reasonably be implemented in ubasic, or where using Lua's more advanced language features make things significantly simpler or easier to understand.

From a code efficiency point of view, there is little difference in cost between a C function for each thing (set_foo(value)) vs a function that takes named parameters (set("foo",value)). Clarity and maintainability matter much more.
Don't forget what the H stands for.

*

Offline lapser

  • *****
  • 1093
Re: hide_osd() script function
« Reply #93 on: 18 / October / 2013, 21:09:58 »
on_off is not a descriptive name. There are lots of things on a camera you can turn on and off.

The concept isn't consistent with the existing API. This isn't necessarily a reason to reject it (some of the existing patterns suck, and there are already plenty inconsistencies) but where possible having consistent interfaces is preferable.

String parameters limit the possibility of making the interface compatible between ubasic and lua. You can use literal strings in ubasic, but you can't put them in variables or manipulate them. I'm not necessarily against leaving features out of ubasic, but all else being equal it's preferable to support both. Lua centric interfaces makes sense for things that can't reasonably be implemented in ubasic, or where using Lua's more advanced language features make things significantly simpler or easier to understand.

From a code efficiency point of view, there is little difference in cost between a C function for each thing (set_foo(value)) vs a function that takes named parameters (set("foo",value)). Clarity and maintainability matter much more.
Thanks for the thoughts. I just got the new function working, but I need to clean up the code and do some more testing before posting a patch.

"on_off" may not be the best name, but it describes exactly what the function can do. It can turn anything in the camera on or off, not just the CHDK OSD elements. My current version also turns the backlight off and on. It's very easy to extend the function to turn off/on anything.

The concept is partly consistent with the press, release, and click functions. They all take a string parameter that represents the key name. Using a fixed string in uBasic isn't a problem. I was only manipulating the strings for testing all the options in Lua.

Because the function uses strings instead of "magic" numbers, it should be easier to understand the script. As I said, the function can be expanded to turn anything on and off in the future. It's simple enough that it should be easy to maintain. It's much easier to understand the on_off C code than to figure out the press/release/click C code.

Instead of using +, -, and ~, I'm now using ON, OFF, and TOGGLE in caps. ON, OFF, or TOGGLE can appear anywhere in the string, and unrecognized words are ignored. For example, you could write:

on_off("turn console ON")
or
on_off("TOGGLE backlight")

Similarly, you can enter the item you're changing anywhere in the string. You can change "all", "osd", "title", "console", and "backlight" with the current implementation. I'm still working on "display".

Other possible names for the function might be:
"switch_state"
"switch_status"
"status"

I like the look of:
switch_state("console OFF")
switch_state("TOGGLE backlight")
s=switch_state("display")

The word "switch" can be used as a verb meaning "change", as in the first two examples, or as a noun, as in "get me that switch", like in the 3rd example.

Here's the lua part of the modifications:
Code: [Select]
//******* on_off function start
static const char *swname[]={"all","osd","title","console","backlight","display",NULL};

static int luaCB_on_off( lua_State* L )
{
  const char *stype=luaL_checkstring( L, 1 );
  int do_or=0;  //1 off, 1 on, 0 toggle, 0 no change
  int do_eor=0; //0 off, 1 on, 1 toggle, 0 no change
  int i=0;
  while(strstr(stype,swname[i])==NULL)
  {
    i++;
    if(swname[i]==NULL)return luaL_error( L, "unknown on_off type" );
  }
  if(strstr(stype,"ON")!=NULL)do_or=do_eor=1;
  else if(strstr(stype,"OFF")!=NULL)do_or=1;
  else if(strstr(stype,"TOGGLE")!=NULL)do_eor=1;
  int r=0; //result
  switch(i)
  {
    case 0: //all
      r=camera_info.state.all_off=(camera_info.state.all_off|do_or)^do_eor;
      break;
    case 1: //osd
      r=camera_info.state.osd_off=(camera_info.state.osd_off|do_or)^do_eor;
      break;
    case 2: //title
      r=camera_info.state.title_off=(camera_info.state.title_off|do_or)^do_eor;
      console_redraw(); //move console to or from bottom line
      break;
    case 3: //console
      r=camera_info.state.console_off=(camera_info.state.console_off|do_or)^do_eor;
      break;
    case 4: //backlight
      r=camera_info.state.backlight_off=(camera_info.state.backlight_off|do_or)^do_eor;
      if (r) TurnOffBackLight();
      else TurnOnBackLight();
      break;
    case 5: //display
      r=camera_info.state.display_off=(camera_info.state.display_off|do_or)^do_eor;
      //add display function here
      break;     
  }
  if((i<4) && r)gui_set_need_restore(); //clear screen when turning something off
  lua_pushnumber(L,r); //change to boolean?
  return 1;
}
//******* on_off function end

Here's the rough Lua test script. Everything worked so far.

Code: [Select]
--[[
@title On_Off 1 Lua
@param d Dummy
@default d 1
--]]
set_console_layout(10,0,45,5)
set_console_autoredraw(2) -- 2 means no console timeout
print("<SET> backlight | <DISP> error")
print("<LEFT> osd | <RIGHT> console")
print("<UP> title line | <DOWN> all")
print("<MENU> exit")
stype={"osd","console","title_line","all","backlight"}
repeat
  sp=string.format("a=%s t=%s c=%s o=%s",tostring(on_off("all")),tostring(on_off("title_line")),tostring(on_off("console")),tostring(on_off("osd")))
  repeat
    draw_string(0,0,sp,264,256)
    wait_click(250)
  until not is_key("no_key")
  print(sp)
  if(is_key("display") or is_key("video"))then force_error() end -- call nil function
  if(is_key("set"))then  on_off("TOGGLE backlight") end
  if(is_key("left"))then  on_off("TOGGLE osd") end
  if(is_key("right"))then
    on_off("TOGGLE console")
  end
  if(is_key("up"))then on_off("TOGGLE title") end
  if(is_key("down"))then on_off("TOGGLE all") end
until is_key("menu")
draw_clear()
--flash on off individually
for i=1,5 do
  on_off("OFF "..stype[i])
  sleep(1000)
  on_off("ON "..stype[i])
  sleep(1000)
end
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos


*

Offline lapser

  • *****
  • 1093
Re: hide_osd() script function
« Reply #94 on: 21 / October / 2013, 02:35:48 »
I finished the new function, which is called "switch_state". Documentation on the function is in the attached text file. I posted the patch file, and custom builds for each camera (no patching or compiling needed) using Google Drive. You can download it all from this link:

https://drive.google.com/folderview?id=0B3woLhMB_tdqWDFYZk1pVkl4TVk&usp=sharing#list

Select and click the file you want. Then click the button in the lower right corner with the down arrow to download the file.

I'm really happy with the way it turned out, parsing a single string parameter to do multiple things. This opens up new possibilities that could prove interesting. For example, you could put a math expression using Lua global variables, and floating point constants and functions into the string. The function would calculate the expression using (double), and then would return an integer result. That's for the future, though.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

Re: hide_osd() script function
« Reply #95 on: 29 / November / 2013, 18:27:40 »
Hi,

I came to this thread after it was created from a question I posted earlier this year. http://chdk.setepontos.com/index.php?topic=10734.10

I've now actually managed to get round to solving my problem (hiding <ALT> and script name), and Waterwingz's patch looks like it should fit the bill. Are there any builds of CHDK available that include the set_draw_title_line() functions? If not I guess I'll have to compile my own build for the time being, but I'm not really sure what's required at the moment.

Thanks for all the interest.

Cheers,
Jon

Re: hide_osd() script function
« Reply #96 on: 29 / November / 2013, 18:33:22 »
Are there any builds of CHDK available that include the set_draw_title_line() functions? If not I guess I'll have to compile my own build for the time being, but I'm not really sure what's required at the moment.
It was added to the 1.3.0 dev branch weeks ago.

http://mighty-hoernsche.de/trunk/
« Last Edit: 29 / November / 2013, 18:35:31 by waterwingz »
Ported :   A1200    SD940   G10    Powershot N    G16

Re: hide_osd() script function
« Reply #97 on: 29 / November / 2013, 19:47:49 »
That's brilliant! It works exactly as I'd hoped. Thanks for your effort.

Cheers,
Jon


Re: hide_osd() script function
« Reply #98 on: 29 / November / 2013, 20:00:35 »
That's brilliant! It works exactly as I'd hoped. Thanks for your effort.
Not sure what your application is,  but the set_exit_key() function adds a new dimension too.
Ported :   A1200    SD940   G10    Powershot N    G16

 

Related Topics