Skip to content

Commit

Permalink
Expose the communication functions (#97)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DaikiMaekawa authored and at-wat committed Mar 7, 2019
1 parent 43ec3b6 commit 5c8bb8a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
.lvimrc
build*
*~
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 11 additions & 3 deletions include/communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions include/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions include/shvel-param.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 2 additions & 25 deletions src/communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@

/* yp-spur用 */
#include <communication.h>
#include <serial.h>

/* ライブラリ用 */
#include <ypspur-coordinator.h>

/**
* @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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/odometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
1 change: 1 addition & 0 deletions src/param.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

/* yp-spur用 */
#include <communication.h>
#include <serial.h>
#include <param.h>
#include <control.h>
#include <command.h>
Expand Down
20 changes: 20 additions & 0 deletions src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

/* yp-spur用 */
#include <serial.h>
#include <communication.h>
#include <utility.h>
#include <param.h>
#include <ypspur-coordinator.h>
Expand Down Expand Up @@ -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__)
Expand Down

0 comments on commit 5c8bb8a

Please sign in to comment.