Skip to content

Commit 32eb838

Browse files
committed
feat(display.go): move tickers into their own method
This allows them to only be started if necessary. If the user has chosen to keep the display on and not-dimmed all the time, the tickers can't start as their tick value must be a positive integer.
1 parent 18ae51f commit 32eb838

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

display.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,42 @@ func watchTsEvents() {
193193
}
194194
}
195195

196+
// startBacklightTickers starts the two tickers for dimming and switching off the display
197+
// if they're not already set. This is done separately to the init routine as the "never dim"
198+
// option has the value set to zero, but time.NewTicker only accept positive values.
199+
func startBacklightTickers() {
200+
LoadConfig()
201+
if dim_ticker == nil && config.DisplayDimAfterSec != 0 {
202+
fmt.Printf("display: dim_ticker has started.")
203+
dim_ticker = time.NewTicker(time.Duration(config.DisplayDimAfterSec) * time.Second)
204+
defer dim_ticker.Stop()
205+
206+
go func() {
207+
for {
208+
select {
209+
case <-dim_ticker.C:
210+
tick_displayDim()
211+
}
212+
}
213+
}()
214+
}
215+
216+
if off_ticker == nil && config.DisplayOffAfterSec != 0 {
217+
fmt.Printf("display: off_ticker has started.")
218+
off_ticker = time.NewTicker(time.Duration(config.DisplayOffAfterSec) * time.Second)
219+
defer off_ticker.Stop()
220+
221+
go func() {
222+
for {
223+
select {
224+
case <-off_ticker.C:
225+
tick_displayOff()
226+
}
227+
}
228+
}()
229+
}
230+
}
231+
196232
func init() {
197233
go func() {
198234
waitCtrlClientConnected()
@@ -205,24 +241,7 @@ func init() {
205241
requestDisplayUpdate()
206242
}()
207243

208-
go func() {
209-
LoadConfig()
210-
// Start display auto-sleeping tickers
211-
dim_ticker = time.NewTicker(time.Duration(config.DisplayDimAfterMs) * time.Millisecond)
212-
defer dim_ticker.Stop()
213-
214-
off_ticker = time.NewTicker(time.Duration(config.DisplayOffAfterMs) * time.Millisecond)
215-
defer off_ticker.Stop()
216-
217-
for {
218-
select {
219-
case <-dim_ticker.C:
220-
tick_displayDim()
221-
case <-off_ticker.C:
222-
tick_displayOff()
223-
}
224-
}
225-
}()
244+
startBacklightTickers()
226245

227246
go watchTsEvents()
228247
}

0 commit comments

Comments
 (0)