44#define DBG_LEVEL DBG_LOG
55#include <rtdbg.h>
66
7- #define TABLE_MAX_SIZE 32
7+ // Thread
8+ #define THREAD_PRIORITY ((RT_THREAD_PRIORITY_MAX / 3) + 1)
9+ #define THREAD_STACK_SIZE 512
10+ #define THREAD_TIMESLICE 10
11+ static rt_thread_t trd_cmd = RT_NULL ;
812
13+ // Message Queue
14+ #define MAX_MSGS 16
15+ static rt_mq_t msg_cmd = RT_NULL ;
16+
17+ // Table
18+ #define TABLE_MAX_SIZE 32
19+ struct command
20+ {
21+ rt_int16_t robot_cmd ;
22+ void (* handler )(command_info_t info );
23+ };
924static struct command command_table [TABLE_MAX_SIZE ];
10- static uint8_t command_table_size = 0 ;
25+ static uint16_t command_table_size = 0 ;
1126
12- rt_err_t command_register (rt_int8_t cmd , rt_err_t (* handler )(rt_int8_t cmd , void * param ))
27+ rt_err_t command_register (rt_int16_t cmd , void (* handler )(command_info_t info ))
1328{
1429 if (command_table_size > TABLE_MAX_SIZE - 1 )
1530 {
@@ -18,41 +33,71 @@ rt_err_t command_register(rt_int8_t cmd, rt_err_t (*handler)(rt_int8_t cmd, void
1833 }
1934 command_table [command_table_size ].robot_cmd = cmd ;
2035 command_table [command_table_size ].handler = handler ;
21- command_table_size += 1 ;
36+ command_table_size ++ ;
2237
2338 return RT_EOK ;
2439}
2540
26- rt_err_t command_unregister (rt_int8_t cmd )
41+ rt_err_t command_unregister (rt_int16_t cmd )
2742{
28- // TODO
43+ for (int i = 0 ; i < command_table_size ; i ++ )
44+ {
45+ if (command_table [i ].robot_cmd == cmd )
46+ {
47+ for (int j = i ; j < command_table_size - 1 ; j ++ )
48+ {
49+ rt_memcpy (& command_table [j ], & command_table [j + 1 ], sizeof (struct command ));
50+ }
51+ command_table_size -- ;
52+ break ;
53+ }
54+ }
2955
3056 return RT_EOK ;
3157}
3258
33- rt_err_t command_handle (rt_int8_t cmd , void * param )
59+ rt_err_t command_send (command_info_t info )
60+ {
61+ return rt_mq_send (msg_cmd , info , sizeof (struct command_info ));
62+ }
63+
64+ static void command_thread_entry (void * param )
3465{
35- // look-up table and call callback
36- for (uint16_t i = 0 ; i < command_table_size ; i ++ )
66+ struct command_info info ;
67+
68+ while (1 )
3769 {
38- if (command_table [i ].robot_cmd == cmd )
70+ rt_mq_recv (msg_cmd , & info , sizeof (struct command_info ), RT_WAITING_FOREVER );
71+ // look-up table and call callback
72+ for (int i = 0 ; i < command_table_size ; i ++ )
3973 {
40- command_table [i ].handler (command_table [i ].robot_cmd , param );
41- return RT_EOK ;
74+ if (command_table [i ].robot_cmd == info .cmd )
75+ {
76+ command_table [i ].handler (& info );
77+ break ;
78+ }
4279 }
4380 }
44-
45- return RT_ERROR ;
4681}
4782
48- rt_err_t command_send ( command_sender_t sender , rt_int8_t cmd , void * param , rt_uint16_t len )
83+ void command_init ( void )
4984{
50- if (sender -> send != RT_NULL )
85+ msg_cmd = rt_mq_create ("command" , sizeof (struct command_info ), MAX_MSGS , RT_IPC_FLAG_FIFO );
86+ if (msg_cmd == RT_NULL )
5187 {
52- sender -> send (cmd , param , len );
88+ LOG_E ("Failed to creat meassage queue" );
89+ return ;
5390 }
54-
55- return RT_EOK ;
56- }
5791
92+ trd_cmd = rt_thread_create ("command" ,
93+ command_thread_entry , RT_NULL ,
94+ THREAD_STACK_SIZE ,
95+ THREAD_PRIORITY , THREAD_TIMESLICE );
5896
97+ if (trd_cmd == RT_NULL )
98+ {
99+ LOG_E ("Failed to creat thread" );
100+ return ;
101+ }
102+ rt_thread_startup (trd_cmd );
103+ }
0 commit comments