Subversion Repositories group.electronics

Rev

Rev 18 | 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"

#define DEPRESS_TIME    1

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 time_rot = 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          = 0B00000000;
  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) | (1<<CS00);      // /1024 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 (!time_rot) {
                report.rot1 = 0;
                report.rot2 = 0;
                cbi(PORTB, PB0);
        }       

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

/*
        if (bit_is_clear(PINB, PB0)) 
                report.X++;
        if (bit_is_clear(PINB, PB1))
                report.Y--;
        if (bit_is_clear(PINB, PB2))
                report.X--;
        if (bit_is_clear(PINB, PB3))
                report.Y++;
*/
        if (bit_is_clear(PIND, PD5))
                report.A = 1;

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

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

void doInt() {
  if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT1) ) {
        time_rot = 60;
        report.rot1 = 1;
        report.rot2 = 0;
        sbi(PORTB, PB0);
  } else if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT2) ) {
        time_rot = 60;
        report.rot1 = 0;
        report.rot2 = 1;
        sbi(PORTB, PB0);
  }

  pcIntMask = 0;
}

ISR(TIMER0_OVF_vect) {
        timer0_ovf++;

        if (time_rot) {
                time_rot--;
        }
}

ISR(PCINT0_vect)
{
  pcIntCurr = PINB;
  pcIntMask = pcIntCurr ^ pcIntLast;
  pcIntLast = pcIntCurr;
  doInt();
}