diff --git a/goTool/README.md b/goTool/README.md index 893480e..3f7b122 100644 --- a/goTool/README.md +++ b/goTool/README.md @@ -24,6 +24,8 @@ go run . -port /dev/ttyUSB0 clients |---------|--------------|-------------| | `version` | `0x03` | Prints `version` and `git_hash` from firmware | | `clients` | `0x04` | Lists slaves registered on the master via ESP-NOW | +| `deadzone` | `0x06` | Get/set accelerometer deadzone LSB (`-set`, `-value`, `-client`, `-all`) | +| `accel` | `0x18` | Read current BMA456 XYZ (raw LSB, ±2g); alias `accel-read` | | `unicast-test` | `0x07` | Sends ESP-NOW unicast test to one slave (`-client`, `-seq`) | | `test` | — | Run an automated scenario (JSON configs under `testdata/`) | | `serve` | — | Web dashboard at `http://localhost:8080` (WebSocket live updates) | diff --git a/goTool/client_api.go b/goTool/client_api.go index e4e176c..e91a108 100644 --- a/goTool/client_api.go +++ b/goTool/client_api.go @@ -101,6 +101,25 @@ func decodeClientsPayload(payload []byte) ([]*pb.ClientInfo, error) { return info.GetClients(), nil } +func (s *serialPort) readAccel() (*pb.AccelReadResponse, error) { + payload, err := s.exchange(byte(pb.MessageType_ACCEL_READ), "ACCEL_READ") + if err != nil { + return nil, err + } + var msg pb.UartMessage + if err := proto.Unmarshal(payload[1:], &msg); err != nil { + return nil, fmt.Errorf("decode: %w", err) + } + if msg.GetType() != pb.MessageType_ACCEL_READ { + return nil, fmt.Errorf("unexpected type %v", msg.GetType()) + } + r := msg.GetAccelReadResponse() + if r == nil { + return nil, fmt.Errorf("missing accel_read_response") + } + return r, nil +} + func (s *serialPort) getVersion() (*pb.VersionResponse, error) { payload, err := s.exchange(byte(pb.MessageType_VERSION), "VERSION") if err != nil { diff --git a/goTool/cmd_accel.go b/goTool/cmd_accel.go new file mode 100644 index 0000000..7ab5351 --- /dev/null +++ b/goTool/cmd_accel.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" +) + +func runAccel(sp *serialPort) error { + r, err := sp.readAccel() + if err != nil { + return err + } + if !r.GetSuccess() { + return fmt.Errorf("accel read failed (sensor not ready?)") + } + fmt.Printf("accel: x=%d y=%d z=%d (raw LSB, ±2g)\n", r.GetX(), r.GetY(), r.GetZ()) + return nil +} diff --git a/goTool/main.go b/goTool/main.go index bc73576..0a80570 100644 --- a/goTool/main.go +++ b/goTool/main.go @@ -16,6 +16,7 @@ func usage() { fmt.Fprintf(os.Stderr, " version firmware version and git hash\n") fmt.Fprintf(os.Stderr, " clients registered ESP-NOW slaves on the master\n") fmt.Fprintf(os.Stderr, " deadzone get/set accelerometer deadzone (LSB)\n") + fmt.Fprintf(os.Stderr, " accel read current accelerometer XYZ (raw LSB)\n") fmt.Fprintf(os.Stderr, " unicast-test send ESP-NOW unicast test to one slave\n") fmt.Fprintf(os.Stderr, " test run automated scenario (see testdata/)\n") fmt.Fprintf(os.Stderr, " serve web dashboard (Bootstrap + WebSocket)\n") @@ -50,7 +51,7 @@ func main() { os.Exit(2) } runErr = runServe(*portName, *baud, flag.Args()[1:]) - case "version", "clients", "client-info", "deadzone", "accel-deadzone", "unicast-test", "unicast_test", "led-ring", "led_ring", "find-me", "find_me", "restart", "ota", "ota-progress", "ota_progress": + case "version", "clients", "client-info", "deadzone", "accel-deadzone", "accel", "accel-read", "accel_read", "unicast-test", "unicast_test", "led-ring", "led_ring", "find-me", "find_me", "restart", "ota", "ota-progress", "ota_progress": if *portName == "" { fmt.Fprintf(os.Stderr, "command %q requires -port\n\n", cmd) usage() @@ -68,6 +69,8 @@ func main() { runErr = runClients(sp) case "deadzone", "accel-deadzone": runErr = runDeadzone(sp, flag.Args()[1:]) + case "accel", "accel-read", "accel_read": + runErr = runAccel(sp) case "unicast-test", "unicast_test": runErr = runUnicastTest(sp, flag.Args()[1:]) case "led-ring", "led_ring": diff --git a/goTool/pb/uart_messages.pb.go b/goTool/pb/uart_messages.pb.go index 3236dfb..a82da2d 100644 --- a/goTool/pb/uart_messages.pb.go +++ b/goTool/pb/uart_messages.pb.go @@ -41,6 +41,7 @@ const ( MessageType_OTA_SLAVE_PROGRESS MessageType = 21 MessageType_FIND_ME MessageType = 22 MessageType_RESTART MessageType = 23 + MessageType_ACCEL_READ MessageType = 24 ) // Enum value maps for MessageType. @@ -63,6 +64,7 @@ var ( 21: "OTA_SLAVE_PROGRESS", 22: "FIND_ME", 23: "RESTART", + 24: "ACCEL_READ", } MessageType_value = map[string]int32{ "UNKNOWN": 0, @@ -82,6 +84,7 @@ var ( "OTA_SLAVE_PROGRESS": 21, "FIND_ME": 22, "RESTART": 23, + "ACCEL_READ": 24, } ) @@ -138,6 +141,8 @@ type UartMessage struct { // *UartMessage_EspnowFindMeResponse // *UartMessage_RestartRequest // *UartMessage_RestartResponse + // *UartMessage_AccelReadRequest + // *UartMessage_AccelReadResponse Payload isUartMessage_Payload `protobuf_oneof:"payload"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache @@ -376,6 +381,24 @@ func (x *UartMessage) GetRestartResponse() *RestartResponse { return nil } +func (x *UartMessage) GetAccelReadRequest() *AccelReadRequest { + if x != nil { + if x, ok := x.Payload.(*UartMessage_AccelReadRequest); ok { + return x.AccelReadRequest + } + } + return nil +} + +func (x *UartMessage) GetAccelReadResponse() *AccelReadResponse { + if x != nil { + if x, ok := x.Payload.(*UartMessage_AccelReadResponse); ok { + return x.AccelReadResponse + } + } + return nil +} + type isUartMessage_Payload interface { isUartMessage_Payload() } @@ -464,6 +487,14 @@ type UartMessage_RestartResponse struct { RestartResponse *RestartResponse `protobuf:"bytes,22,opt,name=restart_response,json=restartResponse,proto3,oneof"` } +type UartMessage_AccelReadRequest struct { + AccelReadRequest *AccelReadRequest `protobuf:"bytes,23,opt,name=accel_read_request,json=accelReadRequest,proto3,oneof"` +} + +type UartMessage_AccelReadResponse struct { + AccelReadResponse *AccelReadResponse `protobuf:"bytes,24,opt,name=accel_read_response,json=accelReadResponse,proto3,oneof"` +} + func (*UartMessage_AckPayload) isUartMessage_Payload() {} func (*UartMessage_EchoPayload) isUartMessage_Payload() {} @@ -506,6 +537,10 @@ func (*UartMessage_RestartRequest) isUartMessage_Payload() {} func (*UartMessage_RestartResponse) isUartMessage_Payload() {} +func (*UartMessage_AccelReadRequest) isUartMessage_Payload() {} + +func (*UartMessage_AccelReadResponse) isUartMessage_Payload() {} + type Ack struct { state protoimpl.MessageState `protogen:"open.v1"` unknownFields protoimpl.UnknownFields @@ -1034,6 +1069,111 @@ func (x *AccelDeadzoneResponse) GetSlavesUpdated() uint32 { return 0 } +// Host → device: read current BMA456 accelerometer sample (raw LSB, ±2g range). +type AccelReadRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AccelReadRequest) Reset() { + *x = AccelReadRequest{} + mi := &file_uart_messages_proto_msgTypes[10] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AccelReadRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AccelReadRequest) ProtoMessage() {} + +func (x *AccelReadRequest) ProtoReflect() protoreflect.Message { + mi := &file_uart_messages_proto_msgTypes[10] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AccelReadRequest.ProtoReflect.Descriptor instead. +func (*AccelReadRequest) Descriptor() ([]byte, []int) { + return file_uart_messages_proto_rawDescGZIP(), []int{10} +} + +type AccelReadResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` + X int32 `protobuf:"zigzag32,2,opt,name=x,proto3" json:"x,omitempty"` + Y int32 `protobuf:"zigzag32,3,opt,name=y,proto3" json:"y,omitempty"` + Z int32 `protobuf:"zigzag32,4,opt,name=z,proto3" json:"z,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AccelReadResponse) Reset() { + *x = AccelReadResponse{} + mi := &file_uart_messages_proto_msgTypes[11] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AccelReadResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AccelReadResponse) ProtoMessage() {} + +func (x *AccelReadResponse) ProtoReflect() protoreflect.Message { + mi := &file_uart_messages_proto_msgTypes[11] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AccelReadResponse.ProtoReflect.Descriptor instead. +func (*AccelReadResponse) Descriptor() ([]byte, []int) { + return file_uart_messages_proto_rawDescGZIP(), []int{11} +} + +func (x *AccelReadResponse) GetSuccess() bool { + if x != nil { + return x.Success + } + return false +} + +func (x *AccelReadResponse) GetX() int32 { + if x != nil { + return x.X + } + return 0 +} + +func (x *AccelReadResponse) GetY() int32 { + if x != nil { + return x.Y + } + return 0 +} + +func (x *AccelReadResponse) GetZ() int32 { + if x != nil { + return x.Z + } + return 0 +} + type EspNowUnicastTestRequest struct { state protoimpl.MessageState `protogen:"open.v1"` ClientId uint32 `protobuf:"varint,1,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"` @@ -1044,7 +1184,7 @@ type EspNowUnicastTestRequest struct { func (x *EspNowUnicastTestRequest) Reset() { *x = EspNowUnicastTestRequest{} - mi := &file_uart_messages_proto_msgTypes[10] + mi := &file_uart_messages_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1056,7 +1196,7 @@ func (x *EspNowUnicastTestRequest) String() string { func (*EspNowUnicastTestRequest) ProtoMessage() {} func (x *EspNowUnicastTestRequest) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[10] + mi := &file_uart_messages_proto_msgTypes[12] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1069,7 +1209,7 @@ func (x *EspNowUnicastTestRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use EspNowUnicastTestRequest.ProtoReflect.Descriptor instead. func (*EspNowUnicastTestRequest) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{10} + return file_uart_messages_proto_rawDescGZIP(), []int{12} } func (x *EspNowUnicastTestRequest) GetClientId() uint32 { @@ -1096,7 +1236,7 @@ type EspNowUnicastTestResponse struct { func (x *EspNowUnicastTestResponse) Reset() { *x = EspNowUnicastTestResponse{} - mi := &file_uart_messages_proto_msgTypes[11] + mi := &file_uart_messages_proto_msgTypes[13] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1108,7 +1248,7 @@ func (x *EspNowUnicastTestResponse) String() string { func (*EspNowUnicastTestResponse) ProtoMessage() {} func (x *EspNowUnicastTestResponse) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[11] + mi := &file_uart_messages_proto_msgTypes[13] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1121,7 +1261,7 @@ func (x *EspNowUnicastTestResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use EspNowUnicastTestResponse.ProtoReflect.Descriptor instead. func (*EspNowUnicastTestResponse) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{11} + return file_uart_messages_proto_rawDescGZIP(), []int{13} } func (x *EspNowUnicastTestResponse) GetSuccess() bool { @@ -1162,7 +1302,7 @@ type LedRingProgressRequest struct { func (x *LedRingProgressRequest) Reset() { *x = LedRingProgressRequest{} - mi := &file_uart_messages_proto_msgTypes[12] + mi := &file_uart_messages_proto_msgTypes[14] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1174,7 +1314,7 @@ func (x *LedRingProgressRequest) String() string { func (*LedRingProgressRequest) ProtoMessage() {} func (x *LedRingProgressRequest) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[12] + mi := &file_uart_messages_proto_msgTypes[14] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1187,7 +1327,7 @@ func (x *LedRingProgressRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use LedRingProgressRequest.ProtoReflect.Descriptor instead. func (*LedRingProgressRequest) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{12} + return file_uart_messages_proto_rawDescGZIP(), []int{14} } func (x *LedRingProgressRequest) GetMode() uint32 { @@ -1265,7 +1405,7 @@ type LedRingProgressResponse struct { func (x *LedRingProgressResponse) Reset() { *x = LedRingProgressResponse{} - mi := &file_uart_messages_proto_msgTypes[13] + mi := &file_uart_messages_proto_msgTypes[15] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1277,7 +1417,7 @@ func (x *LedRingProgressResponse) String() string { func (*LedRingProgressResponse) ProtoMessage() {} func (x *LedRingProgressResponse) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[13] + mi := &file_uart_messages_proto_msgTypes[15] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1290,7 +1430,7 @@ func (x *LedRingProgressResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use LedRingProgressResponse.ProtoReflect.Descriptor instead. func (*LedRingProgressResponse) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{13} + return file_uart_messages_proto_rawDescGZIP(), []int{15} } func (x *LedRingProgressResponse) GetSuccess() bool { @@ -1331,7 +1471,7 @@ type EspNowFindMeRequest struct { func (x *EspNowFindMeRequest) Reset() { *x = EspNowFindMeRequest{} - mi := &file_uart_messages_proto_msgTypes[14] + mi := &file_uart_messages_proto_msgTypes[16] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1343,7 +1483,7 @@ func (x *EspNowFindMeRequest) String() string { func (*EspNowFindMeRequest) ProtoMessage() {} func (x *EspNowFindMeRequest) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[14] + mi := &file_uart_messages_proto_msgTypes[16] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1356,7 +1496,7 @@ func (x *EspNowFindMeRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use EspNowFindMeRequest.ProtoReflect.Descriptor instead. func (*EspNowFindMeRequest) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{14} + return file_uart_messages_proto_rawDescGZIP(), []int{16} } func (x *EspNowFindMeRequest) GetClientId() uint32 { @@ -1376,7 +1516,7 @@ type EspNowFindMeResponse struct { func (x *EspNowFindMeResponse) Reset() { *x = EspNowFindMeResponse{} - mi := &file_uart_messages_proto_msgTypes[15] + mi := &file_uart_messages_proto_msgTypes[17] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1388,7 +1528,7 @@ func (x *EspNowFindMeResponse) String() string { func (*EspNowFindMeResponse) ProtoMessage() {} func (x *EspNowFindMeResponse) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[15] + mi := &file_uart_messages_proto_msgTypes[17] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1401,7 +1541,7 @@ func (x *EspNowFindMeResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use EspNowFindMeResponse.ProtoReflect.Descriptor instead. func (*EspNowFindMeResponse) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{15} + return file_uart_messages_proto_rawDescGZIP(), []int{17} } func (x *EspNowFindMeResponse) GetSuccess() bool { @@ -1428,7 +1568,7 @@ type RestartRequest struct { func (x *RestartRequest) Reset() { *x = RestartRequest{} - mi := &file_uart_messages_proto_msgTypes[16] + mi := &file_uart_messages_proto_msgTypes[18] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1440,7 +1580,7 @@ func (x *RestartRequest) String() string { func (*RestartRequest) ProtoMessage() {} func (x *RestartRequest) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[16] + mi := &file_uart_messages_proto_msgTypes[18] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1453,7 +1593,7 @@ func (x *RestartRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use RestartRequest.ProtoReflect.Descriptor instead. func (*RestartRequest) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{16} + return file_uart_messages_proto_rawDescGZIP(), []int{18} } func (x *RestartRequest) GetClientId() uint32 { @@ -1473,7 +1613,7 @@ type RestartResponse struct { func (x *RestartResponse) Reset() { *x = RestartResponse{} - mi := &file_uart_messages_proto_msgTypes[17] + mi := &file_uart_messages_proto_msgTypes[19] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1485,7 +1625,7 @@ func (x *RestartResponse) String() string { func (*RestartResponse) ProtoMessage() {} func (x *RestartResponse) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[17] + mi := &file_uart_messages_proto_msgTypes[19] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1498,7 +1638,7 @@ func (x *RestartResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use RestartResponse.ProtoReflect.Descriptor instead. func (*RestartResponse) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{17} + return file_uart_messages_proto_rawDescGZIP(), []int{19} } func (x *RestartResponse) GetSuccess() bool { @@ -1525,7 +1665,7 @@ type OtaStartPayload struct { func (x *OtaStartPayload) Reset() { *x = OtaStartPayload{} - mi := &file_uart_messages_proto_msgTypes[18] + mi := &file_uart_messages_proto_msgTypes[20] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1537,7 +1677,7 @@ func (x *OtaStartPayload) String() string { func (*OtaStartPayload) ProtoMessage() {} func (x *OtaStartPayload) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[18] + mi := &file_uart_messages_proto_msgTypes[20] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1550,7 +1690,7 @@ func (x *OtaStartPayload) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaStartPayload.ProtoReflect.Descriptor instead. func (*OtaStartPayload) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{18} + return file_uart_messages_proto_rawDescGZIP(), []int{20} } func (x *OtaStartPayload) GetTotalSize() uint32 { @@ -1571,7 +1711,7 @@ type OtaPayload struct { func (x *OtaPayload) Reset() { *x = OtaPayload{} - mi := &file_uart_messages_proto_msgTypes[19] + mi := &file_uart_messages_proto_msgTypes[21] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1583,7 +1723,7 @@ func (x *OtaPayload) String() string { func (*OtaPayload) ProtoMessage() {} func (x *OtaPayload) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[19] + mi := &file_uart_messages_proto_msgTypes[21] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1596,7 +1736,7 @@ func (x *OtaPayload) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaPayload.ProtoReflect.Descriptor instead. func (*OtaPayload) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{19} + return file_uart_messages_proto_rawDescGZIP(), []int{21} } func (x *OtaPayload) GetSeq() uint32 { @@ -1622,7 +1762,7 @@ type OtaEndPayload struct { func (x *OtaEndPayload) Reset() { *x = OtaEndPayload{} - mi := &file_uart_messages_proto_msgTypes[20] + mi := &file_uart_messages_proto_msgTypes[22] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1634,7 +1774,7 @@ func (x *OtaEndPayload) String() string { func (*OtaEndPayload) ProtoMessage() {} func (x *OtaEndPayload) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[20] + mi := &file_uart_messages_proto_msgTypes[22] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1647,7 +1787,7 @@ func (x *OtaEndPayload) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaEndPayload.ProtoReflect.Descriptor instead. func (*OtaEndPayload) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{20} + return file_uart_messages_proto_rawDescGZIP(), []int{22} } // Device → host status (also used as ACK after each 4 KiB written). @@ -1664,7 +1804,7 @@ type OtaStatusPayload struct { func (x *OtaStatusPayload) Reset() { *x = OtaStatusPayload{} - mi := &file_uart_messages_proto_msgTypes[21] + mi := &file_uart_messages_proto_msgTypes[23] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1676,7 +1816,7 @@ func (x *OtaStatusPayload) String() string { func (*OtaStatusPayload) ProtoMessage() {} func (x *OtaStatusPayload) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[21] + mi := &file_uart_messages_proto_msgTypes[23] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1689,7 +1829,7 @@ func (x *OtaStatusPayload) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaStatusPayload.ProtoReflect.Descriptor instead. func (*OtaStatusPayload) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{21} + return file_uart_messages_proto_rawDescGZIP(), []int{23} } func (x *OtaStatusPayload) GetStatus() uint32 { @@ -1730,7 +1870,7 @@ type OtaSlaveProgressRequest struct { func (x *OtaSlaveProgressRequest) Reset() { *x = OtaSlaveProgressRequest{} - mi := &file_uart_messages_proto_msgTypes[22] + mi := &file_uart_messages_proto_msgTypes[24] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1742,7 +1882,7 @@ func (x *OtaSlaveProgressRequest) String() string { func (*OtaSlaveProgressRequest) ProtoMessage() {} func (x *OtaSlaveProgressRequest) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[22] + mi := &file_uart_messages_proto_msgTypes[24] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1755,7 +1895,7 @@ func (x *OtaSlaveProgressRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaSlaveProgressRequest.ProtoReflect.Descriptor instead. func (*OtaSlaveProgressRequest) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{22} + return file_uart_messages_proto_rawDescGZIP(), []int{24} } func (x *OtaSlaveProgressRequest) GetClientId() uint32 { @@ -1779,7 +1919,7 @@ type OtaSlaveProgressEntry struct { func (x *OtaSlaveProgressEntry) Reset() { *x = OtaSlaveProgressEntry{} - mi := &file_uart_messages_proto_msgTypes[23] + mi := &file_uart_messages_proto_msgTypes[25] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1791,7 +1931,7 @@ func (x *OtaSlaveProgressEntry) String() string { func (*OtaSlaveProgressEntry) ProtoMessage() {} func (x *OtaSlaveProgressEntry) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[23] + mi := &file_uart_messages_proto_msgTypes[25] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1804,7 +1944,7 @@ func (x *OtaSlaveProgressEntry) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaSlaveProgressEntry.ProtoReflect.Descriptor instead. func (*OtaSlaveProgressEntry) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{23} + return file_uart_messages_proto_rawDescGZIP(), []int{25} } func (x *OtaSlaveProgressEntry) GetClientId() uint32 { @@ -1855,7 +1995,7 @@ type OtaSlaveProgressResponse struct { func (x *OtaSlaveProgressResponse) Reset() { *x = OtaSlaveProgressResponse{} - mi := &file_uart_messages_proto_msgTypes[24] + mi := &file_uart_messages_proto_msgTypes[26] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1867,7 +2007,7 @@ func (x *OtaSlaveProgressResponse) String() string { func (*OtaSlaveProgressResponse) ProtoMessage() {} func (x *OtaSlaveProgressResponse) ProtoReflect() protoreflect.Message { - mi := &file_uart_messages_proto_msgTypes[24] + mi := &file_uart_messages_proto_msgTypes[26] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1880,7 +2020,7 @@ func (x *OtaSlaveProgressResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use OtaSlaveProgressResponse.ProtoReflect.Descriptor instead. func (*OtaSlaveProgressResponse) Descriptor() ([]byte, []int) { - return file_uart_messages_proto_rawDescGZIP(), []int{24} + return file_uart_messages_proto_rawDescGZIP(), []int{26} } func (x *OtaSlaveProgressResponse) GetActive() bool { @@ -1922,7 +2062,7 @@ var File_uart_messages_proto protoreflect.FileDescriptor const file_uart_messages_proto_rawDesc = "" + "\n" + - "\x13uart_messages.proto\x12\x04alox\x1a\fnanopb.proto\"\xf0\f\n" + + "\x13uart_messages.proto\x12\x04alox\x1a\fnanopb.proto\"\x83\x0e\n" + "\vUartMessage\x12%\n" + "\x04type\x18\x01 \x01(\x0e2\x11.alox.MessageTypeR\x04type\x12,\n" + "\vack_payload\x18\x02 \x01(\v2\t.alox.AckH\x00R\n" + @@ -1949,7 +2089,9 @@ const file_uart_messages_proto_rawDesc = "" + "\x16espnow_find_me_request\x18\x13 \x01(\v2\x19.alox.EspNowFindMeRequestH\x00R\x13espnowFindMeRequest\x12S\n" + "\x17espnow_find_me_response\x18\x14 \x01(\v2\x1a.alox.EspNowFindMeResponseH\x00R\x14espnowFindMeResponse\x12?\n" + "\x0frestart_request\x18\x15 \x01(\v2\x14.alox.RestartRequestH\x00R\x0erestartRequest\x12B\n" + - "\x10restart_response\x18\x16 \x01(\v2\x15.alox.RestartResponseH\x00R\x0frestartResponseB\t\n" + + "\x10restart_response\x18\x16 \x01(\v2\x15.alox.RestartResponseH\x00R\x0frestartResponse\x12F\n" + + "\x12accel_read_request\x18\x17 \x01(\v2\x16.alox.AccelReadRequestH\x00R\x10accelReadRequest\x12I\n" + + "\x13accel_read_response\x18\x18 \x01(\v2\x17.alox.AccelReadResponseH\x00R\x11accelReadResponseB\t\n" + "\apayload\"\x05\n" + "\x03Ack\"!\n" + "\vEchoPayload\x12\x12\n" + @@ -1986,7 +2128,13 @@ const file_uart_messages_proto_rawDesc = "" + "\bdeadzone\x18\x01 \x01(\rR\bdeadzone\x12\x1b\n" + "\tclient_id\x18\x02 \x01(\rR\bclientId\x12\x18\n" + "\asuccess\x18\x03 \x01(\bR\asuccess\x12%\n" + - "\x0eslaves_updated\x18\x04 \x01(\rR\rslavesUpdated\"I\n" + + "\x0eslaves_updated\x18\x04 \x01(\rR\rslavesUpdated\"\x12\n" + + "\x10AccelReadRequest\"W\n" + + "\x11AccelReadResponse\x12\x18\n" + + "\asuccess\x18\x01 \x01(\bR\asuccess\x12\f\n" + + "\x01x\x18\x02 \x01(\x11R\x01x\x12\f\n" + + "\x01y\x18\x03 \x01(\x11R\x01y\x12\f\n" + + "\x01z\x18\x04 \x01(\x11R\x01z\"I\n" + "\x18EspNowUnicastTestRequest\x12\x1b\n" + "\tclient_id\x18\x01 \x01(\rR\bclientId\x12\x10\n" + "\x03seq\x18\x02 \x01(\rR\x03seq\"G\n" + @@ -2049,7 +2197,7 @@ const file_uart_messages_proto_rawDesc = "" + "\x0faggregate_bytes\x18\x03 \x01(\rR\x0eaggregateBytes\x12\x1f\n" + "\vslave_count\x18\x04 \x01(\rR\n" + "slaveCount\x12:\n" + - "\x06slaves\x18\x05 \x03(\v2\x1b.alox.OtaSlaveProgressEntryB\x05\x92?\x02\x10\x10R\x06slaves*\x9d\x02\n" + + "\x06slaves\x18\x05 \x03(\v2\x1b.alox.OtaSlaveProgressEntryB\x05\x92?\x02\x10\x10R\x06slaves*\xad\x02\n" + "\vMessageType\x12\v\n" + "\aUNKNOWN\x10\x00\x12\a\n" + "\x03ACK\x10\x01\x12\b\n" + @@ -2068,7 +2216,9 @@ const file_uart_messages_proto_rawDesc = "" + "\x10OTA_START_ESPNOW\x10\x14\x12\x16\n" + "\x12OTA_SLAVE_PROGRESS\x10\x15\x12\v\n" + "\aFIND_ME\x10\x16\x12\v\n" + - "\aRESTART\x10\x17b\x06proto3" + "\aRESTART\x10\x17\x12\x0e\n" + + "\n" + + "ACCEL_READ\x10\x18b\x06proto3" var ( file_uart_messages_proto_rawDescOnce sync.Once @@ -2083,7 +2233,7 @@ func file_uart_messages_proto_rawDescGZIP() []byte { } var file_uart_messages_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_uart_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 25) +var file_uart_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 27) var file_uart_messages_proto_goTypes = []any{ (MessageType)(0), // 0: alox.MessageType (*UartMessage)(nil), // 1: alox.UartMessage @@ -2096,21 +2246,23 @@ var file_uart_messages_proto_goTypes = []any{ (*ClientInputResponse)(nil), // 8: alox.ClientInputResponse (*AccelDeadzoneRequest)(nil), // 9: alox.AccelDeadzoneRequest (*AccelDeadzoneResponse)(nil), // 10: alox.AccelDeadzoneResponse - (*EspNowUnicastTestRequest)(nil), // 11: alox.EspNowUnicastTestRequest - (*EspNowUnicastTestResponse)(nil), // 12: alox.EspNowUnicastTestResponse - (*LedRingProgressRequest)(nil), // 13: alox.LedRingProgressRequest - (*LedRingProgressResponse)(nil), // 14: alox.LedRingProgressResponse - (*EspNowFindMeRequest)(nil), // 15: alox.EspNowFindMeRequest - (*EspNowFindMeResponse)(nil), // 16: alox.EspNowFindMeResponse - (*RestartRequest)(nil), // 17: alox.RestartRequest - (*RestartResponse)(nil), // 18: alox.RestartResponse - (*OtaStartPayload)(nil), // 19: alox.OtaStartPayload - (*OtaPayload)(nil), // 20: alox.OtaPayload - (*OtaEndPayload)(nil), // 21: alox.OtaEndPayload - (*OtaStatusPayload)(nil), // 22: alox.OtaStatusPayload - (*OtaSlaveProgressRequest)(nil), // 23: alox.OtaSlaveProgressRequest - (*OtaSlaveProgressEntry)(nil), // 24: alox.OtaSlaveProgressEntry - (*OtaSlaveProgressResponse)(nil), // 25: alox.OtaSlaveProgressResponse + (*AccelReadRequest)(nil), // 11: alox.AccelReadRequest + (*AccelReadResponse)(nil), // 12: alox.AccelReadResponse + (*EspNowUnicastTestRequest)(nil), // 13: alox.EspNowUnicastTestRequest + (*EspNowUnicastTestResponse)(nil), // 14: alox.EspNowUnicastTestResponse + (*LedRingProgressRequest)(nil), // 15: alox.LedRingProgressRequest + (*LedRingProgressResponse)(nil), // 16: alox.LedRingProgressResponse + (*EspNowFindMeRequest)(nil), // 17: alox.EspNowFindMeRequest + (*EspNowFindMeResponse)(nil), // 18: alox.EspNowFindMeResponse + (*RestartRequest)(nil), // 19: alox.RestartRequest + (*RestartResponse)(nil), // 20: alox.RestartResponse + (*OtaStartPayload)(nil), // 21: alox.OtaStartPayload + (*OtaPayload)(nil), // 22: alox.OtaPayload + (*OtaEndPayload)(nil), // 23: alox.OtaEndPayload + (*OtaStatusPayload)(nil), // 24: alox.OtaStatusPayload + (*OtaSlaveProgressRequest)(nil), // 25: alox.OtaSlaveProgressRequest + (*OtaSlaveProgressEntry)(nil), // 26: alox.OtaSlaveProgressEntry + (*OtaSlaveProgressResponse)(nil), // 27: alox.OtaSlaveProgressResponse } var file_uart_messages_proto_depIdxs = []int32{ 0, // 0: alox.UartMessage.type:type_name -> alox.MessageType @@ -2119,30 +2271,32 @@ var file_uart_messages_proto_depIdxs = []int32{ 4, // 3: alox.UartMessage.version_response:type_name -> alox.VersionResponse 6, // 4: alox.UartMessage.client_info_response:type_name -> alox.ClientInfoResponse 8, // 5: alox.UartMessage.client_input_response:type_name -> alox.ClientInputResponse - 19, // 6: alox.UartMessage.ota_start:type_name -> alox.OtaStartPayload - 20, // 7: alox.UartMessage.ota_payload:type_name -> alox.OtaPayload - 21, // 8: alox.UartMessage.ota_end:type_name -> alox.OtaEndPayload - 22, // 9: alox.UartMessage.ota_status:type_name -> alox.OtaStatusPayload + 21, // 6: alox.UartMessage.ota_start:type_name -> alox.OtaStartPayload + 22, // 7: alox.UartMessage.ota_payload:type_name -> alox.OtaPayload + 23, // 8: alox.UartMessage.ota_end:type_name -> alox.OtaEndPayload + 24, // 9: alox.UartMessage.ota_status:type_name -> alox.OtaStatusPayload 9, // 10: alox.UartMessage.accel_deadzone_request:type_name -> alox.AccelDeadzoneRequest 10, // 11: alox.UartMessage.accel_deadzone_response:type_name -> alox.AccelDeadzoneResponse - 11, // 12: alox.UartMessage.espnow_unicast_test_request:type_name -> alox.EspNowUnicastTestRequest - 12, // 13: alox.UartMessage.espnow_unicast_test_response:type_name -> alox.EspNowUnicastTestResponse - 23, // 14: alox.UartMessage.ota_slave_progress_request:type_name -> alox.OtaSlaveProgressRequest - 25, // 15: alox.UartMessage.ota_slave_progress_response:type_name -> alox.OtaSlaveProgressResponse - 13, // 16: alox.UartMessage.led_ring_progress_request:type_name -> alox.LedRingProgressRequest - 14, // 17: alox.UartMessage.led_ring_progress_response:type_name -> alox.LedRingProgressResponse - 15, // 18: alox.UartMessage.espnow_find_me_request:type_name -> alox.EspNowFindMeRequest - 16, // 19: alox.UartMessage.espnow_find_me_response:type_name -> alox.EspNowFindMeResponse - 17, // 20: alox.UartMessage.restart_request:type_name -> alox.RestartRequest - 18, // 21: alox.UartMessage.restart_response:type_name -> alox.RestartResponse - 5, // 22: alox.ClientInfoResponse.clients:type_name -> alox.ClientInfo - 7, // 23: alox.ClientInputResponse.clients:type_name -> alox.ClientInput - 24, // 24: alox.OtaSlaveProgressResponse.slaves:type_name -> alox.OtaSlaveProgressEntry - 25, // [25:25] is the sub-list for method output_type - 25, // [25:25] is the sub-list for method input_type - 25, // [25:25] is the sub-list for extension type_name - 25, // [25:25] is the sub-list for extension extendee - 0, // [0:25] is the sub-list for field type_name + 13, // 12: alox.UartMessage.espnow_unicast_test_request:type_name -> alox.EspNowUnicastTestRequest + 14, // 13: alox.UartMessage.espnow_unicast_test_response:type_name -> alox.EspNowUnicastTestResponse + 25, // 14: alox.UartMessage.ota_slave_progress_request:type_name -> alox.OtaSlaveProgressRequest + 27, // 15: alox.UartMessage.ota_slave_progress_response:type_name -> alox.OtaSlaveProgressResponse + 15, // 16: alox.UartMessage.led_ring_progress_request:type_name -> alox.LedRingProgressRequest + 16, // 17: alox.UartMessage.led_ring_progress_response:type_name -> alox.LedRingProgressResponse + 17, // 18: alox.UartMessage.espnow_find_me_request:type_name -> alox.EspNowFindMeRequest + 18, // 19: alox.UartMessage.espnow_find_me_response:type_name -> alox.EspNowFindMeResponse + 19, // 20: alox.UartMessage.restart_request:type_name -> alox.RestartRequest + 20, // 21: alox.UartMessage.restart_response:type_name -> alox.RestartResponse + 11, // 22: alox.UartMessage.accel_read_request:type_name -> alox.AccelReadRequest + 12, // 23: alox.UartMessage.accel_read_response:type_name -> alox.AccelReadResponse + 5, // 24: alox.ClientInfoResponse.clients:type_name -> alox.ClientInfo + 7, // 25: alox.ClientInputResponse.clients:type_name -> alox.ClientInput + 26, // 26: alox.OtaSlaveProgressResponse.slaves:type_name -> alox.OtaSlaveProgressEntry + 27, // [27:27] is the sub-list for method output_type + 27, // [27:27] is the sub-list for method input_type + 27, // [27:27] is the sub-list for extension type_name + 27, // [27:27] is the sub-list for extension extendee + 0, // [0:27] is the sub-list for field type_name } func init() { file_uart_messages_proto_init() } @@ -2172,6 +2326,8 @@ func file_uart_messages_proto_init() { (*UartMessage_EspnowFindMeResponse)(nil), (*UartMessage_RestartRequest)(nil), (*UartMessage_RestartResponse)(nil), + (*UartMessage_AccelReadRequest)(nil), + (*UartMessage_AccelReadResponse)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -2179,7 +2335,7 @@ func file_uart_messages_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_uart_messages_proto_rawDesc), len(file_uart_messages_proto_rawDesc)), NumEnums: 1, - NumMessages: 25, + NumMessages: 27, NumExtensions: 0, NumServices: 0, }, diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 24c3a90..60db932 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -18,6 +18,7 @@ idf_component_register( "cmd/cmd_version.c" "cmd/cmd_client_info.c" "cmd/cmd_accel_deadzone.c" + "cmd/cmd_accel_read.c" "cmd/cmd_espnow_unicast_test.c" "cmd/cmd_espnow_find_me.c" "cmd/cmd_restart.c" diff --git a/main/README.md b/main/README.md index 5bb72c2..ec94ddd 100644 --- a/main/README.md +++ b/main/README.md @@ -217,6 +217,7 @@ Host and master speak nanopb-encoded `UartMessage` inside UART frames (byte 0 = | 21 | `OTA_SLAVE_PROGRESS` | Implemented (`cmd/cmd_ota_slave_progress.c`) — query per-slave ESP-NOW OTA progress | | 22 | `FIND_ME` | Implemented (`cmd/cmd_espnow_find_me.c`) — `client_id=0` local ring, `>0` ESP-NOW to slave | | 23 | `RESTART` | Implemented (`cmd/cmd_restart.c`) — `client_id=0` reboot master, `>0` ESP-NOW reboot slave | +| 24 | `ACCEL_READ` | Implemented (`cmd/cmd_accel_read.c`) — on-demand BMA456 XYZ (raw LSB) | Regenerate C code: @@ -310,6 +311,29 @@ Sets the **software** deadzone used by `bosch456.c` when logging accel (see [BMA **Response:** `accel_deadzone_response` with applied `deadzone`, `success`, and `slaves_updated` (ESP-NOW count). +### ACCEL_READ command + +Read the **current** BMA456 accelerometer sample on this node (master or slave with sensor). Values are raw LSB in the configured **±2g** range; they are **not** filtered by the software deadzone (unlike periodic `ACC X=…` logs in `bosch456.c`). + +**Request:** framed `18` (`0x18`) only, or `18` + empty `accel_read_request`. + +**Response:** `accel_read_response`: + +| Field | Meaning | +|-------|---------| +| `success` | `true` if BMA456 is ready and I2C read succeeded | +| `x`, `y`, `z` | Raw accel LSB (`sint32`; meaningful only when `success`) | + +If the sensor was not probed at boot (`bma456_is_ready()` false), `success` is `false` and axes are zero. + +Host: + +```bash +go run . -port /dev/ttyUSB0 accel +``` + +Implementation: `bma456_read_accel()` in `bosch456.c` (mutex with the 10 Hz poll task), handler in `cmd/cmd_accel_read.c`. + ### ESPNOW_UNICAST_TEST command Minimal master→slave ESP-NOW unicast check (no BMA456). Use this before debugging `ACCEL_DEADZONE` unicast. @@ -454,7 +478,8 @@ Target: ESP32-S3. Close serial monitor on the UART adapter port before running ` | `cmd/cmd_version.c/h` | VERSION handler | | `cmd/cmd_client_info.c/h` | CLIENT_INFO handler | | `client_registry.c/h` | Registered slave table | -| `bosch456.c/h` | BMA456H I2C driver, accel poll, tap INT, deadzone filter | +| `bosch456.c/h` | BMA456H I2C driver, accel poll, on-demand read, tap INT, deadzone filter | +| `cmd/cmd_accel_read.c` | UART `ACCEL_READ` — current accel XYZ | | `board_input.c/h` | Taster GPIO12, LiPo ADC on GPIO1 / GPIO12 | | `pod_settings.c/h` | NVS persistence (accel deadzone, …) | | `led_ring.c/h` | LED ring (digit display, progress bar) | diff --git a/main/bosch456.c b/main/bosch456.c index 81d72ae..097856c 100644 --- a/main/bosch456.c +++ b/main/bosch456.c @@ -14,6 +14,7 @@ #include "esp_err.h" #include "esp_log.h" #include "freertos/idf_additions.h" +#include "freertos/semphr.h" #include #include @@ -34,6 +35,7 @@ static int16_t s_last_z; static bool s_have_last_sample; static volatile bool s_int_pending; +static SemaphoreHandle_t s_accel_mutex; static esp_err_t check_bma4(const char *api_name, int8_t rslt); @@ -121,6 +123,30 @@ void bma456_set_accel_deadzone(uint32_t deadzone_lsb) { uint32_t bma456_get_accel_deadzone(void) { return s_accel_deadzone; } +esp_err_t bma456_read_accel(int16_t *x, int16_t *y, int16_t *z) { + if (!s_bma456_ready || x == NULL || y == NULL || z == NULL) { + return ESP_ERR_INVALID_STATE; + } + if (s_accel_mutex == NULL || + xSemaphoreTake(s_accel_mutex, pdMS_TO_TICKS(500)) != pdTRUE) { + return ESP_ERR_TIMEOUT; + } + + struct bma4_accel sens_data = {0}; + int8_t ret = bma4_read_accel_xyz(&sens_data, &s_bma456); + xSemaphoreGive(s_accel_mutex); + + if (ret != BMA4_OK) { + bma4_error_codes_print_result("bma4_read_accel_xyz", ret); + return ESP_FAIL; + } + + *x = sens_data.x; + *y = sens_data.y; + *z = sens_data.z; + return ESP_OK; +} + void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z) { if (!s_bma456_ready || !sample_exceeds_deadzone(x, y, z)) { return; @@ -187,11 +213,19 @@ static void read_sensor_task(void *param) { struct bma4_accel sens_data = {0}; while (1) { - int8_t ret = bma4_read_accel_xyz(&sens_data, &s_bma456); - if (ret == BMA4_OK) { + bool got_sample = false; + if (s_accel_mutex != NULL && + xSemaphoreTake(s_accel_mutex, pdMS_TO_TICKS(500)) == pdTRUE) { + int8_t ret = bma4_read_accel_xyz(&sens_data, &s_bma456); + xSemaphoreGive(s_accel_mutex); + if (ret == BMA4_OK) { + got_sample = true; + } else { + bma4_error_codes_print_result("bma4_read_accel_xyz", ret); + } + } + if (got_sample) { bma456_report_accel_if_changed(sens_data.x, sens_data.y, sens_data.z); - } else { - bma4_error_codes_print_result("bma4_read_accel_xyz", ret); } if (s_int_pending) { @@ -343,6 +377,13 @@ esp_err_t init_bma456(i2c_master_bus_handle_t bus_handle) { goto fail; } + if (s_accel_mutex == NULL) { + s_accel_mutex = xSemaphoreCreateMutex(); + if (s_accel_mutex == NULL) { + goto fail; + } + } + if (xTaskCreate(read_sensor_task, "bma456_poll", 4096, NULL, 1, NULL) != pdPASS) { goto fail; diff --git a/main/bosch456.h b/main/bosch456.h index 5faf4af..8b00d61 100644 --- a/main/bosch456.h +++ b/main/bosch456.h @@ -35,4 +35,7 @@ uint32_t bma456_get_accel_deadzone(void); /** Log accel when any axis moved more than deadzone since last reported sample. */ void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z); +/** On-demand read of current accel XYZ (raw LSB). Returns ESP_ERR_INVALID_STATE if sensor not ready. */ +esp_err_t bma456_read_accel(int16_t *x, int16_t *y, int16_t *z); + #endif diff --git a/main/cmd/cmd_accel_read.c b/main/cmd/cmd_accel_read.c new file mode 100644 index 0000000..8ea041b --- /dev/null +++ b/main/cmd/cmd_accel_read.c @@ -0,0 +1,34 @@ +#include "bosch456.h" +#include "cmd_accel_read.h" +#include "uart_cmd.h" + +static const char *TAG = "[ACCEL_READ]"; + +static void reply(bool success, int16_t x, int16_t y, int16_t z) { + alox_UartMessage response; + uart_cmd_init_response(&response, alox_MessageType_ACCEL_READ, + alox_UartMessage_accel_read_response_tag); + response.payload.accel_read_response.success = success; + response.payload.accel_read_response.x = x; + response.payload.accel_read_response.y = y; + response.payload.accel_read_response.z = z; + uart_cmd_send(&response, TAG); +} + +static void handle_accel_read(const uint8_t *data, size_t len) { + (void)data; + (void)len; + + int16_t x = 0; + int16_t y = 0; + int16_t z = 0; + if (bma456_read_accel(&x, &y, &z) == ESP_OK) { + reply(true, x, y, z); + return; + } + reply(false, 0, 0, 0); +} + +void cmd_accel_read_register(void) { + uart_cmd_register(alox_MessageType_ACCEL_READ, handle_accel_read); +} diff --git a/main/cmd/cmd_accel_read.h b/main/cmd/cmd_accel_read.h new file mode 100644 index 0000000..d8e85d7 --- /dev/null +++ b/main/cmd/cmd_accel_read.h @@ -0,0 +1,6 @@ +#ifndef CMD_ACCEL_READ_H +#define CMD_ACCEL_READ_H + +void cmd_accel_read_register(void); + +#endif diff --git a/main/cmd/cmd_handler.c b/main/cmd/cmd_handler.c index 41b05b3..bafd7d0 100644 --- a/main/cmd/cmd_handler.c +++ b/main/cmd/cmd_handler.c @@ -48,6 +48,8 @@ static const char *message_type_name(uint16_t id) { return "FIND_ME"; case alox_MessageType_RESTART: return "RESTART"; + case alox_MessageType_ACCEL_READ: + return "ACCEL_READ"; default: return "UNKNOWN"; } diff --git a/main/powerpod.c b/main/powerpod.c index 0919153..ac1d067 100644 --- a/main/powerpod.c +++ b/main/powerpod.c @@ -1,6 +1,7 @@ #include "app_config.h" #include "cmd_handler.h" #include "cmd_accel_deadzone.h" +#include "cmd_accel_read.h" #include "cmd_espnow_unicast_test.h" #include "cmd_espnow_find_me.h" #include "cmd_restart.h" @@ -177,6 +178,7 @@ void app_main(void) { cmd_version_register(); cmd_client_info_register(); cmd_accel_deadzone_register(); + cmd_accel_read_register(); cmd_espnow_unicast_test_register(); cmd_espnow_find_me_register(); cmd_restart_register(); diff --git a/main/proto/uart_messages.pb.c b/main/proto/uart_messages.pb.c index cb92342..3bd7821 100644 --- a/main/proto/uart_messages.pb.c +++ b/main/proto/uart_messages.pb.c @@ -36,6 +36,12 @@ PB_BIND(alox_AccelDeadzoneRequest, alox_AccelDeadzoneRequest, AUTO) PB_BIND(alox_AccelDeadzoneResponse, alox_AccelDeadzoneResponse, AUTO) +PB_BIND(alox_AccelReadRequest, alox_AccelReadRequest, AUTO) + + +PB_BIND(alox_AccelReadResponse, alox_AccelReadResponse, AUTO) + + PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO) diff --git a/main/proto/uart_messages.pb.h b/main/proto/uart_messages.pb.h index 4f179a1..fb6febf 100644 --- a/main/proto/uart_messages.pb.h +++ b/main/proto/uart_messages.pb.h @@ -27,7 +27,8 @@ typedef enum _alox_MessageType { alox_MessageType_OTA_START_ESPNOW = 20, alox_MessageType_OTA_SLAVE_PROGRESS = 21, alox_MessageType_FIND_ME = 22, - alox_MessageType_RESTART = 23 + alox_MessageType_RESTART = 23, + alox_MessageType_ACCEL_READ = 24 } alox_MessageType; /* Struct definitions */ @@ -88,6 +89,18 @@ typedef struct _alox_AccelDeadzoneResponse { uint32_t slaves_updated; } alox_AccelDeadzoneResponse; +/* Host → device: read current BMA456 accelerometer sample (raw LSB, ±2g range). */ +typedef struct _alox_AccelReadRequest { + char dummy_field; +} alox_AccelReadRequest; + +typedef struct _alox_AccelReadResponse { + bool success; + int32_t x; + int32_t y; + int32_t z; +} alox_AccelReadResponse; + typedef struct _alox_EspNowUnicastTestRequest { uint32_t client_id; uint32_t seq; @@ -218,6 +231,8 @@ typedef struct _alox_UartMessage { alox_EspNowFindMeResponse espnow_find_me_response; alox_RestartRequest restart_request; alox_RestartResponse restart_response; + alox_AccelReadRequest accel_read_request; + alox_AccelReadResponse accel_read_response; } payload; } alox_UartMessage; @@ -228,8 +243,8 @@ extern "C" { /* Helper constants for enums */ #define _alox_MessageType_MIN alox_MessageType_UNKNOWN -#define _alox_MessageType_MAX alox_MessageType_RESTART -#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_RESTART+1)) +#define _alox_MessageType_MAX alox_MessageType_ACCEL_READ +#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_ACCEL_READ+1)) #define alox_UartMessage_type_ENUMTYPE alox_MessageType @@ -255,6 +270,8 @@ extern "C" { + + @@ -269,6 +286,8 @@ extern "C" { #define alox_ClientInputResponse_init_default {{{NULL}, NULL}} #define alox_AccelDeadzoneRequest_init_default {0, 0, 0, 0} #define alox_AccelDeadzoneResponse_init_default {0, 0, 0, 0} +#define alox_AccelReadRequest_init_default {0} +#define alox_AccelReadResponse_init_default {0, 0, 0, 0} #define alox_EspNowUnicastTestRequest_init_default {0, 0} #define alox_EspNowUnicastTestResponse_init_default {0, 0} #define alox_LedRingProgressRequest_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0} @@ -294,6 +313,8 @@ extern "C" { #define alox_ClientInputResponse_init_zero {{{NULL}, NULL}} #define alox_AccelDeadzoneRequest_init_zero {0, 0, 0, 0} #define alox_AccelDeadzoneResponse_init_zero {0, 0, 0, 0} +#define alox_AccelReadRequest_init_zero {0} +#define alox_AccelReadResponse_init_zero {0, 0, 0, 0} #define alox_EspNowUnicastTestRequest_init_zero {0, 0} #define alox_EspNowUnicastTestResponse_init_zero {0, 0} #define alox_LedRingProgressRequest_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0} @@ -336,6 +357,10 @@ extern "C" { #define alox_AccelDeadzoneResponse_client_id_tag 2 #define alox_AccelDeadzoneResponse_success_tag 3 #define alox_AccelDeadzoneResponse_slaves_updated_tag 4 +#define alox_AccelReadResponse_success_tag 1 +#define alox_AccelReadResponse_x_tag 2 +#define alox_AccelReadResponse_y_tag 3 +#define alox_AccelReadResponse_z_tag 4 #define alox_EspNowUnicastTestRequest_client_id_tag 1 #define alox_EspNowUnicastTestRequest_seq_tag 2 #define alox_EspNowUnicastTestResponse_success_tag 1 @@ -399,6 +424,8 @@ extern "C" { #define alox_UartMessage_espnow_find_me_response_tag 20 #define alox_UartMessage_restart_request_tag 21 #define alox_UartMessage_restart_response_tag 22 +#define alox_UartMessage_accel_read_request_tag 23 +#define alox_UartMessage_accel_read_response_tag 24 /* Struct field encoding specification for nanopb */ #define alox_UartMessage_FIELDLIST(X, a) \ @@ -423,7 +450,9 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,led_ring_progress_response,payload.l X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_request,payload.espnow_find_me_request), 19) \ X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_response,payload.espnow_find_me_response), 20) \ X(a, STATIC, ONEOF, MESSAGE, (payload,restart_request,payload.restart_request), 21) \ -X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_response), 22) +X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_response), 22) \ +X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_request,payload.accel_read_request), 23) \ +X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_response,payload.accel_read_response), 24) #define alox_UartMessage_CALLBACK NULL #define alox_UartMessage_DEFAULT NULL #define alox_UartMessage_payload_ack_payload_MSGTYPE alox_Ack @@ -447,6 +476,8 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_res #define alox_UartMessage_payload_espnow_find_me_response_MSGTYPE alox_EspNowFindMeResponse #define alox_UartMessage_payload_restart_request_MSGTYPE alox_RestartRequest #define alox_UartMessage_payload_restart_response_MSGTYPE alox_RestartResponse +#define alox_UartMessage_payload_accel_read_request_MSGTYPE alox_AccelReadRequest +#define alox_UartMessage_payload_accel_read_response_MSGTYPE alox_AccelReadResponse #define alox_Ack_FIELDLIST(X, a) \ @@ -512,6 +543,19 @@ X(a, STATIC, SINGULAR, UINT32, slaves_updated, 4) #define alox_AccelDeadzoneResponse_CALLBACK NULL #define alox_AccelDeadzoneResponse_DEFAULT NULL +#define alox_AccelReadRequest_FIELDLIST(X, a) \ + +#define alox_AccelReadRequest_CALLBACK NULL +#define alox_AccelReadRequest_DEFAULT NULL + +#define alox_AccelReadResponse_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, BOOL, success, 1) \ +X(a, STATIC, SINGULAR, SINT32, x, 2) \ +X(a, STATIC, SINGULAR, SINT32, y, 3) \ +X(a, STATIC, SINGULAR, SINT32, z, 4) +#define alox_AccelReadResponse_CALLBACK NULL +#define alox_AccelReadResponse_DEFAULT NULL + #define alox_EspNowUnicastTestRequest_FIELDLIST(X, a) \ X(a, STATIC, SINGULAR, UINT32, client_id, 1) \ X(a, STATIC, SINGULAR, UINT32, seq, 2) @@ -625,6 +669,8 @@ extern const pb_msgdesc_t alox_ClientInput_msg; extern const pb_msgdesc_t alox_ClientInputResponse_msg; extern const pb_msgdesc_t alox_AccelDeadzoneRequest_msg; extern const pb_msgdesc_t alox_AccelDeadzoneResponse_msg; +extern const pb_msgdesc_t alox_AccelReadRequest_msg; +extern const pb_msgdesc_t alox_AccelReadResponse_msg; extern const pb_msgdesc_t alox_EspNowUnicastTestRequest_msg; extern const pb_msgdesc_t alox_EspNowUnicastTestResponse_msg; extern const pb_msgdesc_t alox_LedRingProgressRequest_msg; @@ -652,6 +698,8 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg; #define alox_ClientInputResponse_fields &alox_ClientInputResponse_msg #define alox_AccelDeadzoneRequest_fields &alox_AccelDeadzoneRequest_msg #define alox_AccelDeadzoneResponse_fields &alox_AccelDeadzoneResponse_msg +#define alox_AccelReadRequest_fields &alox_AccelReadRequest_msg +#define alox_AccelReadResponse_fields &alox_AccelReadResponse_msg #define alox_EspNowUnicastTestRequest_fields &alox_EspNowUnicastTestRequest_msg #define alox_EspNowUnicastTestResponse_fields &alox_EspNowUnicastTestResponse_msg #define alox_LedRingProgressRequest_fields &alox_LedRingProgressRequest_msg @@ -678,6 +726,8 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg; #define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_OtaSlaveProgressResponse_size #define alox_AccelDeadzoneRequest_size 16 #define alox_AccelDeadzoneResponse_size 20 +#define alox_AccelReadRequest_size 0 +#define alox_AccelReadResponse_size 20 #define alox_Ack_size 0 #define alox_ClientInput_size 22 #define alox_EspNowFindMeRequest_size 6 diff --git a/main/proto/uart_messages.proto b/main/proto/uart_messages.proto index 4b80537..1f50a74 100644 --- a/main/proto/uart_messages.proto +++ b/main/proto/uart_messages.proto @@ -22,6 +22,7 @@ enum MessageType { OTA_SLAVE_PROGRESS = 21; FIND_ME = 22; RESTART = 23; + ACCEL_READ = 24; } message UartMessage { @@ -48,6 +49,8 @@ message UartMessage { EspNowFindMeResponse espnow_find_me_response = 20; RestartRequest restart_request = 21; RestartResponse restart_response = 22; + AccelReadRequest accel_read_request = 23; + AccelReadResponse accel_read_response = 24; } } @@ -106,6 +109,16 @@ message AccelDeadzoneResponse { uint32 slaves_updated = 4; } +// Host → device: read current BMA456 accelerometer sample (raw LSB, ±2g range). +message AccelReadRequest {} + +message AccelReadResponse { + bool success = 1; + sint32 x = 2; + sint32 y = 3; + sint32 z = 4; +} + message EspNowUnicastTestRequest { uint32 client_id = 1; uint32 seq = 2;