Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gamepad.c - github.com/ClusterM/nessmd2usb.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b18eb7e0ab3ed4e8b2e8345d3bd790dce7235007 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "defines.h"
#include <avr/io.h>
#include <util/delay.h>
#include "gamepad.h"
#include "bits.h"

#ifdef NES_ENABLED
void init_nes_gamepad()
{
	NES_PORT_DDR |= 1<<NES_LATCH_PIN; // Latch, output
	NES_PORT_DDR |= 1<<NES_CLOCK_PIN; // Clock, output
	NES_PORT_DDR &= ~(1<<NES_DATA_PIN); // Data, input
	NES_PORT_PORT |= 1<<NES_DATA_PIN; // Data, pull-up
#ifdef NES_SECOND_ENABLED	
	NES_PORT_DDR &= ~(1<<NES_DATA_PIN2); // Data, input
	NES_PORT_PORT |= 1<<NES_DATA_PIN2; // Data, pull-up
#endif
#ifdef NES_THIRD_ENABLED	
	NES_PORT_DDR &= ~(1<<NES_DATA_PIN3); // Data, input
	NES_PORT_PORT |= 1<<NES_DATA_PIN3; // Data, pull-up
#endif
#ifdef NES_FORTH_ENABLED	
	NES_PORT_DDR &= ~(1<<NES_DATA_PIN4); // Data, input
	NES_PORT_PORT |= 1<<NES_DATA_PIN4; // Data, pull-up
#endif
}

uint32_t get_nes_gamepad()
{
	uint32_t gamepad_data = 0;
	NES_PORT_PORT |= 1<<NES_LATCH_PIN; // Latch
	_delay_us(10);
	NES_PORT_PORT &= ~(1<<NES_LATCH_PIN); // Latch
	_delay_us(10);
	int b;
	for (b = 0; b < 8; b++)
	{
		NES_PORT_PORT &= ~(1<<NES_CLOCK_PIN); // Clock
		_delay_us(10);
		gamepad_data |= (((NES_PORT_PIN>>NES_DATA_PIN)&1)<<b);
#ifdef NES_SECOND_ENABLED
		gamepad_data |= (uint32_t)(((NES_PORT_PIN>>NES_DATA_PIN2)&1)<<b) << 8;
#endif
#ifdef NES_THIRD_ENABLED
		gamepad_data |= (uint32_t)(((NES_PORT_PIN>>NES_DATA_PIN3)&1)<<b) << 16;
#endif
#ifdef NES_FORTH_ENABLED
		gamepad_data |= (uint32_t)(((NES_PORT_PIN>>NES_DATA_PIN4)&1)<<b) << 24;
#endif
		NES_PORT_PORT |= 1<<NES_CLOCK_PIN; // Clock
		_delay_us(10);
	}		
	return gamepad_data;
}
#endif

uint32_t get_nes_gamepad_decoded(void)
{
	return ~get_nes_gamepad();
}

#ifdef SNES_ENABLED
void init_snes_gamepad()
{
	SNES_PORT_DDR |= 1<<SNES_LATCH_PIN; // Latch, output
	SNES_PORT_DDR |= 1<<SNES_CLOCK_PIN; // Clock, output
	SNES_PORT_DDR &= ~(1<<SNES_DATA_PIN); // Data, input
	SNES_PORT_PORT |= 1<<SNES_DATA_PIN; // Data, pull-up
}

uint16_t get_snes_gamepad()
{
	uint16_t gamepad_data = 0;
	SNES_PORT_PORT &= ~(1<<SNES_LATCH_PIN); // Latch
	int b;
	for (b = 0; b < 16; b++)
	{
		SNES_PORT_PORT &= ~(1<<SNES_CLOCK_PIN); // Clock
		_delay_us(10);
		gamepad_data |= ((uint16_t)((SNES_PORT_PIN>>SNES_DATA_PIN)&1)<<b);
		SNES_PORT_PORT |= 1<<SNES_CLOCK_PIN; // Clock
		_delay_us(10);
	}		
	SNES_PORT_PORT |= 1<<SNES_LATCH_PIN; // Latch
	return gamepad_data;
}
#endif

#ifdef N64_ENABLED
void init_n64_gamepad()
{
	TCCR0 |= _BV(CS00); // Timer 
	N64_PORT_DDR &= ~(1<<N64_DATA_PIN); // Input
	N64_PORT_PORT &= ~(1<<N64_DATA_PIN); // No pull-up (using external resistor)
}

