Skip to content

Commit bceab05

Browse files
committed
Refactored gamepad
1 parent 2397b87 commit bceab05

File tree

1 file changed

+67
-61
lines changed

1 file changed

+67
-61
lines changed

main/main.c

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,41 @@ void cls(void) {
5656
typedef struct {
5757
uint8_t report_id;
5858

59-
// Named buttons (bitfields decoded)
60-
bool a;
61-
bool b;
62-
bool x;
63-
bool y;
64-
65-
bool select;
66-
bool start;
67-
68-
bool l1;
69-
bool r1;
70-
bool l2;
71-
bool r2;
72-
bool l3;
73-
bool r3;
74-
75-
bool home;
76-
77-
bool l4;
78-
bool r4;
79-
80-
// D-Pad
81-
bool up;
82-
bool down;
83-
bool left;
84-
bool right;
85-
86-
uint8_t lx; // Left stick X
87-
uint8_t ly; // Left stick Y
88-
uint8_t rx; // Right stick X
89-
uint8_t ry; // Right stick Y
90-
uint8_t lt; // Left trigger
91-
uint8_t rt; // Right trigger
59+
union {
60+
struct {
61+
uint32_t a : 1;
62+
uint32_t b : 1;
63+
uint32_t x : 1;
64+
uint32_t y : 1;
65+
66+
uint32_t select : 1;
67+
uint32_t start : 1;
68+
69+
uint32_t l1 : 1;
70+
uint32_t r1 : 1;
71+
uint32_t l2 : 1;
72+
uint32_t r2 : 1;
73+
uint32_t l3 : 1;
74+
uint32_t r3 : 1;
75+
76+
uint32_t home : 1;
77+
78+
uint32_t l4 : 1;
79+
uint32_t r4 : 1;
80+
81+
uint32_t up : 1;
82+
uint32_t down : 1;
83+
uint32_t left : 1;
84+
uint32_t right : 1;
85+
86+
uint32_t _reserved : 13; // Up to 32 bits total
87+
};
88+
uint32_t val;
89+
} buttons;
90+
91+
uint8_t lx, ly;
92+
uint8_t rx, ry;
93+
uint8_t lt, rt;
9294
} gamepad_report_t;
9395

9496
typedef struct {
@@ -500,37 +502,39 @@ static void parse_gamepad_report(gamepad_report_t* rpt, const uint8_t* data, int
500502

501503
rpt->report_id = data[0];
502504

503-
uint8_t b1 = data[1];
504-
uint8_t b2 = data[2];
505-
uint8_t b3 = data[3];
506-
507-
rpt->up = (b1 == 0x00);
508-
rpt->right = (b1 == 0x02);
509-
rpt->down = (b1 == 0x04);
510-
rpt->left = (b1 == 0x06);
511-
512-
rpt->a = b3 & 0x40;
513-
rpt->b = b3 & 0x20;
514-
rpt->x = b3 & 0x10;
515-
rpt->y = b3 & 0x08;
505+
uint8_t hat = data[1];
506+
uint8_t b1 = data[2];
507+
uint8_t b2 = data[3];
516508

517-
rpt->l1 = b3 & (1 << 0);
518-
rpt->r1 = b2 & 0x80;
509+
rpt->buttons.val = 0;
519510

520-
rpt->l2 = b3 & (1 << 2);
521-
rpt->r2 = b3 & (1 << 1);
511+
rpt->buttons.up = (hat == 0x00 || hat == 0x01 || hat == 0x07);
512+
rpt->buttons.right = (hat == 0x01 || hat == 0x02 || hat == 0x03);
513+
rpt->buttons.down = (hat == 0x03 || hat == 0x04 || hat == 0x05);
514+
rpt->buttons.left = (hat == 0x05 || hat == 0x06 || hat == 0x07);
522515

523-
rpt->l3 = b2 & 0x04;
524-
rpt->r3 = b2 & 0x08;
525-
;
516+
// Face buttons
517+
rpt->buttons.a = (b2 >> 6) & 1;
518+
rpt->buttons.b = (b2 >> 5) & 1;
519+
rpt->buttons.x = (b2 >> 4) & 1;
520+
rpt->buttons.y = (b2 >> 3) & 1;
526521

527-
rpt->l4 = b2 & 0x02;
528-
rpt->r4 = b2 & 0x01;
522+
// Thumbsticks
523+
rpt->buttons.l1 = (b2 >> 0) & 1;
524+
rpt->buttons.r1 = (b1 >> 7) & 1;
529525

530-
rpt->select = b2 & 0x40;
531-
rpt->start = b2 & 0x20;
526+
// Shoulders and triggers
527+
rpt->buttons.l2 = (b2 >> 2) & 1;
528+
rpt->buttons.r2 = (b2 >> 1) & 1;
529+
rpt->buttons.l3 = (b1 >> 2) & 1;
530+
rpt->buttons.r3 = (b1 >> 3) & 1;
532531

533-
rpt->home = b2 & 0x10;
532+
// Extra buttons
533+
rpt->buttons.l4 = (b1 >> 1) & 1;
534+
rpt->buttons.r4 = (b1 >> 0) & 1;
535+
rpt->buttons.select = (b1 >> 6) & 1;
536+
rpt->buttons.start = (b1 >> 5) & 1;
537+
rpt->buttons.home = (b1 >> 4) & 1;
534538

535539
rpt->lx = data[4];
536540
rpt->ly = data[5];
@@ -549,9 +553,11 @@ static void print_gamepad_report(const gamepad_report_t* rpt, int length) {
549553

550554
const char* btn_labels[] = {"A", "B", "X", "Y", "L1", "R1", "L2", "R2", "L3", "R3",
551555
"L4", "R4", "Select", "Start", "Home", "Left", "Right", "Up", "Down"};
552-
const bool btn_states[] = {rpt->a, rpt->b, rpt->x, rpt->y, rpt->l1, rpt->r1, rpt->l2,
553-
rpt->r2, rpt->l3, rpt->r3, rpt->l4, rpt->r4, rpt->select, rpt->start,
554-
rpt->home, rpt->left, rpt->right, rpt->up, rpt->down};
556+
const bool btn_states[] = {rpt->buttons.a, rpt->buttons.b, rpt->buttons.x, rpt->buttons.y,
557+
rpt->buttons.l1, rpt->buttons.r1, rpt->buttons.l2, rpt->buttons.r2,
558+
rpt->buttons.l3, rpt->buttons.r3, rpt->buttons.l4, rpt->buttons.r4,
559+
rpt->buttons.select, rpt->buttons.start, rpt->buttons.home, rpt->buttons.left,
560+
rpt->buttons.right, rpt->buttons.up, rpt->buttons.down};
555561

556562
button_line[0] = '\0';
557563
strcat(button_line, "Buttons:");

0 commit comments

Comments
 (0)