1
0
mirror of https://github.com/pothos/arduino-n64-controller-library synced 2024-11-24 09:42:21 -05:00

Directly return button states from N64_raw_dump

This commit is contained in:
Daniel Schaal 2017-05-01 11:22:33 +02:00
parent 2340e8057a
commit b603217f47
2 changed files with 59 additions and 131 deletions

View File

@ -14,31 +14,10 @@
#define N64_PINB_LOW DDRB |= pincode
#define N64_PINB_QUERY (PINB & pincode)
void N64Controller::set_up() {
n64_PIN = 2; // might also be set/changed by constructor or begin() afterwards
n64_key_Dup = false;
n64_key_Ddown = false;
n64_key_Dleft = false;
n64_key_Dright = false;
n64_key_Start = false;
n64_key_Z = false;
n64_key_A = false;
n64_key_B = false;
n64_key_Cup = false;
n64_key_Cdown = false;
n64_key_Cleft = false;
n64_key_Cright = false;
n64_key_L = false;
n64_key_R = false;
n64_key_X = 0;
n64_key_Y = 0;
N64Controller::N64Controller() {
}
N64Controller::N64Controller() {
set_up();
}
N64Controller::N64Controller(int serialPin) {
set_up();
n64_PIN = serialPin;
}
void N64Controller::begin(int serialPin) {
@ -89,7 +68,6 @@ void N64Controller::begin() {
} else {
N64_init_PINB(n64_pincode);
}
translate_raw_data();
}
void N64Controller::N64_init_PIND(char pincode) {
@ -405,89 +383,44 @@ void N64Controller::print_N64_status()
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
Serial.println();
Serial.print("Start: ");
Serial.println(N64_status.data1 & 16 ? 1:0);
Serial.println(Start());
Serial.print("Z: ");
Serial.println(N64_status.data1 & 32 ? 1:0);
Serial.println(Z());
Serial.print("B: ");
Serial.println(N64_status.data1 & 64 ? 1:0);
Serial.println(B());
Serial.print("A: ");
Serial.println(N64_status.data1 & 128 ? 1:0);
Serial.println(A());
Serial.print("L: ");
Serial.println(N64_status.data2 & 32 ? 1:0);
Serial.println(L());
Serial.print("R: ");
Serial.println(N64_status.data2 & 16 ? 1:0);
Serial.println(R());
Serial.print("Cup: ");
Serial.println(N64_status.data2 & 0x08 ? 1:0);
Serial.println(C_up());
Serial.print("Cdown: ");
Serial.println(N64_status.data2 & 0x04 ? 1:0);
Serial.println(C_down());
Serial.print("Cright:");
Serial.println(N64_status.data2 & 0x01 ? 1:0);
Serial.println(C_right());
Serial.print("Cleft: ");
Serial.println(N64_status.data2 & 0x02 ? 1:0);
Serial.println(C_left());
Serial.print("Dup: ");
Serial.println(N64_status.data1 & 0x08 ? 1:0);
Serial.println(D_up());
Serial.print("Ddown: ");
Serial.println(N64_status.data1 & 0x04 ? 1:0);
Serial.println(D_down());
Serial.print("Dright:");
Serial.println(N64_status.data1 & 0x01 ? 1:0);
Serial.println(D_right());
Serial.print("Dleft: ");
Serial.println(N64_status.data1 & 0x02 ? 1:0);
Serial.println(D_left());
Serial.print("Stick X:");
Serial.println(N64_status.stick_x, DEC);
Serial.println(axis_x(), DEC);
Serial.print("Stick Y:");
Serial.println(N64_status.stick_y, DEC);
}
void N64Controller::translate_raw_data()
{
// The get_N64_status function sloppily dumps its data 1 bit per byte
// into the get_status_extended char array. It's our job to go through
// that and put each piece neatly into the struct N64_status
int i;
memset(&N64_status, 0, sizeof(N64_status));
// line 1
// bits: A, B, Z, Start, Dup, Ddown, Dleft, Dright
for (i=0; i<8; i++) {
N64_status.data1 |= N64_raw_dump[i] ? (0x80 >> i) : 0;
}
// line 2
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
for (i=0; i<8; i++) {
N64_status.data2 |= N64_raw_dump[8+i] ? (0x80 >> i) : 0;
}
// line 3
// bits: joystick x value
// These are 8 bit values centered at 0x80 (128)
for (i=0; i<8; i++) {
N64_status.stick_x |= N64_raw_dump[16+i] ? (0x80 >> i) : 0;
}
for (i=0; i<8; i++) {
N64_status.stick_y |= N64_raw_dump[24+i] ? (0x80 >> i) : 0;
}
n64_key_A = (bool)N64_raw_dump[0];
n64_key_B = (bool)N64_raw_dump[1];
n64_key_Z = (bool)N64_raw_dump[2];
n64_key_Start = (bool)N64_raw_dump[3];
n64_key_Dup = (bool)N64_raw_dump[4];
n64_key_Ddown = (bool)N64_raw_dump[5];
n64_key_Dleft = (bool)N64_raw_dump[6];
n64_key_Dright = (bool)N64_raw_dump[7];
n64_key_L = (bool)N64_raw_dump[10];
n64_key_R = (bool)N64_raw_dump[11];
n64_key_Cup = (bool)N64_raw_dump[12];
n64_key_Cdown = (bool)N64_raw_dump[13];
n64_key_Cleft = (bool)N64_raw_dump[14];
n64_key_Cright = (bool)N64_raw_dump[15];
n64_key_X = (int) N64_status.stick_x;
n64_key_Y = (int) N64_status.stick_y;
Serial.println(axis_y(), DEC);
}
void N64Controller::update() {
@ -503,5 +436,4 @@ void N64Controller::update() {
N64_PINB_get(n64_pincode);
interrupts();
}
translate_raw_data();
}

View File

@ -22,6 +22,22 @@
#ifndef N64Controller_h
#define N64Controller_h
#define A_IDX 0
#define B_IDX 1
#define Z_IDX 2
#define START_IDX 3
#define D_UP_IDX 4
#define D_DOWN_IDX 5
#define D_LEFT_IDX 6
#define D_RIGHT_IDX 7
#define L_IDX 10
#define R_IDX 11
#define C_UP_IDX 12
#define C_DOWN_IDX 13
#define C_LEFT_IDX 14
#define C_RIGHT_IDX 15
#define X_IDX 16
#define Y_IDX 24
class N64Controller {
public:
@ -32,22 +48,22 @@ class N64Controller {
void update(); // then update always and get button info
// consider to have a delay instead of
// calling update all the time in a loop
inline bool button_D_up() { return n64_key_Dup; };
inline bool button_D_down() { return n64_key_Ddown; };
inline bool button_D_left() { return n64_key_Dleft; };
inline bool button_D_right() { return n64_key_Dright; };
inline bool button_Start() { return n64_key_Start; };
inline bool button_A() { return n64_key_A; };
inline bool button_B() { return n64_key_B; };
inline bool button_Z() { return n64_key_Z; };
inline bool button_L() { return n64_key_L; };
inline bool button_R() { return n64_key_R; };
inline bool button_C_up() { return n64_key_Cup; };
inline bool button_C_down() { return n64_key_Cdown; };
inline bool button_C_left() { return n64_key_Cleft; };
inline bool button_C_right() { return n64_key_Cright; };
inline int axis_x() { return n64_key_X; };
inline int axis_y() { return n64_key_Y; };
inline bool D_up() { return (N64_raw_dump[D_UP_IDX]) > 0; };
inline bool D_down() { return (N64_raw_dump[D_DOWN_IDX]) > 0; };
inline bool D_left() { return (N64_raw_dump[D_LEFT_IDX]) > 0; };
inline bool D_right() { return (N64_raw_dump[D_RIGHT_IDX]) > 0; };
inline bool Start() { return (N64_raw_dump[START_IDX]) > 0; };
inline bool A() { return (N64_raw_dump[A_IDX]) > 0; };
inline bool B() { return (N64_raw_dump[B_IDX]) > 0; };
inline bool Z() { return (N64_raw_dump[Z_IDX]) > 0; };
inline bool L() { return (N64_raw_dump[L_IDX]) > 0; };
inline bool R() { return (N64_raw_dump[R_IDX]) > 0; };
inline bool C_up() { return (N64_raw_dump[C_UP_IDX]) > 0; };
inline bool C_down() { return (N64_raw_dump[C_DOWN_IDX]) > 0; };
inline bool C_left() { return (N64_raw_dump[C_LEFT_IDX]) > 0; };
inline bool C_right() { return (N64_raw_dump[C_RIGHT_IDX]) > 0; };
inline char axis_x() { return axis(X_IDX); };
inline char axis_y() { return axis(Y_IDX); };
void print_N64_status();
private:
@ -55,22 +71,6 @@ class N64Controller {
int n64_PIN; // might also be set by constructor or begin()
char n64_pincode;
bool n64_first_register; // PIN0-7: DDRD PIN8-13: DDRB
bool n64_key_Dup;
bool n64_key_Ddown;
bool n64_key_Dleft;
bool n64_key_Dright;
bool n64_key_Start;
bool n64_key_Z;
bool n64_key_A;
bool n64_key_B;
bool n64_key_Cup;
bool n64_key_Cdown;
bool n64_key_Cleft;
bool n64_key_Cright;
bool n64_key_L;
bool n64_key_R;
int n64_key_X;
int n64_key_Y;
void N64_init_PIND(char pincode);
void N64_PIND_send(char pincode, unsigned char *buffer, char length);
@ -80,17 +80,13 @@ class N64Controller {
void N64_PINB_send(char pincode, unsigned char *buffer, char length);
void N64_PINB_get(char pincode);
void translate_raw_data();
// 8 bytes of data that we get from the controller
struct {
// bits: 0, 0, 0, start, y, x, b, a
unsigned char data1;
// bits: 1, L, R, Z, Dup, Ddown, Dright, Dleft
unsigned char data2;
char stick_x;
char stick_y;
} N64_status;
inline char axis(int index) {
char value = 0;
for (char i=0; i<8; i++) {
value |= N64_raw_dump[index+i] ? (0x80 >> i) : 0;
}
return value;
}
char N64_raw_dump[33]; // 1 received bit per byte
};