From c5ca30cd0a44ee4af082e4f72c77398b169528f6 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sun, 26 Feb 2023 19:08:10 +0000 Subject: [PATCH] Added format converters --- fuse.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/fuse.go b/fuse.go index 32d2a98..d4c141a 100644 --- a/fuse.go +++ b/fuse.go @@ -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 {