Add find-me LED locate on master and slaves via ESP-NOW.
UART FIND_ME (client_id 0 = local ring, >0 = unicast), ESPNOW_FIND_ME payload, CLI/dashboard buttons per slave. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
"powerpod/gotool/pb"
|
||||
)
|
||||
|
||||
func runFindMe(sp *serialPort, args []string) error {
|
||||
fs := flag.NewFlagSet("find-me", flag.ExitOnError)
|
||||
clientID := fs.Uint("client", 0, "0=master LED ring, >0=ESP-NOW unicast to slave id")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
return runFindMeClient(sp, uint32(*clientID))
|
||||
}
|
||||
|
||||
func runFindMeClient(sp *serialPort, clientID uint32) error {
|
||||
resp, err := sp.espnowFindMe(clientID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !resp.GetSuccess() {
|
||||
return fmt.Errorf("find-me rejected (client_id=%d)", resp.GetClientId())
|
||||
}
|
||||
if clientID == 0 {
|
||||
fmt.Println("find-me started on master")
|
||||
} else {
|
||||
fmt.Printf("find-me sent to slave %d\n", clientID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *serialPort) espnowFindMe(clientID uint32) (*pb.EspNowFindMeResponse, error) {
|
||||
msg := &pb.UartMessage{
|
||||
Type: pb.MessageType_FIND_ME,
|
||||
Payload: &pb.UartMessage_EspnowFindMeRequest{
|
||||
EspnowFindMeRequest: &pb.EspNowFindMeRequest{ClientId: clientID},
|
||||
},
|
||||
}
|
||||
body, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode: %w", err)
|
||||
}
|
||||
payload := append([]byte{byte(pb.MessageType_FIND_ME)}, body...)
|
||||
respPayload, err := s.exchangePayload(payload, "FIND_ME")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var respMsg pb.UartMessage
|
||||
if err := proto.Unmarshal(respPayload[1:], &respMsg); err != nil {
|
||||
return nil, fmt.Errorf("decode: %w", err)
|
||||
}
|
||||
r := respMsg.GetEspnowFindMeResponse()
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("missing espnow_find_me_response")
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
Reference in New Issue
Block a user