From 90f64c2b371e3fc4329a7f71f6ab96ba7f2b0fb1 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Thu, 3 Jan 2019 14:16:54 -0700 Subject: [PATCH] add protobuf generation --- Dockerfile | 4 ++- components/nanopb/CMakeLists.txt | 6 +++++ components/nanopb/functions.cmake | 20 ++++++++++++++ main/.gitignore | 1 + main/CMakeLists.txt | 17 +++++++++--- main/messages.proto | 43 +++++++++++++++++++++++++++++++ main/ugv_main.c | 3 +++ 7 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 components/nanopb/CMakeLists.txt create mode 100644 components/nanopb/functions.cmake create mode 100644 main/.gitignore create mode 100644 main/messages.proto diff --git a/Dockerfile b/Dockerfile index e1b123d..0583fcd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,9 @@ gperf \ python \ python-serial \ cmake \ -ninja-build +ninja-build \ +protobuf-compiler \ +python-protobuf RUN mkdir /esp WORKDIR /esp diff --git a/components/nanopb/CMakeLists.txt b/components/nanopb/CMakeLists.txt new file mode 100644 index 0000000..b5b915d --- /dev/null +++ b/components/nanopb/CMakeLists.txt @@ -0,0 +1,6 @@ +set(COMPONENT_SRCS "nanopb/pb_common.c" + "nanopb/pb_decode.c" + "nanopb/pb_encode.c") +set(COMPONENT_ADD_INCLUDEDIRS "nanopb") + +register_component() diff --git a/components/nanopb/functions.cmake b/components/nanopb/functions.cmake new file mode 100644 index 0000000..02f95a8 --- /dev/null +++ b/components/nanopb/functions.cmake @@ -0,0 +1,20 @@ +function(nanopb_generate PROTO_FILE PB_OUT HDR_VAR SRC_VAR) + get_filename_component(PROTO_FILE_NAME ${PROTO_FILE} NAME_WE) + get_filename_component(PROTO_FILE_PATH ${PROTO_FILE} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + get_filename_component(PROTO_FILE_DIR ${PROTO_FILE_PATH} DIRECTORY) + set(PROTO_HDR "${PB_OUT}/${PROTO_FILE_NAME}.pb.h") + set(PROTO_SRC "${PB_OUT}/${PROTO_FILE_NAME}.pb.c") + list(APPEND "${HDR_VAR}" ${PROTO_HDR}) + list(APPEND "${SRC_VAR}" ${PROTO_SRC}) + if(NOT CMAKE_SCRIPT_MODE_FILE) + add_custom_command(OUTPUT ${PROTO_HDR} ${PROTO_SRC} + COMMAND protoc + --plugin=protoc-gen-nanopb=${CMAKE_CURRENT_LIST_DIR}/nanopb/generator/protoc-gen-nanopb + --nanopb_out=${PB_OUT} + --proto_path ${PROTO_FILE_DIR} + ${PROTO_FILE_PATH} + DEPENDS ${PROTO_FILE_PATH}) + endif() + list(APPEND ADDITIONAL_MAKE_CLEAN_FILES ${PROTO_HDR} ${PROTO_SRC}) +endfunction() + diff --git a/main/.gitignore b/main/.gitignore new file mode 100644 index 0000000..70d535c --- /dev/null +++ b/main/.gitignore @@ -0,0 +1 @@ +/pb_out diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9696840..0f3514b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,8 +1,17 @@ -set(COMPONENT_SRCS "ugv_main.c" +include(${CMAKE_CURRENT_LIST_DIR}/../components/nanopb/functions.cmake) + +set(PB_OUT ${CMAKE_CURRENT_SOURCE_DIR}/pb_out) + +nanopb_generate(messages.proto ${PB_OUT} PROTO_HDRS PROTO_SRCS) + +list(APPEND COMPONENT_SRCS "ugv_main.c" "ugv_comms.c" - "u8g2_esp32_hal.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") -set(COMPONENT_REQUIRES "u8g2" "sx127x_driver") + "u8g2_esp32_hal.c" + ${PROTO_SRCS}) +set(COMPONENT_PRIV_INCLUDEDIRS "." ${PB_OUT}) +set(COMPONENT_REQUIRES "u8g2" "sx127x_driver" "nanopb") + +make_directory(${PB_OUT}) register_component() diff --git a/main/messages.proto b/main/messages.proto new file mode 100644 index 0000000..8e41f87 --- /dev/null +++ b/main/messages.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; + +enum UGV_State { + IDLE = 0; + AQUIRING = 1; + DRIVING = 2; + FINISHED = 3; +} + +message Location { + uint32 fix_quality = 1; + float latitude = 2; + float longitude = 3; + float altitude = 4; +} + +message UGV_Status { + UGV_State state = 1; + Location location = 2; +} + +message UGV_Message { + oneof ugv_message { + UGV_Status status = 1; + uint32 command_ack = 2; + } +} + +enum GroundCommandType { + DISABLE = 0; + ENABLE = 1; +} + +message GroundCommand { + uint32 id = 1; + GroundCommandType type = 2; +} + +message GroundMessage { + oneof ground_message { + GroundCommand command = 1; + } +} diff --git a/main/ugv_main.c b/main/ugv_main.c index 62cb229..0034a7e 100644 --- a/main/ugv_main.c +++ b/main/ugv_main.c @@ -9,6 +9,9 @@ #include "u8g2_esp32_hal.h" #include "ugv_comms.h" #include "ugv_config.h" +#include "messages.pb.h" + +#include static const char *TAG = "ugv_main";