Commit deb62b02 authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Lua: added sleep() with floating-point argument

(similar to msleep)

--HG--
branch : lua_fix
parent 8c2dab6fe742
......@@ -54,20 +54,35 @@ static int luaCB_beep(lua_State * L)
}
/***
Pauses for ms miliseconds and allows other tasks to run.
Pauses for `s` seconds (floating point) and allows other tasks to run.
This will block other tasks/events from this script, but will allow
other scripts, ML tasks or Canon tasks.
Timer resolution: 10ms.
TODO: make it identical to task.yield?
@tparam float s number of seconds to sleep.
@function sleep
*/
static int luaCB_sleep(lua_State * L)
{
LUA_PARAM_NUMBER(s, 1);
msleep((int) roundf(s * 1000.0));
return 0;
}
/***
Pauses for `ms` milliseconds and allows other tasks to run.
Consider using `sleep` for large delays, e.g. `sleep(2)` instead of `msleep(2000)` for readability (there's no functional difference between the two).
@tparam int amount number of milliseconds to sleep.
@tparam int ms number of milliseconds to sleep.
@function msleep
*/
static int luaCB_msleep(lua_State * L)
{
LUA_PARAM_INT(amount, 1);
msleep(amount);
LUA_PARAM_INT(ms, 1);
msleep(ms);
return 0;
}
......@@ -109,6 +124,7 @@ static int luaCB_led_blink(lua_State * L)
static const luaL_Reg globallib[] =
{
{ "sleep", luaCB_sleep },
{ "msleep", luaCB_msleep },
{ "beep", luaCB_beep },
{ "led_on", luaCB_led_on },
......
......@@ -20,7 +20,7 @@ function request_mode(mode, mode_str)
printf("Please switch to %s mode.\n", mode_str, mode)
while camera.mode ~= mode do
console.show(); assert(console.visible)
msleep(1000)
sleep(1)
end
end
end
......@@ -239,7 +239,7 @@ function test_camera_gui()
-- half-shutter should exit playback mode
key.press(KEY.HALFSHUTTER)
msleep(1000)
sleep(1)
assert(camera.gui.play == false)
assert(camera.gui.mode == 0)
key.press(KEY.UNPRESS_HALFSHUTTER)
......@@ -321,12 +321,12 @@ function test_menu()
-- or as integer (internal value)
assert(menu.set("Expo", "ISO", "200"))
assert(camera.iso.value == 200)
msleep(1000)
sleep(1)
-- int should be promoted to string because of custom logic in Expo->ISO menu
assert(menu.set("Expo", "ISO", 1600))
assert(camera.iso.value == 1600)
msleep(1000)
sleep(1)
-- move selection (not required for set to work; just for user interface)
assert(menu.select("Expo", "Picture Style"))
......@@ -335,16 +335,16 @@ function test_menu()
-- numeric works too, as it sets the internal index
assert(menu.set("Expo", "Picture Style", "Portrait"))
assert(menu.get("Expo", "Picture Style") == "Portrait")
msleep(1000)
sleep(1)
assert(menu.set("Expo", "Picture Style", 5)) -- OK, selects Neutral
-- skip string test, as other camera models may use different indices
msleep(1000)
sleep(1)
assert(menu.set("Expo", "Picture Style", "Landscape"))
assert(menu.get("Expo", "Picture Style") == "Landscape")
msleep(1000)
sleep(1)
assert(menu.set("Expo", "Picture Style", 1234) == false) -- should fail, out of range
assert(menu.get("Expo", "Picture Style") == "Landscape") -- old selection should not change
msleep(1000)
sleep(1)
assert(menu.select("Overlay"))
assert(menu.select("Movie"))
......@@ -355,32 +355,32 @@ function test_menu()
-- boolean items should be set-able as int (0 or 1)
assert(menu.set("Shoot", "Advanced Bracket", 1))
assert(menu.get("Shoot", "Advanced Bracket", 0) == 1)
msleep(1000)
sleep(1)
-- or as string (if the user interface displays "ON" or "OFF")
-- here, actual string will be "ON, 10s" or similar (also accepted)
assert(menu.set("Shoot", "Intervalometer", "ON"))
assert(menu.get("Shoot", "Intervalometer", 0) == 1)
msleep(1000)
sleep(1)
-- turning off should be straightforward
assert(menu.set("Shoot", "Advanced Bracket", "OFF"))
assert(menu.get("Shoot", "Advanced Bracket", 0) == 0)
assert(menu.get("Shoot", "Advanced Bracket") == "OFF")
msleep(1000)
sleep(1)
assert(menu.set("Shoot", "Intervalometer", 0))
assert(menu.get("Shoot", "Intervalometer", 0) == 0)
assert(menu.get("Shoot", "Intervalometer") == "OFF")
msleep(1000)
sleep(1)
-- move to Intervalometer menu
assert(menu.select("Shoot", "Intervalometer"))
msleep(1000)
sleep(1)
-- enter the submenu
assert(menu.select("Intervalometer", "Take a pic every"))
msleep(1000)
sleep(1)
-- note: setting menu by string works by brute force
-- that is, trying every possible value and comparing the string
......@@ -390,68 +390,68 @@ function test_menu()
assert(menu.set("Intervalometer", "Take a pic every", "1m30s"))
assert(menu.get("Intervalometer", "Take a pic every", 0) == 90)
assert(menu.get("Intervalometer", "Take a pic every") == "1m30s")
msleep(1000)
sleep(1)
-- actual string will be 10s
assert(menu.set("Intervalometer", "Take a pic every", "10"))
assert(menu.get("Intervalometer", "Take a pic every", 0) == 10)
assert(menu.get("Intervalometer", "Take a pic every") == "10s")
msleep(1000)
sleep(1)
-- integer should work as well - e.g. 1m10s should work now
assert(menu.set("Intervalometer", "Take a pic every", 70))
assert(menu.get("Intervalometer", "Take a pic every", 0) == 70)
assert(menu.get("Intervalometer", "Take a pic every") == "1m10s")
msleep(1000)
sleep(1)
-- out of range, should fail
assert(menu.set("Intervalometer", "Take a pic every", 7000000) == false)
assert(menu.get("Intervalometer", "Take a pic every", 0) == 70)
assert(menu.get("Intervalometer", "Take a pic every") == "1m10s")
msleep(1000)
sleep(1)
-- exit submenu
assert(menu.select("Shoot", "Intervalometer"))
msleep(1000)
sleep(1)
-- let's try some more submenus
-- todo: more multi-level submenus
assert(menu.select("Advanced Bracket", "Frames")); msleep(1000)
assert(menu.select("Advanced Bracket", "Sequence")); msleep(1000)
assert(menu.select("Advanced Bracket", "ISO shifting")); msleep(1000)
assert(menu.select("Bulb Timer", "Exposure duration")); msleep(1000)
assert(menu.select("Shoot Preferences", "Snap Simulation")); msleep(1000)
assert(menu.select("Misc key settings", "Sticky HalfShutter")); msleep(1000)
assert(menu.select("Play mode actions", "Trigger key(s)")); msleep(1000)
assert(menu.select("LiveView zoom tweaks", "Zoom on HalfShutter")); msleep(1000)
assert(menu.select("Lens info", "Lens ID")); msleep(1000)
assert(menu.select("Shoot", "Intervalometer")); msleep(1000)
assert(menu.select("Advanced Bracket", "Frames")); sleep(1)
assert(menu.select("Advanced Bracket", "Sequence")); sleep(1)
assert(menu.select("Advanced Bracket", "ISO shifting")); sleep(1)
assert(menu.select("Bulb Timer", "Exposure duration")); sleep(1)
assert(menu.select("Shoot Preferences", "Snap Simulation")); sleep(1)
assert(menu.select("Misc key settings", "Sticky HalfShutter")); sleep(1)
assert(menu.select("Play mode actions", "Trigger key(s)")); sleep(1)
assert(menu.select("LiveView zoom tweaks", "Zoom on HalfShutter")); sleep(1)
assert(menu.select("Lens info", "Lens ID")); sleep(1)
assert(menu.select("Shoot", "Intervalometer")); sleep(1)
-- selection in the Modified menu
-- note: we have already modified "Take a pic every"
-- so at least this setting should appear there
assert(menu.set("Shoot", "Intervalometer", "ON")); msleep(1000)
assert(menu.select("Modified", "Intervalometer")); msleep(1000) -- we should find Intervalometer in the Modified menu
assert(menu.select("Modified", "Take a pic every")); msleep(1000) -- same for "Take pic every" (smaller font)
assert(menu.select("Modified", "Intervalometer")); msleep(1000) -- scroll back to intervalometer
assert(menu.set("Shoot", "Intervalometer", "OFF")); msleep(1000) -- turn it off
assert(menu.select("Modified", "Take a pic every") == false); msleep(1000) -- now "Take a pic every" should disappear (submenus are only expanded when the main entry is on)
assert(menu.set("Shoot", "Intervalometer", "ON")); msleep(1000) -- re-enable intervalometer
assert(menu.select("Modified", "Take a pic every")); msleep(1000) -- now "Take a pic every" should re-appear
assert(menu.set("Intervalometer", "Take a pic every", 10)); msleep(1000) -- set "Take a pic every" back to default
assert(menu.select("Modified", "Intervalometer")); msleep(1000) -- scroll back to intervalometer
assert(menu.set("Shoot", "Intervalometer", "OFF")); msleep(1000) -- turn it off
assert(menu.select("Modified", "Take a pic every") == false); msleep(1000) -- now "Take a pic every" should disappear
assert(menu.select("Shoot", "Intervalometer")); msleep(1000) -- move back to Shoot -> Intervalometer
assert(menu.select("Intervalometer", "Take a pic every")); msleep(1000) -- set other intervalometer settings to default (just in case)
assert(menu.select("Intervalometer", "Start trigger")); msleep(1000) -- the menu.select calls are just for visual effect
assert(menu.set("Intervalometer", "Start trigger", "Leave Menu")); msleep(500);
assert(menu.select("Intervalometer", "Start after")); msleep(500)
assert(menu.set("Intervalometer", "Start after", "3s")); msleep(500);
assert(menu.select("Intervalometer", "Stop after")); msleep(500)
assert(menu.set("Intervalometer", "Stop after", "Disabled")); msleep(500);
assert(menu.select("Shoot", "Intervalometer")); msleep(1000)
assert(menu.select("Modified", "Intervalometer") == false); msleep(1000) -- now Modified -> Intervalometer should disappear
assert(menu.set("Shoot", "Intervalometer", "ON")); sleep(1)
assert(menu.select("Modified", "Intervalometer")); sleep(1) -- we should find Intervalometer in the Modified menu
assert(menu.select("Modified", "Take a pic every")); sleep(1) -- same for "Take pic every" (smaller font)
assert(menu.select("Modified", "Intervalometer")); sleep(1) -- scroll back to intervalometer
assert(menu.set("Shoot", "Intervalometer", "OFF")); sleep(1) -- turn it off
assert(menu.select("Modified", "Take a pic every") == false); sleep(1) -- now "Take a pic every" should disappear (submenus are only expanded when the main entry is on)
assert(menu.set("Shoot", "Intervalometer", "ON")); sleep(1) -- re-enable intervalometer
assert(menu.select("Modified", "Take a pic every")); sleep(1) -- now "Take a pic every" should re-appear
assert(menu.set("Intervalometer", "Take a pic every", 10)); sleep(1) -- set "Take a pic every" back to default
assert(menu.select("Modified", "Intervalometer")); sleep(1) -- scroll back to intervalometer
assert(menu.set("Shoot", "Intervalometer", "OFF")); sleep(1) -- turn it off
assert(menu.select("Modified", "Take a pic every") == false); sleep(1) -- now "Take a pic every" should disappear
assert(menu.select("Shoot", "Intervalometer")); sleep(1) -- move back to Shoot -> Intervalometer
assert(menu.select("Intervalometer", "Take a pic every")); sleep(1) -- set other intervalometer settings to default (just in case)
assert(menu.select("Intervalometer", "Start trigger")); sleep(1) -- the menu.select calls are just for visual effect
assert(menu.set("Intervalometer", "Start trigger", "Leave Menu")); sleep(0.5);
assert(menu.select("Intervalometer", "Start after")); sleep(0.5)
assert(menu.set("Intervalometer", "Start after", "3s")); sleep(0.5);
assert(menu.select("Intervalometer", "Stop after")); sleep(0.5)
assert(menu.set("Intervalometer", "Stop after", "Disabled")); sleep(0.5);
assert(menu.select("Shoot", "Intervalometer")); sleep(1)
assert(menu.select("Modified", "Intervalometer") == false); sleep(1) -- now Modified -> Intervalometer should disappear
-- non-existent menus; should fail
assert(menu.select("Dinosaur") == false)
......@@ -544,18 +544,18 @@ function test_keys()
for i = 1,10 do
-- open Canon menu
camera.gui.menu = true
msleep(1000)
sleep(1)
assert(camera.gui.menu == true)
assert(camera.gui.idle == false)
key.press(KEY.HALFSHUTTER)
msleep(200)
sleep(0.2)
assert(key.last == KEY.HALFSHUTTER)
msleep(1000)
sleep(1)
-- half-shutter should close Canon menu
assert(camera.gui.menu == false)
assert(camera.gui.idle == true)
key.press(KEY.UNPRESS_HALFSHUTTER)
msleep(200)
sleep(0.2)
assert(key.last == KEY.UNPRESS_HALFSHUTTER)
end
printf("Half-shutter test OK.\n")
......@@ -909,7 +909,7 @@ function test_camera_take_pics()
request_mode(MODE.M, "M")
camera.shutter = 1/50
msleep(2000)
sleep(2)
printf("Snap simulation test...\n")
assert(menu.set("Shoot Preferences", "Snap Simulation", 1))
......@@ -918,7 +918,7 @@ function test_camera_take_pics()
assert(dryos.shooting_card.file_number == initial_file_num)
assert(menu.set("Shoot Preferences", "Snap Simulation", 0))
msleep(2000)
sleep(2)
printf("Single picture...\n")
-- let's also check if we can find the image file
......@@ -947,7 +947,7 @@ function test_camera_take_pics()
assert(camera.gui.play_photo == true)
assert(camera.gui.play_movie == false)
msleep(2000)
sleep(2)
printf("Two burst pictures...\n")
printf("Ideally, the camera should be in some continuous shooting mode (not checked).\n")
......@@ -1011,7 +1011,7 @@ function test_camera_take_pics()
assert(size_cr2 or size_jpg)
end
msleep(2000)
sleep(2)
printf("Bulb picture...\n")
local t0 = dryos.ms_clock
......@@ -1038,7 +1038,7 @@ function test_camera_take_pics()
printf("Picture taking tests completed.\n")
printf("\n")
msleep(5000)
sleep(5)
end
function test_lv()
......@@ -1049,7 +1049,7 @@ function test_lv()
printf("LiveView is running; stopping...\n")
lv.stop()
assert(not lv.enabled, "LiveView did not stop")
msleep(2000)
sleep(2)
end
assert(not lv.enabled)
......@@ -1062,7 +1062,7 @@ function test_lv()
assert(lv.vidmode == "PH-LV")
assert(camera.gui.idle == true)
msleep(2000)
sleep(2)
-- lv.overlays status
local function print_overlays_status()
......@@ -1075,29 +1075,29 @@ function test_lv()
local old_gdr = menu.get("Overlay", "Global Draw")
for i=1,10 do
key.press(KEY.INFO)
msleep(200); print_overlays_status()
msleep(1000)
sleep(0.2); print_overlays_status()
sleep(1)
if lv.overlays ~= 1 then
-- Canon overlays disabled?
-- Enable ML overlays
assert(menu.set("Overlay", "Global Draw", "ON"))
msleep(200); print_overlays_status()
sleep(0.2); print_overlays_status()
assert(lv.overlays == 2)
msleep(1000)
sleep(1)
-- Disable ML overlays
assert(menu.set("Overlay", "Global Draw", "OFF"))
msleep(200); print_overlays_status()
sleep(0.2); print_overlays_status()
assert(lv.overlays == false)
msleep(1000)
sleep(1)
end
end
-- restore original Global Draw setting
assert(menu.set("Overlay", "Global Draw", old_gdr))
assert(menu.get("Overlay", "Global Draw") == old_gdr)
msleep(200); print_overlays_status()
sleep(0.2); print_overlays_status()
console.show(); assert(console.visible)
msleep(2000)
sleep(2)
for i,z in pairs{1, 5, 10, 5, 1, 10, 1} do
printf("Setting zoom to x%d...\n", z)
......@@ -1120,7 +1120,7 @@ function test_lv()
assert(not lens.autofocusing)
assert(lv.vidmode == "PAUSED-LV");
msleep(2000)
sleep(2)
printf("Resuming LiveView...\n")
lv.resume()
......@@ -1129,7 +1129,7 @@ function test_lv()
assert(not lens.autofocusing)
assert(lv.vidmode == "PH-LV");
msleep(2000)
sleep(2)
printf("Stopping LiveView...\n")
lv.stop()
......@@ -1140,7 +1140,7 @@ function test_lv()
assert(not lens.autofocusing)
assert(lv.vidmode == "PH-NOLV")
msleep(1000)
sleep(1)
printf("LiveView tests completed.\n")
printf("\n")
......@@ -1161,9 +1161,9 @@ function test_lens_focus()
printf("(or, remove the lens from the camera to skip this test)\n")
while not lens.af and lens.name ~= "" do
console.show(); assert(console.visible)
msleep(1000)
sleep(1)
end
msleep(1000)
sleep(1)
end
-- note: some lenses may be able to AF only in LiveView
......@@ -1209,7 +1209,7 @@ function test_lens_focus()
end
end
msleep(1000)
sleep(1)
printf("Focus distance: %s\n", lens.focus_distance)
-- note: focus direction is not consistent
......@@ -1218,7 +1218,7 @@ function test_lens_focus()
while lens.focus(-1,3,true) do end
-- note: focus distance and position may not be reported right away
msleep(500)
sleep(0.5)
printf("Focus distance: %s\n", lens.focus_distance)
printf("Focus motor position: %d\n", lens.focus_pos)
......@@ -1233,14 +1233,14 @@ function test_lens_focus()
steps_front = steps_front + 1
end
msleep(500)
sleep(0.5)
printf("\n")
printf("Focus distance: %s\n", lens.focus_distance)
printf("Focus motor position: %d\n", lens.focus_pos)
local focus_pos_1 = lens.focus_pos
msleep(500)
sleep(0.5)
printf("Focusing backward with step size %d, wait=%s...\n", step, wait)
local steps_back = 0
......@@ -1249,14 +1249,14 @@ function test_lens_focus()
steps_back = steps_back + 1
end
msleep(500)
sleep(0.5)
printf("\n")
printf("Focus distance: %s\n", lens.focus_distance)
printf("Focus motor position: %d\n", lens.focus_pos)
local focus_pos_2 = lens.focus_pos
msleep(500)
sleep(0.5)
local motor_steps_front = math.abs(focus_pos_1 - focus_pos_0)
local motor_steps_back = math.abs(focus_pos_2 - focus_pos_1)
......@@ -1264,7 +1264,7 @@ function test_lens_focus()
printf("Focus range: %s steps forward, %s steps backward. \n", steps_front, steps_back)
printf("Motor steps: %s forward, %s backward, %s lost. \n", motor_steps_front, motor_steps_back, motor_steps_lost)
msleep(500)
sleep(0.5)
end
end
printf("\nFocus test completed.\n")
......@@ -1311,7 +1311,7 @@ function test_movie()
console.hide(); assert(not console.visible)
movie.start()
assert(movie.recording)
msleep(1000)
sleep(1)
movie.stop()
assert(not movie.recording)
console.show(); assert(console.visible)
......@@ -1342,7 +1342,7 @@ function api_tests()
test_camera_gui()
test_menu()
test_camera_take_pics()
msleep(1000)
sleep(1)
test_multitasking()
test_keys()
test_lv()
......
......@@ -116,7 +116,7 @@ function main()
-- wait until menu redrawing finishes, then clear the screen once
-- during the game, we will use only incremental redraws
-- that don't require double buffering
msleep(500)
sleep(0.5)
display.clear()
game_speed = 3
......
......@@ -10,7 +10,7 @@
-- Otherwise, the script will keep running in background (just like modules).
--
-- Note: task support is extremely hackish and has many limitations.
-- For example, msleep does not allow other tasks from this script to run.
-- For example, (m)sleep does not allow other tasks from this script to run.
-- To allow multitasking within the same script, you must call task.yield()
-- however, only one task is allowed call task.yield(), as switching between tasks
-- will corrupt Lua internals.
......@@ -24,13 +24,13 @@
-- close ML menu and open the console
menu.close()
console.show()
msleep(500)
sleep(0.5)
io.write("Unload test...\n")
function my_task()
io.write("User task here.\n")
msleep(2000)
sleep(2)
io.write("User task exiting.\n")
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment