itd/cmd/itgui/motion.go

63 lines
1.4 KiB
Go
Raw Normal View History

2021-10-25 16:45:19 +00:00
package main
import (
2022-05-01 22:22:28 +00:00
"context"
2022-05-05 21:00:49 +00:00
"fmt"
2021-10-25 16:45:19 +00:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
2023-04-21 02:54:58 +00:00
"go.elara.ws/itd/api"
2021-10-25 16:45:19 +00:00
)
2022-05-05 21:00:49 +00:00
func motionTab(ctx context.Context, client *api.Client, w fyne.Window) fyne.CanvasObject {
// Create titledText for each coordinate
xText := newTitledText("X Coordinate", "0")
yText := newTitledText("Y Coordinate", "0")
zText := newTitledText("Z Coordinate", "0")
var ctxCancel func()
// Create start button
toggleBtn := widget.NewButton("Start", nil)
// Set button's on tapped callback
toggleBtn.OnTapped = func() {
switch toggleBtn.Text {
case "Start":
// Create new context for motion
motionCtx, cancel := context.WithCancel(ctx)
// Set ctxCancel to function so that stop button can run it
ctxCancel = cancel
// Watch motion
motionCh, err := client.WatchMotion(motionCtx)
if err != nil {
guiErr(err, "Error watching motion", false, w)
return
2021-10-25 16:45:19 +00:00
}
2022-05-05 21:00:49 +00:00
go func() {
// For every motion event
for motion := range motionCh {
// Set coordinates
xText.SetBody(fmt.Sprint(motion.X))
yText.SetBody(fmt.Sprint(motion.Y))
zText.SetBody(fmt.Sprint(motion.Z))
}
}()
// Set button text to "Stop"
toggleBtn.SetText("Stop")
case "Stop":
// Cancel motion context
ctxCancel()
// Set button text to "Start"
toggleBtn.SetText("Start")
2021-10-25 16:45:19 +00:00
}
2022-05-05 21:00:49 +00:00
}
return container.NewVScroll(container.NewVBox(
toggleBtn,
xText,
yText,
zText,
))
2021-10-25 16:45:19 +00:00
}