Subversion Repositories group.electronics

Rev

Rev 63 | Rev 96 | 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 "lcd.h"
#include "util.h"
#include "wire.h"
#include "config.h"
#include "hiddesc.h"

#define ROTS_ATTACHED   1
#define STAT            0
#define SENT            1

#define SWITCHES        1
#define ON              1
#define OFF             0

#define DETECTED        0
#define TIMER           1
#define DEBOUNCED       2

/*
 * Keyboard modifier codes
 */
#define MOD_CONTROL_LEFT    (1<<0)
#define MOD_SHIFT_LEFT      (1<<1)
#define MOD_ALT_LEFT        (1<<2)
#define MOD_GUI_LEFT        (1<<3)
#define MOD_CONTROL_RIGHT   (1<<4)
#define MOD_SHIFT_RIGHT     (1<<5)
#define MOD_ALT_RIGHT       (1<<6)
#define MOD_GUI_RIGHT       (1<<7)


void doInt(uint8_t pcint);
uint8_t getKey(void);

uint8_t oldpotVal = 0;

volatile uint8_t tmr0_ovf = 0;
volatile uint8_t tmr2_ovf = 0;
volatile uint32_t systime = 0;
volatile uint8_t lcdupdate = 1;

uint8_t emblock[] = {   0B00011111,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00010001,
                        0B00011111 };


volatile struct {
        uint8_t current;
        uint8_t last;
        uint8_t mask;
} pcInt[3];

// switches = [detected][timer][debounced]
volatile uint8_t switches[1][3] = { {0,0,0} };

// rotData = [rot#][(stat|sent)]
//volatile uint8_t rotData[2][2] = { {0,0}, {0,0} };

volatile struct {
        union {
                uint8_t data;
                struct {
                        uint8_t stat:4;
                        uint8_t sent:4;
                };
        };
} rotData[2];

uint8_t keyMap[] = {    0x1E, 0x1F, 0x20,
                        0x21, 0x22, 0x23,
                        0x24, 0x25, 0x26,
                        0x25, 0x27, 0x20 };

uint8_t keySelect = 1;

struct {
        uint8_t report_id;
        uint8_t modifier;
        uint8_t keycode;
} reportKeyboard;

struct{
        uint8_t report_id;
  union {
    uint8_t data1[2];
    struct {
        uint8_t rx:8;
        uint8_t ry:8;
    };
  };

  union {
    uint16_t data2;
    struct {
        uint16_t buttons:12;
        uint16_t rot2a:1;
        uint16_t rot2b:1;
        uint16_t rot1a:1;
        uint16_t rot1b:1;
    };
  };
} reportJoystick;

void usbSendHidReport(uchar * data, uchar len) {
        while(1)
        {
                usbPoll();
                if (usbInterruptIsReady())
                {
                        usbSetInterrupt(data, len);
                        break;
                }
        }
}

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

        if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
                switch (rq->bRequest) {
                        case USBRQ_HID_GET_REPORT:
                                if (rq->wValue.bytes[0] == 1)
                                        return sizeof(reportKeyboard);
                                else if (rq->wValue.bytes[0] == 2)
                                        return sizeof(reportJoystick);
                                else
                                        return 0;
                        case USBRQ_HID_GET_IDLE:
                                return 1;
                        default:
                                return 0;
                }
        }
        return 0;
}

void hadUsbReset(void) {
}