int get_n64_gamepad(uint8_t* data)
{
	int b, bit;
	N64SEND_0; N64SEND_0; N64SEND_0; N64SEND_0; N64SEND_0; N64SEND_0; N64SEND_0; N64SEND_1; N64SEND_STOP;
	for (b = 0; b < 4; b++)
	{
		data[b] = 0;
		for (bit = 0; bit < 8; bit++)
		{		
			TCNT0 = 0;
			while (!N64SIGNAL) if (TCNT0 >= 0xF0) return 0;
			TCNT0 = 0;
			while(N64SIGNAL) if (TCNT0 >= 0xF0) return 0;
			data[b] = data[b]<<1;
			if (TCNT0 < 0x24 * F_CPU / 20000000UL) data[b] |= 1;
		}
	}
	return 1;
}
#endif

#ifdef SMD_ENABLED
void init_smd_gamepad()
{
	SMD_SELECT_PORT_DDR |= 1<<SMD_SELECT_PIN; // Select, output
	SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA0_PIN); // Data 0, input
	SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA1_PIN); // Data 1, input
	SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA2_PIN); // Data 2, input
	SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA3_PIN); // Data 3, input
	SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA4_PIN); // Data 4, input
	SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA5_PIN); // Data 5, input
	SMD_DATA_PORT_PORT |= 1<<SMD_DATA0_PIN; // Data 0, pull-up
	SMD_DATA_PORT_PORT |= 1<<SMD_DATA1_PIN; // Data 1, pull-up
	SMD_DATA_PORT_PORT |= 1<<SMD_DATA2_PIN; // Data 2, pull-up
	SMD_DATA_PORT_PORT |= 1<<SMD_DATA3_PIN; // Data 3, pull-up
	SMD_DATA_PORT_PORT |= 1<<SMD_DATA4_PIN; // Data 4, pull-up
	SMD_DATA_PORT_PORT |= 1<<SMD_DATA5_PIN; // Data 5, pull-up
#ifdef SMD_SECOND_ENABLED	
	SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA0_PIN2); // Data 0, input
	SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA1_PIN2); // Data 1, input
	SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA2_PIN2); // Data 2, input
	SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA3_PIN2); // Data 3, input
	SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA4_PIN2); // Data 4, input
	SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA5_PIN2); // Data 5, input
	SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA0_PIN2; // Data 0, pull-up
	SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA1_PIN2; // Data 1, pull-up
	SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA2_PIN2; // Data 2, pull-up
	SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA3_PIN2; // Data 3, pull-up
	SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA4_PIN2; // Data 4, pull-up
	SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA5_PIN2; // Data 5, pull-up
#endif
}

uint32_t get_smd_gamepad()
{
	uint8_t gamepad_data_low = 0xFF;
	uint8_t gamepad_data_high = 0xFF;
	uint8_t gamepad_data_low2 = 0xFF;
	uint8_t gamepad_data_high2 = 0xFF;
	SMD_SELECT_PORT_PORT &= ~(1<<SMD_SELECT_PIN); // Select - low
	_delay_us(50);
	gamepad_data_low = ((SMD_DATA_PORT_PIN>>SMD_DATA0_PIN)&1) 
		| (((SMD_DATA_PORT_PIN>>SMD_DATA1_PIN)&1)<<1) 
		| (((SMD_DATA_PORT_PIN>>SMD_DATA2_PIN)&1)<<2)
		| (((SMD_DATA_PORT_PIN>>SMD_DATA3_PIN)&1)<<3)
		| (((SMD_DATA_PORT_PIN>>SMD_DATA4_PIN)&1)<<4)
		| (((SMD_DATA_PORT_PIN>>SMD_DATA5_PIN)&1)<<5);
#ifdef SMD_SECOND_ENABLED
	gamepad_data_low2 = ((SMD_DATA_PORT_PIN2>>SMD_DATA0_PIN2)&1) 
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA1_PIN2)&1)<<1) 
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA2_PIN2)&1)<<2)
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA3_PIN2)&1)<<3)
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA4_PIN2)&1)<<4)
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA5_PIN2)&1)<<5);
#endif
	SMD_SELECT_PORT_PORT |= 1<<SMD_SELECT_PIN; // Select - high
	_delay_us(50);
	gamepad_data_high = ((SMD_DATA_PORT_PIN>>SMD_DATA0_PIN)&1) 
		| (((SMD_DATA_PORT_PIN>>SMD_DATA1_PIN)&1)<<1) 
		| (((SMD_DATA_PORT_PIN>>SMD_DATA2_PIN)&1)<<2)
		| (((SMD_DATA_PORT_PIN>>SMD_DATA3_PIN)&1)<<3)
		| (((SMD_DATA_PORT_PIN>>SMD_DATA4_PIN)&1)<<4)
		| (((SMD_DATA_PORT_PIN>>SMD_DATA5_PIN)&1)<<5);
