Added an ID to the devices
This commit is contained in:
+16
-4
@@ -12,11 +12,12 @@ func replCmdAction(cmd *cobra.Command, args []string) {
|
||||
PS1: "\rESDI > ",
|
||||
})
|
||||
|
||||
listDevicesREPLCmd := repl.Command{
|
||||
Name: "ls",
|
||||
Usage: "lists the currently available devices",
|
||||
perClerk := peripheral.NewPeripheralDeviceClerk()
|
||||
|
||||
discoverDevicesREPLCmd := repl.Command{
|
||||
Name: "discover",
|
||||
Usage: "discovers connected devices",
|
||||
Action: func(r *repl.REPL, args []string) error {
|
||||
perClerk := peripheral.NewPeripheralDeviceClerk()
|
||||
err := perClerk.FindDevices()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -26,6 +27,17 @@ func replCmdAction(cmd *cobra.Command, args []string) {
|
||||
},
|
||||
}
|
||||
|
||||
listDevicesREPLCmd := repl.Command{
|
||||
Name: "list",
|
||||
Usage: "lists connected devices",
|
||||
Action: func(r *repl.REPL, args []string) error {
|
||||
_ = perClerk.ListDevices()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
r.RegisterCMD(discoverDevicesREPLCmd)
|
||||
r.RegisterCMD(listDevicesREPLCmd)
|
||||
|
||||
r.Start()
|
||||
|
||||
@@ -36,7 +36,8 @@ func (s PeripheralDeviceState) String() string {
|
||||
type PeripheralDevice struct {
|
||||
State PeripheralDeviceState
|
||||
CommState CommState
|
||||
Name [32]byte
|
||||
ID uint8
|
||||
Name string
|
||||
WT *WalkieTalkie
|
||||
}
|
||||
|
||||
@@ -93,7 +94,8 @@ func (p *PeripheralDevice) ToConnectedIdling() {
|
||||
}
|
||||
|
||||
func (p *PeripheralDevice) Merge(packet *IdentificationPacket) {
|
||||
p.Name = packet.Name
|
||||
p.Name = string(packet.Name[:])
|
||||
p.ID = packet.DeviceID
|
||||
}
|
||||
|
||||
func (p *PeripheralDevice) Disconnect() error {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
type IdentificationPacket struct {
|
||||
StartMarker byte
|
||||
DeviceID uint8
|
||||
PktType PacketType
|
||||
Name [32]byte
|
||||
EndMarker byte
|
||||
@@ -33,6 +34,9 @@ func (pkt *IdentificationPacket) Validate() bool {
|
||||
fmt.Println(pkt.EndMarker)
|
||||
fmt.Println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑")
|
||||
|
||||
// Change this to return an error and add a validation against available
|
||||
// device ids
|
||||
|
||||
if pkt.StartMarker != startOfText ||
|
||||
pkt.EndMarker != endOfText {
|
||||
return false
|
||||
|
||||
@@ -13,6 +13,7 @@ const (
|
||||
)
|
||||
|
||||
type PeripheralDeviceClerk struct {
|
||||
// mu sync.RWMutex
|
||||
Devices map[string]*PeripheralDevice
|
||||
}
|
||||
|
||||
@@ -54,11 +55,27 @@ func (clerk *PeripheralDeviceClerk) FindDevices() error {
|
||||
fmt.Println(p, string(newDevice.Name[:]))
|
||||
}
|
||||
|
||||
// go clerk.clerkBackgroundJob()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (clerk *PeripheralDeviceClerk) ListDevices() error {
|
||||
for _, d := range clerk.Devices {
|
||||
fmt.Printf("[%d] %s\n", d.ID, d.Name)
|
||||
}
|
||||
|
||||
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()
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user