Skip to content

Commit

Permalink
Reverted wobbles' code being pushed to the main branch
Browse files Browse the repository at this point in the history
Signed-off-by: Garrett Greenwood <garrettagreenwood@gmail.com>
  • Loading branch information
Serisium committed Jun 7, 2016
1 parent 3c08590 commit ff574cf
Showing 1 changed file with 57 additions and 42 deletions.
99 changes: 57 additions & 42 deletions firmware/main.c
Expand Up @@ -5,12 +5,12 @@

#include "controller.h"
#include "libs/Neopixel.h"
#include "animation.h"
#include "usb.h"
#include "usbdrv/usbdrv.h"

#define DEBUG_MATCH 0 // Enable PIN_TIMER toggle on timer match
#define GCN_RETRY_LIMIT 5 // Number of times to retry the GCN signal line

#define PIN_LED PA1
#define PIN_DEBUG PA2
#define PIN_GC PA6 // Needs to be connected to (DI)
//#define PIN_TIMER PA7 // Needs to be connected to (OC0B). Displays compare match toggles if DEBUG_MATCH is set
Expand All @@ -24,10 +24,9 @@
#define SEND_ONE() do { CLEAR_BIT(PORTA, PIN_GC); _delay_us(1); SET_BIT(PORTA, PIN_GC); _delay_us(3); } while(0)

void setup_pins(void) {
SET_BIT(DDRA, PIN_GC); // Set PIN_GC as input, GCN data signal
CLEAR_BIT(DDRA, PIN_GC); // Set PIN_GC as input, GCN data signal
SET_BIT(PORTA, PIN_GC); // Enable pull-up resistor on PIN_GC
SET_BIT(DDRA, PIN_DEBUG); // Set PIN_DEBUG as output for debugging
SET_BIT(DDRA, PIN_LED); // Set the LED pin as output

// Ensure that USB pins are inputs with pullup resistor disabled
CLEAR_BIT(DDRB, PB2);
Expand Down Expand Up @@ -93,6 +92,21 @@ void setup_usi(void) {
SET_BIT(USICR, USICS0);
}

void setup_usb(void) {
cli();
wdt_enable(WDTO_1S); // enable 1s watchdog timer
usbInit();

usbDeviceDisconnect(); // enforce re-enumeration
uint8_t i;
for(i = 0; i<250; i++) {
wdt_reset();
_delay_ms(2);
}
usbDeviceConnect();
sei();
}

void init_controller(void) {
SET_BIT(PORTA, PIN_GC); // Set positive output on PIN_GC
SET_BIT(DDRA, PIN_GC); // Set PIN_GC as output
Expand All @@ -109,25 +123,23 @@ void init_controller(void) {
uint8_t request_message(uint8_t *message_buffer) {
uint8_t cur_byte = 0;

USISR = 0b11100111; // Reset USI Interrupt flags and set timer value to 8
USISR = 0b11101000; // Reset USI Interrupt flags and set timer value to 8
disable_usi();
disable_timer0();

asm("nop; nop; nop;");

//SET_BIT(DDRA, PIN_GC); // Set PIN_GC as output
//CLEAR_BIT(PORTA, PIN_GC);
SET_BIT(DDRA, PIN_GC); // Set PIN_GC as output
CLEAR_BIT(PORTA, PIN_GC);

// Send controller data request
//SEND_ZERO(); SEND_ONE(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO();
//SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ONE(); SEND_ONE();
//SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ONE(); SEND_ZERO();
//SEND_ZERO();

//SET_BIT(PORTA, PIN_GC);
//CLEAR_BIT(DDRA, PIN_GC); // Set PIN_GC as input
SEND_ZERO(); SEND_ONE(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO();
SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ONE(); SEND_ONE();
SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ZERO(); SEND_ONE(); SEND_ZERO();
SEND_ZERO();

while(GET_BIT(PINA, PIN_GC)) {}
SET_BIT(PORTA, PIN_GC);
CLEAR_BIT(DDRA, PIN_GC); // Set PIN_GC as input

// Start reading the message
enable_usi();
Expand Down Expand Up @@ -188,37 +200,40 @@ int main(void)
setup_pins();
setup_timer0();
setup_usi();
//setup_usb();
//init_controller();
setup_usb();
init_controller();

ledsetup();
uint8_t retry_count = 0;

uint8_t message_buffer[11] = {0};
uint8_t message_buffer[8] = {0};
Controller *controller = (Controller*)message_buffer;

State state;
init_animation(&state);

while(1) {
//for(uint8_t i = 255; i; --i) {
//showColor(i, 0, 0, 8);
//_delay_ms(5);
//}

//showColor((Color) {255, 80, 150});
_delay_ms(1);
SET_BIT(PORTA, PIN_LED);
_delay_ms(1);
CLEAR_BIT(PORTA, PIN_LED);

// Zero out input array
//for(uint8_t i = 0; i < 8; ++i) {
//message_buffer[i] = 0xf0;
//}

// Try to grab the controller state
//if(request_message(message_buffer)) {
//next_frame(&state, controller);
//}
showColor(255, 0, 0, 8);
usbPoll();
wdt_reset();

if(usbInterruptIsReady()) {
// Zero out input array
for(uint8_t i = 0; i < 8; ++i) {
message_buffer[i] = 0xf0;
}
// Try to grab the controller state
retry_count = GCN_RETRY_LIMIT;
while(retry_count--) {
if(request_message(message_buffer)) {
build_report(controller, &reportBuffer);
usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
if(reportBuffer.rx == 255) {
_delay_us(1);
}
break;
}

if(retry_count == 1) {
showColor(0, 255, 0, 8);
}
}
}
}
}

0 comments on commit ff574cf

Please sign in to comment.