Skip to content

Commit e459bba

Browse files
author
josh
committed
donno
1 parent 16be182 commit e459bba

10 files changed

+295594
-220448
lines changed

Diff for: examples/respiration/respiration.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func respirationwsHandler(w http.ResponseWriter, r *http.Request) {
102102
_, msg, err := conn.ReadMessage()
103103
if err != nil {
104104
respirationconnectionsMutex.Lock()
105-
log.Printf("Disconnecting %v because %v\n", conn, err)
105+
// log.Printf("Disconnecting %v because %v\n", conn, err)
106106
delete(respirationconnections, conn)
107107
respirationconnectionsMutex.Unlock()
108108
conn.Close()
@@ -146,7 +146,7 @@ func baseBandwsHandler(w http.ResponseWriter, r *http.Request) {
146146
_, msg, err := conn.ReadMessage()
147147
if err != nil {
148148
baseBandconnectionsMutex.Lock()
149-
log.Printf("Disconnecting %v because %v\n", conn, err)
149+
// log.Printf("Disconnecting %v because %v\n", conn, err)
150150
delete(baseBandconnections, conn)
151151
baseBandconnectionsMutex.Unlock()
152152
conn.Close()
@@ -199,7 +199,7 @@ func openXethru(comm string, baudrate int, baseband chan xethru.BaseBandAmpPhase
199199
port.Flush()
200200

201201
x2 := xethru.Open("x2m200", port)
202-
202+
// x2.Rese
203203
reset, err := x2.Reset()
204204
if err != nil {
205205
log.Printf("serial.Reset: %v\n", err)

Diff for: examples/respiration/respiration.json

+295,444-220,394
Large diffs are not rendered by default.

Diff for: examples/respiration/www/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@
356356
// bars = data.length;
357357
bar_x = (respctx.canvas.width/2);
358358
bar_y = 5
359-
bar_width = -data.Movement*50
359+
bar_width = -data.movement*50
360360
bar_height = respctx.canvas.height -10
361361
respctx.fillRect(bar_x, bar_y, bar_width, bar_height);
362362

@@ -366,7 +366,7 @@
366366
respctx.textBaseline = "middle";
367367

368368
respctx.fillStyle = 'black';
369-
text = data.State + " " + data.RPM + " " + distanceFiltered.toFixed(2)+ " " + data.Distance.toFixed(2);
369+
text = data.state + " " + data.rpm + " " + distanceFiltered.toFixed(2)+ " " + data.distance.toFixed(2);
370370
respctx.fillText(text, respctx.canvas.width/2, respctx.canvas.height/2);
371371
// for (var i = 0; i < bars; i++) {
372372
// bar_x = i * ampcanvas.width/bars;

Diff for: status_jsonenums.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ import (
99

1010
var (
1111
_statusNameToValue = map[string]status{
12-
"respApp": respApp,
13-
"sleepApp": sleepApp,
12+
"respApp": respApp,
13+
"sleepApp": sleepApp,
14+
"basebandAP": basebandAP,
1415
}
1516

1617
_statusValueToName = map[status]string{
17-
respApp: "respApp",
18-
sleepApp: "sleepApp",
18+
respApp: "respApp",
19+
sleepApp: "sleepApp",
20+
basebandAP: "basebandAP",
1921
}
2022
)
2123

2224
func init() {
2325
var v status
2426
if _, ok := interface{}(v).(fmt.Stringer); ok {
2527
_statusNameToValue = map[string]status{
26-
interface{}(respApp).(fmt.Stringer).String(): respApp,
27-
interface{}(sleepApp).(fmt.Stringer).String(): sleepApp,
28+
interface{}(respApp).(fmt.Stringer).String(): respApp,
29+
interface{}(sleepApp).(fmt.Stringer).String(): sleepApp,
30+
interface{}(basebandAP).(fmt.Stringer).String(): basebandAP,
2831
}
2932
}
3033
}

Diff for: status_string.go

+10-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: x2m200.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ func (x *x2m200Frame) Read(b []byte) (n int, err error) {
100100
n = copy(b, p)
101101
return n, nil
102102
}
103-
} else {
104-
return 0, errPacketNoStartByte
105103
}
106-
return 0, nil
104+
return 0, errPacketNoStartByte
105+
// return 0, nil
107106
}
108107

109108
func validator(b []byte) (bool, []byte, error) {
110-
var k []byte
111-
k = append(k, b...)
109+
// var k []byte
110+
// k = append(k, b...)
112111

113112
const (
114113
wait = iota

Diff for: x2m200Parsers.go

+49-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package xethru
33
import (
44
"encoding/binary"
55
"errors"
6-
"log"
76
"math"
87
"time"
98
)
@@ -16,6 +15,43 @@ const (
1615
basebandIQStartByte = 0x0c
1716
)
1817

18+
// type Parser interface {
19+
// Parse([]byte) error
20+
// }
21+
//
22+
// type RespirationApp struct {
23+
// n int
24+
// }
25+
//
26+
// func Mangle(p Parser) {}
27+
28+
//
29+
// type SleepApp struct {
30+
// n int
31+
// }
32+
//
33+
// type BaseBandAPApp struct {
34+
// n int
35+
// }
36+
//
37+
// type BaseBandIQApp struct {
38+
// n int
39+
// }
40+
//
41+
// func (r RespirationApp) Parse(b []byte) error {
42+
// return nil
43+
// }
44+
45+
// func (r SleepApp) Parse(b []byte) error {
46+
// return nil
47+
// }
48+
// func (r BaseBandAPApp) Parse(b []byte) error {
49+
// return nil
50+
// }
51+
// func (r BaseBandIQApp) Parse(b []byte) error {
52+
// return nil
53+
// }
54+
1955
func parse(b []byte) (interface{}, error) {
2056
// log.Printf("%02x\n", b)
2157
if len(b) == 0 {
@@ -46,9 +82,11 @@ var (
4682
errNoData = errors.New("no data to parse")
4783
)
4884

85+
const respsize = 29
86+
4987
func parseRespiration(b []byte) (Respiration, error) {
5088
// Check to make sure respiration data is long enough
51-
if len(b) != 29 {
89+
if len(b) != respsize {
5290
return Respiration{}, errParseRespDataNotEnoughBytes
5391
}
5492
data := Respiration{}
@@ -69,9 +107,11 @@ var (
69107
errParseRespDataNotEnoughBytes = errors.New("response does not contain enough bytes")
70108
)
71109

110+
const sleepsize = 33
111+
72112
func parseSleep(b []byte) (Sleep, error) {
73113
// Make sure we have enough bytes to parse packet without panic
74-
if len(b) != 33 {
114+
if len(b) != sleepsize {
75115
return Sleep{}, errParseSleepDataNotEnoughBytes
76116
}
77117
data := Sleep{}
@@ -92,27 +132,16 @@ var (
92132
errParseSleepDataNotEnoughBytes = errors.New("response does not contain enough bytes")
93133
)
94134

95-
const (
96-
x2m200AppData = 0x50
97-
x2m200BaseBandIQ = 0x0C
98-
x2m200BaseBandAP = 0x0D
99-
iqheadersize = 29
100-
apheadersize = 29
101-
)
135+
const apheadersize = 29
102136

103137
func parseBaseBandAP(b []byte) (BaseBandAmpPhase, error) {
104138
// Make sure we have enough bytes to parse header without panic
105139
if len(b) < apheadersize {
106140
return BaseBandAmpPhase{}, errParseBaseBandAPNotEnoughBytes
107141
}
108-
x2m200basebandap := binary.LittleEndian.Uint32(b[1:5])
109-
if x2m200basebandap != x2m200BaseBandAP {
110-
log.Println(x2m200basebandap, x2m200BaseBandAP)
111-
return BaseBandAmpPhase{}, errParseBaseBandAPDataHeader
112-
}
113-
114142
var ap BaseBandAmpPhase
115143
ap.Time = time.Now().UnixNano()
144+
ap.Status = status(binary.LittleEndian.Uint32(b[1:5]))
116145
ap.Counter = binary.LittleEndian.Uint32(b[5:9])
117146
ap.Bins = binary.LittleEndian.Uint32(b[9:13])
118147
ap.BinLength = float64(math.Float32frombits(binary.LittleEndian.Uint32(b[13:17])))
@@ -137,26 +166,22 @@ func parseBaseBandAP(b []byte) (BaseBandAmpPhase, error) {
137166
}
138167

139168
var (
140-
// errParseBaseBandAPNoData = errors.New("baseband data is zero length")
141-
// errParseBaseBandAPNotBaseBand = errors.New("baseband data does not start with x2m200AppData")
142169
errParseBaseBandAPNotEnoughBytes = errors.New("baseband data does contain enough bytes")
143-
errParseBaseBandAPDataHeader = errors.New("baseband data does contain ap baseband header")
144170
errParseBaseBandAPIncompletePacket = errors.New("baseband data does contain a full packet of data")
145171
)
146172

173+
const iqheadersize = 29
174+
147175
func parseBaseBandIQ(b []byte) (BaseBandIQ, error) {
148176
// Make sure we have enough bytes to parse header without panic
149177
if len(b) < iqheadersize {
150178
return BaseBandIQ{}, errParseBaseBandIQNotEnoughBytes
151179
}
152-
x2m200basebandiq := binary.LittleEndian.Uint32(b[1:5])
153-
if x2m200basebandiq != x2m200BaseBandIQ {
154-
// log.Println(x2m200basebandiq, x2m200BaseBandIQ)
155-
return BaseBandIQ{}, errParseBaseBandIQDataHeader
156-
}
157180

158181
var iq BaseBandIQ
182+
159183
iq.Time = time.Now().UnixNano()
184+
iq.Status = status(binary.LittleEndian.Uint32(b[1:5]))
160185
iq.Counter = binary.LittleEndian.Uint32(b[5:9])
161186
iq.Bins = binary.LittleEndian.Uint32(b[9:13])
162187
iq.BinLength = float64(math.Float32frombits(binary.LittleEndian.Uint32(b[13:17])))
@@ -183,9 +208,6 @@ func parseBaseBandIQ(b []byte) (BaseBandIQ, error) {
183208
}
184209

185210
var (
186-
errParseBaseBandIQNoData = errors.New("baseband data is zero length")
187-
errParseBaseBandIQNotBaseBand = errors.New("baseband data does not start with x2m200AppData")
188211
errParseBaseBandIQNotEnoughBytes = errors.New("baseband data does contain enough bytes")
189-
errParseBaseBandIQDataHeader = errors.New("baseband data does contain iq baseband header")
190212
errParseBaseBandIQIncompletePacket = errors.New("baseband data does contain a full packet of data")
191213
)

Diff for: x2m200Parsers_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,65 @@ func TestParseSleep(t *testing.T) {
128128
}
129129
}
130130
}
131+
132+
func TestBasebandAmpPhase(t *testing.T) {
133+
cases := []struct {
134+
b []byte
135+
err error
136+
resp BaseBandAmpPhase
137+
}{
138+
{
139+
[]byte{appDataByte, basebandPhaseAmpltudeStartByte},
140+
errParseBaseBandAPNotEnoughBytes,
141+
BaseBandAmpPhase{}}, {
142+
[]byte{appDataByte, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
143+
nil,
144+
BaseBandAmpPhase{}}, {
145+
[]byte{appDataByte, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
146+
errParseBaseBandAPIncompletePacket,
147+
BaseBandAmpPhase{}}, {
148+
[]byte{appDataByte, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
149+
nil,
150+
BaseBandAmpPhase{}},
151+
}
152+
for n, c := range cases {
153+
// log.Println(len(c.b))
154+
_, err := parseBaseBandAP(c.b)
155+
// log.Printf("%#v, %#v \n", resp, err)
156+
if err != c.err {
157+
t.Errorf("test %d Expected: %v, got %v\n", n, c.err, err)
158+
}
159+
// TODO: Validate response
160+
}
161+
}
162+
163+
func TestBasebandIQ(t *testing.T) {
164+
cases := []struct {
165+
b []byte
166+
err error
167+
resp BaseBandIQ
168+
}{
169+
{
170+
[]byte{appDataByte, basebandPhaseAmpltudeStartByte},
171+
errParseBaseBandIQNotEnoughBytes,
172+
BaseBandIQ{}}, {
173+
[]byte{appDataByte, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
174+
nil,
175+
BaseBandIQ{}}, {
176+
[]byte{appDataByte, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
177+
errParseBaseBandIQIncompletePacket,
178+
BaseBandIQ{}}, {
179+
[]byte{appDataByte, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
180+
nil,
181+
BaseBandIQ{}},
182+
}
183+
for n, c := range cases {
184+
// log.Println(len(c.b))
185+
_, err := parseBaseBandIQ(c.b)
186+
// log.Printf("%#v, %#v \n", resp, err)
187+
if err != c.err {
188+
t.Errorf("test %d Expected: %v, got %v\n", n, c.err, err)
189+
}
190+
// TODO: Validate response
191+
}
192+
}

Diff for: x2m200Respiration.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ type status uint32
2525
//go:generate jsonenums -type=status
2626
//go:generate stringer -type=status
2727
const (
28-
respApp status = 594935334
29-
sleepApp status = 594911596
28+
respApp status = 594935334
29+
sleepApp status = 594911596
30+
basebandAP status = 0x0d
31+
basebandIQ status = 0x0c
3032
)
3133

3234
// Sleep is the struct
@@ -46,6 +48,7 @@ type respirationState uint32
4648

4749
type BaseBandAmpPhase struct {
4850
Time int64 `json:"time"`
51+
Status status `json:"status"`
4952
Counter uint32 `json:"counter"`
5053
Bins uint32 `json:"bins"`
5154
BinLength float64 `json:"binlength"`
@@ -58,6 +61,7 @@ type BaseBandAmpPhase struct {
5861

5962
type BaseBandIQ struct {
6063
Time int64 `json:"time"`
64+
Status status `json:"status"`
6165
Counter uint32 `json:"counter"`
6266
Bins uint32 `json:"bins"`
6367
BinLength float64 `json:"binlength"`

Diff for: xethru.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ import (
3030
func Open(device string, port io.ReadWriter) Framer {
3131
// fmt.Println("New instance of Xethru")
3232
// if device == "x2m200" {
33-
return &x2m200Frame{
33+
x := &x2m200Frame{
3434
w: port,
3535
r: bufio.NewReader(port),
3636
}
37+
// TODO: disable all feeds
38+
return x
3739
// }
3840
// return &x2m200Frame{w: port, r: port}
3941
}
4042

41-
// Framer is a wrapper for a serial protocol. it insets the start, crc and end bytes for you
43+
// Framer is a wrapper for a serial protocol. it inserts the start, crc and end bytes for you
4244
type Framer interface {
4345
io.Writer
4446
io.Reader
45-
Ping(t time.Duration) (bool, error)
47+
// Ping(t time.Duration) (bool, error)
4648
Reset() (bool, error)
4749
}
4850

0 commit comments

Comments
 (0)