diff --git a/cmd/repl.go b/cmd/repl.go new file mode 100644 index 0000000..503783e --- /dev/null +++ b/cmd/repl.go @@ -0,0 +1,113 @@ +package cmd + +import ( + "esdi/peripheral" + "fmt" + "strconv" + + repl "github.com/ESilva15/ESgoRepl" + + "github.com/spf13/cobra" +) + +func replCmdAction(cmd *cobra.Command, args []string) { + r := repl.NewREPL(repl.REPLCfg{ + PS1: "\rESDI > ", + }) + + perClerk := peripheral.NewPeripheralDeviceClerk() + + discoverDevicesREPLCmd := repl.Command{ + Name: "discover", + Usage: "discovers connected devices", + Action: func(r *repl.REPL, args []string) error { + err := perClerk.FindDevices() + if err != nil { + return err + } + + return nil + }, + } + + listDevicesREPLCmd := repl.Command{ + Name: "list", + Usage: "lists connected devices", + Action: func(r *repl.REPL, args []string) error { + _ = perClerk.ListDevices() + + return nil + }, + } + + listDeviceAPIREPLCmd := repl.Command{ + Name: "v-api", + Usage: "shows API of a device - pass its ID", + Action: func(r *repl.REPL, args []string) error { + // We should add this to the REPL instead + if len(args) < 1 { + return fmt.Errorf("requires at least on argument") + } + + // First and only argument should be the ID of the device we want to use + targetID, err := strconv.ParseInt(args[0], 10, 0) + if err != nil { + return err + } + + err = perClerk.ListDeviceAPI(uint8(targetID)) + if err != nil { + fmt.Println("failed to view device API: ", err.Error()) + } + + return nil + }, + } + + runDeviceAPIREPLCmd := repl.Command{ + Name: "v-run", + Usage: "runs a funcion of a device - pass its ID and function name", + Action: func(r *repl.REPL, args []string) error { + // We should add this to the REPL instead + if len(args) < 3 { + return fmt.Errorf("requires at least on argument") + } + + // First and only argument should be the ID of the device we want to use + targetID, err := strconv.ParseInt(args[0], 10, 0) + if err != nil { + return err + } + + fnName := args[1] + fnArgs := args[2:] + + err = perClerk.RunDeviceFunction(uint8(targetID), fnName, fnArgs) + if err != nil { + return err + } + + return nil + }, + } + + r.RegisterCMD(discoverDevicesREPLCmd) + r.RegisterCMD(listDevicesREPLCmd) + r.RegisterCMD(listDeviceAPIREPLCmd) + r.RegisterCMD(runDeviceAPIREPLCmd) + + r.Start() + r.Close() +} + +// removeLabelCmd represents the removeLabel command +var replCmd = &cobra.Command{ + Use: "repl", + Short: "run the repl to interact with the display", + Long: ``, + Run: replCmdAction, +} + +func init() { + rootCmd.AddCommand(replCmd) +} diff --git a/cmd/tui.go b/cmd/tui.go deleted file mode 100644 index 7c7fae6..0000000 --- a/cmd/tui.go +++ /dev/null @@ -1,27 +0,0 @@ -package cmd - -import ( - "esdi/tui" - "fmt" - - "github.com/spf13/cobra" -) - -func tuiCmdAction(cmd *cobra.Command, args []string) { - err := tui.Init() - if err != nil { - fmt.Printf("error running TUI: %s\n", err.Error()) - } -} - -// removeLabelCmd represents the removeLabel command -var tuiCmd = &cobra.Command{ - Use: "tui", - Short: "runs an interactive application", - Long: ``, - Run: tuiCmdAction, -} - -func init() { - rootCmd.AddCommand(tuiCmd) -} diff --git a/go.mod b/go.mod index aface14..bd3c5de 100644 --- a/go.mod +++ b/go.mod @@ -6,21 +6,14 @@ require ( github.com/ESilva15/ESgoRepl v0.1.0 github.com/ESilva15/gobngsdk v0.0.2 github.com/ESilva15/goirsdk v0.2.0 - github.com/rivo/tview v0.42.0 github.com/spf13/cobra v1.8.1 github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 ) require ( - github.com/gdamore/encoding v1.0.1 // indirect - github.com/gdamore/tcell/v2 v2.8.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect - github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.29.0 // indirect - golang.org/x/term v0.28.0 // indirect - golang.org/x/text v0.21.0 // indirect + golang.org/x/text v0.19.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5f85c31..3dbe5e9 100644 --- a/go.sum +++ b/go.sum @@ -5,24 +5,10 @@ github.com/ESilva15/gobngsdk v0.0.2/go.mod h1:cKLaZRgM0tGGXDvosaSjHnxeyC5Y6rg7zC github.com/ESilva15/goirsdk v0.2.0 h1:zmtYyH3ayGRcDfzzLrJmEpX6zdmCHTUqbs4tnV2hXmI= github.com/ESilva15/goirsdk v0.2.0/go.mod h1:5borQbw+L4fe9b58JFHRHZwrzz0sMVAteuYbRnFOc0Q= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw= -github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo= -github.com/gdamore/tcell/v2 v2.8.1 h1:KPNxyqclpWpWQlPLx6Xui1pMk8S+7+R37h3g07997NU= -github.com/gdamore/tcell/v2 v2.8.1/go.mod h1:bj8ori1BG3OYMjmb3IklZVWfZUJ1UBQt9JXrOCOhGWw= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/rivo/tview v0.42.0 h1:b/ftp+RxtDsHSaynXTbJb+/n/BxDEi+W3UfF5jILK6c= -github.com/rivo/tview v0.42.0/go.mod h1:cSfIYfhpSGCjp3r/ECJb+GKS7cGJnqV8vfjQPwoXyfY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= @@ -30,71 +16,10 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= -golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= -golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= -golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg= -golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= -golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/peripheral/devices/cdash_display.go b/peripheral/devices/cdash_display.go index f2ad2cf..741662a 100644 --- a/peripheral/devices/cdash_display.go +++ b/peripheral/devices/cdash_display.go @@ -11,7 +11,6 @@ import ( const ( newWindowCMDID types.Command = 3 destroyWindowCMDID types.Command = 4 - moveWindowCMDID types.Command = 5 ) var ( @@ -69,13 +68,6 @@ var CDashDisplay = Device{ ArgCheck: destroyWindowArgCheck, Fn: destroyWindow, }, - "move-window": { - Identifier: moveWindowCMDID, - Name: "move-window", - Desc: "Moves a selected window - pass the window ID", - ArgCheck: moveWindowArgCheck, - Fn: moveWindow, - }, }, } @@ -91,6 +83,8 @@ func createWindowArgCheck(args []string) error { func createWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) { // Parse the command // x0, y0, width, height, title # add other decorations later on + // fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args) + x0, err := strconv.ParseInt(args[0], 10, 0) if err != nil { return 0, []byte{}, err @@ -138,6 +132,8 @@ func destroyWindowArgCheck(args []string) error { func destroyWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) { // Parse the command + fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args) + type UIWindowDestructPacket struct { WinID int16 } @@ -158,24 +154,3 @@ func destroyWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error return dCMD.GetIdentifier(), bytes, nil } - -func moveWindowArgCheck(args []string) error { - if len(args) != 1 { - return fmt.Errorf("wrong parameters, got %d, want %d. "+ - "Command asks for: winID", len(args), 1) - } - - return nil -} - -func moveWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) { - // We can edit this struct to be a generic 'window edit' struct. Either - // that or implement a way of passing arbitrary fields to update - type UIWindowMovePacket struct { - WinID int16 - Direction uint8 - Quantity uint8 - } - - return dCMD.GetIdentifier(), []byte{}, nil -} diff --git a/peripheral/devices/devices.go b/peripheral/devices/devices.go index da48ea7..b3fd134 100644 --- a/peripheral/devices/devices.go +++ b/peripheral/devices/devices.go @@ -1,10 +1,7 @@ // Package devices will define the available devices we can communicate with package devices -import ( - "esdi/peripheral/types" - "fmt" -) +import "esdi/peripheral/types" // IDs for our devices. They need to be correctly mapped on the devices // themselves so we can discover them @@ -69,7 +66,6 @@ func (dCMD *DeviceCMD) Run(args []string) (types.Command, []byte, error) { } // Run the function - fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args) return dCMD.Fn(dCMD, args) } diff --git a/peripheral/peripheral.go b/peripheral/peripheral.go index 4afd76e..8d90d3e 100644 --- a/peripheral/peripheral.go +++ b/peripheral/peripheral.go @@ -5,7 +5,6 @@ import ( "esdi/peripheral/devices" "fmt" "path/filepath" - "strings" ) type PeripheralType string @@ -48,13 +47,12 @@ func (clerk *PeripheralDeviceClerk) FindDeviceAPI(ID uint8) *devices.Device { return nil } -func (clerk *PeripheralDeviceClerk) FindDevices() (string, error) { +func (clerk *PeripheralDeviceClerk) FindDevices() error { ports, err := clerk.listPorts() if err != nil { - return "", err + return err } - var s strings.Builder for _, p := range ports { newDevice := NewPeripheralDevice(p) @@ -75,17 +73,17 @@ func (clerk *PeripheralDeviceClerk) FindDevices() (string, error) { fmt.Println(p, string(newDevice.Name[:])) } - return s.String(), nil + // go clerk.clerkBackgroundJob() + + return nil } -func (clerk *PeripheralDeviceClerk) ListDevices() (string, error) { - var s strings.Builder - +func (clerk *PeripheralDeviceClerk) ListDevices() error { for _, d := range clerk.Devices { - s.WriteString(fmt.Sprintf("[%2d] %s\n", d.ID, d.Name)) + fmt.Printf("[%2d] %s\n", d.ID, d.Name) } - return s.String(), nil + return nil } func (clerk *PeripheralDeviceClerk) getDevice(ID uint8) *PeripheralDevice { @@ -96,43 +94,53 @@ func (clerk *PeripheralDeviceClerk) getDevice(ID uint8) *PeripheralDevice { return clerk.Devices[ID] } -func (clerk *PeripheralDeviceClerk) ListDeviceAPI(ID uint8) (string, error) { +func (clerk *PeripheralDeviceClerk) ListDeviceAPI(ID uint8) error { dev := clerk.getDevice(ID) if dev == nil { - return "", fmt.Errorf("device with ID %d not found", ID) + return fmt.Errorf("device with ID %d not found", ID) } - var s strings.Builder for _, f := range dev.DeviceAPI.API { - s.WriteString(fmt.Sprintf("%s\t%s\n", f.Name, f.Desc)) + fmt.Printf("%s\t%s\n", f.Name, f.Desc) } - return s.String(), nil + return nil } -func (clerk *PeripheralDeviceClerk) RunDeviceFunction(ID uint8, f string, - args []string) (string, error) { +func (clerk *PeripheralDeviceClerk) RunDeviceFunction(ID uint8, f string, args []string) error { dev := clerk.getDevice(ID) if dev == nil { - return "", fmt.Errorf("device with ID %d not found", ID) + return fmt.Errorf("device with ID %d not found", ID) } devAPI := dev.DeviceAPI cmd := devAPI.HasFunction(f) if cmd == nil { - return "", fmt.Errorf("requested function %s doesnt exist", f) + return fmt.Errorf("requested function %s doesnt exist", f) } // Execute the function command, payload, err := cmd.Run(args) if err != nil { - return "", err + return err } err = dev.SendCommand(command, payload) if err != nil { - return "", err + return err } - return "", nil + return nil } + +// func (c *PeripheralDeviceClerk) clerkBackgroundJob() { +// for { +// c.mu.RLock() +// for _, d := range c.Devices { +// // Check the state of the device +// if d.CommState == CommIdle { +// } +// } +// c.mu.Unlock() +// } +// } diff --git a/tui/commands.go b/tui/commands.go deleted file mode 100644 index 8114088..0000000 --- a/tui/commands.go +++ /dev/null @@ -1,215 +0,0 @@ -package tui - -import ( - "esdi/peripheral" - "fmt" - "strconv" - "strings" -) - -type Command struct { - Name string - Usage string - ArgCheck func(args []string) error - Action func(args []string) (string, error) -} - -func (c *Command) Run(args []string) (string, error) { - if c.ArgCheck != nil { - err := c.ArgCheck(args) - if err != nil { - return "", err - } - } - - return c.Action(args) -} - -type CommandManager struct { - Commands map[string]Command -} - -func NewCommandManager() *CommandManager { - return &CommandManager{ - Commands: make(map[string]Command), - } -} - -func (cm *CommandManager) RegisterCommand(c Command) { - cm.Commands[c.Name] = c -} - -func (cm *CommandManager) RunCommand(args []string) (string, error) { - if len(args) < 1 { - return "", fmt.Errorf("require at least one argument: the command") - } - - command, exists := cm.Commands[args[0]] - if !exists { - return "", fmt.Errorf("no such command `%s`", args[0]) - } - - return command.Run(args[1:]) -} - -func getCommands() *CommandManager { - perClerk := peripheral.NewPeripheralDeviceClerk() - cm := NewCommandManager() - - discoverDevicesREPLCmd := Command{ - Name: "discover", - Usage: "discovers connected devices", - Action: func(args []string) (string, error) { - output, err := perClerk.FindDevices() - if err != nil { - return "", err - } - - return output, nil - }, - } - cm.RegisterCommand(discoverDevicesREPLCmd) - - listDevicesREPLCmd := Command{ - Name: "list", - Usage: "lists connected devices", - Action: func(args []string) (string, error) { - output, err := perClerk.ListDevices() - if err != nil { - return "", err - } - - return output, nil - }, - } - cm.RegisterCommand(listDevicesREPLCmd) - - listDeviceAPIREPLCmd := Command{ - Name: "v-api", - Usage: "shows API of a device - pass its ID", - ArgCheck: func(args []string) error { - if len(args) < 1 { - return fmt.Errorf("requires at least on argument") - } - - return nil - }, - Action: func(args []string) (string, error) { - // First and only argument should be the ID of the device we want to use - targetID, err := strconv.ParseInt(args[0], 10, 0) - if err != nil { - return "", err - } - - output, err := perClerk.ListDeviceAPI(uint8(targetID)) - if err != nil { - fmt.Println("failed to view device API: ", err.Error()) - } - - return output, nil - }, - } - cm.RegisterCommand(listDeviceAPIREPLCmd) - - runDeviceAPIREPLCmd := Command{ - Name: "v-run", - Usage: "runs a funcion of a device - pass its ID and function name", - ArgCheck: func(args []string) error { - // We should add this to the REPL instead - if len(args) < 3 { - return fmt.Errorf("requires at least on argument") - } - return nil - }, - Action: func(args []string) (string, error) { - // First and only argument should be the ID of the device we want to use - targetID, err := strconv.ParseInt(args[0], 10, 0) - if err != nil { - return "", err - } - - fnName := args[1] - fnArgs := args[2:] - - output, err := perClerk.RunDeviceFunction(uint8(targetID), fnName, fnArgs) - if err != nil { - return "", err - } - - return output, nil - }, - } - cm.RegisterCommand(runDeviceAPIREPLCmd) - - helpCmd := Command{ - Name: ".help", - Usage: " type `.help` for help", - Action: func(args []string) (string, error) { - var s strings.Builder - for _, cmd := range cm.Commands { - s.WriteString(fmt.Sprintf("%-20s\t%s\n", cmd.Name, cmd.Usage)) - } - - return s.String(), nil - }, - } - cm.RegisterCommand(helpCmd) - - return cm -} - -func parseArgs(l string) ([]string, error) { - args := []string{} - input := strings.TrimSpace(l) - - type ArgParserState uint8 - const ( - readingQuotedStr ArgParserState = iota - readingNormalStr - storeArg - ) - - curState := readingNormalStr - - curStr := []byte{} - for k := 0; k < len(input); { - switch curState { - case readingNormalStr: - switch input[k] { - case '"': - curState = readingQuotedStr - case ' ': - curState = storeArg - default: - curStr = append(curStr, input[k]) - } - k++ - case readingQuotedStr: - switch input[k] { - case '"': - if k+1 < len(input) { - k++ - } - curState = storeArg - default: - curStr = append(curStr, input[k]) - } - k++ - case storeArg: - args = append(args, string(curStr)) - curStr = nil - curState = readingNormalStr - } - - if k == len(input) { - args = append(args, string(curStr)) - curStr = nil - } - } - - if curState == readingQuotedStr { - return []string{}, fmt.Errorf("quoted string was not closed: %s", curStr) - } - - return args, nil -} diff --git a/tui/tui.go b/tui/tui.go deleted file mode 100644 index bcddcba..0000000 --- a/tui/tui.go +++ /dev/null @@ -1,77 +0,0 @@ -package tui - -import ( - "fmt" - "os" - - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" -) - -func Init() error { - cm := getCommands() - - app := tview.NewApplication() - - output := tview.NewTextView(). - SetDynamicColors(true). - SetScrollable(true). - SetWrap(true) - - output. - SetBorder(true). - SetTitle("OUTPUT:") - - input := tview.NewInputField(). - SetLabel("> "). - SetFieldWidth(0) - - input. - SetBorder(true). - SetTitle("INPUT:") - - input.SetDoneFunc(func(key tcell.Key) { - if key != tcell.KeyEnter { - return - } - - text := input.GetText() - fmt.Fprintf(output, "> %s\n", text) - - args, err := parseArgs(text) - if err != nil { - fmt.Fprintf(output, "ERROR\n%s", err.Error()) - return - } - - cmdOutput, err := cm.RunCommand(args) - if err != nil { - fmt.Fprintf(output, "ERROR\n%s", err.Error()) - return - } else { - fmt.Fprintf(output, "%s\n", cmdOutput) - } - - input.SetText("") - output.ScrollToEnd() - }) - - layout := tview.NewFlex(). - SetDirection(tview.FlexRow). - AddItem(output, 0, 1, false). - AddItem(input, 3, 0, true) - - app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { - if event.Key() == tcell.KeyCtrlC { - app.Stop() - return nil - } - return event - }) - - if err := app.SetRoot(layout, true).Run(); err != nil { - fmt.Fprintln(os.Stderr, err) - } - - return nil -}