-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpictures.go
164 lines (148 loc) · 5.18 KB
/
pictures.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
// pictures.go
// Copyright (C) 2018 Steve Merrony
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tello
import (
"fmt"
"io/ioutil"
"sort"
)
// TakePicture requests the Tello to take a JPEG snapshot.
// The process takes a little while to complete and the video may freeze
// during photography. Sometime the Tello does not honour the request.
// The pictures are stored in the tello struct until saved by eg. SaveAllPics().
func (tello *Tello) TakePicture() (err error) {
tello.ctrlMu.Lock()
defer tello.ctrlMu.Unlock()
tello.ctrlSeq++
pkt := newPacket(ptSet, msgDoTakePic, tello.ctrlSeq, 0)
tello.ctrlConn.Write(packetToBuffer(pkt))
//log.Println("Sent take picture request")
return nil
}
func (tello *Tello) sendFileSize() {
tello.ctrlMu.Lock()
defer tello.ctrlMu.Unlock()
tello.ctrlSeq++
tello.ctrlConn.Write(packetToBuffer(newPacket(ptData1, msgFileSize, tello.ctrlSeq, 1)))
}
func (tello *Tello) sendFileAckPiece(done byte, fID uint16, pieceNum uint32) {
tello.ctrlMu.Lock()
defer tello.ctrlMu.Unlock()
tello.ctrlSeq++
pkt := newPacket(ptData1, msgFileData, tello.ctrlSeq, 7)
pkt.payload[0] = done
pkt.payload[1] = byte(fID)
pkt.payload[2] = byte(fID >> 8)
pkt.payload[3] = byte(pieceNum)
pkt.payload[4] = byte(pieceNum >> 8)
pkt.payload[5] = byte(pieceNum >> 16)
pkt.payload[6] = byte(pieceNum >> 24)
tello.ctrlConn.Write(packetToBuffer(pkt))
}
func (tello *Tello) sendFileDone(fID uint16, size int) {
tello.ctrlMu.Lock()
defer tello.ctrlMu.Unlock()
tello.ctrlSeq++
pkt := newPacket(ptGet, msgFileDone, tello.ctrlSeq, 6)
pkt.payload[0] = byte(fID)
pkt.payload[1] = byte(fID >> 8)
pkt.payload[2] = byte(size)
pkt.payload[3] = byte(size >> 8)
pkt.payload[4] = byte(size >> 16)
pkt.payload[5] = byte(size >> 24)
tello.ctrlConn.Write(packetToBuffer(pkt))
}
// reassembleFile reassembles a chunked file in tello.fileTemp into a contiguous byte array in tello.files
func (tello *Tello) reassembleFile() {
var fd FileData
tello.fdMu.Lock()
defer tello.fdMu.Unlock()
fd.FileType = tello.fileTemp.filetype
fd.FileSize = tello.fileTemp.accumSize
// we expect the pieces to be in order
for _, p := range tello.fileTemp.pieces {
// the chunks may not be in order, we must sort them
if p.numChunks > 1 {
sort.Slice(p.chunks, func(i, j int) bool {
return int(p.chunks[i].chunkNum) < int(p.chunks[j].chunkNum)
})
}
for _, c := range p.chunks {
fd.FileBytes = append(fd.FileBytes, c.chunkData...)
}
}
tello.files = append(tello.files, fd)
tello.fileTemp = fileInternal{}
for l := range tello.filesListeners { // Notify file listeners
l <- fd
}
}
// NumPics returns the number of JPEG pictures we are storing in memory
func (tello *Tello) NumPics() (np int) {
tello.fdMu.Lock()
defer tello.fdMu.Unlock()
for _, f := range tello.files {
if f.FileType == FtJPEG {
np++
}
}
return np
}
// SaveAllPics writes all JPEG pictures to disk using the given prefix
// and a generated index number. It returns the number of pictures written &/or an error.
// If there is no error, the pictures are removed from memory.
func (tello *Tello) SaveAllPics(prefix string) (np int, err error) {
tello.fdMu.Lock()
defer tello.fdMu.Unlock()
for _, f := range tello.files {
if f.FileType == FtJPEG {
filename := fmt.Sprintf("%s_%d.jpg", prefix, np)
err = ioutil.WriteFile(filename, f.FileBytes, 0644)
if err != nil {
return 0, err
}
np++
}
}
tello.files = nil
return np, nil
}
// GrabFiles returns the latest batch of files reassembled, removing them from memory.
func (tello *Tello) GrabFiles() []FileData {
tello.fdMu.Lock()
defer tello.fdMu.Unlock()
res := make([]FileData, 0, len(tello.files))
for _, f := range tello.files {
res = append(res, f)
}
tello.files = tello.files[:0] // Reset all reassembled files (keeping capacity)
return res
}
// ListenFiles returns a channel that will be filled when a new file is reassembled, and a function to stop listening
func (tello *Tello) ListenFiles() (chan FileData, func()) {
tello.fdMu.Lock()
defer tello.fdMu.Unlock()
res := make(chan FileData)
tello.filesListeners[res] = res
return res, func() {
if _, present := tello.filesListeners[res]; present {
delete(tello.filesListeners, res)
close(res)
}
}
}