/
tm16xx.go
152 lines (126 loc) · 2.73 KB
/
tm16xx.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2015, Homin Lee <homin.lee@suapapa.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tm1638
import "github.com/davecheney/gpio"
// tm16xx represent a tm16xx module
type tm16xx struct {
data, clk, strobe gpio.Pin
displays int
}
// Newtm16xx returns point of tm16xx which is initialized given gpio numbers
func newTm16xx(data, clk, strobe int,
activeDisplay bool, intensity byte) (*tm16xx, error) {
var d tm16xx
var err error
d.data, err = gpio.OpenPin(data, gpio.ModeOutput)
if err != nil {
return nil, err
}
d.clk, err = gpio.OpenPin(clk, gpio.ModeOutput)
if err != nil {
return nil, err
}
d.strobe, err = gpio.OpenPin(strobe, gpio.ModeOutput)
if err != nil {
return nil, err
}
d.data.SetMode(gpio.ModeOutput)
d.clk.SetMode(gpio.ModeOutput)
d.strobe.SetMode(gpio.ModeOutput)
d.strobe.Set()
d.clk.Set()
d.sendCmd(0x40)
v := min(7, intensity)
if activeDisplay {
v |= 8
}
d.sendCmd(0x80 | v)
d.strobe.Clear()
d.sendCmd(0xC0)
for i := 0; i < 16; i++ {
d.sendCmd(0x00)
}
d.strobe.Set()
return &d, nil
}
// SetupDisplay initialized the display
func (d tm16xx) SetupDisplay(active bool, intensity byte) {
v := min(7, intensity)
if active {
v |= 8
}
d.sendCmd(0x80 | v)
d.strobe.Clear()
d.clk.Clear()
d.clk.Set()
d.strobe.Set()
}
// DisplayDigit displays a digit
func (d tm16xx) DisplayDigit(digit byte, pos int, dot bool) {
d.sendChar(byte(pos), fontNumber[digit&0x0F], dot)
}
// DisplayError display Error
func (d tm16xx) DisplayError() {
d.setDisplay(fontErrorData)
}
// ClearDigit clear digit in given position
func (d tm16xx) ClearDigit(pos int, dot bool) {
d.sendChar(byte(pos), 0, dot)
}
func (d tm16xx) setDisplay(val []byte) {
for i, c := range val {
d.sendChar(byte(i), c, false)
}
}
func (d tm16xx) sendCmd(cmd byte) {
d.strobe.Clear()
d.send(cmd)
d.strobe.Set()
}
func (d tm16xx) sendData(addr, data byte) {
d.sendCmd(0x44)
d.strobe.Clear()
d.send(0xC0 | addr)
d.send(data)
d.strobe.Set()
}
func (d tm16xx) send(data byte) {
for i := 0; i < 8; i++ {
d.clk.Clear()
if data&1 == 0 {
d.data.Clear()
} else {
d.data.Set()
}
data >>= 1
d.clk.Set()
}
}
func (d tm16xx) receive() (temp byte) {
d.data.SetMode(gpio.ModeInput)
d.data.Set() // TODO: is this makes data pin pull up?
for i := 0; i < 8; i++ {
temp >>= 1
d.clk.Clear()
if d.data.Get() {
temp |= 0x80
}
d.clk.Set()
}
d.data.SetMode(gpio.ModeOutput)
d.data.Clear()
return
}
func (d tm16xx) sendChar(pos byte, data byte, dot bool) {
if dot {
data |= 0x80
}
d.sendData(pos<<1, data)
}
// Close closes all open pins
func (d tm16xx) Close() {
d.clk.Close()
d.data.Close()
d.strobe.Close()
}