#ifdef SMD_SECOND_ENABLED
	gamepad_data_high2 = ((SMD_DATA_PORT_PIN2>>SMD_DATA0_PIN2)&1) 
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA1_PIN2)&1)<<1) 
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA2_PIN2)&1)<<2)
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA3_PIN2)&1)<<3)
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA4_PIN2)&1)<<4)
		| (((SMD_DATA_PORT_PIN2>>SMD_DATA5_PIN2)&1)<<5);
#endif
	return ((uint32_t)gamepad_data_high2<<24) | ((uint32_t)gamepad_data_low2<<16) | ((uint32_t)gamepad_data_high<<8) | gamepad_data_low;
}
#endif

#ifdef SMD_USE_DENDY9_PIN
void init_dendy_9pin(uint8_t n)
{ // 3, 4, 6
	if (!n)
	{
		SMD_DATA_PORT_DDR &= ~(1<<SMD_DATA1_PIN); // Data 1 aka data, input
		SMD_DATA_PORT_DDR |= 1<<SMD_DATA2_PIN; // Data 2 aka latch, output
		SMD_DATA_PORT_DDR |= 1<<SMD_DATA3_PIN; // Data 3 aka clock, output
		SMD_DATA_PORT_DDR |= 1<<SMD_DATA4_PIN; // Data 4 aka VCC, output
		SMD_DATA_PORT_PORT |= 1<<SMD_DATA1_PIN; // Data 1 aka data, pull-up
		SMD_DATA_PORT_PORT |= 1<<SMD_DATA2_PIN; // Data 2 aka latch, hi
		SMD_DATA_PORT_PORT |= 1<<SMD_DATA3_PIN; // Data 3 aka clock, hi
		SMD_DATA_PORT_PORT |= 1<<SMD_DATA4_PIN; // Data 4 aka VCC, hi
	} else {
#ifdef SMD_SECOND_ENABLED
		SMD_DATA_PORT_DDR2 &= ~(1<<SMD_DATA1_PIN2); // Data 1 aka data, input
		SMD_DATA_PORT_DDR2 |= 1<<SMD_DATA2_PIN2; // Data 2 aka latch, output
		SMD_DATA_PORT_DDR2 |= 1<<SMD_DATA3_PIN2; // Data 3 aka clock, output
		SMD_DATA_PORT_DDR2 |= 1<<SMD_DATA4_PIN2; // Data 4 aka VCC, output
		SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA1_PIN2; // Data 1 aka data, pull-up
		SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA2_PIN2; // Data 2 aka latch, hi
		SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA3_PIN2; // Data 3 aka clock, hi
		SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA4_PIN2; // Data 4 aka VCC, hi
#endif
	}
}

uint8_t get_dendy_9pin(uint8_t n)
{
	uint8_t gamepad_data = 0;
	if (!n)
	{
		SMD_DATA_PORT_PORT &= ~(1<<SMD_DATA2_PIN); // Data 2 aka latch, low
		_delay_us(10);
		int b;
		for (b = 0; b < 8; b++)
		{
			SMD_DATA_PORT_PORT &= ~(1<<SMD_DATA3_PIN); // Data 3 aka clock, low
			_delay_us(10);
			gamepad_data |= (((SMD_DATA_PORT_PIN>>SMD_DATA1_PIN)&1)<<b);
			SMD_DATA_PORT_PORT |= 1<<SMD_DATA3_PIN; // Data 3 aka clock, hi
			_delay_us(10);
		}		
		SMD_DATA_PORT_PORT |= 1<<SMD_DATA2_PIN; // Data 2 aka latch, hi
	} else {
#ifdef SMD_SECOND_ENABLED
		SMD_DATA_PORT_PORT2 &= ~(1<<SMD_DATA2_PIN2); // Data 2 aka latch, low
		_delay_us(10);
		int b;
		for (b = 0; b < 8; b++)
		{
			SMD_DATA_PORT_PORT2 &= ~(1<<SMD_DATA3_PIN2); // Data 3 aka clock, low
			_delay_us(10);
			gamepad_data |= (((SMD_DATA_PORT_PIN2>>SMD_DATA1_PIN2)&1)<<b);
			SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA3_PIN2; // Data 3 aka clock, hi
			_delay_us(10);
		}		
		SMD_DATA_PORT_PORT2 |= 1<<SMD_DATA2_PIN2; // Data 2 aka latch, hi
#endif
	}
	return gamepad_data;
}
#endif

