-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.c
More file actions
68 lines (64 loc) · 1.77 KB
/
controller.c
File metadata and controls
68 lines (64 loc) · 1.77 KB
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
#include "controller.h"
#include "cpu.h"
#include "ppu.h"
struct controller player1 = {
.buttons = 0,
.isPolling = false,
.bitCounter = 0,
};
struct controller player2 = {
.buttons = 0,
.isPolling = false,
.bitCounter = 0,
};
void poll_controllers(uint8_t *data) {
if (((*data) & 0x01) > 0) { // Only care about strobe bit
player1.bitCounter = 0;
player2.bitCounter = 0;
player1.strobeBit = true;
player2.strobeBit = true;
//printf("Strobe bit on\n");
} else {
player1.strobeBit = false;
player2.strobeBit = false;
//printf("Strobe bit off\n");
}
if (player1.isPolling) {
player1.isPolling = false;
player2.isPolling = false;
return;
}
// Somehow poll controllers through usb keyboard
// controller_byte = whatever
//if (player1.buttons > 0) {
// CLEAR_CONTROLLER(player1.buttons);
//} else {
//if (ppu.framecount < 30) {
SET_START_BUTTON(player1.buttons);
//} else {
// CLEAR_START_BUTTON(player1.buttons);
// SET_RIGHT_BUTTON(player1.buttons);
//}
if (ppu.framecount < 300){
SET_A_BUTTON(player1.buttons);
SET_RIGHT_BUTTON(player1.buttons);
}
// else{
// CLEAR_A_BUTTON(player1.buttons);
// }
// SET_A_BUTTON(player1.buttons);
//
//}
//printf("Polling controllers %X\n", player1.buttons);
player1.isPolling = true;
player2.isPolling = true;
}
uint8_t readController(struct controller *player) {
if (player->strobeBit)
return player->buttons & 0x01;
if (player->bitCounter >= 8)
return 1;
uint8_t returned = (((player->buttons & (1 << player->bitCounter))) > 0) ? 1 : 0;
player->bitCounter++;
return returned;
}