1+ -- AccelBacklight.lua
2+
3+ -- Helper functions to run when var changed in form
4+ ---- ----------------------------------------------
5+ local function accelAngleChanged (value )
6+ accelAngle = value
7+ system .pSave (" accelAngle" ,accelAngle )
8+ end
9+
10+ local function idleModeChanged (value )
11+ local switchCaseTable = {
12+ [1 ] = 0 ,
13+ [2 ] = 1 ,
14+ [3 ] = 2 ,
15+ [4 ] = 3
16+ }
17+ idleMode = switchCaseTable [value ]
18+ system .pSave (" idleMode" ,idleMode )
19+ end
20+
21+ local function ActiveModeChanged (value )
22+ local switchCaseTable = {
23+ [1 ] = 0 ,
24+ [2 ] = 1 ,
25+ [3 ] = 2 ,
26+ [4 ] = 3
27+ }
28+ activeMode = switchCaseTable [value ]
29+ system .pSave (" activeMode" ,activeMode )
30+ end
31+
32+ local function printToCmdChanged (value )
33+ printToCmd = value
34+ system .pSave (" printToCmd" ,printToCmd )
35+ end
36+ ---- ----------------------------------------------
37+
38+ -- Form to set vars
39+ local function initForm ()
40+ local backlightModeSwitchCaseTable = {
41+ [0 ] = 1 ,
42+ [1 ] = 2 ,
43+ [2 ] = 3 ,
44+ [3 ] = 4
45+ }
46+
47+ form .addRow (2 )
48+ form .addLabel ({label = " Activation Angle" })
49+ form .addIntbox (accelAngle , - 90 , 90 , 40 , 0 , 5 , accelAngleChanged )
50+
51+ form .addRow (2 )
52+ form .addLabel ({label = " Idle Backlight Mode" })
53+ form .addSelectbox ({" Always Off" , " 10s" , " 60s" , " Always On" }, backlightModeSwitchCaseTable [idleMode ], true , idleModeChanged , {})
54+
55+ form .addRow (2 )
56+ form .addLabel ({label = " Active Backlight Mode" })
57+ form .addSelectbox ({" Always Off" , " 10s" , " 60s" , " Always On" }, backlightModeSwitchCaseTable [activeMode ], true , ActiveModeChanged , {})
58+
59+ form .addRow (2 )
60+ form .addLabel ({label = " Print to Console" })
61+ form .addSelectbox ({" No" , " Yes" }, printToCmd , true , printToCmdChanged , {})
62+
63+ collectgarbage ()
64+ end
65+
66+ -- Helper function to print only when printToCmd is set to 1
67+ local function printCmd (text )
68+ if (printToCmd == 2 ) then
69+ print (text )
70+ end
71+ end
72+
73+ -- Loop function is called in regular intervals
74+ local function loop ()
75+ if (system .getIMU ()[" p" ] > accelAngle ) then
76+ if (lastMode == activeMode ) then
77+ return
78+ end
79+ dt = system .getDateTime ()
80+ printCmd (" Turning ON backlight - " .. (string.format (" Time: %d-%02d-%02d, %d:%02d:%02d" , dt .year , dt .mon , dt .day , dt .hour , dt .min , dt .sec )))
81+ system .setProperty (" BacklightMode" , activeMode )
82+ lastMode = activeMode
83+ else
84+ if (lastMode == idleMode ) then
85+ return
86+ end
87+ dt = system .getDateTime ()
88+ printCmd (" Turning OFF backlight - " .. (string.format (" Time: %d-%02d-%02d, %d:%02d:%02d" , dt .year , dt .mon , dt .day , dt .hour , dt .min , dt .sec )))
89+ system .setProperty (" BacklightMode" , idleMode )
90+ lastMode = idleMode
91+ end
92+ end
93+
94+
95+ -- Application initialization.
96+ local function init (code )
97+ accelAngle = system .pLoad (" accelAngle" )
98+ if (accelAngle == nil ) then
99+ accelAngle = 40
100+ system .pSave (" accelAngle" , accelAngle )
101+ end
102+
103+ idleMode = system .pLoad (" idleMode" )
104+ if (idleMode == nil ) then
105+ idleMode = 0
106+ system .pSave (" idleMode" , idleMode )
107+ end
108+
109+ activeMode = system .pLoad (" activeMode" )
110+ if (activeMode == nil ) then
111+ activeMode = 3
112+ system .pSave (" activeMode" , activeMode )
113+ end
114+
115+ printToCmd = system .pLoad (" printToCmd" )
116+ if (printToCmd == nil ) then
117+ printToCmd = 1
118+ system .pSave (" printToCmd" , printToCmd )
119+ end
120+
121+ lastMode = system .getProperty (" BacklightMode" )
122+
123+ system .registerForm (1 , MENU_APPS , " Accel. Backlight Config" , initForm , nil , printForm )
124+
125+ dt = system .getDateTime ()
126+ print (" Application initialized - " .. (string.format (" Time: %d-%02d-%02d, %d:%02d:%02d" , dt .year , dt .mon , dt .day , dt .hour , dt .min , dt .sec )))
127+ collectgarbage ()
128+ end
129+
130+ -- Application interface
131+ return {init = init , loop = loop , author = " matm616" , version = " 1.0" , name = " AccelBacklight" }
0 commit comments