int main(void) {
        analogInit();

  /*
  DDR : 1 = Output, 0 = Input
  PORT: 1 = Pullup for Input, otherwise set output
  PIN : Read input pin
  */

  /*
        PB0     - Output                - Keypad 2
        PB1     - Output                - Keypad 7
        PB2     - Output                - Keypad 6
        PB3     - Output                - Keypad 4
        PB4     - Input, Pullup         - Function select
        PB5     - Input, Pullup         - Function select
  */
  DDRB          = 0B00001111;
  PORTB         = 0B00111111;

  /*
        PD0     - Input, Pullup, PCINT16        - Rotary 1a
        PD1     - Input, Pullup, PCINT17        - Rotary 1b


        PD4     - Output                - Keypad select status led
        PD5     - Input, Pullup         - Keypad 3
        PD6     - Input, Pullup         - Keypad 1
        PD7     - Input, Pullup         - Keypad 5
  */
  DDRD          = 0B00010000;
  PORTD         = 0B11110011;

  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
  PCICR |= ( 1 << PCIE2 ); //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<< CS00) ;                   // /1 prescaler

        TIMSK2 = (1<<TOIE2);
        TCNT2 = 0x00;
        TCCR2B = (1<<CS21 | 1<<CS20);

        sei();
        i2c_master();
        lcd_init();
        lcd_createChar(0x00, emblock);

        char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
        lcd_setCursor(0, 1);
        lcd_print(strTime);

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

        wdt_enable(WDTO_1S);
        usbInit();
        //sei();
 
        reportKeyboard.report_id = 1;
        reportJoystick.report_id = 2;

        cbi(PORTD, PD4);
  for(;;) {
    wdt_reset();
    usbPoll();

        /*
         * This is some really bad deboucing code
         */

        // Detect the button press, wait 100 timer cycles (1.7ms / cycle)
        if (rbi(PINB, PB5) == 0 && switches[0][DETECTED]==0) {
                switches[0][DETECTED] = 1;
                switches[0][TIMER] = 100;
        }

        // After timer is zero, check switch again.
        //  If switch still pressed, its debounced
        //  Otherwise, reset the debounce
        if (switches[0][DETECTED] == 1 && switches[0][TIMER] == 0 && switches[0][DEBOUNCED]==0) {
                if (rbi(PINB, PB5) == 0)        
                        switches[0][DEBOUNCED] = 1;
                else {
                        switches[0][DETECTED] = 0;
                        switches[0][TIMER] = 0;
                }
        }

        // If the switch has come up, do another debounce
        if (rbi(PINB, PB5) && switches[0][DETECTED]==2) {
                switches[0][TIMER] = 100;
                switches[0][DETECTED] = 3;
        }

        // After the up switch timer is zero, do the debounce check
        // Otherwise, assume switch is still down
        if (switches[0][DETECTED] == 3 && switches[0][TIMER] == 0) {
                if (rbi(PINB, PB5))
                        switches[0][DETECTED] = 0;
        }

        // Process the switch
        if (switches[0][DEBOUNCED] == 1) {
                xbi(keySelect, 0);

                if (keySelect == 0)
                        sbi(PORTD, PD4);
                else
                        cbi(PORTD, PD4);

                switches[0][DETECTED] = 2;
                switches[0][DEBOUNCED] = 0;
        }
        
        usbPoll();      

    if(usbInterruptIsReady()){
        reportJoystick.data1[0] = (-128 + analogRead(0));
        reportJoystick.data1[1] = (-128 + analogRead(1));
        reportJoystick.data2 = 0x0000;                          // Clear all the buttons

        reportKeyboard.modifier = 0x00;
        reportKeyboard.keycode = 0x00;

        uint8_t key = getKey();
        if (rbi(keySelect, 0)) {
                // Keypad is joystick
                if (key > 0)
                        reportJoystick.data2 |= (1 << (--key));
        } else {
                // Keypad is keyboard
                if (key > 0) {
                        if (key==10 || key==12)
                                reportKeyboard.modifier |= (1<<1);      //Left shift
                        reportKeyboard.keycode = keyMap[--key];
                }
        }

        // 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)
        // rotData = [rot#][(stat|sent)]
        uint8_t rot = 0;
        for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
                if (rotData[rot].stat == 0x01 && rotData[rot].sent == 0) {
                        rotData[rot].sent = 1;
                        switch (rot) {
                                case(0):        reportJoystick.rot1a = 1; break;
                                case(1):        reportJoystick.rot2a = 1; break;
                        }
                } else if (rotData[rot].stat == 0x02 && rotData[rot].sent == 0) {
                        rotData[rot].sent = 1;
                        switch (rot) {
                                case(0):      reportJoystick.rot1b = 1; break;
                                case(1):      reportJoystick.rot2b = 1; break;
                        }
                } else {
                        rotData[rot].sent = 0;
                }
                rotData[rot].stat = 0;

                if (rbi(PINB, PB4))
                        rotData[rot].sent = 0;
        }

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

        usbSendHidReport(&reportKeyboard, sizeof(reportKeyboard));
        usbSendHidReport(&reportJoystick, sizeof(reportJoystick));

    }

        if (lcdupdate) {
                usbPoll();
                lcdupdate = 0;

                char syschar[10];
                ultoa(systime, syschar, 10);
                lcd_overprint_right(syschar, 10, 5, 1);

                uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
                if (potVal != oldpotVal) {
                        lcd_percent_graph(potVal, 0, 0);
                        oldpotVal = potVal;

                        char pot[3];
                        utoa(potVal, pot, 10);
                        lcd_overprint_right(pot, 3, 11, 0);

                        // Set percentage
                        lcd_setCursor(15, 0);
                        lcd_char(0x25);
                }

        }

  }
}

