diff --git a/.drone.yml b/.drone.yml index d842f26..d228748 100644 --- a/.drone.yml +++ b/.drone.yml @@ -31,6 +31,7 @@ steps: - go build -o out/inotify cmd/inotify/inotify.go - go build -o out/gui cmd/gui/gui.go - go build -o out/fan cmd/fan/fan.go + - go build -o out/hdmi cmd/hdmi/hdmi.go - cp CHANGELOG.md out/ - name: gitea-release image: plugins/gitea-release diff --git a/cmd/hdmi/README.md b/cmd/hdmi/README.md new file mode 100644 index 0000000..bf7d0ff --- /dev/null +++ b/cmd/hdmi/README.md @@ -0,0 +1,3 @@ +# hdmi + +This is the source for the screen on/off tool. systemd timers are great but if the system auto-reboots overnight for update purposes the screen will remain on post-boot. This tool will ensure the screen turns back off if a PiFrame is rebooted during off hours. diff --git a/cmd/hdmi/hdmi.go b/cmd/hdmi/hdmi.go new file mode 100644 index 0000000..22a842c --- /dev/null +++ b/cmd/hdmi/hdmi.go @@ -0,0 +1,91 @@ +package main + +import ( + "log" + "strconv" + "strings" + "time" + "os/exec" + + "git.kemonine.info/PiFrame/config" +) + +const ( + CMD_VCGENCMD = "/opt/vc/bin//vcgencmd" + CMD_VCGENCMD_DISPLAY_POWER = "display_power" +) + +func main() { + // Load the config file + pfConfig, _ := config.LoadConfig() + pfConfig.Print() + + // Read config values + hdmiOn := pfConfig.String(config.CONFIG_KEY_HDMI_ON) + hdmiOff := pfConfig.String(config.CONFIG_KEY_HDMI_OFF) + + // Strip off systemd stuff we don't care about for this purpose + hdmiOn = strings.TrimLeft(hdmiOn, "*-*-* ") + hdmiOnSplit := strings.Split(hdmiOn, ":") + + // Split the config into hours/minutes/seconds + hdmiOff = strings.TrimLeft(hdmiOff, "*-*-* ") + hdmiOffSplit := strings.Split(hdmiOff, ":") + + log.Print(hdmiOnSplit) + log.Print(hdmiOffSplit) + + // Parse hdmi on hours/minutes/seconds into ints so they can be used to create real date/time + hdmiOnHour, err := strconv.Atoi(hdmiOnSplit[0]) + if err != nil { + log.Fatalf("Could not parse hdmi on hour : %s", err) + } + hdmiOnMinute, err := strconv.Atoi(hdmiOnSplit[1]) + if err != nil { + log.Fatalf("Could not parse hdmi on minute : %s", err) + } + hdmiOnSecond, err := strconv.Atoi(hdmiOnSplit[2]) + if err != nil { + log.Fatalf("Could not parse htmi on second : %s", err) + } + + // Parse hdmi off hours/minutes/seconds into ints so they can be used to create real date/time + hdmiOffHour, err := strconv.Atoi(hdmiOffSplit[0]) + if err != nil { + log.Fatalf("Could not parse hdmi off hour : %s", err) + } + hdmiOffMinute, err := strconv.Atoi(hdmiOffSplit[1]) + if err != nil { + log.Fatalf("Could not parse hdmi off minute : %s", err) + } + hdmiOffSecond, err := strconv.Atoi(hdmiOffSplit[2]) + if err != nil { + log.Fatalf("Could not parse hdmi off second : %s", err) + } + + // Setup date/time vars for comparison + currentTime := time.Now() + hdmiOnTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), hdmiOnHour, hdmiOnMinute, hdmiOnSecond, 0, currentTime.Location()) + hdmiOffTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), hdmiOffHour, hdmiOffMinute, hdmiOffSecond, 0, currentTime.Location()) + + log.Print(currentTime) + log.Print(hdmiOnTime) + log.Print(hdmiOffTime) + + // Turn on/off screen depending on current time + vcgencmdOnOffFlag := "-1" + if currentTime.After(hdmiOnTime) && currentTime.Before(hdmiOffTime) { + log.Print("Turning ON screen") + vcgencmdOnOffFlag = "1" + } + if currentTime.After(hdmiOffTime) && currentTime.Before(hdmiOnTime) { + log.Print("Turning OFF screen") + vcgencmdOnOffFlag = "0" + } + + hdmiControl := exec.Command(CMD_VCGENCMD, CMD_VCGENCMD_DISPLAY_POWER, vcgencmdOnOffFlag) + err = hdmiControl.Run() + if err != nil { + log.Fatalf("Error turning display on/off (%s) : %s", vcgencmdOnOffFlag, err) + } +}