-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdac.go
406 lines (343 loc) · 9.24 KB
/
dac.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
# Copyright 2016 Tim Greiser
# Based on work by Jacob Potter, some comments are from his
# protocol documents
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package etherdream
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"io"
"net"
"os"
"runtime"
"strings"
"sync"
"time"
)
var mut = &sync.Mutex{}
// ScanRate controls the playback speed of the ether dream
var ScanRate = flag.Int("scan-rate", 24000, "Number of points per second to play back.")
// Assuming the ether dream scans 30 times per second
var frameRate = 30
// Count frames
var frameCount = 0
var bufferSize = 1799
// PointSize is the number of bytes in a point struct
const PointSize uint16 = 18
func whenToPlay() int {
return bufferSize - FramePoints()
}
// ProtocolError indicates a protocol level error. I've
// never seen one, but maybe you will.
type ProtocolError struct {
Msg string
}
func (e *ProtocolError) Error() string {
return e.Msg
}
// DAC is the interface to the Ether Dream Digital to
// Analog Converter that turns network signals into
// ILDA control singnals for a laser scanner.
type DAC struct {
Host string
Port string
FirmwareString string
LastStatus *DACStatus
Reader io.Reader
Writer io.WriteCloser
PointsPlayed int
buf bytes.Buffer
conn net.Conn
}
// NewDAC will connect to an Ether Dream device over TCP
// or it will return an error
func NewDAC(host string) (*DAC, error) {
if !flag.Parsed() {
flag.Parse()
}
// connect to the DAC over TCP
r, w := io.Pipe()
dac := &DAC{Host: host, Port: "7765", Reader: r, Writer: w}
err := dac.init()
return dac, err
}
// Close the network connection, you should. -- Yoda
func (d *DAC) Close() {
d.conn.Close()
}
func (d *DAC) init() error {
if *Debug {
fmt.Println("Connecting to TCP")
}
c, err := net.DialTimeout("tcp", d.Host+":"+d.Port, time.Second*15)
if err != nil {
return err
}
d.conn = c
if _, err = d.ReadResponse("?"); err != nil {
return err
}
if err = d.Send([]byte("v")); err != nil {
return err
}
by, err := d.Read(32)
if err != nil {
return err
}
d.FirmwareString = strings.TrimSpace(strings.Replace(string(by), "\x00", " ", -1))
if *Debug {
fmt.Printf("Firmware: %v\n", d.FirmwareString)
}
return nil
}
func (d *DAC) Read(l int) ([]byte, error) {
if l > d.buf.Len() {
// read more bytes into the buffer
_, err := io.CopyN(&d.buf, d.conn, int64(l))
if err != nil {
return []byte{}, err
}
}
ret := make([]byte, l)
_, err := d.buf.Read(ret)
return ret, err
}
// ReadResponse reads the ACK/NACK response to a command
func (d *DAC) ReadResponse(cmd string) (*DACStatus, error) {
data, err := d.Read(22)
if err != nil {
fmt.Printf("Error: %v\n", err)
return nil, err
}
resp := data[0]
cmdR := data[1]
status := NewDACStatus(data[2:])
//fmt.Printf("\nRead response: Resp=%s Cmd=%s Status=%s\n", string(resp), string(cmdR), status.String())
if cmdR != []byte(cmd)[0] {
return nil, &ProtocolError{fmt.Sprintf("Expected resp for %s, got %s", cmd, string(cmdR))}
}
if resp != []byte("a")[0] {
return nil, &ProtocolError{fmt.Sprintf("Expected ACK, got %s Resp=%s\n%s", string(cmdR), string(resp), status.String())}
}
d.LastStatus = status
return status, nil
}
// Send a command to the DAC
func (d DAC) Send(cmd []byte) error {
_, err := d.conn.Write(cmd)
return err
}
// BeginCmd starts playback
const BeginCmd = 0x62
// Begin Playback
// This causes the DAC to begin producing output. lwm is
// currently unused. rate is the number of points per second
// to be read from the buffer. If the playback system was
// Prepared and there was data in the buffer, then the DAC
// will reply with ACK; otherwise, it replies with NAK - Invalid.
func (d *DAC) Begin(lwm uint16, rate uint32) (*DACStatus, error) {
var cmd = make([]byte, 7)
cmd[0] = BeginCmd
binary.LittleEndian.PutUint16(cmd[1:3], lwm)
binary.LittleEndian.PutUint32(cmd[3:7], rate)
if err := d.Send(cmd); err != nil {
return nil, err
}
s, err := d.ReadResponse(string(BeginCmd))
fmt.Printf("Begin: %v\n\n", s)
return s, err
}
// Update should not exist?
// Maybe this is the 'q' command now.
func (d *DAC) Update(lwm uint16, rate uint32) (*DACStatus, error) {
var cmd = make([]byte, 7)
cmd[0] = 'u'
binary.LittleEndian.PutUint16(cmd[1:3], lwm)
binary.LittleEndian.PutUint32(cmd[3:7], rate)
if err := d.Send(cmd); err != nil {
return nil, err
}
return d.ReadResponse("u")
}
func (d *DAC) Write(b []byte) (*DACStatus, error) {
l := uint16(len(b))
cmd := make([]byte, l+3)
cmd[0] = 'd'
binary.LittleEndian.PutUint16(cmd[1:3], l/PointSize)
copy(cmd[3:], b)
if *Debug {
fmt.Printf("DAC Write %v points\n", l/PointSize)
}
if err := d.Send(cmd); err != nil {
return nil, err
}
return d.ReadResponse("d")
}
// Prepare command
func (d *DAC) Prepare() (*DACStatus, error) {
if err := d.Send([]byte("p")); err != nil {
return nil, err
}
return d.ReadResponse("p")
}
// Stop command
func (d *DAC) Stop() (*DACStatus, error) {
if err := d.Send([]byte("s")); err != nil {
return nil, err
}
return d.ReadResponse("s")
}
// EmergencyStop command causes the light engine to
// enter the E-Stop state, regardless of its previous
// state. It is always ACKed.
func (d *DAC) EmergencyStop() (*DACStatus, error) {
if err := d.Send([]byte("\xFF")); err != nil {
return nil, err
}
return d.ReadResponse("\xFF")
}
// ClearEmergencyStop command. If the light engine was in
// E-Stop state due to an emergency stop command (either from
// a local stop condition or over the network), then this
// command resets it to be Ready. It is ACKed if the DAC was
// previously in E-Stop; otherwise it is replied to with a NAK -
// Invalid. If the condition that caused the emergency stop is
// still active (E-Stop input still asserted, temperature still
// out of bounds, etc.), then a NAK - Stop Condition is sent.
func (d *DAC) ClearEmergencyStop() (*DACStatus, error) {
if err := d.Send([]byte("c")); err != nil {
return nil, err
}
return d.ReadResponse("c")
}
// Ping command
func (d *DAC) Ping() (*DACStatus, error) {
if err := d.Send([]byte("?")); err != nil {
return nil, err
}
return d.ReadResponse("?")
}
// ShouldPrepare or not? State 1 and 2 are good. Some Flags
// need prepare to reset an invalid state.
func (d DAC) ShouldPrepare() bool {
return d.LastStatus.PlaybackState == 0 ||
d.LastStatus.PlaybackFlags&2 == 2 ||
d.LastStatus.PlaybackFlags&4 == 4
}
// Measure how long it takes to play 10,000 points
func (d *DAC) Measure(stream PointStream) {
*Debug = true
t0 := time.Now()
go d.Play(stream)
for {
if d.PointsPlayed >= 100000 {
t1 := time.Now()
fmt.Printf("%v took %v\n", d.PointsPlayed, t1.Sub(t0).String())
os.Exit(0)
}
runtime.Gosched()
}
}
// Play a stream generator and begin sending output to the laser
func (d *DAC) Play(stream PointStream) {
// First, prepare the stream
if d.LastStatus.PlaybackState == 2 {
if *Debug {
fmt.Printf("Error: Already playing?!")
}
} else if d.ShouldPrepare() {
st, err := d.Prepare()
if err != nil {
fmt.Printf("ERROR: Failed to prepare: %v\n\n", err)
}
if *Debug {
fmt.Printf("DAC prepared: %v\n\n", st)
}
}
started := 0
// Start stream
go stream(d.Writer)
OuterLoop:
for {
// Read calls from the pipe
cap := 1799 - d.LastStatus.BufferFullness
by := make([]byte, FramePoints()*int(PointSize))
idx := 0
when := whenToPlay()
if *Debug {
fmt.Printf("Buffer capacity: %v is lessThan: %v\n", cap, when)
}
if int(cap) <= when {
time.Sleep(time.Millisecond * 5)
d.Ping()
continue
}
fp := FramePoints()
for idx < fp {
bdx := idx * int(PointSize)
_, err := d.Reader.Read(by[bdx:])
if err != nil {
if err == io.EOF {
break OuterLoop
}
fmt.Printf("Error playing stream: %v", err)
break
}
idx++
}
mut.Lock()
st, err := d.Write(by)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
d.PointsPlayed += len(by) / int(PointSize)
if *Debug {
fmt.Printf("Points: %v\nStatus: %v\n", d.PointsPlayed, st)
}
if started == 0 {
st, err := d.Begin(0, uint32(*ScanRate))
if err != nil {
fmt.Printf("ERROR on Begin: %v\n\n", err)
}
started = 1
if *Debug {
fmt.Printf("\nBegin executed: %v\n", st)
}
}
mut.Unlock()
runtime.Gosched()
}
}
// FindFirstDAC starts a UDP server to listen for broadcast packets on your network. Return the UDPAddr
// of the first Ether Dream DAC located
func FindFirstDAC() (*net.UDPAddr, *BroadcastPacket, error) {
// listen for broadcast packets
sock, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: 7654,
})
if err != nil {
return nil, nil, err
}
var data [36]byte
_, addr, err := sock.ReadFromUDP(data[0:])
if err != nil {
return nil, nil, err
}
bp := NewBroadcastPacket(data)
return addr, bp, nil
}