-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (113 loc) · 2.97 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"flag"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"periph.io/x/host/v3"
"syscall"
"time"
)
const (
tickerPrecision = 3500000
)
var (
LogLevel = flag.String("loglevel", "info", "Log Level")
ConfigFile = flag.String("configfile", "config.yaml", "A yaml config file to use")
ConfigData = &Config{}
TZLocation *time.Location
secondTicker *time.Ticker
timeError int
timeCalibrating bool
Dimmer1 *Dimmer
)
func main() {
// Set the default debug lever
// Parse the runtime flags
flag.Parse()
lvl, _ := log.ParseLevel(*LogLevel)
log.SetLevel(lvl)
ConfigData.Initialize()
err := ConfigData.LoadFile(*ConfigFile)
if err != nil {
log.Errorf("Could not open config file %v", err)
}
log.Infof("Config contents are %+v", ConfigData)
TZLocation, _ = time.LoadLocation(ConfigData.TimeZone)
// setup signal catching
sigs := make(chan os.Signal, 1)
// catch all signals since not explicitly listing
signal.Notify(sigs)
setTicker()
// Set up our peripherals
if _, err := host.Init(); err != nil {
log.Fatalf("Problem setting up periph host - %v", err)
}
i2cInit()
Dimmer1, err = NewDimmer(ConfigData.PWMPin)
if err != nil {
log.Fatalf("Problem setting up dimmer - %v", err)
}
defer Dimmer1.Stop()
log.Info("Set up dimmer")
Dimmer1.Fade(FullBright, 2*time.Second)
time.Sleep(4 * time.Second)
Dimmer1.Fade(0, 2*time.Second)
StartServer()
for {
select {
case s := <-sigs:
switch s {
case syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGINT:
AppCleanup()
os.Exit(1)
case syscall.SIGHUP:
// case syscall.SIGINFO:
}
case currentTime := <-secondTicker.C:
calTicker(currentTime)
// log.Debugf("Time is %v with error %d", currentTime, timeError)
updateLEDClock(currentTime)
if currentTime.Second() == 0 {
action := checkTimer(currentTime)
if action != nil {
switch *action {
case FadeUp:
Dimmer1.Fade(FullBright, time.Duration(ConfigData.FadeUpTime)*time.Minute)
case FadeDown:
Dimmer1.Fade(0, time.Duration(ConfigData.FadeDownTime)*time.Minute)
}
}
}
}
}
}
func AppCleanup() {
i2cStop()
StopServer()
}
func setTicker() {
nextSecond := time.Now().Truncate(time.Second)
timeWait := time.Until(nextSecond)
time.Sleep(timeWait)
secondTicker = time.NewTicker(time.Second)
log.Infof("Started ticker at %v", time.Now().Nanosecond())
}
func calTicker(currentTime time.Time) {
nsError := currentTime.Nanosecond()
if nsError > 500000000 {
timeError = 1000000000 - nsError
} else {
timeError = nsError
}
if timeError > tickerPrecision*2 || timeError < -tickerPrecision/2 {
log.Info("Calibrating ticker")
newInterval := time.Second - time.Duration(timeError)/2
secondTicker.Reset(newInterval)
timeCalibrating = true
} else if timeCalibrating && (timeError < tickerPrecision || timeError < -tickerPrecision) {
secondTicker.Reset(time.Second)
timeCalibrating = false
log.Info("Ticker calibrated")
}
}