summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/main.go b/main.go
index 60de258..69e2f58 100644
--- a/main.go
+++ b/main.go
@@ -27,7 +27,13 @@ type Brightness struct {
ToggledValue int
}
+type Input struct {
+ Lock bool
+ Value string
+}
+
var currentBrightness Brightness
+var currentInput Input
func update() {
if currentBrightness.Lock == false {
@@ -79,6 +85,49 @@ func ddcutilSetBrightness(brightness Brightness) {
}
}
+func ddcutilSetInput(input Input) {
+ if currentInput.Lock == false {
+ currentInput.Lock = true
+ cmd := exec.Command("ddcutil", "setvcp", "xF4",
+ input.Value,
+ "--i2c-source-addr=x50",
+ "--noverify")
+ log.Println(cmd.String())
+ _, err := cmd.CombinedOutput()
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ currentInput.Value = input.Value
+ currentInput.Lock = false
+ } else {
+ log.Println("Active lock, no operation performed")
+ return
+ }
+}
+
+func setInput(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "POST" {
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ var input Input
+ json.Unmarshal([]byte(body), &input)
+ newValue := Input{Value: input.Value}
+ if newValue.Value == currentInput.Value {
+ log.Println("Input is the same")
+ } else {
+ ddcutilSetInput(newValue)
+ log.Println("Current input is", currentInput.Value)
+ }
+ } else {
+ log.Println("Active lock, no operation performed")
+ return
+ }
+}
+
func set(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := io.ReadAll(r.Body)
@@ -172,5 +221,6 @@ func main() {
http.HandleFunc("/decrease", decrease)
http.HandleFunc("/increase", increase)
http.HandleFunc("/toggle", toggle)
+ http.HandleFunc("/set-input", setInput)
log.Fatal(http.ListenAndServe(configuration.Listen, nil))
}