Skip to content

Commit b603217

Browse files
committed
Directly return button states from N64_raw_dump
1 parent 2340e80 commit b603217

File tree

2 files changed

+59
-131
lines changed

2 files changed

+59
-131
lines changed

N64Controller/N64Controller.cpp

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,10 @@
1414
#define N64_PINB_LOW DDRB |= pincode
1515
#define N64_PINB_QUERY (PINB & pincode)
1616

17-
void N64Controller::set_up() {
18-
n64_PIN = 2; // might also be set/changed by constructor or begin() afterwards
19-
n64_key_Dup = false;
20-
n64_key_Ddown = false;
21-
n64_key_Dleft = false;
22-
n64_key_Dright = false;
23-
n64_key_Start = false;
24-
n64_key_Z = false;
25-
n64_key_A = false;
26-
n64_key_B = false;
27-
n64_key_Cup = false;
28-
n64_key_Cdown = false;
29-
n64_key_Cleft = false;
30-
n64_key_Cright = false;
31-
n64_key_L = false;
32-
n64_key_R = false;
33-
n64_key_X = 0;
34-
n64_key_Y = 0;
35-
}
36-
3717
N64Controller::N64Controller() {
38-
set_up();
3918
}
19+
4020
N64Controller::N64Controller(int serialPin) {
41-
set_up();
4221
n64_PIN = serialPin;
4322
}
4423
void N64Controller::begin(int serialPin) {
@@ -89,7 +68,6 @@ void N64Controller::begin() {
8968
} else {
9069
N64_init_PINB(n64_pincode);
9170
}
92-
translate_raw_data();
9371
}
9472

