Skip to content

Commit 8dde45d

Browse files
authored
Merge pull request #17 from sogwms/master
[controller/ps2] update init interface && add rocker support
2 parents cea35db + 4e759b7 commit 8dde45d

File tree

3 files changed

+87
-39
lines changed

3 files changed

+87
-39
lines changed

controller/ps2.c

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
2921
static rt_thread_t tid_ps2 = RT_NULL;
3022
static 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+
3258
static 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

6490
static 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,

controller/ps2.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
#include <rtdevice.h>
66
#include <command.h>
77

8-
// PIN
9-
#define PS2_DEFAULT_CS_PIN 28 // GET_PIN(B, 12)
10-
#define PS2_DEFAULT_CLK_PIN 29 // GET_PIN(B, 13)
11-
#define PS2_DEFAULT_DO_PIN 31 // GET_PIN(B, 15)
12-
#define PS2_DEFAULT_DI_PIN 30 // GET_PIN(B, 14)
13-
148
// COMMAND
159
#define PS2_CMD_VIBRATE 1
1610

@@ -37,13 +31,12 @@
3731
#define PS2_BTN_FORK (1 << 14)
3832
#define PS2_BTN_SQUARE (1 << 15)
3933

40-
#define PS2_ROCKER_LX (9)
41-
#define PS2_ROCKER_LY (10)
42-
#define PS2_ROCKER_RX (11)
43-
#define PS2_ROCKER_RY (12)
34+
#define PS2_ROCKER_LX (16)
35+
#define PS2_ROCKER_LY (17)
36+
#define PS2_ROCKER_RX (18)
37+
#define PS2_ROCKER_RY (19)
4438

4539
typedef struct ps2_ctrl_data *ps2_ctrl_data_t;
46-
typedef struct ps2 *ps2_t;
4740

4841
struct ps2_ctrl_data
4942
{
@@ -54,7 +47,7 @@ struct ps2_ctrl_data
5447
uint8_t right_stick_y;
5548
};
5649

57-
void ps2_init(void);
50+
void ps2_init(rt_base_t cs_pin, rt_base_t clk_pin, rt_base_t do_pin, rt_base_t di_pin);
5851
int ps2_scan(ps2_ctrl_data_t pt);
5952
int ps2_read_light(void);
6053

controller/ps2_table.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ps2.h"
22

3-
#define PS2_TABLE_SIZE 16
3+
#define PS2_TABLE_SIZE 20
44
#define PS2_DEFAULT_TABLE \
55
{ \
66
{PS2_BTN_SELECT ,COMMAND_CAR_STOP}, \
@@ -18,12 +18,15 @@
1818
{PS2_BTN_TRIANGLE ,COMMAND_NONE}, \
1919
{PS2_BTN_CICLE ,COMMAND_NONE}, \
2020
{PS2_BTN_FORK ,COMMAND_NONE}, \
21-
{PS2_BTN_SQUARE ,COMMAND_NONE} \
21+
{PS2_BTN_SQUARE ,COMMAND_NONE}, \
22+
{PS2_ROCKER_LX ,COMMAND_NONE}, \
23+
{PS2_ROCKER_LY ,COMMAND_NONE}, \
24+
{PS2_ROCKER_RX ,COMMAND_NONE}, \
25+
{PS2_ROCKER_RY ,COMMAND_NONE} \
2226
}
2327

2428
struct ps2_table
2529
{
2630
int ps2_cmd;
2731
int standard_cmd;
2832
};
29-

0 commit comments

Comments
 (0)