From b60351319f8a420486380ade66641385ba31835d Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 22 Oct 2025 23:15:33 +0100 Subject: [PATCH] Adding a way to find new devices --- cmd/repl.go | 24 ++++++++++++++++--- peripheral/peripheral.go | 51 ++++++++++++++++++++++++++++++++++++++++ repl/repl.go | 5 ++-- 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 peripheral/peripheral.go diff --git a/cmd/repl.go b/cmd/repl.go index 6d7a5de..10f19b4 100644 --- a/cmd/repl.go +++ b/cmd/repl.go @@ -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 diff --git a/peripheral/peripheral.go b/peripheral/peripheral.go new file mode 100644 index 0000000..dca813b --- /dev/null +++ b/peripheral/peripheral.go @@ -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 diff --git a/repl/repl.go b/repl/repl.go index 1d377fd..aeca455 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -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() {