-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmit.go
122 lines (102 loc) · 2.27 KB
/
transmit.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
package intertechno
import (
"time"
)
const (
// [0..8] The 2log-Number of times the signal is repeated.
// The actual number of repeats will be 2^repeats.
// 2 would be bare minimum, 4 seems robust, 8 is maximum (and overkill).
repeats = 4
// Duration of one period, in microseconds.
// One bit takes 8 periods (but only 4 for 'dim' signal).
periodusec = 260
addressBits = 26
unitBits = 4
dimvalueBits = 4
)
func (im *Manager) transmit(c Command) {
for i := 1 << repeats; i > 0; i-- {
im.sendStartPulse()
im.sendAddress(c.Address)
im.sendBit(c.Group)
if c.Action == ActionDim {
im.sendDim(c.Dimvalue, c.Unit)
} else {
im.sendBit(c.Action != 0)
im.sendUnit(c.Unit)
}
im.sendStopPulse()
}
}
func (im *Manager) sendStartPulse() {
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
// Actually 10.5T insteat of 10.44T. Close enough.
time.Sleep(time.Microsecond * time.Duration(periodusec*10+(periodusec>>1)))
}
func (im *Manager) sendAddress(address int) {
for i := addressBits - 1; i >= 0; i-- {
im.sendBit((address>>i)&1 != 0)
}
}
func (im *Manager) sendUnit(unit int) {
for i := unitBits - 1; i >= 0; i-- {
im.sendBit(unit&(1<<i) != 0)
}
}
func (im *Manager) sendDim(dimvalue int, unit int) {
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepPeriodusec()
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepPeriodusec()
im.sendUnit(unit)
for i := dimvalueBits - 1; i >= 0; i-- {
im.sendBit(dimvalue&(1<<i) != 0)
}
}
func (im *Manager) sendStopPulse() {
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepCustomPeriodusec(40)
}
func (im *Manager) sendBit(isBitOne bool) {
if isBitOne {
// Send '1'
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepCustomPeriodusec(5)
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepPeriodusec()
} else {
// Send '0'
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepPeriodusec()
im.setPinHigh()
sleepPeriodusec()
im.setPinLow()
sleepCustomPeriodusec(5)
}
}
func sleepCustomPeriodusec(m int) {
time.Sleep(time.Microsecond * time.Duration(periodusec*m))
}
func sleepPeriodusec() {
time.Sleep(time.Microsecond * time.Duration(periodusec))
}
func (im *Manager) setPinHigh() {
im.pin.High()
}
func (im *Manager) setPinLow() {
im.pin.Low()
}