Rev 36 | Rev 39 | 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"#define ROTS_ATTACHED 2#define STAT 0#define SENT 1/** 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 analogRead(uint8_t pin);volatile uint8_t pcIntCurr[3] = {0,0,0};volatile uint8_t pcIntLast[3] = {0,0,0};volatile uint8_t pcIntMask[3] = {0,0,0};// rotdata = [rot#][(stat|sent)]volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };uint8_t keyMap[] = { 0x1E, 0x1F, 0x20,0x21, 0x22, 0x23,0x24, 0x25, 0x26,0x20, 0x27, 0x25 };struct {uint8_t report_id;uint8_t modifier;uint8_t reserved;uint8_t keycode[6];} 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;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);elsereturn 0;case USBRQ_HID_GET_IDLE:return 1;default:return 0;}}return 0;}void hadUsbReset(void) {}int main(void) {ACSR |= (1<<ACD); // Disable analog comparatorADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;/*DDR : 1 = Output, 0 = InputPORT: 1 = Pullup for Input, otherwise set outputPIN : Read input pin*//*PB0 - Output - Keypad 1PB1 - Output - Keypad 2PB2 - Output - Keypad 3PB3 - Output - Keypad 4PB4 - Input, Pullup - Function selectPB5 - Input, Pullup - Function select*/DDRB = 0B00001111;PORTB = 0B00111111;/*PD0 - Input, Pullup, PCINT16 - Rotary 1aPD1 - Input, Pullup, PCINT17 - Rotary 1bPD4 - Output - Keypad select status ledPD5 - Input, Pullup - Keypad 6PD6 - Input, Pullup - Keypad 7PD7 - Input, Pullup - Keypad 8*/DDRD = 0B00010000;PORTD = 0B11100011;PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sourcesPCICR |= ( 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<< CS01) ; // /8 prescalerusbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */_delay_ms(500);usbDeviceConnect();wdt_enable(WDTO_1S);usbInit();sei();reportKeyboard.report_id = 1;reportKeyboard.reserved = 0x00;reportJoystick.report_id = 2;for(;;) {wdt_reset();usbPoll();if(usbInterruptIsReady()){reportJoystick.data1[0] = (-128 + analogRead(0));reportJoystick.data1[1] = (-128 + analogRead(1));reportJoystick.data2 = 0x0000; // Clear all the buttonsreportKeyboard.modifier = 0x00;reportKeyboard.keycode[0] = 0x00;uint8_t key = getKey();if (rbi(PINB, PB5)) {cbi(PORTD, PD4);// Keypad is joystickif (key > 0)reportJoystick.data2 |= (1 << (key -1));} else {sbi(PORTD, PD4);// Keypad is keyboardif (key > 0) {key--;//if (key==0x20 || key==0x25)// reportKeyboard.modifier |= (1<<1); //Left shif//reportKeyboard.keycode = keyMap[key];reportKeyboard.keycode[0] = 0x30;}}// 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));}}}uint8_t analogRead(uint8_t pin) {ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);ADCSRA |= (1<<ADSC); // Start convertingwhile (((ADCSRA >> ADSC) & 1)) {} //Wait until conversion finisheduint8_t result = ADCH;//ADCSRA |= (0<<ADSC); // Stop convertingreturn result;}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 changefor (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 doInt(uint8_t pcint) {// 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) {pcIntMask[pcint] = 0;return;}// Check which pin caused the interrupt. If they both// equal 0, the pin that interrupted is the directionif (rbi(pcIntCurr[pcint], PCINT17) == 0&& rbi(pcIntCurr[pcint], PCINT17) == 0&& rbi(pcIntMask[pcint], PCINT16) ) {rotdata[rot][STAT] = 1;} else if (rbi(pcIntCurr[pcint], PCINT16) == 0&& rbi(pcIntCurr[pcint], PCINT17) == 0&& rbi(pcIntMask[pcint], PCINT17) ) {rotdata[rot][STAT] = 2;}// Clear the mask so we know we've delth with itpcIntMask[pcint] = 0;}/* Not used for the momentISR(TIMER0_OVF_vect) {timer0_ovf++;}*/ISR(PCINT1_vect){// Save the state and work out which pin caused// the interrupt to occurpcIntCurr[1] = PIND;pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];pcIntLast[1] = pcIntCurr[1];doInt(1);}ISR(PCINT2_vect){// Save the state and work out which pin caused// the interrupt to occurpcIntCurr[2] = PIND;pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];pcIntLast[2] = pcIntCurr[2];doInt(2);}