-
Notifications
You must be signed in to change notification settings - Fork 1
/
x2webdimming.go
95 lines (83 loc) · 2.34 KB
/
x2webdimming.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"gopkg.in/ini.v1"
)
// X2 pro web series
const lcdBrightness = "/sys/class/backlight/backlight_bempc/brightness"
// X2 extreme web series (lvds)
//const lcdBrightness = "/sys/class/backlight/backlight_lvds/brightness"
//const lcdActualBrightness = "/sys/class/backlight/backlight_bempc/actual_brightness"
const configFile = "/etc/beijer/misc.conf"
const maxBrightness = 100
func changeIniFileBrightness(value int) error {
cfg, err := ini.Load(configFile)
if err != nil {
fmt.Println("Failed to read config file.")
return err
}
fmt.Println("Ini file Brightness: ", cfg.Section("General").Key("brightness"))
fmt.Println("Storing new value ", value)
cfg.Section("General").Key("brightness").SetValue(strconv.Itoa(value))
cfg.SaveTo(configFile)
return nil
}
func setBrightness(value string) error {
brightness, err := strconv.Atoi(value)
if err != nil {
fmt.Println("Failed to convert to int")
return err
}
if brightness > maxBrightness {
brightness = maxBrightness
}
if brightness < 0 {
brightness = 0
}
err = ioutil.WriteFile(lcdBrightness, []byte(strconv.Itoa(brightness)), os.ModeExclusive)
if err != nil {
fmt.Println("Failed to set brightness", err.Error())
return err
}
fmt.Println("Did set new brightness, value ", brightness)
err = changeIniFileBrightness(brightness)
if err != nil {
fmt.Println("Failed to store to ini file")
return err
}
return nil
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from %s", r.URL.Path[1:])
}
func brightnessHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Backlight brightness handler")
fmt.Println("Request URL", r.URL.Query())
brightness := r.URL.Query().Get("brightness")
if brightness != "" {
fmt.Printf("New brightness level: %s\n", brightness)
setBrightness(brightness)
}
}
func rebootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Reboot handler")
cmd := exec.Command("reboot")
err := cmd.Run()
if err != nil {
fmt.Println("Failed to reboot device...")
}
fmt.Fprintf(w, "Rebooting device. Bye!")
}
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/backlightsvc", brightnessHandler)
http.HandleFunc("/reboot", rebootHandler)
fmt.Println("Starting server...")
log.Fatal(http.ListenAndServe(":8080", nil))
}