uint8_t getKey() {
        uint8_t col, row = 0;
        uint8_t key = 0;
        uint8_t n = 1;

        for (row=0; row<=3; row++) {
                cbi(PORTB, row);
                _delay_us(10);                          // Wait for the port to change

                for (col=5; col<=7; col++) {
                        if (rbi(PIND, col) == 0)
                                key = n;
                        n++;
                }       

                sbi(PORTB, row);
        }
        return key;
}

/*
 *
 * Process the Pin Change Interrupt.
 * pcint provides what bank caused the interrupt
 *
 */
void pcInterrupt(uint8_t pcint) {

        switch (pcint) {
                case 0: pcInt[pcint].current = PINB; break;
                case 1: pcInt[pcint].current = PIND; break;
                case 2: pcInt[pcint].current = PINC; break;
        }
        pcInt[pcint].current = PIND;
        pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
        pcInt[pcint].last = pcInt[pcint].current;

        // Select what rotary we are dealing with
        //   based on the pc interrupt that fired.
        uint8_t rot = 0;
        if (pcint == 1) 
                rot = 1;

        // If rot stat is not 0, we havn't sent
        //  our last results yet. Skip this click.
        if (rotData[rot].stat != 0) {
                pcInt[pcint].mask = 0;
                return;
        }
        // Check which pin caused the interrupt. If they both
        //  equal 0, the pin that interrupted is the direction
        if (rbi(pcInt[pcint].current, PCINT17) == 0 
                && rbi(pcInt[pcint].current, PCINT17) == 0 
                && rbi(pcInt[pcint].mask, PCINT16) ) {
                        rotData[rot].stat = 1;
        } else if (rbi(pcInt[pcint].current, PCINT16) == 0 
                && rbi(pcInt[pcint].current, PCINT17) == 0 
                && rbi(pcInt[pcint].mask, PCINT17) ) {
                        rotData[rot].stat = 2;
        }

        // Clear the mask so we know we've delth with it
        pcInt[pcint].mask = 0;
}

ISR(TIMER0_OVF_vect) {
        tmr0_ovf++;
        if (tmr0_ovf>=46) {
                systime++;
                tmr0_ovf = 0;
        }
        if (switches[0][DETECTED] && switches[0][TIMER])
                switches[0][TIMER]--;
}

ISR(TIMER2_OVF_vect) {
        tmr2_ovf++;
        if (tmr2_ovf>=58) {
                lcdupdate=1;
                tmr2_ovf = 0;
        //xbi(PORTD, PD4);
        }
}


ISR(PCINT0_vect) {
        pcInterrupt(0);
}

ISR(PCINT1_vect) {
        pcInterrupt(1);
}

ISR(PCINT2_vect) {
        pcInterrupt(2);
}