-
Notifications
You must be signed in to change notification settings - Fork 87
/
gamepad.c
267 lines (217 loc) · 7.29 KB
/
gamepad.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "gamepad.h"
#include <driver/adc.h>
#include "esp_adc_cal.h"
#include "pin_definitions.h"
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
#define READ_BIT I2C_MASTER_READ /*!< I2C master read */
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */
static volatile bool input_task_is_running = false;
static volatile input_gamepad_state gamepad_state;
static input_gamepad_state previous_gamepad_state;
static uint8_t debounce[GAMEPAD_INPUT_MAX];
static volatile bool input_gamepad_initialized = false;
static SemaphoreHandle_t xSemaphore;
static uint32_t i2c_frequency = 100000;
static i2c_port_t i2c_port = I2C_NUM_0;
static esp_err_t i2c_master_driver_initialize()
{
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_SDA,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_SCL,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = i2c_frequency
};
return i2c_param_config(i2c_port, &conf);
}
static uint8_t i2c_keypad_read()
{
int len = 1;
uint8_t *data = malloc(len);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, I2C_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
uint8_t val = data[0];
free(data);
return val;
}
input_gamepad_state gamepad_input_read_raw()
{
input_gamepad_state state = {0};
#ifdef CONFIG_ESPLAY20_HW
int joyX = adc1_get_raw(IO_X);
int joyY = adc1_get_raw(IO_Y);
if (joyX > 2048 + 1024)
{
state.values[GAMEPAD_INPUT_LEFT] = 1;
state.values[GAMEPAD_INPUT_RIGHT] = 0;
}
else if (joyX > 1024)
{
state.values[GAMEPAD_INPUT_LEFT] = 0;
state.values[GAMEPAD_INPUT_RIGHT] = 1;
}
else
{
state.values[GAMEPAD_INPUT_LEFT] = 0;
state.values[GAMEPAD_INPUT_RIGHT] = 0;
}
if (joyY > 2048 + 1024)
{
state.values[GAMEPAD_INPUT_UP] = 1;
state.values[GAMEPAD_INPUT_DOWN] = 0;
}
else if (joyY > 1024)
{
state.values[GAMEPAD_INPUT_UP] = 0;
state.values[GAMEPAD_INPUT_DOWN] = 1;
}
else
{
state.values[GAMEPAD_INPUT_UP] = 0;
state.values[GAMEPAD_INPUT_DOWN] = 0;
}
state.values[GAMEPAD_INPUT_SELECT] = !(gpio_get_level(SELECT));
state.values[GAMEPAD_INPUT_START] = !(gpio_get_level(START));
state.values[GAMEPAD_INPUT_A] = !(gpio_get_level(A));
state.values[GAMEPAD_INPUT_B] = !(gpio_get_level(B));
#endif
#ifdef CONFIG_ESPLAY_MICRO_HW
uint8_t i2c_data = i2c_keypad_read();
for (int i = 0; i < 8; ++i)
{
if(((1<<i)&i2c_data) == 0)
state.values[i] = 1;
else
state.values[i] = 0;
}
#endif
state.values[GAMEPAD_INPUT_MENU] = !(gpio_get_level(MENU));
state.values[GAMEPAD_INPUT_L] = !(gpio_get_level(L_BTN));
state.values[GAMEPAD_INPUT_R] = !(gpio_get_level(R_BTN));
return state;
}
static void input_task(void *arg)
{
input_task_is_running = true;
// Initialize state
for (int i = 0; i < GAMEPAD_INPUT_MAX; ++i)
{
debounce[i] = 0xff;
}
while (input_task_is_running)
{
// Shift current values
for (int i = 0; i < GAMEPAD_INPUT_MAX; ++i)
{
debounce[i] <<= 1;
}
// Read hardware
input_gamepad_state state = gamepad_input_read_raw();
// Debounce
xSemaphoreTake(xSemaphore, portMAX_DELAY);
for (int i = 0; i < GAMEPAD_INPUT_MAX; ++i)
{
debounce[i] |= state.values[i] ? 1 : 0;
uint8_t val = debounce[i] & 0x03; //0x0f;
switch (val)
{
case 0x00:
gamepad_state.values[i] = 0;
break;
case 0x03: //0x0f:
gamepad_state.values[i] = 1;
break;
default:
// ignore
break;
}
}
previous_gamepad_state = gamepad_state;
xSemaphoreGive(xSemaphore);
// delay
vTaskDelay(10 / portTICK_PERIOD_MS);
}
input_gamepad_initialized = false;
vSemaphoreDelete(xSemaphore);
// Remove the task from scheduler
vTaskDelete(NULL);
// Never return
while (1)
{
vTaskDelay(1);
}
}
void gamepad_read(input_gamepad_state *out_state)
{
if (!input_gamepad_initialized)
abort();
xSemaphoreTake(xSemaphore, portMAX_DELAY);
*out_state = gamepad_state;
xSemaphoreGive(xSemaphore);
}
void gamepad_init()
{
xSemaphore = xSemaphoreCreateMutex();
if (xSemaphore == NULL)
{
printf("xSemaphoreCreateMutex failed.\n");
abort();
}
//Configure button
gpio_config_t btn_config;
btn_config.intr_type = GPIO_INTR_ANYEDGE; //Enable interrupt on both rising and falling edges
btn_config.mode = GPIO_MODE_INPUT; //Set as Input
#ifdef CONFIG_ESPLAY20_HW
btn_config.pin_bit_mask = (uint64_t) //Bitmask
((uint64_t)1 << A) |
((uint64_t)1 << B) |
((uint64_t)1 << SELECT) |
((uint64_t)1 << MENU) |
((uint64_t)1 << START);
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(IO_X, ADC_ATTEN_11db);
adc1_config_channel_atten(IO_Y, ADC_ATTEN_11db);
#endif
#ifdef CONFIG_ESPLAY_MICRO_HW
i2c_master_driver_initialize();
i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
btn_config.pin_bit_mask = (uint64_t) //Bitmask
((uint64_t)1 << L_BTN) |
((uint64_t)1 << R_BTN) |
((uint64_t)1 << MENU);
#endif
btn_config.pull_up_en = GPIO_PULLUP_ENABLE; //Disable pullup
btn_config.pull_down_en = GPIO_PULLDOWN_DISABLE; //Enable pulldown
gpio_config(&btn_config);
input_gamepad_initialized = true;
// Start background polling
xTaskCreatePinnedToCore(&input_task, "input_task", 1024 * 2, NULL, 5, NULL, 1);
printf("input_gamepad_init done.\n");
}
void input_gamepad_terminate()
{
if (!input_gamepad_initialized)
abort();
i2c_driver_delete(i2c_port);
input_task_is_running = false;
}