/
digital_clock.go
103 lines (88 loc) · 1.85 KB
/
digital_clock.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
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/JokerQyou/rpi"
"github.com/JokerQyou/rpi/pcd8544"
"github.com/kidoman/embd"
"github.com/kidoman/embd/sensor/bmp085"
_ "github.com/kidoman/embd/host/rpi"
)
const (
SCLK = 0
DIN = 1
DC = 2
CS = 3
RST = 4
CONTRAST = 60
)
func init() {
rpi.WiringPiSetup()
pcd8544.LCDInit(SCLK, DIN, DC, CS, RST, CONTRAST)
pcd8544.LCDclear()
pcd8544.LCDdisplay()
}
func gpio_cleanup() {
pcd8544.LCDclear()
pcd8544.LCDdisplay()
rpi.PinMode(SCLK, rpi.INPUT)
rpi.PinMode(DIN, rpi.INPUT)
rpi.PinMode(DC, rpi.INPUT)
rpi.PinMode(CS, rpi.INPUT)
rpi.PinMode(RST, rpi.INPUT)
}
func get_time() (string, string) {
t := time.Now()
return t.Format("15:04:05"), t.Format("01-02 Mon")
}
func main() {
sig := make(chan os.Signal, 1)
if err := embd.InitI2C(); err != nil {
panic(err)
}
defer embd.CloseI2C()
bus := embd.NewI2CBus(1)
baro := bmp085.New(bus)
defer baro.Close()
// Draw the outline
pcd8544.LCDdrawrect(6-1, 6-1, pcd8544.LCDWIDTH-6, pcd8544.LCDHEIGHT-6, pcd8544.BLACK)
// date string changes slowly
var (
date_str string
temp_str string
)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
s := <-sig
fmt.Println(s)
gpio_cleanup()
os.Exit(0)
}()
keep_running := true
for keep_running {
// Get temperature
temp, err := baro.Temperature()
if err != nil {
gpio_cleanup()
panic(err)
}
_temp_str := fmt.Sprint(strconv.FormatFloat(temp, 'f', 2, 64), " C")
time_str, _date_str := get_time()
pcd8544.LCDdrawstring(20, 12, time_str)
if date_str != _date_str {
date_str = _date_str
pcd8544.LCDdrawstring(18, 24, date_str)
}
if temp_str != _temp_str {
temp_str = _temp_str
pcd8544.LCDdrawstring(20, 36, temp_str)
}
pcd8544.LCDdisplay()
// wait for 1 sec
time.Sleep(time.Second)
}
}