From c6e7d03e816434eeb74b0e66f409ac7385252d99 Mon Sep 17 00:00:00 2001 From: Alexey 'Cluster' Avdyukhin Date: Wed, 29 Jan 2014 07:52:31 +0400 Subject: Gamepad functions moved to gamepad.c --- Makefile | 2 +- defines.h | 7 +++ gamepad.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gamepad.h | 54 ++++++++++++++++ main.c | 118 +--------------------------------- nes2wii.h | 36 ++--------- 6 files changed, 281 insertions(+), 148 deletions(-) create mode 100644 gamepad.c create mode 100644 gamepad.h diff --git a/Makefile b/Makefile index d1142d8..0529140 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PRG = nes2wii -OBJ = main.o wiimote.o +OBJ = main.o wiimote.o gamepad.o LFUSE = CF HFUSE = C9 MCU_PROGRAMMER = m16 diff --git a/defines.h b/defines.h index bbd2069..324d9b9 100644 --- a/defines.h +++ b/defines.h @@ -34,6 +34,13 @@ #define SMD_DATA4_PIN 6 #define SMD_DATA5_PIN 7 +#define DUALSHOCK_ENABLED +#define DUALSHOCK_PORT D +#define DUALSHOCK_DATA_PIN 2 +#define DUALSHOCK_COMMAND_PIN 3 +#define DUALSHOCK_ATTENTION_PIN 5 +#define DUALSHOCK_CLOCK_PIN 5 + #define RED_LED_PORT B #define RED_LED_PIN 4 #define GREEN_LED_PORT B diff --git a/gamepad.c b/gamepad.c new file mode 100644 index 0000000..dd6967a --- /dev/null +++ b/gamepad.c @@ -0,0 +1,212 @@ +#include "defines.h" +#include +#include +#include "gamepad.h" + +void init_nes_gamepad() +{ + NES_PORT_DDR |= 1<>NES_DATA_PIN)&1)<>SNES_DATA_PIN)&1)<= 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; +} + +void init_smd_gamepad() +{ + SMD_SELECT_PORT_DDR |= 1<>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); + SMD_SELECT_PORT_PORT |= 1<>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); + return ((uint16_t)gamepad_data_high<<8) | gamepad_data_low; +} + +void init_dualshock_gamepad() +{ + DUALSHOCK_PORT_DDR |= (1<> bit) & 1) // 1? + DUALSHOCK_PORT_PORT |= (1<> DUALSHOCK_DATA_PIN) & 1) // Reading data... 1? + data[b] |= (1<> 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 + } + */ + } + DUALSHOCK_PORT_PORT |= (1< +#include "defines.h" + +#define GLUE(a,b) a##b +#define DDR(p) GLUE(DDR,p) +#define PORT(p) GLUE(PORT,p) +#define PIN(p) GLUE(PIN,p) + +#define N64_PORT_PORT PORT(N64_PORT) +#define N64_PORT_DDR DDR(N64_PORT) +#define N64_PORT_PIN PIN(N64_PORT) + +#define NES_PORT_PORT PORT(NES_PORT) +#define NES_PORT_DDR DDR(NES_PORT) +#define NES_PORT_PIN PIN(NES_PORT) + +#define SNES_PORT_PORT PORT(NES_PORT) +#define SNES_PORT_DDR DDR(NES_PORT) +#define SNES_PORT_PIN PIN(NES_PORT) + +#define SMD_SELECT_PORT_PORT PORT(SMD_SELECT_PORT) +#define SMD_SELECT_PORT_DDR DDR(SMD_SELECT_PORT) +#define SMD_DATA_PORT_PORT PORT(SMD_DATA_PORT) +#define SMD_DATA_PORT_DDR DDR(SMD_DATA_PORT) +#define SMD_DATA_PORT_PIN PIN(SMD_DATA_PORT) + +#define DUALSHOCK_PORT_PORT PORT(DUALSHOCK_PORT) +#define DUALSHOCK_PORT_DDR DDR(DUALSHOCK_PORT) +#define DUALSHOCK_PORT_PIN PIN(DUALSHOCK_PORT) + +#define WAIT(t) {TCNT0=0; while(TCNT0 < (F_CPU / 1000000UL) * t);} + +#define N64SEND(t) {N64_PORT_DDR |= (1<>N64_DATA_PIN)&1)) + +void init_nes_gamepad(); +uint8_t get_nes_gamepad(); +void init_snes_gamepad(); +uint16_t get_snes_gamepad(); +void init_n64_gamepad(); +int get_n64_gamepad(uint8_t* data); +void init_smd_gamepad(); +uint16_t get_smd_gamepad(); +void init_dualshock_gamepad(); +int dualshock_command(uint8_t* command, uint8_t* data, int length); +int get_dualshock_gamepad(uint8_t* data, int size, uint8_t motor_small, uint8_t motor_large); + +#endif \ No newline at end of file diff --git a/main.c b/main.c index cd446e8..2ce1a48 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,9 @@ #include "defines.h" #include #include +#include #include "wiimote.h" +#include "gamepad.h" // classic controller id const unsigned char classic_controller_id[6] = {0x00, 0x00, 0xA4, 0x20, 0x01, 0x01}; @@ -21,121 +23,6 @@ const unsigned char cal_data[32] = { volatile int red_led_timer = 0; -void init_nes_gamepad() -{ - NES_PORT_DDR |= 1<>NES_DATA_PIN)&1)<>SNES_DATA_PIN)&1)<= 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; -} - -void init_smd_gamepad() -{ - SMD_SELECT_PORT_DDR |= 1<>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); - SMD_SELECT_PORT_PORT |= 1<>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); - return ((uint16_t)gamepad_data_high<<8) | gamepad_data_low; -} void wiimote_query() { @@ -148,7 +35,6 @@ int main() RED_LED_PORT_DDR |= (1<>N64_DATA_PIN)&1)) - #define PRESS_A but_dat[5] &= 0b11101111; // BZL BB BY BA BX BZR BDL BDU #define PRESS_B but_dat[5] &= 0b10111111; // BZL BB BY BA BX BZR BDL BDU #define PRESS_X but_dat[5] &= 0b11110111; // BZL BB BY BA BX BZR BDL BDU @@ -59,6 +28,11 @@ #define PRESS_LEFT but_dat[5] &= 0b11111101; // BZL BB BY BA BX BZR BDL BDU #define PRESS_RIGHT but_dat[4] &= 0b01111111; // BDR BDD BLT B- BH B+ BRT 1 +#define RED_LED_PORT_PORT PORT(RED_LED_PORT) +#define RED_LED_PORT_DDR DDR(RED_LED_PORT) +#define GREEN_LED_PORT_PORT PORT(GREEN_LED_PORT) +#define GREEN_LED_PORT_DDR DDR(GREEN_LED_PORT) + #define RED_ON RED_LED_PORT_PORT |= (1<