Added format converters

This commit is contained in:
Yannick Ulrich 2023-02-26 19:08:10 +00:00
parent c046c67dbd
commit c5ca30cd0a
1 changed files with 60 additions and 9 deletions

69
fuse.go
View File

@ -43,6 +43,50 @@ func (i Device) VersionBytes() ([]byte, error) {
return []byte(v + "\n"), err
}
func converterU8(ctx context.Context, in <-chan uint8) <-chan []byte {
out := make(chan []byte, 2)
go func() {
for {
select {
case <- ctx.Done():
return
case event := <-in:
out <- []byte(strconv.Itoa(int(event)) + "\n")
}
}
}()
return out
}
func converterU32(ctx context.Context, in <-chan uint32) <-chan []byte {
out := make(chan []byte, 2)
go func() {
for {
select {
case <- ctx.Done():
return
case event := <-in:
out <- []byte(strconv.Itoa(int(event)) + "\n")
}
}
}()
return out
}
func converterMotionValues(ctx context.Context, in <-chan infinitime.MotionValues) <-chan []byte {
out := make(chan []byte, 2)
go func() {
for {
select {
case <- ctx.Done():
return
case event := <-in:
out <- []byte(strconv.Itoa(int(event.X)) + " " + strconv.Itoa(int(event.Y)) + " " + strconv.Itoa(int(event.Z)) + "\n")
}
}
}()
return out
}
type ITProperty struct {
name string
@ -559,15 +603,22 @@ func startFuse(ctx context.Context, dev *infinitime.Device) error {
Str("target", k.String("fuse.mountpoint")).
Send()
mydev := Device{dev : dev};
properties[0].gen = mydev.dev.WatchHeartRate;
// properties[0].f = mydev.HeartRateBytes;
// properties[1].f = mydev.BatteryLevelBytes;
// properties[2].f = mydev.MotionBytes;
// properties[3].f = mydev.StepCountBytes;
// properties[4].f = mydev.VersionBytes;
// properties[5].f = mydev.AddressBytes;
properties[0].gen = func(ctx context.Context) (<-chan []byte, error) {
ans, err := dev.WatchHeartRate(ctx)
return converterU8(ctx, ans), err
}
properties[1].gen = func(ctx context.Context) (<-chan []byte, error) {
ans, err := dev.WatchBatteryLevel(ctx)
return converterU8(ctx, ans), err
}
properties[2].gen = func(ctx context.Context) (<-chan []byte, error) {
ans, err := dev.WatchMotion(ctx)
return converterMotionValues(ctx, ans), err
}
properties[3].gen = func(ctx context.Context) (<-chan []byte, error) {
ans, err := dev.WatchStepCount(ctx)
return converterU32(ctx, ans), err
}
myfs, err = dev.FS()
if err != nil {