piframe-go/cmd/hdmi/hdmi.go

92 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-09-06 08:11:19 +00:00
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)
}
}