Repository navigation
Expand file tree
/
Copy pathbmapi_gofiles_bmapiEncoder.go
More file actions
190 lines (182 loc) · 4.02 KB
/
Copy pathbmapi_gofiles_bmapiEncoder.go
File metadata and controls
190 lines (182 loc) · 4.02 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package bondmachine
const (
bmapiEncoder = `package bmapiusbuart
import (
"context"
"log"
"time"
)
func (ba *BMAPI) encoder(ctx context.Context) {
if ba.debug {
log.Println("encoder: starting")
}
for {
select {
case <-ctx.Done():
if ba.debug {
log.Println("encoder: exiting")
}
return
default:
if ctx.Err() != nil {
if ba.debug {
log.Println("encoder: exiting")
}
return
}
// All the actions depends on the internal state of the library (ba.state)
switch ba.stateGet() {
case stateWAIT:
if ba.debug {
log.Println("encoder: BMAPI in state WAIT")
}
select {
case <-ctx.Done():
if ba.debug {
log.Println("encoder: exiting")
}
return
case ba.sendChan <- cmdHANDSH | ba.hs:
}
if ba.debug {
log.Printf("encoder: HandShake sent %08b - new state HSSENT", cmdHANDSH|ba.hs)
}
ba.stateSet(stateHSSENT)
ba.notresetChan <- struct{}{}
case stateMASKRECV:
if ba.debug {
log.Println("encoder: BMAPI in state MASKRECV")
}
select {
case <-ctx.Done():
if ba.debug {
log.Println("encoder: exiting")
}
return
case ba.sendChan <- ba.hsmask & (cmdHANDSH | ba.hs):
}
if ba.debug {
log.Printf("encoder: Ack sent %08b - new state ACK", ba.hsmask&(cmdHANDSH|ba.hs))
}
ba.stateSet(stateACK)
ba.notresetChan <- struct{}{}
case stateCONNECT:
if ba.debug {
log.Println("encoder: BMAPI in state CONNECT")
}
timeout := time.NewTimer(timeoutDelay * time.Second)
select {
case <-ctx.Done():
if ba.debug {
log.Println("encoder: exiting")
}
return
case <-ba.keep:
cmd := cmdKEEP
ba.sendChan <- cmd
if ba.debug {
log.Printf("encoder: KEEP sent\n")
}
case o0Data := <-ba.o0Send:
if o0Data != ba.o0 {
if ba.monitor {
ba.monitorChan <- struct{}{}
}
ba.o0 = o0Data
reg := uint8(0)
cmd := cmdNEWVAL | reg
ba.sendChan <- cmd
// BMAPI Customize
mask8 := uint8(255)
for i := 0; i < regsizeB; i++ {
value := uint8((o0Data << (8 * i)) & mask8)
ba.sendChan <- value
if ba.debug {
log.Printf("encoder: NEWVAL sent - Reg:%d - Value:%d\n", reg, value)
}
}
}
case o0Valid := <-ba.o0validSend:
if o0Valid != ba.o0valid {
if ba.monitor {
ba.monitorChan <- struct{}{}
}
ba.o0valid = o0Valid
if ba.debug {
log.Println("encoder: o0valid changed to", o0Valid)
}
reg := uint8(0)
if o0Valid {
if ba.debug {
log.Printf("encoder: DVALIDH sent - Reg: %d\n", reg)
}
cmd := cmdDVALIDH | reg
ba.sendChan <- cmd
} else {
if ba.debug {
log.Printf("encoder: DVALIDL sent - Reg: %d\n", reg)
}
cmd := cmdDVALIDL | reg
ba.sendChan <- cmd
}
}
case i0Recv := <-ba.i0recvSend:
if i0Recv != ba.i0recv {
if ba.monitor {
ba.monitorChan <- struct{}{}
}
ba.i0recv = i0Recv
if ba.debug {
log.Println("encoder: i0recv changed to", i0Recv)
}
reg := uint8(0)
if i0Recv {
if ba.debug {
log.Printf("encoder: RECVH sent - Reg: %d\n", reg)
}
cmd := cmdDRECVH | reg
ba.sendChan <- cmd
} else {
if ba.debug {
log.Printf("encoder: RECVL sent - Reg: %d\n", reg)
}
cmd := cmdDRECVL | reg
ba.sendChan <- cmd
}
}
case <-timeout.C:
}
timeout.Stop()
}
}
}
}
func (ba *BMAPI) sendAll() {
reg := uint8(0)
cmd := cmdNEWVAL | reg
ba.sendChan <- cmd
// FakeAccel Customize
mask8 := uint8(255)
for i := 0; i < regsizeB; i++ {
value := uint8((ba.o0 << (8 * i)) & mask8)
ba.sendChan <- value
}
reg = uint8(0)
if ba.o0valid {
cmd := cmdDVALIDH | reg
ba.sendChan <- cmd
} else {
cmd := cmdDVALIDL | reg
ba.sendChan <- cmd
}
reg = uint8(0)
if ba.i0recv {
cmd := cmdDRECVH | reg
ba.sendChan <- cmd
} else {
cmd := cmdDRECVL | reg
ba.sendChan <- cmd
}
}
`
)