Subversion Repositories group.electronics

Rev

Rev 18 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

#define F_CPU 12000000
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <usbdrv.h>

#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "hiddesc.h"

struct{
  union {
    uint8_t data;
    struct {
      uint8_t X:2;
      uint8_t Y:2;
      uint8_t B:1;
      uint8_t A:1;
      uint8_t ROT1:1;
      uint8_t ROT2:1;
    };
  };
} reportBuffer;

usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  usbRequest_t *rq = (void *)data;

    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
            return sizeof(reportBuffer);
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
            return 1;
        } 
    }

  return 0;
}

void hadUsbReset(void) {

}

int main(void) {

  ACSR |= (1<<ACD); // Disable analog comparator
 
        sbi(DDRD, PD6);
        sbi(PORTD, PD6);
 
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
  _delay_ms(500);
  usbDeviceConnect();

  wdt_enable(WDTO_1S);
  usbInit();
  sei();
 
        // Setup the joystick ports 
        cbi (DDRB, PB0);
        sbi (PORTB, PB0);
        cbi (DDRB, PB1);
        sbi (PORTB, PB1);
        cbi (DDRB, PB2);
        sbi (PORTB, PB2);
        cbi (DDRB, PB3);
        sbi (PORTB, PB3);
        cbi (DDRB, PB4);
        sbi (PORTB, PB4);

        // Setup the buttons
        cbi (DDRD, PD4);
        sbi (PORTD, PD4);
        cbi (DDRD, PD5);
        sbi (PORTD, PD5);

        cbi(PORTD, PD6);

  for(;;) {
    wdt_reset();
    usbPoll();

        cbi (PORTD, PD6);
        if (bit_is_clear(PIND, PD5))
                sbi (PORTD, PD6);

    if(usbInterruptIsReady()){
      
      reportBuffer.data = 0x05; // Center pad, little endian

        if (bit_is_clear(PINB, PB0)) 
                reportBuffer.X++;
        if (bit_is_clear(PINB, PB1))
                reportBuffer.Y--;
        if (bit_is_clear(PINB, PB2))
                reportBuffer.X--;
        if (bit_is_clear(PINB, PB3))
                reportBuffer.Y++;

        if (bit_is_clear(PIND, PD4))
                reportBuffer.A = 1;
        if (bit_is_clear(PIND, PD5))
                reportBuffer.B = 1;

      /* called after every poll of the interrupt endpoint */
      usbSetInterrupt(&reportBuffer, sizeof(reportBuffer));

    }

  }

}