diff options
Diffstat (limited to 'apps/plugins/lua_scripts/fade2sleep.lua')
-rw-r--r-- | apps/plugins/lua_scripts/fade2sleep.lua | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/apps/plugins/lua_scripts/fade2sleep.lua b/apps/plugins/lua_scripts/fade2sleep.lua new file mode 100644 index 0000000000..03a4533591 --- /dev/null +++ b/apps/plugins/lua_scripts/fade2sleep.lua | |||
@@ -0,0 +1,107 @@ | |||
1 | --Bilgus 12-2016 | ||
2 | --revisited 8-2019 | ||
3 | require "actions" | ||
4 | require "buttons" | ||
5 | require "sound" | ||
6 | require "audio" | ||
7 | TIMEOUT = 0 | ||
8 | |||
9 | local SOUND_VOLUME = rb.sound_settings.SOUND_VOLUME | ||
10 | rb.sound_settings = nil | ||
11 | package.loaded["sound_defines"] = nil | ||
12 | |||
13 | function say_msg(message, timeout) | ||
14 | rb.splash(1, message) | ||
15 | rb.sleep(timeout * rb.HZ) | ||
16 | end | ||
17 | |||
18 | function say_value(value,message,timeout) | ||
19 | local message = string.format(message .. "%d", value) | ||
20 | say_msg(message, timeout) | ||
21 | end | ||
22 | |||
23 | function ShowMainMenu() -- we invoke this function every time we want to display the main menu of the script | ||
24 | local s = 0 | ||
25 | local mult = 1 | ||
26 | local unit = " Minutes" | ||
27 | |||
28 | |||
29 | while s == 0 or s == 5 do -- don't exit of program until user selects Exit | ||
30 | if mult < 1 then | ||
31 | mult = 1 | ||
32 | s = 0 | ||
33 | end | ||
34 | mainmenu = {"More", mult * 1 .. unit, mult * 5 .. unit, mult * 10 .. unit, mult * 15 .. unit, "Less", "Exit"} -- define the items of the menu | ||
35 | s = rb.do_menu("Reduce volume + sleep over", mainmenu, s, false) -- actually tell Rockbox to draw the menu | ||
36 | |||
37 | -- In the line above: "Test" is the title of the menu, mainmenu is an array with the items | ||
38 | -- of the menu, nil is a null value that needs to be there, and the last parameter is | ||
39 | -- whether the theme should be drawn on the menu or not. | ||
40 | -- the variable s will hold the index of the selected item on the menu. | ||
41 | -- the index is zero based. This means that the first item is 0, the second one is 1, etc. | ||
42 | if s == 0 then mult = mult + 1 | ||
43 | elseif s == 1 then TIMEOUT = mult | ||
44 | elseif s == 2 then TIMEOUT = mult * 5 | ||
45 | elseif s == 3 then TIMEOUT = mult * 10 | ||
46 | elseif s == 4 then TIMEOUT = mult * 15 | ||
47 | elseif s == 5 then mult = mult - 1 -- User selected to exit | ||
48 | elseif s == 6 then os.exit() -- User selected to exit | ||
49 | elseif s == -2 then os.exit() -- -2 index is returned from do_menu() when user presses the key to exit the menu (on iPods, it's the left key). | ||
50 | -- In this case, user probably wants to exit (or go back to last menu). | ||
51 | else rb.splash(2 * rb.HZ, "Error! Selected index: " .. s) -- something strange happened. The program shows this message when | ||
52 | -- the selected item is not on the index from 0 to 3 (in this case), and displays | ||
53 | -- the selected index. Having this type of error handling is not | ||
54 | -- required, but it might be nice to have Especially while you're still | ||
55 | -- developing the plugin. | ||
56 | end | ||
57 | end | ||
58 | end | ||
59 | |||
60 | ShowMainMenu() | ||
61 | rb.set_sleeptimer_duration(TIMEOUT) | ||
62 | rb.lcd_clear_display() | ||
63 | rb.lcd_update() | ||
64 | |||
65 | local volume = rb.sound_current(SOUND_VOLUME) | ||
66 | local vol_min = rb.sound_min(SOUND_VOLUME) | ||
67 | local volsteps = -(vol_min - volume) | ||
68 | local seconds = (TIMEOUT * 60) / volsteps | ||
69 | local sec_left = (TIMEOUT * 60) | ||
70 | local hb = 0 | ||
71 | local action = rb.get_action(rb.contexts.CONTEXT_STD, 0) | ||
72 | if rb.audio_status() == 1 then | ||
73 | while ((volume > vol_min) and (action ~= rb.actions.ACTION_STD_CANCEL)) do | ||
74 | rb.lcd_clear_display() | ||
75 | say_value(volume,sec_left .. " Sec, Volume: ", 1) | ||
76 | local i = seconds * 2 | ||
77 | while ((i > 0) and (action ~= rb.actions.ACTION_STD_CANCEL)) do | ||
78 | i = i - 1 | ||
79 | rb.lcd_drawline(hb, 1, hb, 1) | ||
80 | rb.lcd_update() | ||
81 | if hb >= rb.LCD_WIDTH then | ||
82 | hb = 0 | ||
83 | rb.lcd_clear_display() | ||
84 | say_value(volume,sec_left .. " Sec, Volume: ", 1) | ||
85 | end | ||
86 | hb = hb + 1 | ||
87 | rb.sleep(rb.HZ / 2) | ||
88 | action = rb.get_action(rb.contexts.CONTEXT_STD, 0) | ||
89 | rb.yield() | ||
90 | end | ||
91 | volume = volume - 1 | ||
92 | rb.sound_set(SOUND_VOLUME, volume); | ||
93 | sec_left = sec_left - seconds | ||
94 | |||
95 | end | ||
96 | rb.audio_stop() | ||
97 | rb.lcd_clear_display() | ||
98 | rb.lcd_update() | ||
99 | |||
100 | os.exit(1, "Playback Stopped") | ||
101 | |||
102 | else | ||
103 | rb.lcd_clear_display() | ||
104 | rb.lcd_update() | ||
105 | |||
106 | os.exit(2, "Nothing is playing") | ||
107 | end | ||