diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e4f7ee..a1aa354 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,12 +29,14 @@ cmake_minimum_required(VERSION 3.16) # Build Type set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE) -if(NOT DEFINED CMAKE_BUILD_TYPE) - message(STATUS "'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)") - set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build") -endif() -if(DEFINED CMAKE_BUILD_TYPE) - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${CMAKE_CONFIGURATION_TYPES}") +if(NOT MSVC) + if(NOT DEFINED CMAKE_BUILD_TYPE) + message(STATUS "'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)") + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build") + endif() + if(DEFINED CMAKE_BUILD_TYPE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${CMAKE_CONFIGURATION_TYPES}") + endif() endif() project( diff --git a/cmake/RMVLGenConfig.cmake b/cmake/RMVLGenConfig.cmake index c974c35..0a1288c 100644 --- a/cmake/RMVLGenConfig.cmake +++ b/cmake/RMVLGenConfig.cmake @@ -67,8 +67,12 @@ __find_imported_modules(optlc OPTLightCtrl) # all targets have been configured in the corresponding CMakeLists.txt, no need to configure again # 3rdparty (download) -if(open62541_IN_3RD) - list(APPEND RMVL_MODULES_3RD_DOWNLOAD_CONFIGCMAKE "include(\$\{RMVL_INSTALL_PATH\}/lib/cmake/open62541/open62541Config.cmake)\n") +if(WITH_OPEN62541) + if(open62541_IN_3RD) + list(APPEND RMVL_MODULES_3RD_DOWNLOAD_CONFIGCMAKE "include(\$\{RMVL_INSTALL_PATH\}/lib/cmake/open62541/open62541Config.cmake)\n") + else() + list(APPEND RMVL_MODULES_3RD_DOWNLOAD_CONFIGCMAKE "find_package(open62541 REQUIRED)\n") + endif() endif() # -------------------------------------------------------------------------------------------- diff --git a/cmake/RMVLModule.cmake b/cmake/RMVLModule.cmake index b4cd653..5676eda 100644 --- a/cmake/RMVLModule.cmake +++ b/cmake/RMVLModule.cmake @@ -173,7 +173,7 @@ macro(rmvl_add_module _name) ${the_module} INTERFACE ${MD_EXTRA_HEADER} $ - $ + $ ) foreach(_dep ${MD_DEPENDS}) target_link_libraries( @@ -190,7 +190,7 @@ macro(rmvl_add_module _name) ${the_module} PUBLIC ${MD_EXTRA_HEADER} $ - $ + $ PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ) foreach(_dep ${MD_DEPENDS}) diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt index 5bc8cf2..a7cf725 100644 --- a/modules/core/CMakeLists.txt +++ b/modules/core/CMakeLists.txt @@ -4,11 +4,13 @@ find_package(Threads REQUIRED) if(WITH_OPENCV) - rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS}) if(WIN32) + rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT} opencv_world) install(DIRECTORY "${OpenCV_INCLUDE_DIRS}/opencv2" DESTINATION ${RMVL_INCLUDE_INSTALL_PATH}) install(FILES ${OpenCV_DLL_PATHS} DESTINATION ${RMVL_BIN_INSTALL_PATH}) install(FILES ${OpenCV_LIB_PATHS} DESTINATION ${RMVL_3P_LIB_INSTALL_PATH}) + else() + rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS}) endif() else() rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT}) diff --git a/modules/core/include/rmvl/core/util.hpp b/modules/core/include/rmvl/core/util.hpp index 5ed4ae9..06cc231 100755 --- a/modules/core/include/rmvl/core/util.hpp +++ b/modules/core/include/rmvl/core/util.hpp @@ -508,7 +508,7 @@ struct hash_aggregate std::size_t retval{}; reflect::for_each(r, [&retval](const auto &val) { // boost::hash_combine - retval = retval ^ (std::hash>{}(val) << 1); + retval = retval ^ (std::hash>{}(val) << 1); }); return retval; } diff --git a/modules/core/src/serial.cpp b/modules/core/src/serial.cpp index 0d7e9da..b66e032 100755 --- a/modules/core/src/serial.cpp +++ b/modules/core/src/serial.cpp @@ -104,7 +104,7 @@ void SerialPort::Impl::open() 0, nullptr, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, + 0, nullptr); if (_handle == INVALID_HANDLE_VALUE) { @@ -113,6 +113,8 @@ void SerialPort::Impl::open() return; } + SetupComm(_handle, 1024, 1024); + COMMTIMEOUTS timeouts{}; if (_mode.read_mode == SerialReadMode::BLOCK) { @@ -126,8 +128,6 @@ void SerialPort::Impl::open() timeouts.ReadTotalTimeoutConstant = 0; timeouts.ReadTotalTimeoutMultiplier = 0; } - timeouts.WriteTotalTimeoutConstant = 1; - timeouts.WriteTotalTimeoutMultiplier = 1; if (!SetCommTimeouts(_handle, &timeouts)) { WARNING_("Failed to set the serial port timeout."); diff --git a/modules/light/src/hik_light_control/light_control.cpp b/modules/light/src/hik_light_control/light_control.cpp index 10365d5..0fdef24 100644 --- a/modules/light/src/hik_light_control/light_control.cpp +++ b/modules/light/src/hik_light_control/light_control.cpp @@ -25,7 +25,7 @@ HikLightController::Impl::Impl(const LightConfig &cfg, std::string_view id) { SerialPortMode mode{}; mode.baud_rate = BaudRate::BR_19200; - mode.read_mode = SerialReadMode::BLOCK; + mode.read_mode = SerialReadMode::NONBLOCK; _sp = std::make_unique(id, mode); } else @@ -70,7 +70,15 @@ int HikLightController::Impl::get(int chn) const return -1; if (buf == "NG") return -1; - return std::stoi(std::string(buf.begin() + 1, buf.end())); + try + { + return std::stoi(std::string(buf.begin() + 1, buf.end())); + } + catch (const std::exception &) + { + ERROR_("Failed to obtain brightness, try to increase the 'DELAY_AFTER_WRITE' parameter"); + return -1; + } } bool HikLightController::Impl::set(int chn, int val) diff --git a/samples/light/hik_light.cpp b/samples/light/hik_light.cpp index 354f3b4..4e06d2b 100644 --- a/samples/light/hik_light.cpp +++ b/samples/light/hik_light.cpp @@ -12,6 +12,7 @@ #include #include +#include static void program_help() { @@ -22,13 +23,17 @@ static void program_help() static void cmd_help() { - constexpr const char *usage = "\nCommands Usage:\n" - " help, ?, usage \033[32m# show this help message\033[0m\n" + constexpr const char *usage = "\n\033[34mCommon functions usage:\033[0m\n" + " h, help, ?, usage \033[32m# show this help message\033[0m\n" + " exit, quit, q \033[32m# exit the program\033[0m\n" + "\n\033[34mParameters control usage:\033[0m\n" + " ctl get delay \033[32m# get the delay time after writing\033[0m\n" + " ctl set delay \033[32m# set the delay time after writing\033[0m\n" + "\n\033[34mCommands usage:\033[0m\n" " open \033[32m# open all the channels\033[0m\n" " close \033[32m# close all the channels\033[0m\n" " get \033[32m# get the brightness of the specified channel\033[0m\n" - " set \033[32m# set the brightness of the specified channel\033[0m\n" - " exit, quit, q \033[32m# exit the program\033[0m\n"; + " set \033[32m# set the brightness of the specified channel\033[0m\n"; std::cout << usage << std::endl; } @@ -54,6 +59,102 @@ static std::vector split(std::string_view str, char delim) return res; } +static void parametersCtl(std::vector &cmds) +{ + if (cmds.size() < 3) + { + warning(); + return; + } + if (cmds.size() == 3) + { + if (cmds[1] == "get") + { + if (cmds[2] == "delay") + std::cout << rm::para::hik_light_control_param.DELAY_AFTER_WRITE << '\n'; + else + warning(); + } + else + warning(); + } + else if (cmds.size() == 4) + { + if (cmds[1] == "set") + { + if (cmds[2] == "delay") + { + uint32_t val = static_cast(std::stoi(cmds[3])); + rm::para::hik_light_control_param.DELAY_AFTER_WRITE = val; + std::cout << "'Success'\n"; + } + else + warning(); + } + else + warning(); + } + else + warning(); +} + +static void functionCtl(rm::HikLightController &lc, bool &stop, std::vector &cmds) +{ + if (cmds.size() == 1) + { + if (cmds[0] == "h" || cmds[0] == "help" || cmds[0] == "?" || cmds[0] == "usage") + cmd_help(); + else if (cmds[0] == "open") + { + if (!lc.open()) + std::cout << "'Failed to open all the channels.'\n"; + else + std::cout << "'Success'\n"; + } + else if (cmds[0] == "close") + { + if (!lc.close()) + std::cout << "'Failed to close all the channels.'\n"; + else + std::cout << "'Success'\n"; + } + else if (cmds[0] == "exit" || cmds[0] == "quit" || cmds[0] == "q") + stop = true; + else + warning(); + } + else if (cmds.size() == 2) + { + if (cmds[0] == "get") + { + int chn = std::stoi(cmds[1]); + int val = lc.get(chn); + if (val < 0) + std::cout << "'Failed to get the brightness of channel " << chn << "'\n"; + else + std::cout << val << '\n'; + } + else + warning(); + } + else if (cmds.size() == 3) + { + if (cmds[0] == "set") + { + int chn = std::stoi(cmds[1]); + int val = std::stoi(cmds[2]); + if (!lc.set(chn, val)) + std::cout << "'Failed to set the brightness of channel " << chn << "'\n"; + else + std::cout << "'Success'\n"; + } + else + warning(); + } + else if (cmds.size() > 3) + warning(); +} + int main(int argc, char *argv[]) { if (argc != 2) @@ -64,71 +165,28 @@ int main(int argc, char *argv[]) rm::LightConfig cfg{}; cfg.handle_mode = rm::LightHandleMode::Serial; - auto lc = rm::HikLightController(cfg, argv[1]); + std::string id{argv[1]}; + auto lc = rm::HikLightController(cfg, id); if (!lc.isOpened()) { ERROR_("Failed to open the light controller."); return -1; } - while (true) + bool stop{}; + while (!stop) { std::cout << ">>> "; std::string cmd{}; std::getline(std::cin, cmd); auto cmds = split(cmd, ' '); - if (cmds.size() == 1) - { - if (cmds[0] == "help" || cmds[0] == "?" || cmds[0] == "usage") - cmd_help(); - else if (cmds[0] == "open") - { - if (!lc.open()) - std::cout << "'Failed to open all the channels.'\n"; - else - std::cout << "'Success'\n"; - } - else if (cmds[0] == "close") - { - if (!lc.close()) - std::cout << "'Failed to close all the channels.'\n"; - else - std::cout << "'Success'\n"; - } - else if (cmds[0] == "exit" || cmds[0] == "quit" || cmds[0] == "q") - break; - else - warning(); - } - else if (cmds.size() == 2) - { - if (cmds[0] == "get") - { - int chn = std::stoi(cmds[1]); - int val = lc.get(chn); - if (val < 0) - std::cout << "'Failed to get the brightness of channel " << chn << "'\n"; - else - std::cout << val << '\n'; - } - else - warning(); - } - else if (cmds.size() == 3) - { - if (cmds[0] == "set") - { - int chn = std::stoi(cmds[1]); - int val = std::stoi(cmds[2]); - if (!lc.set(chn, val)) - std::cout << "'Failed to set the brightness of channel " << chn << "'\n"; - else - std::cout << "'Success'\n"; - } - else - warning(); - } - else if (cmds.size() > 3) - warning(); + if (cmds.empty()) + continue; + // Parameters control + if (cmds[0] == "ctl") + parametersCtl(cmds); + // Function commands + else + functionCtl(lc, stop, cmds); } }