Adding a way to find new devices

This commit is contained in:
2025-10-22 23:15:33 +01:00
parent a748c9644a
commit b60351319f
3 changed files with 75 additions and 5 deletions
+21 -3
View File
@@ -1,17 +1,35 @@
package cmd
import (
"esdi/peripheral"
"esdi/repl"
"github.com/spf13/cobra"
)
func replCmdAction(cmd *cobra.Command, args []string) {
repl := repl.NewREPL(repl.REPLCfg{
r := repl.NewREPL(repl.REPLCfg{
PS1: "\rESDI > ",
})
repl.Start()
repl.Close()
listDevicesREPLCmd := repl.Command{
Name: "ls",
Usage: "lists the currently available devices",
Action: func(r *repl.REPL, args []string) error {
perClerk := peripheral.NewPeripheralDeviceClerk()
err := perClerk.FindDevices()
if err != nil {
return err
}
return nil
},
}
r.RegisterCMD(listDevicesREPLCmd)
r.Start()
r.Close()
}
// removeLabelCmd represents the removeLabel command
+51
View File
@@ -0,0 +1,51 @@
// Package peripheral will handle communicating with peripherals
package peripheral
import (
"fmt"
"path/filepath"
)
type PeripheralType string
const (
DisplayPeripheral PeripheralType = "display"
)
type PeripheralDevice struct {
Port string
Name [32]byte
}
type PeripheralDeviceClerk struct {
Devices map[string]PeripheralDevice
}
func NewPeripheralDeviceClerk() *PeripheralDeviceClerk {
return &PeripheralDeviceClerk{}
}
func (clerk *PeripheralDeviceClerk) listPorts() ([]string, error) {
ttyUSBs, err := filepath.Glob("/dev/ttyUSB*")
if err != nil {
return []string{}, err
}
return ttyUSBs, nil
}
func (clerk *PeripheralDeviceClerk) FindDevices() error {
ports, err := clerk.listPorts()
if err != nil {
return err
}
for _, p := range ports {
fmt.Println(p)
}
return nil
}
// Add a peripheral device manager
// Something that auto discovers available devices
+3 -2
View File
@@ -38,8 +38,9 @@ func (r *REPL) Close() {
// Do whatever cleanup we have to do
}
func (r *REPL) RegisterCMD() {
// Code to register a new command
func (r *REPL) RegisterCMD(newCMD Command) {
// Add a verification to check that the command doesn't exist. Fatal if it does
r.Commands[newCMD.Name] = newCMD
}
func (r *REPL) printPrompt() {