uint32_t get_smd_gamepad_decoded(void)
{
	uint32_t result = 0;
	uint8_t smd_detected[2] = {0, 0};
	uint8_t b, c, d;	
	for (c = 0; c < 4; c++)
	{
		uint32_t smd_gamepad_data = get_smd_gamepad();
		for (d = 0; d < 2; d++) // for each controller
		{			
			if ((smd_gamepad_data & 0b00001111) || (c < 2)) // 3-button mode
			{
				for (b = 0; b <= 13; b++)
				{
					if (!((smd_gamepad_data>>b)&1))
					{
						switch (b)
						{
							case 0: // Up
								set_bit(result, 8 + d*16);
								break;
							case 1: // Down
								set_bit(result, 9 + d*16);
								break;
							case 2: // always low
							case 3:
								smd_detected[d] = 1;
								break;
							case 4: // A
								set_bit(result, 0 + d*16);
								break;
							case 5: // Start
								set_bit(result, 6 + d*16);
								break;
							case 10: // Left
								set_bit(result, 10 + d*16);
								break;
							case 11: // Right
								set_bit(result, 11 + d*16);
								break;
							case 12: // B
								set_bit(result, 1 + d*16);
								break;
							case 13: // C
								set_bit(result, 2 + d*16);
								break;
						}
					}
				}
			} else { // 6-button mode
				for (b = 4; b <= 11; b++)
				{
					if (!((smd_gamepad_data>>b)&1))
					{
						switch (b)
						{
							case 4: // A
								set_bit(result, 0 + d*16);
								break;
							case 5: // Start
								set_bit(result, 6 + d*16);
								break;
							case 8: // Z
								set_bit(result, 5 + d*16);
								break;
							case 9: // Y
								set_bit(result, 4 + d*16);
								break;
							case 10: // X
								set_bit(result, 3 + d*16);
								break;
							case 11: // Mode
								set_bit(result, 7 + d*16);
								break;
						}
					}
				}
			}
			smd_gamepad_data >>= 16;
		}
	}
	if (!smd_detected[0] || !smd_detected[1]) // SMD gamepad is not connected?
	{
#ifdef SMD_USE_DENDY9_PIN
		// so maybe it's 9-pin dendy gamepad?
		for (d = 0; d < 2; d++)
		{
			if (!smd_detected[d])
			{
				init_dendy_9pin(d);
				_delay_us(50);
				uint32_t dendy_data = ~get_dendy_9pin(d);
				result &= ~(0xFFFFUL << (d*16));
				result |= ((dendy_data & 0x0F) | ((dendy_data & 0xF0) << 4)) << (16*d);
			}
		}
		init_smd_gamepad(); // back to SMD mode
#endif
	}

	return result;
}

#ifdef DUALSHOCK_ENABLED
void init_dualshock_gamepad()
{
	DUALSHOCK_PORT_DDR |= (1<<DUALSHOCK_COMMAND_PIN); // Command pin - output
	DUALSHOCK_PORT_PORT |= (1<<DUALSHOCK_COMMAND_PIN); // Command pin - login high
	DUALSHOCK_PORT_DDR &= ~(1<<DUALSHOCK_DATA_PIN);   // Data pin - input
	DUALSHOCK_PORT_PORT |= (1<<DUALSHOCK_DATA_PIN);   // Data pin - pull-up
	DUALSHOCK_PORT_DDR |= (1<<DUALSHOCK_CLOCK_PIN);   // Clock - output
	DUALSHOCK_PORT_PORT |= (1<<DUALSHOCK_CLOCK_PIN);  // Clock - logic high	
	DUALSHOCK_ATTENTION_DDR |= (1<<DUALSHOCK_ATTENTION_PIN); // Attention - output
	DUALSHOCK_ATTENTION_PORT |= (1<<DUALSHOCK_ATTENTION_PIN); // Attention - logic high	
#ifdef DUALSHOCK_SECOND_ENABLED
	DUALSHOCK_ATTENTION_DDR |= (1<<DUALSHOCK_ATTENTION_PIN2); // Attention 2 - output
	DUALSHOCK_ATTENTION_PORT |= (1<<DUALSHOCK_ATTENTION_PIN2); // Attention 2 - logic high	
#endif
	/*
	DUALSHOCK_PORT_DDR &= ~(1<<DUALSHOCK_ACK_PIN);   // Ack pin - input
	DUALSHOCK_PORT_PORT |= (1<<DUALSHOCK_ACK_PIN);   // Ack pin - pull-up
	*/
}

