Subversion Repositories group.electronics

Rev

Rev 22 | Rev 24 | 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 rot_stat = 0;
volatile uint8_t rot_sent = 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
  */

  /*
        PB0     - Output                - Status LED
        PB1     - Input, Pullup, PCINT1 - Rotary 1
        PB2     - Input, Pullup, PCINT2 - Rotary 2
  */
  DDRB          = 0B00000001;
  PORTB         = 0B00000110;

  /*
        PD4     - Input, Pullup         - Rotary function select
        PD5     - Input, Pullup         - Button
        PD6     - Input, Pullup         - Button
        PD7     - Output                - Status LED
  */
  DDRD          = 0B10000000;
  PORTD         = 0B01110000;

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

  // Timers not used for the moment
  // Setup timer0 - Enable overflow, 8 times prescaler
  //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(usbInterruptIsReady()){
        report.data = 0x05; // Center pad, little endian

        // Send our buttons 
        if (bit_is_clear(PIND, PD5))
                report.A = 1;
        if (bit_is_clear(PIND, PD6))
                report.B = 1;

        // Now work out what rotary to send, if any
        // Also record if we sent a positive response, 
        //  so we can send a '0' next time (if selected on PD4)
        if (rot_stat == 0x01 && rot_sent == 0) {
                report.rot1 = 1;
                rot_sent = 1;
        } else if (rot_stat == 0x02 && rot_sent == 0) {
                report.rot2 = 1;
                rot_sent = 1;
        } else {
                rot_sent = 0;
        }

        // Reset our stat so ready for next turn
        rot_stat = 0;

        // If our function select is set, dont bother
        //  sending a 'o' between consequtive 1's.
        if (rbi(PIND, PD4))
                rot_sent = 0;

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

void doInt() {
        // If rot_stat is not 0, we havn't sent
        //  our last results yet. Skip this click.
        if (rot_stat != 0) {
                pcIntMask = 0;
                return;
        }

        // Check which pin caused the interrupt. If they both
        //  equal 0, the pin that interrupted is the direction
        if (rbi(pcIntCurr, PCINT1) == 0 
                && rbi(pcIntCurr, PCINT2) == 0 
                && rbi(pcIntMask, PCINT1) ) {
                        rot_stat = 1;
        } else if (rbi(pcIntCurr, PCINT1) == 0 
                && rbi(pcIntCurr, PCINT2) == 0 
                && rbi(pcIntMask, PCINT2) ) {
                        rot_stat = 2;
        }

        // Clear the mask so we know we've delth with it
        pcIntMask = 0;
}

/* Not used for the moment
ISR(TIMER0_OVF_vect) {
        timer0_ovf++;
}
*/

ISR(PCINT0_vect)
{
        // Save the state and work out which pin caused
        //  the interrupt to occur
        pcIntCurr = PINB;
        pcIntMask = pcIntCurr ^ pcIntLast;
        pcIntLast = pcIntCurr;
        doInt();
}