-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsr_writer.go
123 lines (101 loc) · 2.35 KB
/
sr_writer.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
//
// Generates files of the Sigrok Zip format (srzip).
// See https://sigrok.org/wiki/File_format:Sigrok/v2
//
// Copyright 2020-2021 Darell Tan. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the README file.
//
package main
import (
"archive/zip"
"errors"
"fmt"
"io"
"math"
"os"
)
// Maximum number of samples in each file part
const SamplesLimit = 0x280000
type SrZip struct {
zipFile *zip.Writer
channels []*AnalogChannel
SampleRate uint
}
type AnalogChannel struct {
samples uint64
part int
w io.Writer
srzip *SrZip
channel int
name string
}
func NewSrZip(zipFile *zip.Writer) *SrZip {
return &SrZip{zipFile: zipFile}
}
// Convenience method to specify just a filename.
func NewSrZipFile(name string) (*SrZip, error) {
w, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
return NewSrZip(zip.NewWriter(w)), nil
}
func (sr *SrZip) createFile(name, contents string) error {
w, err := sr.zipFile.Create(name)
if err != nil {
return err
}
_, err = w.Write([]byte(contents))
return err
}
func (sr *SrZip) Close() error {
err := sr.createFile("version", "2\n")
if err != nil {
return err
}
metadata := fmt.Sprintf(`
[device 1]
samplerate=%d
total analog=%d
`, sr.SampleRate, len(sr.channels))
// write channel names
for _, ch := range sr.channels {
metadata += fmt.Sprintf("analog%d=%s\n", ch.channel, ch.name)
}
err = sr.createFile("metadata", metadata)
if err != nil {
return err
}
return sr.zipFile.Close()
}
func (sr *SrZip) NewAnalogChannel(name string) *AnalogChannel {
c := &AnalogChannel{srzip: sr, name: name, channel: len(sr.channels) + 1}
sr.channels = append(sr.channels, c)
c.update() // initialize for the first time
return c
}
func (c *AnalogChannel) Write(v float32) error {
v2 := math.Float32bits(v)
c.w.Write([]byte{ // little-endian
byte(v2),
byte(v2 >> 8),
byte(v2 >> 16),
byte(v2 >> 24),
})
c.samples++
return c.update()
}
// Handles splitting of channel data into parts
func (c *AnalogChannel) update() error {
if c.samples%SamplesLimit == 0 {
c.part++
name := fmt.Sprintf("analog-1-%d-%d", c.channel, c.part)
w2, err := c.srzip.zipFile.Create(name)
if err != nil {
return errors.New("can't create part for analog ch " + c.name)
}
c.w = w2
}
return nil
}