int dualshock_command(uint8_t* command, uint8_t* data, int length, uint8_t controller_number)
{
	if (!controller_number)
		DUALSHOCK_ATTENTION_PORT &= ~(1<<DUALSHOCK_ATTENTION_PIN); // Attention!
	else
		DUALSHOCK_ATTENTION_PORT &= ~(1<<DUALSHOCK_ATTENTION_PIN2); // Attention!
	_delay_us(20);
	int b, bit;
	for (b = 0; b < length; b++) // Each byte...
	{
		data[b] = 0;
		for (bit = 0; bit < 8; bit++)
		{
			if ((command[b] >> bit) & 1) // 1?
				DUALSHOCK_PORT_PORT |= (1<<DUALSHOCK_COMMAND_PIN); // 1!
			else DUALSHOCK_PORT_PORT &= ~(1<<DUALSHOCK_COMMAND_PIN); // 0!			
			DUALSHOCK_PORT_PORT &= ~(1<<DUALSHOCK_CLOCK_PIN);  // Clock - logic low
			_delay_us(20);
			if ((DUALSHOCK_PORT_PIN >> DUALSHOCK_DATA_PIN) & 1) // Reading data... 1?
				data[b] |= (1<<bit); // 1!
			DUALSHOCK_PORT_PORT |= (1<<DUALSHOCK_CLOCK_PIN);  // Clock - logic high			
			_delay_us(20);
		}
		if (b == 1 && data[1] == 0xFF) // Alternative device detection
		{
			if (!controller_number)	
				DUALSHOCK_ATTENTION_PORT |= (1<<DUALSHOCK_ATTENTION_PIN);  // No attention...
			else
				DUALSHOCK_ATTENTION_PORT |= (1<<DUALSHOCK_ATTENTION_PIN2);  // No attention...
			return 0;
		}
		/*
		if (b<length-1) // Waiting for ACK
		{
			int t;
			for (t = 0; t < 50; t++)
			{			
				if (!((DUALSHOCK_PORT_PIN >> DUALSHOCK_ACK_PIN)&1)) // ACK reveived
				{
					ok = 1;
					break;
				}
				_delay_us(1);
			}
			if ((b < 2) && !ok) return 0; // No ACK in first two bytes? Aboooort! Saving time
		}
		*/
	}
	if (!controller_number)	
		DUALSHOCK_ATTENTION_PORT |= (1<<DUALSHOCK_ATTENTION_PIN);  // No attention...
	else
		DUALSHOCK_ATTENTION_PORT |= (1<<DUALSHOCK_ATTENTION_PIN2);  // No attention...
	_delay_us(20);
	return 1;
}

int get_dualshock_gamepad(uint8_t* data, int size, uint8_t motor_small, uint8_t motor_large, uint8_t controller_number) // pointer to uint8_t[21], number of bytes to request, vibration...
{
	static uint8_t dualshock_configered[2] = {0, 0};

	uint8_t command_query[21] = {0x01, 0x42, 0, motor_small, motor_large, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	if (!dualshock_command(command_query, data, size, controller_number))
	{
		dualshock_configered[controller_number] = 0;
		return 0;
	}
	if (!dualshock_configered[controller_number]) // Need to reconfigure dualshock
	{
		uint8_t command_config_mode[5] = {0x01, 0x43, 0x00, 0x01, 0x00};
		if (!dualshock_command(command_config_mode, data, sizeof(command_config_mode), controller_number)) return 0;
		uint8_t command_analog_mode[9] = {0x01, 0x44, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00};
		if (!dualshock_command(command_analog_mode, data, sizeof(command_analog_mode), controller_number)) return 0;
		uint8_t command_config_motors[9] = {0x01, 0x4D, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF};
		if (!dualshock_command(command_config_motors, data, sizeof(command_config_motors), controller_number)) return 0;
		uint8_t command_config_pressure[9] = {0x01, 0x4F, 0x00, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00};
		if (!dualshock_command(command_config_pressure, data, sizeof(command_config_pressure), controller_number)) return 0;
		uint8_t command_config_mode_exit[8] = {0x01, 0x43, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
		if (!dualshock_command(command_config_mode_exit, data, sizeof(command_config_mode_exit), controller_number)) return 0;
		dualshock_configered[controller_number] = 1;
		if (!dualshock_command(command_query, data, size, controller_number)) return 0;
	}
	return 1;
}
#endif