Subversion Repositories group.electronics

Rev

Rev 21 | Rev 23 | 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 <usbdrv.h>

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

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


void doInt(void);

volatile uint8_t pcIntCurr = 0;
volatile uint8_t pcIntLast = 0;
volatile uint8_t pcIntMask = 0;

volatile uint8_t timer0_ovf = 0;
volatile uint8_t rot_time = 0;
volatile uint8_t rot_stat = 0;
volatile uint8_t rot_pos = 0;

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;
    };
  };
} report;

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(report);
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
            return 1;
        } 
    }

  return 0;
}

void hadUsbReset(void) {
}

int main(void) {

  ACSR |= (1<<ACD); // Disable analog comparator

  // DDR : 1 = Output, 0 = Input
  // PORT: 1 = Pullup for Input, otherwise set output
  // PIN : Read input pin
  DDRB          = 0B00000001;
  PORTB         = 0B00000110;

  DDRD          = 0B10000000;
  PORTD         = 0B01100000;

  //PORTB |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //turn on pullups
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts

  // Setup timer0
  TIMSK0 = (1<<TOIE0);                  // Eable timer overflow for Timer0
  TCNT0 = 0x00;                         // Set Timer0 to 0
  TCCR0B = (1<< CS01) ; // /8 prescaler

  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
  _delay_ms(500);
  usbDeviceConnect();

  wdt_enable(WDTO_1S);
  usbInit();
  sei();
 

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

        //if (pcIntMask);
        //      doInt();

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

        if (bit_is_clear(PIND, PD5))
                report.A = 1;
        if (bit_is_clear(PIND, PD6))
                report.B = 1;

        if (rot_stat == 0x01 && rot_pos == 0) {
                report.rot1 = 1;
                rot_pos = 0;
        } else if (rot_stat == 0x02 && rot_pos == 0) {
                report.rot2 = 1;
                rot_pos = 0;
        } else {
                rot_pos = 0;
        }

        rot_stat = 0;
        cbi(PORTB, PB0);

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

void doInt() {
        if (rot_stat != 0) {
                pcIntMask = 0;
                return;
        }
                
  if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT1) ) {
        rot_stat = 1;
        sbi(PORTB, PB0);
  } else if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT2) ) {
        rot_stat = 2;
        sbi(PORTB, PB0);
  }
  pcIntMask = 0;
}

ISR(TIMER0_OVF_vect) {
        timer0_ovf++;

        //xbi(PORTD, PD7);

}

ISR(PCINT0_vect)
{
  pcIntCurr = PINB;
  pcIntMask = pcIntCurr ^ pcIntLast;
  pcIntLast = pcIntCurr;
        xbi(PORTD, PD7);
  doInt();
}