-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
75 lines (61 loc) · 2.01 KB
/
utils.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
package makerbot
import (
"encoding/binary"
"strconv"
"strings"
"time"
)
// https://gist.github.com/alexmcroberts/219127816e7a16c7bd70
type epochTime time.Time
func (t epochTime) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(time.Time(t).Unix(), 10)), nil
}
func (t *epochTime) UnmarshalJSON(s []byte) (err error) {
r := strings.Replace(string(s), `"`, ``, -1)
q, err := strconv.ParseInt(r, 10, 64)
if err != nil {
return err
}
*(*time.Time)(t) = time.Unix(q/1000, 0)
return
}
func (t epochTime) String() string { return time.Time(t).String() }
// CameraFrameFormat specifies which format a camera frame is in
type CameraFrameFormat uint32
const (
// CameraFrameFormatInvalid means that the camera frame is invalid(?)
CameraFrameFormatInvalid CameraFrameFormat = iota
// CameraFrameFormatYUYV means that the camera frame is in YUYV format
CameraFrameFormatYUYV
// CameraFrameFormatJPEG means that the camera frame is in JPEG format
CameraFrameFormatJPEG
)
// CameraFrame is a single camera snapshot returned by the printer
type CameraFrame struct {
Data []byte
Metadata *CameraFrameMetadata
}
// CameraFrameMetadata holds information about a camera frame returned
// by the printer
type CameraFrameMetadata struct {
FileSize uint32 // Frame's file size in bytes
Width uint32 // Frame's width in pixels
Height uint32 // Frame's height in pixels
Format CameraFrameFormat // Format that the frame is in (invalid, YUYV, JPEG)
}
func unpackCameraFrameMetadata(packed []byte) CameraFrameMetadata {
return CameraFrameMetadata{
FileSize: binary.BigEndian.Uint32(packed[0:4]) - 16,
Width: binary.BigEndian.Uint32(packed[4:8]),
Height: binary.BigEndian.Uint32(packed[8:12]),
Format: CameraFrameFormat(binary.BigEndian.Uint32(packed[12:16])),
}
}
func parseInfoFields(inf *[]string) *map[string]string {
var fields map[string]string
for _, field := range *inf {
spl := strings.Split(field, "=")
fields[spl[0]] = fields[spl[1]]
}
return &fields
}