Added ECHO cmd and fixed some timing issues
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
@@ -410,6 +411,66 @@ func (s *serialPort) espnowUnicastTest(clientID, seq uint32) (*pb.EspNowUnicastT
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// EchoPingResult is the host-side round-trip for ESP-NOW echo ping.
|
||||
type EchoPingResult struct {
|
||||
Success bool `json:"success"`
|
||||
ClientID uint32 `json:"client_id"`
|
||||
TimestampUs uint64 `json:"timestamp_us"`
|
||||
RttMs float64 `json:"rtt_ms"` // goTool: full UART round-trip
|
||||
EspRttUs uint32 `json:"esp_rtt_us"` // master: µs delta ping send → pong recv
|
||||
}
|
||||
|
||||
func (s *serialPort) echoPing(clientID uint32) (*EchoPingResult, error) {
|
||||
t0 := time.Now()
|
||||
timestampUs := uint64(t0.UnixMicro())
|
||||
req := &pb.EspNowEchoPingRequest{
|
||||
ClientId: clientID,
|
||||
TimestampUs: timestampUs,
|
||||
}
|
||||
msg := &pb.UartMessage{
|
||||
Type: pb.MessageType_ESPNOW_ECHO_PING,
|
||||
Payload: &pb.UartMessage_EspnowEchoPingRequest{
|
||||
EspnowEchoPingRequest: req,
|
||||
},
|
||||
}
|
||||
body, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode: %w", err)
|
||||
}
|
||||
payload := append([]byte{byte(pb.MessageType_ESPNOW_ECHO_PING)}, body...)
|
||||
respPayload, err := s.exchangePayload(payload, "ESPNOW_ECHO_PING")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rttMs := float64(time.Since(t0).Microseconds()) / 1000.0
|
||||
|
||||
var respMsg pb.UartMessage
|
||||
if err := proto.Unmarshal(respPayload[1:], &respMsg); err != nil {
|
||||
return nil, fmt.Errorf("decode: %w", err)
|
||||
}
|
||||
r := respMsg.GetEspnowEchoPingResponse()
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("missing espnow_echo_ping_response")
|
||||
}
|
||||
if !r.GetSuccess() {
|
||||
return &EchoPingResult{
|
||||
Success: false,
|
||||
ClientID: r.GetClientId(),
|
||||
RttMs: rttMs,
|
||||
}, nil
|
||||
}
|
||||
if r.GetTimestampUs() != timestampUs {
|
||||
return nil, fmt.Errorf("timestamp mismatch: sent %d got %d", timestampUs, r.GetTimestampUs())
|
||||
}
|
||||
return &EchoPingResult{
|
||||
Success: true,
|
||||
ClientID: r.GetClientId(),
|
||||
TimestampUs: r.GetTimestampUs(),
|
||||
RttMs: rttMs,
|
||||
EspRttUs: r.GetEspRttUs(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *managedSerial) FindMe(clientID uint32) error {
|
||||
return m.withPort(func(sp *serialPort) error {
|
||||
return runFindMeClient(sp, clientID)
|
||||
@@ -490,6 +551,20 @@ func (s *serialPort) EspnowUnicastTest(clientID, seq uint32) (*pb.EspNowUnicastT
|
||||
return s.espnowUnicastTest(clientID, seq)
|
||||
}
|
||||
|
||||
func (s *serialPort) EchoPing(clientID uint32) (*EchoPingResult, error) {
|
||||
return s.echoPing(clientID)
|
||||
}
|
||||
|
||||
func (m *managedSerial) EchoPing(clientID uint32) (*EchoPingResult, error) {
|
||||
var result *EchoPingResult
|
||||
err := m.withPort(func(sp *serialPort) error {
|
||||
var e error
|
||||
result, e = sp.echoPing(clientID)
|
||||
return e
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *serialPort) LedRing(req *pb.LedRingProgressRequest) (*pb.LedRingProgressResponse, error) {
|
||||
return s.ledRingProgress(req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user