piframe-go/cmd/hdmi/hdmi.go

82 lines
2.5 KiB
Go

package main
import (
"log"
"os/exec"
"strconv"
"strings"
"time"
"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(false)
// 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, ":")
// 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())
// Turn on/off screen depending on current time
vcgencmdOnOffFlag := "-1"
if currentTime.After(hdmiOnTime) && currentTime.Before(hdmiOffTime) {
vcgencmdOnOffFlag = "1"
}
if currentTime.After(hdmiOffTime) && currentTime.Before(hdmiOnTime) {
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)
}
}