9573
void N64Controller::N64_init_PIND(char pincode) {
@@ -405,89 +383,44 @@ void N64Controller::print_N64_status()
405383
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
406384
Serial.println();
407385
Serial.print("Start: ");
408-
Serial.println(N64_status.data1 & 16 ? 1:0);
386+
Serial.println(Start());
409387

410388
Serial.print("Z: ");
411-
Serial.println(N64_status.data1 & 32 ? 1:0);
389+
Serial.println(Z());
412390

413391
Serial.print("B: ");
414-
Serial.println(N64_status.data1 & 64 ? 1:0);
392+
Serial.println(B());
415393

416394
Serial.print("A: ");
417-
Serial.println(N64_status.data1 & 128 ? 1:0);
395+
Serial.println(A());
418396

419397
Serial.print("L: ");
420-
Serial.println(N64_status.data2 & 32 ? 1:0);
398+
Serial.println(L());
421399
Serial.print("R: ");
422-
Serial.println(N64_status.data2 & 16 ? 1:0);
400+
Serial.println(R());
423401

424402
Serial.print("Cup: ");
425-
Serial.println(N64_status.data2 & 0x08 ? 1:0);
403+
Serial.println(C_up());
426404
Serial.print("Cdown: ");
427-
Serial.println(N64_status.data2 & 0x04 ? 1:0);
405+
Serial.println(C_down());
428406
Serial.print("Cright:");
429-
Serial.println(N64_status.data2 & 0x01 ? 1:0);
407+
Serial.println(C_right());
430408
Serial.print("Cleft: ");
431-
Serial.println(N64_status.data2 & 0x02 ? 1:0);
409+
Serial.println(C_left());
432410

433411
Serial.print("Dup: ");
434-
Serial.println(N64_status.data1 & 0x08 ? 1:0);
412+
Serial.println(D_up());
435413
Serial.print("Ddown: ");
436-
Serial.println(N64_status.data1 & 0x04 ? 1:0);
414+
Serial.println(D_down());
437415
Serial.print("Dright:");
438-
Serial.println(N64_status.data1 & 0x01 ? 1:0);
416+
Serial.println(D_right());
439417
Serial.print("Dleft: ");
440-
Serial.println(N64_status.data1 & 0x02 ? 1:0);
418+
Serial.println(D_left());
441419

442420
Serial.print("Stick X:");
443-
Serial.println(N64_status.stick_x, DEC);
421+
Serial.println(axis_x(), DEC);
444422
Serial.print("Stick Y:");
445-
Serial.println(N64_status.stick_y, DEC);
446-
}
447-
448-
void N64Controller::translate_raw_data()
449-
{
450-
// The get_N64_status function sloppily dumps its data 1 bit per byte
451-
// into the get_status_extended char array. It's our job to go through
452-
// that and put each piece neatly into the struct N64_status
453-
int i;
454-
memset(&N64_status, 0, sizeof(N64_status));
455-
// line 1
456-
// bits: A, B, Z, Start, Dup, Ddown, Dleft, Dright
457-
for (i=0; i<8; i++) {
458-
N64_status.data1 |= N64_raw_dump[i] ? (0x80 >> i) : 0;
459-
}
460-
// line 2
461-
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
462-
for (i=0; i<8; i++) {
463-
N64_status.data2 |= N64_raw_dump[8+i] ? (0x80 >> i) : 0;
464-
}
465-
// line 3
466-
// bits: joystick x value
467-
// These are 8 bit values centered at 0x80 (128)
468-
for (i=0; i<8; i++) {
469-
N64_status.stick_x |= N64_raw_dump[16+i] ? (0x80 >> i) : 0;
470-
}
471-
for (i=0; i<8; i++) {
472-
N64_status.stick_y |= N64_raw_dump[24+i] ? (0x80 >> i) : 0;
473-
}
474-
475-
n64_key_A = (bool)N64_raw_dump[0];
476-
n64_key_B = (bool)N64_raw_dump[1];
477-
n64_key_Z = (bool)N64_raw_dump[2];
478-
n64_key_Start = (bool)N64_raw_dump[3];
479-
n64_key_Dup = (bool)N64_raw_dump[4];
480-
n64_key_Ddown = (bool)N64_raw_dump[5];
481-
n64_key_Dleft = (bool)N64_raw_dump[6];
482-
n64_key_Dright = (bool)N64_raw_dump[7];
483-
n64_key_L = (bool)N64_raw_dump[10];
484-
n64_key_R = (bool)N64_raw_dump[11];
485-
n64_key_Cup = (bool)N64_raw_dump[12];
486-
n64_key_Cdown = (bool)N64_raw_dump[13];
487-
n64_key_Cleft = (bool)N64_raw_dump[14];
488-
n64_key_Cright = (bool)N64_raw_dump[15];
489-
n64_key_X = (int) N64_status.stick_x;
490-
n64_key_Y = (int) N64_status.stick_y;
423+
Serial.println(axis_y(), DEC);
491424
}
492425

493426
void N64Controller::update() {
@@ -503,5 +436,4 @@ void N64Controller::update() {
503436
N64_PINB_get(n64_pincode);
504437
interrupts();
505438
}
506-
translate_raw_data();
507439
}

N64Controller/N64Controller.h

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@
2222
#ifndef N64Controller_h
2323
#define N64Controller_h
2424

25+
#define A_IDX 0
26+
#define B_IDX 1
27+
#define Z_IDX 2
28+
#define START_IDX 3
29+
#define D_UP_IDX 4
30+
#define D_DOWN_IDX 5
31+
#define D_LEFT_IDX 6
32+
#define D_RIGHT_IDX 7
33+
#define L_IDX 10
34+
#define R_IDX 11
35+
#define C_UP_IDX 12
36+
#define C_DOWN_IDX 13
37+
#define C_LEFT_IDX 14
38+
#define C_RIGHT_IDX 15
39+
#define X_IDX 16
40+
#define Y_IDX 24
2541

