@@ -9,14 +9,6 @@ Change From YFRobot. www.yfrobot.com
99#define DBG_LEVEL DBG_LOG
1010#include <rtdbg.h>
1111
12- #define HAL_CS_H () rt_pin_write(PS2_DEFAULT_CS_PIN, PIN_HIGH)
13- #define HAL_CS_L () rt_pin_write(PS2_DEFAULT_CS_PIN, PIN_LOW)
14- #define HAL_CLK_H () rt_pin_write(PS2_DEFAULT_CLK_PIN, PIN_HIGH)
15- #define HAL_CLK_L () rt_pin_write(PS2_DEFAULT_CLK_PIN, PIN_LOW)
16- #define HAL_DO_H () rt_pin_write(PS2_DEFAULT_DO_PIN, PIN_HIGH)
17- #define HAL_DO_L () rt_pin_write(PS2_DEFAULT_DO_PIN, PIN_LOW)
18- #define HAL_GET_DI () rt_pin_read(PS2_DEFAULT_DI_PIN)
19-
2012#define THREAD_DELAY_TIME 50
2113
2214#define THREAD_PRIORITY 16
@@ -29,6 +21,40 @@ static uint8_t light_mode = PS2_NO_MODE;
2921static rt_thread_t tid_ps2 = RT_NULL ;
3022static struct ps2_table table [PS2_TABLE_SIZE ] = PS2_DEFAULT_TABLE ;
3123
24+ static rt_base_t ps2_cs_pin ;
25+ static rt_base_t ps2_clk_pin ;
26+ static rt_base_t ps2_do_pin ;
27+ static rt_base_t ps2_di_pin ;
28+
29+ static void hal_cs_high (void )
30+ {
31+ rt_pin_write (ps2_cs_pin , PIN_HIGH );
32+ }
33+ static void hal_cs_low (void )
34+ {
35+ rt_pin_write (ps2_cs_pin , PIN_LOW );
36+ }
37+ static void hal_clk_high (void )
38+ {
39+ rt_pin_write (ps2_clk_pin , PIN_HIGH );
40+ }
41+ static void hal_clk_low (void )
42+ {
43+ rt_pin_write (ps2_clk_pin , PIN_LOW );
44+ }
45+ static void hal_do_high (void )
46+ {
47+ rt_pin_write (ps2_do_pin , PIN_HIGH );
48+ }
49+ static void hal_do_low (void )
50+ {
51+ rt_pin_write (ps2_do_pin , PIN_LOW );
52+ }
53+ static int hal_read_di (void )
54+ {
55+ return rt_pin_read (ps2_di_pin );
56+ }
57+
3258static void _delay_us (uint16_t us )
3359{
3460 for (int i = 0 ; i < us ; i ++ )
@@ -45,31 +71,31 @@ static uint8_t _transfer(uint8_t data)
4571 for (uint16_t i = 0x01 ; i < 0x0100 ; i <<= 1 )
4672 {
4773 if (i & data )
48- HAL_DO_H ();
74+ hal_do_high ();
4975 else
50- HAL_DO_L ();
76+ hal_do_low ();
5177
52- HAL_CLK_H ();
78+ hal_clk_high ();
5379 KEEP_TIME ();
54- HAL_CLK_L ();
55- if (HAL_GET_DI ())
80+ hal_clk_low ();
81+ if (hal_read_di ())
5682 temp = i | temp ;
5783 KEEP_TIME ();
58- HAL_CLK_H ();
84+ hal_clk_high ();
5985 }
6086
6187 return temp ;
6288}
6389
6490static void transfer (const uint8_t * pb_send , uint8_t * pb_recv , uint8_t len )
6591{
66- HAL_CS_L ();
92+ hal_cs_low ();
6793 _delay_us (16 );
6894 for (uint8_t i = 0 ; i < len ; i ++ )
6995 {
7096 pb_recv [i ] = _transfer (pb_send [i ]);
7197 }
72- HAL_CS_H ();
98+ hal_cs_high ();
7399 _delay_us (16 );
74100}
75101
@@ -130,18 +156,44 @@ static void ps2_thread_entry(void *param)
130156 }
131157 }
132158 }
159+
160+ // rocker
161+ if (ps2_read_light () == PS2_RED_MODE )
162+ {
163+ if (table [PS2_ROCKER_LX ].standard_cmd != COMMAND_NONE )
164+ {
165+ command_handle (table [PS2_ROCKER_LX ].standard_cmd , & ctrl_data .left_stick_x );
166+ }
167+ if (table [PS2_ROCKER_LY ].standard_cmd != COMMAND_NONE )
168+ {
169+ command_handle (table [PS2_ROCKER_LY ].standard_cmd , & ctrl_data .left_stick_y );
170+ }
171+ if (table [PS2_ROCKER_RX ].standard_cmd != COMMAND_NONE )
172+ {
173+ command_handle (table [PS2_ROCKER_RX ].standard_cmd , & ctrl_data .right_stick_x );
174+ }
175+ if (table [PS2_ROCKER_RY ].standard_cmd != COMMAND_NONE )
176+ {
177+ command_handle (table [PS2_ROCKER_RY ].standard_cmd , & ctrl_data .right_stick_y );
178+ }
179+ }
133180 }
134181}
135182
136- void ps2_init (void )
183+ void ps2_init (rt_base_t cs_pin , rt_base_t clk_pin , rt_base_t do_pin , rt_base_t di_pin )
137184{
138- rt_pin_mode (PS2_DEFAULT_CS_PIN , PIN_MODE_OUTPUT );
139- rt_pin_mode (PS2_DEFAULT_CLK_PIN , PIN_MODE_OUTPUT );
140- rt_pin_mode (PS2_DEFAULT_DO_PIN , PIN_MODE_OUTPUT );
141- rt_pin_mode (PS2_DEFAULT_DI_PIN , PIN_MODE_INPUT );
142-
143- HAL_CS_H ();
144- HAL_CLK_H ();
185+ ps2_cs_pin = cs_pin ;
186+ ps2_clk_pin = clk_pin ;
187+ ps2_do_pin = do_pin ;
188+ ps2_di_pin = di_pin ;
189+
190+ rt_pin_mode (ps2_cs_pin , PIN_MODE_OUTPUT );
191+ rt_pin_mode (ps2_clk_pin , PIN_MODE_OUTPUT );
192+ rt_pin_mode (ps2_do_pin , PIN_MODE_OUTPUT );
193+ rt_pin_mode (ps2_di_pin , PIN_MODE_INPUT );
194+
195+ hal_cs_high ();
196+ hal_clk_high ();
145197
146198 tid_ps2 = rt_thread_create ("ps2" ,
147199 ps2_thread_entry , RT_NULL ,
0 commit comments