clock.go 3.85 KB
Newer Older
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
1
2
3
4
package main

import (
	"fmt"
5
6
	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/rpi" // This loads the RPi driver
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
7
	"github.com/tarm/serial"
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
8
9
10
	"gitlab.com/Depili/go-rgb-led-matrix/bdf"
	"gitlab.com/Depili/go-rgb-led-matrix/matrix"
	"gitlab.com/jessevdk/go-flags"
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
11
12
13
14
15
16
	"os"
	"os/signal"
	"time"
)

var Options struct {
17
18
19
20
21
22
23
24
25
26
	Font        string `short:"F" long:"font" description:"Font for event name" default:"fonts/6x12.bdf"`
	Matrix      string `short:"m" long:"matrix" description:"Matrix to connect to" required:"true"`
	SerialName  string `long:"serial-name" description:"Serial device for arduino" default:"/dev/ttyUSB0"`
	SerialBaud  int    `long:"serial-baud" value-name:"BAUD" default:"57600"`
	TextRed     int    `short:"r" long:"red" description:"Red component of text color" default:"128"`
	TextGreen   int    `short:"g" long:"green" description:"Green component of text color" default:"128"`
	TextBlue    int    `short:"b" long:"blue" description:"Blue component of text color" default:"0"`
	LocalTime   string `short:"t" long:"local-time" description:"Local timezone" default:"Europe/Helsinki"`
	ForeignTime string `short:"T" long:"foreign-time" description:"Foreign timezone" default:"Europe/Moscow"`
	TimePin     int    `short:"p" long:"time-pin" description:"Pin to select foreign timezone, active low" default:"15"`
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
27
28
29
30
31
32
33
34
35
}

var parser = flags.NewParser(&Options, flags.Default)

func main() {
	if _, err := parser.Parse(); err != nil {
		panic(err)
	}

36
37
	textColor := [3]byte{byte(Options.TextRed), byte(Options.TextGreen), byte(Options.TextBlue)}

Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
38
39
40
41
42
43
44
45
46
47
48
	serialConfig := serial.Config{
		Name: Options.SerialName,
		Baud: Options.SerialBaud,
		// ReadTimeout:    options.SerialTimeout,
	}

	serial, err := serial.OpenPort(&serialConfig)
	if err != nil {
		panic(err)
	}

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
	fmt.Printf("Serial open.\n")

	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}

	timePin, err := embd.NewDigitalPin(Options.TimePin)
	if err != nil {
		panic(err)
	} else if err := timePin.SetDirection(embd.In); err != nil {
		panic(err)
	}

	fmt.Printf("GPIO initialized.\n")

	local, err := time.LoadLocation(Options.LocalTime)
	if err != nil {
		panic(err)
	}

	foreign, err := time.LoadLocation(Options.ForeignTime)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Timezones loaded.\n")

Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	font, err := bdf.Parse(Options.Font)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Fonts loaded.\n")

	m := matrix.Init(Options.Matrix, 32, 32)
	defer m.Close()

	// Trap SIGINT aka Ctrl-C
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt)

89
90
	hourBitmap := font.TextBitmap("15")
	minuteBitmap := font.TextBitmap("04")
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
91
	secondBitmap := font.TextBitmap("05")
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
92
93
94
95
96
97
98
99
100
101
102

	updateTicker := time.NewTicker(time.Millisecond * 10)
	send := make([]byte, 1)

	for {
		select {
		case <-sigChan:
			// SIGINT received, shutdown gracefully
			m.Close()
			os.Exit(1)
		case <-updateTicker.C:
103
104
105
106
107
108
109
110
			var t time.Time
			if i, _ := timePin.Read(); err != nil {
				panic(err)
			} else if i == 1 {
				t = time.Now().In(local)
			} else {
				t = time.Now().In(foreign)
			}
111
112
113
114
115
116
117
118
119
			if t.Year() > 2000 {
				hourBitmap = font.TextBitmap(t.Format("15"))
				minuteBitmap = font.TextBitmap(t.Format("04"))
				secondBitmap = font.TextBitmap(t.Format("05"))
			} else {
				hourBitmap = font.TextBitmap("XX")
				minuteBitmap = font.TextBitmap("XX")
				secondBitmap = font.TextBitmap("")
			}
120
			seconds := t.Second()
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
121
			m.Fill(matrix.ColorBlack())
122
123
124
125
126
127
128
129
130
131
132
133
			m.SetPixel(14, 15, textColor)
			m.SetPixel(14, 16, textColor)
			m.SetPixel(15, 15, textColor)
			m.SetPixel(15, 16, textColor)

			m.SetPixel(18, 15, textColor)
			m.SetPixel(18, 16, textColor)
			m.SetPixel(19, 15, textColor)
			m.SetPixel(19, 16, textColor)
			m.Scroll(hourBitmap, textColor, 10, 0, 0, 14)
			m.Scroll(minuteBitmap, textColor, 10, 17, 0, 14)
			m.Scroll(secondBitmap, textColor, 21, 8, 0, 14)
Vesa-Pekka Palmu's avatar
Vesa-Pekka Palmu committed
134
135
136
137
138
139
140
141
142
			send[0] = byte(seconds)
			_, err := serial.Write(send)
			if err != nil {
				panic(err)
			}
			m.Send()
		}
	}
}