2642
class N64Controller {
2743
public:
@@ -32,66 +48,46 @@ class N64Controller {
3248
void update(); // then update always and get button info
3349
// consider to have a delay instead of
3450
// calling update all the time in a loop
35-
inline bool button_D_up() { return n64_key_Dup; };
36-
inline bool button_D_down() { return n64_key_Ddown; };
37-
inline bool button_D_left() { return n64_key_Dleft; };
38-
inline bool button_D_right() { return n64_key_Dright; };
39-
inline bool button_Start() { return n64_key_Start; };
40-
inline bool button_A() { return n64_key_A; };
41-
inline bool button_B() { return n64_key_B; };
42-
inline bool button_Z() { return n64_key_Z; };
43-
inline bool button_L() { return n64_key_L; };
44-
inline bool button_R() { return n64_key_R; };
45-
inline bool button_C_up() { return n64_key_Cup; };
46-
inline bool button_C_down() { return n64_key_Cdown; };
47-
inline bool button_C_left() { return n64_key_Cleft; };
48-
inline bool button_C_right() { return n64_key_Cright; };
49-
inline int axis_x() { return n64_key_X; };
50-
inline int axis_y() { return n64_key_Y; };
51+
inline bool D_up() { return (N64_raw_dump[D_UP_IDX]) > 0; };
52+
inline bool D_down() { return (N64_raw_dump[D_DOWN_IDX]) > 0; };
53+
inline bool D_left() { return (N64_raw_dump[D_LEFT_IDX]) > 0; };
54+
inline bool D_right() { return (N64_raw_dump[D_RIGHT_IDX]) > 0; };
55+
inline bool Start() { return (N64_raw_dump[START_IDX]) > 0; };
56+
inline bool A() { return (N64_raw_dump[A_IDX]) > 0; };
57+
inline bool B() { return (N64_raw_dump[B_IDX]) > 0; };
58+
inline bool Z() { return (N64_raw_dump[Z_IDX]) > 0; };
59+
inline bool L() { return (N64_raw_dump[L_IDX]) > 0; };
60+
inline bool R() { return (N64_raw_dump[R_IDX]) > 0; };
61+
inline bool C_up() { return (N64_raw_dump[C_UP_IDX]) > 0; };
62+
inline bool C_down() { return (N64_raw_dump[C_DOWN_IDX]) > 0; };
63+
inline bool C_left() { return (N64_raw_dump[C_LEFT_IDX]) > 0; };
64+
inline bool C_right() { return (N64_raw_dump[C_RIGHT_IDX]) > 0; };
65+
inline char axis_x() { return axis(X_IDX); };
66+
inline char axis_y() { return axis(Y_IDX); };
5167

5268
void print_N64_status();
5369
private:
5470
void set_up();
5571
int n64_PIN; // might also be set by constructor or begin()
5672
char n64_pincode;
5773
bool n64_first_register; // PIN0-7: DDRD PIN8-13: DDRB
58-
bool n64_key_Dup;
59-
bool n64_key_Ddown;
60-
bool n64_key_Dleft;
61-
bool n64_key_Dright;
62-
bool n64_key_Start;
63-
bool n64_key_Z;
64-
bool n64_key_A;
65-
bool n64_key_B;
66-
bool n64_key_Cup;
67-
bool n64_key_Cdown;
68-
bool n64_key_Cleft;
69-
bool n64_key_Cright;
70-
bool n64_key_L;
71-
bool n64_key_R;
72-
int n64_key_X;
73-
int n64_key_Y;
74-
74+
7575
void N64_init_PIND(char pincode);
7676
void N64_PIND_send(char pincode, unsigned char *buffer, char length);
7777
void N64_PIND_get(char pincode);
7878

7979
void N64_init_PINB(char pincode);
8080
void N64_PINB_send(char pincode, unsigned char *buffer, char length);
8181
void N64_PINB_get(char pincode);
82-
83-
void translate_raw_data();
84-
85-
// 8 bytes of data that we get from the controller
86-
struct {
87-
// bits: 0, 0, 0, start, y, x, b, a
88-
unsigned char data1;
89-
// bits: 1, L, R, Z, Dup, Ddown, Dright, Dleft
90-
unsigned char data2;
91-
char stick_x;
92-
char stick_y;
93-
} N64_status;
94-
82+
83+
inline char axis(int index) {
84+
char value = 0;
85+
for (char i=0; i<8; i++) {
86+
value |= N64_raw_dump[index+i] ? (0x80 >> i) : 0;
87+
}
88+
return value;
89+
}
90+
9591
char N64_raw_dump[33]; // 1 received bit per byte
9692
};
9793

0 commit comments

Comments
 (0)