Skip to content

Commit 6a7f9ce

Browse files
committed
feat(rpc): implement display backlight control methods
1 parent f79da4c commit 6a7f9ce

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

jsonrpc.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,14 @@ func rpcTryUpdate() error {
172172
return nil
173173
}
174174

175-
func rpcSetBacklightSettings(data *BacklightSettings) error {
175+
func rpcSetBacklightSettings(params BacklightSettings) error {
176176
LoadConfig()
177177

178-
blConfig := *data
178+
blConfig := params
179179

180-
if blConfig.MaxBrightness > 100 || blConfig.MaxBrightness < 0 {
181-
return fmt.Errorf("maxBrightness must be between 0 and 100")
180+
// NOTE: by default, the frontend limits the brightness to 64, as that's what the device originally shipped with.
181+
if blConfig.MaxBrightness > 255 || blConfig.MaxBrightness < 0 {
182+
return fmt.Errorf("maxBrightness must be between 0 and 255")
182183
}
183184

184185
if blConfig.DimAfter < 0 {
@@ -190,12 +191,24 @@ func rpcSetBacklightSettings(data *BacklightSettings) error {
190191
}
191192

192193
config.DisplayMaxBrightness = blConfig.MaxBrightness
193-
config.DisplayDimAfterMs = int64(blConfig.DimAfter)
194-
config.DisplayOffAfterMs = int64(blConfig.OffAfter)
194+
config.DisplayDimAfterSec = blConfig.DimAfter
195+
config.DisplayOffAfterSec = blConfig.OffAfter
195196

196197
if err := SaveConfig(); err != nil {
197198
return fmt.Errorf("failed to save config: %w", err)
198199
}
200+
201+
log.Printf("rpc: display: settings applied, max_brightness: %d, dim after: %ds, off after: %ds", config.DisplayMaxBrightness, config.DisplayDimAfterSec, config.DisplayOffAfterSec)
202+
203+
// If the device started up with auto-dim and/or auto-off set to zero, the display init
204+
// method will not have started the tickers. So in case that has changed, attempt to start the tickers now.
205+
startBacklightTickers()
206+
207+
// Wake the display after the settings are altered, this ensures the tickers
208+
// are reset to the new settings, and will bring the display up to maxBrightness.
209+
// Calling with force set to true, to ignore the current state of the display, and force
210+
// it to reset the tickers.
211+
wakeDisplay(true)
199212
return nil
200213
}
201214

@@ -204,8 +217,8 @@ func rpcGetBacklightSettings() (*BacklightSettings, error) {
204217

205218
return &BacklightSettings{
206219
MaxBrightness: config.DisplayMaxBrightness,
207-
DimAfter: int(config.DisplayDimAfterMs),
208-
OffAfter: int(config.DisplayOffAfterMs),
220+
DimAfter: int(config.DisplayDimAfterSec),
221+
OffAfter: int(config.DisplayOffAfterSec),
209222
}, nil
210223
}
211224

@@ -448,6 +461,6 @@ var rpcHandlers = map[string]*jsonrpc.RPCHandler{
448461
"pluginList": {Func: plugin.RpcPluginList},
449462
"pluginUpdateConfig": {Func: plugin.RpcPluginUpdateConfig, Params: []string{"name", "enabled"}},
450463
"pluginUninstall": {Func: plugin.RpcPluginUninstall, Params: []string{"name"}},
451-
"setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"settings"}},
464+
"setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"params"}},
452465
"getBacklightSettings": {Func: rpcGetBacklightSettings},
453466
}

0 commit comments

Comments
 (0)