I was thinking of not putting anything but scripts to the chdk/scripts folder. If (when) the path is hardcoded to chdk/scripts, would it be possible to load libs from e.g. chdk/lib? I think I triedrequire "../lib/libname"but no avail.
What if libs were first searched from one location (chdk/lualib), and then if not found, from another (chdk/scripts)? Or the other way round...
function saferun(code) local status local f,msg=loadstring(code,"code") if not f then print("compile err") print(msg) sleep(5000) else print("compile ok") sleep(2000) status,msg = pcall(f) if not status then print("run err") print(msg) sleep(5000) end endend-- now put all your code in a big stringsaferun([[allmy lua code]])
...Another thing that should be documented is how large numbers we can use. I assume it's +-2^15 or +-2^31, one short from one (negative?) end (this one we're missing in our ubasic documentation as well).
It should be a 32 bit int, +2147483647 -2147483648Ubasic does use shorts in some places, but not for variables as far as I can tell.
From above I can see sleep() makes Lua wait for the next tick that loops may create automatic "sleeps" to prevent lockups (But when? At each iteration?). I somehow remember someone saying that some commands related to camera functionality do this as well (like shoot(), obviously) but couldn't find on a quick search.
Another thing that should be documented is how large numbers we can use. I assume it's +-2^15 or +-2^31, one short from one (negative?) end (this one we're missing in our ubasic documentation as well).
It looks to me like the following yield (and thus wait for another 10ms cycle):shootsleep (duh )click, press, releasewait_clickmd_detect_motion
Additionally, a "count hook" is used so that lua will yield every 1000 lua VM instructions (with some exceptions that I'm not totally clear on.) You can't easily tell how many VM instructions a bit of lua code turns into, but in general every keyword, variable reference, operator or function call will turn into one or more. Comments and whitespace are taken care of by the compiler, so you don't have to worry about them.
while not is_pressed "up" doend
Additionally, a "count hook" is used so that lua will yield every 1000 lua VM instructions (with some exceptions that I'm not totally clear on.) You can't easily tell how many VM instructions a bit of lua code turns into, but in general every keyword,
One thought that has occurred to me, for both languages, is that we could just sleep based on execution time. Maybe the 10ms resolution of get_tick_count would be a problem ? I don't suppose ARM has anything like an x86 TSC ?