mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-01-18 03:28:19 +01:00
feat(display.go): Add display brightness settings (#17)
* feat(display.go): impl setDisplayBrightness() Implements setDisplayBrightness(brightness int) which allows setting the backlight brightness on JetKVM's hardware. Needs to be implemented into the RPC, config and frontend. * feat(config): add backlight control settings * feat(display): add automatic dimming & switch off to display WIP, dims the display to 50% of the BacklightMaxBrightness after BacklightDimAfterMS expires. Turns the display off after BacklightOffAfterMS * feat(rpc): add methods to get and set BacklightSettings * WIP: feat(settings): add Max backlight setting * chore: use constant for backlight control file * fix: only attempt to wake the display if it's off * feat(display): wake on touch * fix: re-use buffer between reads * fix: wakeDisplay() on start to fix warm start issue If the application had turned off the display before exiting, it wouldn't be brought on when the application restarted without a device reboot. * chore: various comment & string updates * fix: newline on set brightness log Noticed by @eric https://github.com/jetkvm/kvm/pull/17#discussion_r1903338705 * fix: set default value for display Set the DisplayMaxBrightness to the default brightness used out-of-the-box by JetKVM. Also sets the dim/timeout to 2 minutes and 30 mintes respectively. * feat(display.go): use tickers to countdown to dim/off As suggested by tutman in https://github.com/jetkvm/kvm/pull/17, use tickers set to the duration of dim/off to avoid a loop running every second. The tickers are reset to the dim/off times whenever wakeDisplay() is called. * chore: update config Changed Dim & Off values to seconds instead of milliseconds, there's no need for it to be that precise. * feat(display.go): wakeDisplay() force Adds the force boolean to wakedisplay() which allows skipping the backlightState == 0 check, this means you can force a ticker reset, even if the display is currently in the "full bright" state * 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. * feat(display.go): stop tickers when auto-dim/auto-off is disabled * feat(rpc): implement display backlight control methods * feat(ui): implement display backlight control * chore: update variable names As part of @joshuasing's review on #17, updated variables & constants to match the Go best practices. Signed-off-by: Cameron Fleming <cameron@nevexo.space> * fix(display): move backlightTicker setup into screen setup goroutine Signed-off-by: Cameron Fleming <cameron@nevexo.space> * chore: fix some start-up timing issues * fix(display): Don't attempt to start the tickers if the display is disabled If max_brightness is zero, then there's no point in trying to dim it (or turn it off...) * fix: wakeDisplay() doesn't need to stop the tickers The tickers only need to be reset, if they're disabled, they won't have been started. * fix: Don't wake up the display if it's turned off --------- Signed-off-by: Cameron Fleming <cameron@nevexo.space>
This commit is contained in:
60
jsonrpc.go
60
jsonrpc.go
@@ -34,6 +34,12 @@ type JSONRPCEvent struct {
|
||||
Params interface{} `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
type BacklightSettings struct {
|
||||
MaxBrightness int `json:"max_brightness"`
|
||||
DimAfter int `json:"dim_after"`
|
||||
OffAfter int `json:"off_after"`
|
||||
}
|
||||
|
||||
func writeJSONRPCResponse(response JSONRPCResponse, session *Session) {
|
||||
responseBytes, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
@@ -225,6 +231,56 @@ func rpcTryUpdate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func rpcSetBacklightSettings(params BacklightSettings) error {
|
||||
LoadConfig()
|
||||
|
||||
blConfig := params
|
||||
|
||||
// NOTE: by default, the frontend limits the brightness to 64, as that's what the device originally shipped with.
|
||||
if blConfig.MaxBrightness > 255 || blConfig.MaxBrightness < 0 {
|
||||
return fmt.Errorf("maxBrightness must be between 0 and 255")
|
||||
}
|
||||
|
||||
if blConfig.DimAfter < 0 {
|
||||
return fmt.Errorf("dimAfter must be a positive integer")
|
||||
}
|
||||
|
||||
if blConfig.OffAfter < 0 {
|
||||
return fmt.Errorf("offAfter must be a positive integer")
|
||||
}
|
||||
|
||||
config.DisplayMaxBrightness = blConfig.MaxBrightness
|
||||
config.DisplayDimAfterSec = blConfig.DimAfter
|
||||
config.DisplayOffAfterSec = blConfig.OffAfter
|
||||
|
||||
if err := SaveConfig(); err != nil {
|
||||
return fmt.Errorf("failed to save config: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("rpc: display: settings applied, max_brightness: %d, dim after: %ds, off after: %ds", config.DisplayMaxBrightness, config.DisplayDimAfterSec, config.DisplayOffAfterSec)
|
||||
|
||||
// If the device started up with auto-dim and/or auto-off set to zero, the display init
|
||||
// method will not have started the tickers. So in case that has changed, attempt to start the tickers now.
|
||||
startBacklightTickers()
|
||||
|
||||
// Wake the display after the settings are altered, this ensures the tickers
|
||||
// are reset to the new settings, and will bring the display up to maxBrightness.
|
||||
// Calling with force set to true, to ignore the current state of the display, and force
|
||||
// it to reset the tickers.
|
||||
wakeDisplay(true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func rpcGetBacklightSettings() (*BacklightSettings, error) {
|
||||
LoadConfig()
|
||||
|
||||
return &BacklightSettings{
|
||||
MaxBrightness: config.DisplayMaxBrightness,
|
||||
DimAfter: int(config.DisplayDimAfterSec),
|
||||
OffAfter: int(config.DisplayOffAfterSec),
|
||||
}, nil
|
||||
}
|
||||
|
||||
const (
|
||||
devModeFile = "/userdata/jetkvm/devmode.enable"
|
||||
sshKeyDir = "/userdata/dropbear/.ssh"
|
||||
@@ -385,7 +441,7 @@ func callRPCHandler(handler RPCHandler, params map[string]interface{}) (interfac
|
||||
}
|
||||
args[i] = reflect.ValueOf(newStruct).Elem()
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid parameter type for: %s", paramName)
|
||||
return nil, fmt.Errorf("invalid parameter type for: %s, type: %s", paramName, paramType.Kind())
|
||||
}
|
||||
} else {
|
||||
args[i] = convertedValue.Convert(paramType)
|
||||
@@ -560,4 +616,6 @@ var rpcHandlers = map[string]RPCHandler{
|
||||
"getWakeOnLanDevices": {Func: rpcGetWakeOnLanDevices},
|
||||
"setWakeOnLanDevices": {Func: rpcSetWakeOnLanDevices, Params: []string{"params"}},
|
||||
"resetConfig": {Func: rpcResetConfig},
|
||||
"setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"params"}},
|
||||
"getBacklightSettings": {Func: rpcGetBacklightSettings},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user