1
0
mirror of https://github.com/mcgurk/Arduino-USB-HID-RetroJoystickAdapter synced 2024-11-22 00:52:22 -05:00

Update temp.ino

This commit is contained in:
Jarno Lehtinen 2018-12-07 14:31:50 +02:00 committed by GitHub
parent 952046f071
commit 328a27126f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
// https://www.protovision.games/hardw/build4player.php?language=en // https://www.protovision.games/hardw/build4player.php?language=en
// http://cloud.cbm8bit.com/penfold42/joytester.zip // http://cloud.cbm8bit.com/penfold42/joytester.zip
// https://arduino.stackexchange.com/questions/8758/arduino-interruption-on-pin-change/8926 // https://arduino.stackexchange.com/questions/8758/arduino-interruption-on-pin-change/8926
// http://www.nongnu.org/avr-libc/user-manual/inline_asm.html
// http://ww1.microchip.com/downloads/en/devicedoc/atmel-7766-8-bit-avr-atmega16u4-32u4_datasheet.pdf // http://ww1.microchip.com/downloads/en/devicedoc/atmel-7766-8-bit-avr-atmega16u4-32u4_datasheet.pdf
// http://ww1.microchip.com/downloads/en/devicedoc/atmel-0856-avr-instruction-set-manual.pdf // http://ww1.microchip.com/downloads/en/devicedoc/atmel-0856-avr-instruction-set-manual.pdf
// http://www.pighixxx.net/wp-content/uploads/2016/07/pro_micro_pinout_v1_0_red.png // http://www.pighixxx.net/wp-content/uploads/2016/07/pro_micro_pinout_v1_0_red.png
@ -41,7 +42,7 @@
#define fire1C 4 // 8,PB4 = PB4 (H) #define fire1C 4 // 8,PB4 = PB4 (H)
#define fire2C 5 // 9,PB5 = PB5 (J) #define fire2C 5 // 9,PB5 = PB5 (J)
// LEDS: // LEDS (inverted):
// RX = D17,PB0 // RX = D17,PB0
// TX = -,PD5 // TX = -,PD5
@ -52,34 +53,36 @@ volatile uint8_t *ptr;
ISR(INT2_vect, ISR_NAKED) { // rising edge, output joystick 3 ISR(INT2_vect, ISR_NAKED) { // rising edge, output joystick 3
asm volatile( asm volatile(
" push r0 \n" // save register r0 //" push r0 \n" // save register r0
" lds r0, output1 \n" //" lds r0, output1 \n"
" out %[pin], r0 \n" " out %[pin], %[gpio] \n" // GPIOR0 address is 30, so we can use it directly with out-command (which works with 0-31)
" pop r0 \n" // restore previous r0 //" pop r0 \n" // restore previous r0
" rjmp INT2_vect_part_2 \n" // go to part 2 for required prologue and epilogue " rjmp INT2_vect_part_2 \n" // go to part 2 for required prologue and epilogue
:: [pin] "I" (_SFR_IO_ADDR(PORTB))); :: [pin] "I" (_SFR_IO_ADDR(PORTB)), [gpio] "I" (_SFR_IO_ADDR(GPIOR0)));
} }
ISR(INT2_vect_part_2) { mode = 1; last_joystick = 1; last_interrupt = TCNT1; } ISR(INT2_vect_part_2) { ptr = &GPIOR0; last_interrupt = TCNT1; }
ISR(INT3_vect, ISR_NAKED) { // falling edge, output joystick 4 ISR(INT3_vect, ISR_NAKED) { // falling edge, output joystick 4
asm volatile( asm volatile(
" push r0 \n" // save register r0 " push r0 \n" // save register r0
" lds r0, output2 \n" " lds r0, %[gpio] \n" // GPIOR1 address is 42 and out-command works only with 0-31
" out %[pin], r0 \n" " out %[pin], r0 \n" // so we need lds-command
" pop r0 \n" // restore previous r0 " pop r0 \n" // restore previous r0
" rjmp INT3_vect_part_2 \n" // go to part 2 for required prologue and epilogue " rjmp INT3_vect_part_2 \n" // go to part 2 for required prologue and epilogue
:: [pin] "I" (_SFR_IO_ADDR(PORTB))); :: [pin] "I" (_SFR_IO_ADDR(PORTB)), [gpio] "I" (_SFR_IO_ADDR(GPIOR1)));
} }
ISR(INT3_vect_part_2) { mode = 1; last_joystick = 2; last_interrupt = TCNT1; } ISR(INT3_vect_part_2) { ptr = &GPIOR1; last_interrupt = TCNT1; }
void setup() { void setup() {
ptr = &output1;
pinMode(5, INPUT_PULLUP); // pin5 (PC6) is input pinMode(5, INPUT_PULLUP); // pin5 (PC6) is input
pinMode(7, INPUT_PULLUP); // pin7 (PE6) is input pinMode(7, INPUT_PULLUP); // pin7 (PE6) is input
DDRB = 0xff; //all PB-ports are outputs DDRB = 0xff; //all PB-ports are outputs
DDRF = 0; PORTF = 0xff; // all PF-ports are inputs with pullups DDRF = 0; PORTF = 0xff; // all PF-ports are inputs with pullups
DDRD = B00100000; PORTD = B11110011; // all PD-ports are inputs (except PD5) with pullups (PD2,PD3 without pullup) DDRD = B00100000; PORTD = B11010011; // all PD-ports are inputs (except PD5) with pullups (PD2,PD3 without pullup), TX-LED on
TIMSK0 = 0; // disable timer0 interrupts (Arduino Uno/Pro Micro millis()/micros() update ISR) TIMSK0 = 0; // disable timer0 interrupts (Arduino Uno/Pro Micro millis()/micros() update ISR)
@ -101,7 +104,7 @@ volatile uint8_t joy1, joy2;
void loop() { void loop() {
uint8_t PF, PD, PC, PE; uint8_t PF, PD, PC, PE;
PF = PINF; PD = PIND; PC = PINC; PE = PINE; PF = PINF; PD = PIND; PC = PINC; PE = PINE;
joy1 = 0xff; joy2 = 0xff; // all signals are inverted (also LED) joy1 = 0xff; joy2 = 0xff; // all signals are inverted
if (up1) bitClear(joy1,upC); if (up1) bitClear(joy1,upC);
if (down1) bitClear(joy1,downC); if (down1) bitClear(joy1,downC);
if (left1) bitClear(joy1,leftC); if (left1) bitClear(joy1,leftC);
@ -112,24 +115,9 @@ void loop() {
if (right2) bitClear(joy2,rightC); if (right2) bitClear(joy2,rightC);
if (fire1) { bitClear(joy1,fire1C); bitClear(joy2,fire1C); } if (fire1) { bitClear(joy1,fire1C); bitClear(joy2,fire1C); }
if (fire2) { bitClear(joy1,fire2C); bitClear(joy2,fire2C); } if (fire2) { bitClear(joy1,fire2C); bitClear(joy2,fire2C); }
//mode = 1;
//if (mode) { bitClear(joy1,0); bitClear(joy2,0); } // RX LED is in PB0
if (mode) { PORTD &= ~_BV(5); } else { PORTD |= _BV(5); } // TX LED is in PD5
//bitClear(joy1,0); bitClear(joy2,0);
//joy1 = 0; joy2 = 0;
//PORTD |= _BV(5);
output1 = joy1; output2 = joy2;
if (mode == 1) { GPIOR0 = joy1; GPIOR1 = joy2;
uint16_t now = TCNT1; // TCNT1 is special 16-bit register, so it must be copied to variable before it can be used
if ((now - last_interrupt) > 15625) mode = 0; // if there is no select-signal in 1s, fallback to 3-joystick mode PORTB = *ptr; // can ISR occur while only one byte from 16-bit address is copied? should this be done interrupt off?
} else { //delayMicroseconds(10);
if (last_joystick == 1) PORTB = output1; // 3-joystick mode, update repeatedly and only joystick 3
if (last_joystick == 2) PORTB = output2; // 3-joystick mode, update repeatedly and only joystick 4
}
//PORTB = output1; // 3-joystick mode, update repeatedly and only joystick 3
//Serial.print(joy1, BIN); Serial.print(" "); Serial.println(joy2, BIN); delay(100);
//Serial.print(mode, BIN); Serial.print(" "); Serial.println(last_interrupt);
//delayMicroseconds(50);
delayMicroseconds(10);
} }