@@ -112,12 +112,12 @@ function KoboPowerD:init()
112
112
self .initial_is_fl_on = true
113
113
self .autowarmth_job_running = false
114
114
115
- if self .device . hasFrontlight () then
115
+ if self .device : hasFrontlight () then
116
116
-- If this device has natural light (currently only KA1 & Forma)
117
117
-- Use the SysFS interface, and ioctl otherwise.
118
118
-- NOTE: On the Forma, nickel still appears to prefer using ntx_io to handle the FL,
119
119
-- but it does use sysfs for the NL...
120
- if self .device . hasNaturalLight () then
120
+ if self .device : hasNaturalLight () then
121
121
local nl_config = G_reader_settings :readSetting (" natural_light_config" )
122
122
if nl_config then
123
123
for key ,val in pairs (nl_config ) do
@@ -138,11 +138,20 @@ function KoboPowerD:init()
138
138
self :_syncKoboLightOnStart ()
139
139
end
140
140
end
141
+ -- See discussion in https://github.com/koreader/koreader/issues/3118#issuecomment-334995879
142
+ -- for the reasoning behind this bit of insanity.
143
+ if self :isFrontlightOnHW () then
144
+ -- Use setIntensity to ensure it sets fl_intensity, and because we don't want the ramping behavior of turnOn
145
+ self :setIntensity (self :frontlightIntensityHW ())
146
+ else
147
+ -- Use setIntensityHW so as *NOT* to set fl_intensity, so toggle will still work.
148
+ self :setIntensityHW (0 )
149
+ end
141
150
end
142
151
end
143
152
144
153
function KoboPowerD :saveSettings ()
145
- if self .device . hasFrontlight () then
154
+ if self .device : hasFrontlight () then
146
155
-- Store BasePowerD values into settings (and not our hw_intensity, so
147
156
-- that if frontlight was toggled off, we save and restore the previous
148
157
-- untoggled intensity and toggle state at next startup)
@@ -201,10 +210,6 @@ function KoboPowerD:isFrontlightOnHW()
201
210
return self .hw_intensity > 0
202
211
end
203
212
204
- function KoboPowerD :turnOffFrontlightHW ()
205
- self :_setIntensity (0 ) -- will call setIntensityHW(0)
206
- end
207
-
208
213
function KoboPowerD :setIntensityHW (intensity )
209
214
if self .fl == nil then return end
210
215
if self .fl_warmth == nil then
@@ -271,25 +276,52 @@ function KoboPowerD:isChargingHW()
271
276
return self :read_str_file (self .is_charging_file ) == " Charging\n "
272
277
end
273
278
279
+ function KoboPowerD :turnOffFrontlightHW ()
280
+ if self :isFrontlightOff () then
281
+ return
282
+ end
283
+ local util = require (" ffi/util" )
284
+ util .runInSubProcess (function ()
285
+ for i = 1 ,5 do
286
+ self :_setIntensity (math.floor (self .fl_intensity - ((self .fl_intensity / 5 ) * i )))
287
+ -- NOTE: We generally don't need to sleep when using sysfs as a backend...
288
+ if not self .device :hasNaturalLight () then
289
+ if (i < 5 ) then
290
+ util .usleep (35 * 1000 )
291
+ end
292
+ end
293
+ end
294
+ end , false , true )
295
+ end
296
+ function KoboPowerD :turnOnFrontlightHW ()
297
+ if self :isFrontlightOn () then
298
+ return
299
+ end
300
+ local util = require (" ffi/util" )
301
+ util .runInSubProcess (function ()
302
+ for i = 1 ,5 do
303
+ self :_setIntensity (math.ceil (self .fl_min + ((self .fl_intensity / 5 ) * i )))
304
+ if not self .device :hasNaturalLight () then
305
+ if (i < 5 ) then
306
+ util .usleep (35 * 1000 )
307
+ end
308
+ end
309
+ end
310
+ end , false , true )
311
+ end
312
+
274
313
-- Turn off front light before suspend.
275
314
function KoboPowerD :beforeSuspend ()
276
315
if self .fl == nil then return end
277
- -- just turn off frontlight without remembering its state
278
- self . fl : setBrightness ( 0 )
316
+ -- Turn off the frontlight
317
+ self : turnOffFrontlight ( )
279
318
end
280
319
281
320
-- Restore front light state after resume.
282
321
function KoboPowerD :afterResume ()
283
322
if self .fl == nil then return end
284
- -- just re-set it to self.hw_intensity that we haven't change on Suspend
285
- if self .fl_warmth == nil then
286
- self .fl :setBrightness (self .hw_intensity )
287
- else
288
- if self .auto_warmth then
289
- self :calculateAutoWarmth ()
290
- end
291
- self .fl :setNaturalBrightness (self .hw_intensity , self .fl_warmth )
292
- end
323
+ -- Turn the frontlight back on
324
+ self :turnOnFrontlight ()
293
325
end
294
326
295
327
return KoboPowerD
0 commit comments