From 3271a1796b58f0e85c8e278e332de0bdda2d9cd2 Mon Sep 17 00:00:00 2001 From: Cameron Fleming Date: Fri, 3 Jan 2025 11:23:32 +0000 Subject: [PATCH] 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. --- display.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/display.go b/display.go index f312eb6..607e764 100644 --- a/display.go +++ b/display.go @@ -4,6 +4,9 @@ import ( "fmt" "log" "time" + "os" + "errors" + "strconv" ) var currentScreen = "ui_Boot_Screen" @@ -83,6 +86,29 @@ func updateStaticContents() { updateLabelIfChanged("ui_Status_Content_Device_Id_Content_Label", GetDeviceID()) } +// setDisplayBrightness sets /sys/class/backlight/backlight/brightness to alter +// the backlight brightness of the JetKVM hardware's display. +func setDisplayBrightness(brightness int) (error) { + if brightness > 100 || brightness < 0 { + return errors.New("brightness value out of bounds, must be between 0 and 100") + } + + // Check the display backlight class is available + if _, err := os.Stat("/sys/class/backlight/backlight/brightness"); errors.Is(err, os.ErrNotExist) { + return errors.New("brightness value cannot be set, possibly not running on JetKVM hardware.") + } + + // Set the value + bs := []byte(strconv.Itoa(brightness)) + err := os.WriteFile("/sys/class/backlight/backlight/brightness", bs, 0644) + if err != nil { + return err + } + + fmt.Print("display: set brightness to %v", brightness) + return nil +} + func init() { go func() { waitCtrlClientConnected()