From 5c8bb8af20cc2e2a554b3acbeeca2adc7e83164c Mon Sep 17 00:00:00 2001 From: Daiki Maekawa Date: Thu, 7 Mar 2019 13:43:50 +0900 Subject: [PATCH] Expose the communication functions (#97) * expose the communication functions * make an independent lib * fix typo * update the ignore rules * link to ypspur * address review comments * extern C added * const src array --- .gitignore | 1 + CMakeLists.txt | 10 ++++++++++ include/communication.h | 14 +++++++++++--- include/serial.h | 1 + include/shvel-param.h | 1 + src/communication.c | 27 ++------------------------- src/odometry.c | 2 +- src/param.c | 1 + src/serial.c | 20 ++++++++++++++++++++ 9 files changed, 48 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 6c59188..4e51e29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp .lvimrc build* +*~ diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d53bbc..09e9a01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,11 @@ add_library(ypspur SHARED src/yprintf.c src/utility.c ) + +add_library(ypspur-comm SHARED + src/communication.c +) + if(WIN32) target_link_libraries(ypspur wsock32 ws2_32) endif(WIN32) @@ -274,6 +279,7 @@ set_target_properties(ypspur-md-static PROPERTIES install(TARGETS ypspur-coordinator ypspur + ypspur-comm ypspur-static ypspur-md ypspur-md-static @@ -288,6 +294,10 @@ install(TARGETS install(FILES include/yp-spur.h DESTINATION include ) +install(FILES include/communication.h + DESTINATION include/ypspur +) + if(SSM_FOUND) install(DIRECTORY include/ssmtype diff --git a/include/communication.h b/include/communication.h index ce33036..1eb9f96 100644 --- a/include/communication.h +++ b/include/communication.h @@ -25,8 +25,16 @@ #define COMMUNICATION_INT_BYTE 0x07 #define COMMUNICATION_END_BYTE 0x0a -int encode(unsigned char *src, int len, unsigned char *dst, int buf_max); -int decord(unsigned char *src, int len, unsigned char *dst, int buf_max); -int encode_write(char *data, int len); +#ifdef __cplusplus +extern "C" +{ +#endif + +int encode(const unsigned char *src, int len, unsigned char *dst, int buf_max); +int decode(const unsigned char *src, int len, unsigned char *dst, int buf_max); + +#ifdef __cplusplus +} +#endif #endif // COMMUNICATION_H diff --git a/include/serial.h b/include/serial.h index e16095f..0e5d8f0 100644 --- a/include/serial.h +++ b/include/serial.h @@ -42,5 +42,6 @@ int serial_write(char *buf, int len); int serial_recieve(int (*serial_event)(char *, int, double, void *), void *data); void serial_flush_in(void); void serial_flush_out(void); +int encode_write(char *data, int len); #endif // SERIAL_H diff --git a/include/shvel-param.h b/include/shvel-param.h index 5731017..8005bef 100644 --- a/include/shvel-param.h +++ b/include/shvel-param.h @@ -81,6 +81,7 @@ typedef enum PARAM_enc_denominator, PARAM_servo = 64, PARAM_watch_dog_limit, + PARAM_heartbeat, PARAM_io_dir = 96, PARAM_io_data, PARAM_ad_mask, diff --git a/src/communication.c b/src/communication.c index ad5db94..ccbd963 100644 --- a/src/communication.c +++ b/src/communication.c @@ -35,15 +35,11 @@ /* yp-spur用 */ #include -#include - -/* ライブラリ用 */ -#include /** * @brief エンコード */ -int encode(unsigned char *src, int len, unsigned char *dst, int buf_max) +int encode(const unsigned char *src, int len, unsigned char *dst, int buf_max) { int pos, s_pos, w_pos; unsigned short b; @@ -87,7 +83,7 @@ int encode(unsigned char *src, int len, unsigned char *dst, int buf_max) * @param buf_max[in] デコード後のデータバッファのサイズ * @return デコード後のバイト数 */ -int decord(unsigned char *src, int len, unsigned char *dst, int buf_max) +int decode(const unsigned char *src, int len, unsigned char *dst, int buf_max) { unsigned short dat, b; int pos, s_pos, w_pos; @@ -123,22 +119,3 @@ int decord(unsigned char *src, int len, unsigned char *dst, int buf_max) return -rerr; return w_pos; } - -int encode_write(char *data, int len) -{ - unsigned char buf[128]; - int encode_len, ret; - - buf[0] = COMMUNICATION_START_BYTE; - encode_len = encode((unsigned char *)data, len, buf + 1, 126); - buf[encode_len + 1] = COMMUNICATION_END_BYTE; - - ret = serial_write((char *)buf, encode_len + 2); - if (ret <= 0) - { - return -1; - } - serial_flush_out(); - - return 0; -} diff --git a/src/odometry.c b/src/odometry.c index 513d1a5..adaf402 100644 --- a/src/odometry.c +++ b/src/odometry.c @@ -474,7 +474,7 @@ int odometry_receive(char *buf, int len, double receive_time, void *data) unsigned char data[48]; /* デコード処理 */ - decoded_len = decord((unsigned char *)com_buf, com_wp, (unsigned char *)data, 48); + decoded_len = decode((unsigned char *)com_buf, com_wp, (unsigned char *)data, 48); if ((mode == ISOCHRONOUS && decoded_len != decoded_len_req) || (mode == INTERRUPT && decoded_len != 6)) { diff --git a/src/param.c b/src/param.c index aeee204..5c0a543 100644 --- a/src/param.c +++ b/src/param.c @@ -40,6 +40,7 @@ /* yp-spur用 */ #include +#include #include #include #include diff --git a/src/serial.c b/src/serial.c index 5d9e0ed..a3cfe14 100644 --- a/src/serial.c +++ b/src/serial.c @@ -38,6 +38,7 @@ /* yp-spur用 */ #include +#include #include #include #include @@ -508,6 +509,25 @@ int serial_recieve(int (*serial_event)(char *, int, double, void *), void *data) } } +int encode_write(char *data, int len) +{ + unsigned char buf[128]; + int encode_len, ret; + + buf[0] = COMMUNICATION_START_BYTE; + encode_len = encode((unsigned char *)data, len, buf + 1, 126); + buf[encode_len + 1] = COMMUNICATION_END_BYTE; + + ret = serial_write((char *)buf, encode_len + 2); + if (ret <= 0) + { + return -1; + } + serial_flush_out(); + + return 0; +} + int serial_write(char *buf, int len) { #if !defined(__MINGW32__)