Rev 97 | Rev 99 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* atcpad.c
*
* Created: 7/06/2013 10:15:34 PM
* Author: pfowler
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include "usbdrv.h"
#include "atcpad.h"
#include "avrutil.h"
#include "lcd.h"
#include "wire.h"
#include "hiddesc.h"
#define BUTTONS 1
#define ON 1
#define OFF 0
// * = 0x25, #=0x20
// F9 = 0x42, F12 = 0x45
uint8_t keyMap[] = { 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23,
0x24, 0x25, 0x26,
0x42, 0x27, 0x45 };
uint8_t keySelect = 1;
uint8_t lcdRectangle[] = {
0B00011111,
0B00010001,
0B00010001,
0B00010001,
0B00010001,
0B00010001,
0B00010001,
0B00011111 };
uint8_t getKey(void);
volatile struct {
uint8_t detected;
uint8_t timer;
uint8_t debounced;
} buttons[BUTTONS];
int main(void)
{
setup();
while(1)
{
loop();
}
}
void setup() {
/*
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 interrupts
analogInit();
sysclockInit();
reportKeyboard.report_id = 1;
reportJoystick.report_id = 2;
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
_delay_ms(500);
usbDeviceConnect();
wdt_enable(WDTO_1S);
usbInit();
usbPoll();
sei();
i2c_master();
lcd_init();
lcd_createChar(0x00, lcdRectangle);
usbPoll();
char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
lcd_setCursor(0, 1);
lcd_print(strTime);
}
void loop() {
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) // Left shift, for *, #
// reportKeyboard.modifier |= (1<<1);
reportKeyboard.keycode = keyMap[--key];
}
}
usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
}
}
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;
}
void millis_tick() {
if (buttons[0].detected && buttons[0].timer)
buttons[0].timer--;
//lcdupdate = 1;
}
ISR(TIMER0_OVF_vect) {
tmr0_ovf++;
if (tmr0_ovf>=sys_ovf_tick) {
systime++;
tmr0_ovf = 0;
millis_tick();
}
}
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:
usbMsgPtr = &idleRate;
return 1;
default:
return 0;
}
}
return 0;
}
void hadUsbReset(void) {
}