Blame | 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 <stdlib.h>
#include <string.h>
#include "usbdrv.h"
#include "atcpad.h"
#include "avrutil.h"
#include "lcd.h"
#include "wire.h"
#include "hiddesc.h"
#define BUTTONS 1
#define LCD_UPDATE 11
#define ROTARY_ON_TIME 30
#define ROTARY_OFF_TIME 15
void pcInterrupt(uint8_t);
uint8_t getKey(void);
void updateLcd();
void checkRotarys();
void checkButtons(void);
// * = 0x25, #=0x20, 0 = 0x27
// F9 = 0x42, F12 = 0x45, ` = 0x35
uint8_t keyMap[] = {
0x1E, 0x1F, 0x20, // 1, 2, 3
0x21, 0x22, 0x23, // 4, 5, 6
0x24, 0x25, 0x26, // 7, 8, 9
0x42, 0x35, 0x45 }; // F9, `, F12
uint8_t lcdRectangle[] = {
0B00011111,
0B00010001,
0B00010001,
0B00010001,
0B00010001,
0B00010001,
0B00010001,
0B00011111 };
uint8_t oldpotVal = 0;
uint8_t keySelect = 1;
// Start these of at different times, so they
// don't kick of it the same loop run
volatile uint8_t lcdTimer = 8;
volatile struct {
uint8_t current;
uint8_t last;
uint8_t mask;
} pcInt[3];
volatile struct {
uint8_t detected;
uint8_t timer;
uint8_t waitup;
} buttons[BUTTONS];
volatile struct {
uint8_t direction;
uint8_t timer;
} rotary[2];
int main(void) {
setup();
while(1)
{
wdt_reset();
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;
/*
PC2 - Input, Pullup, PCINT16 - Rotary 2a
PC3 - Input, Pullup, PCINT16 - Rotary 2b
*/
DDRC = 0B00000000;
PORTC = 0B00001100;
/*
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;
// Pin Change Interrupts
PCMSK1 |= (( 1 << PCINT10 ) | ( 1 << PCINT11 )); //Rotary Encoder 2
PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //Rotary Encoder 1
PCICR |= ((1 << PCIE1 ) | (1 << PCIE2));
analogInit();
sysclockInit();
reportKeyboard.report_id = 1;
reportJoystick.report_id = 2;
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
_delay_ms(500);
usbDeviceConnect();
sei();
wdt_enable(WDTO_1S);
usbInit();
i2c_master();
lcd_init();
lcd_createChar(0x00, lcdRectangle);
char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
lcd_setCursor(0, 1);
lcd_print(strTime);
buttons[0].detected = 0;
buttons[0].timer = 0;
buttons[0].waitup = 0;
cbi(PORTD, 4);
}
void loop() {
usbPoll();
if (lcdTimer==0) {
updateLcd();
lcdTimer = LCD_UPDATE;
}
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;
checkButtons();
checkRotarys();
}
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 checkRotarys() {
uint8_t i = 0;
for (i=0; i<=1; i++) {
// If our rotary timer has run out, turn direction
// off and set the cool down period
if (rotary[i].direction && rotary[i].timer == 0) {
rotary[i].direction = 0x00;
rotary[i].timer = ROTARY_OFF_TIME;
}
// If timer is still going, set the correct button
// of the joystick to press
if (rotary[i].timer != 0) {
if (rotary[i].direction == 1) {
switch (i) {
case 0: reportJoystick.rot1a = 1; break;
case 1: reportJoystick.rot2a = 1; break;
}
} else if (rotary[i].direction == 2) {
switch (i) {
case 0: reportJoystick.rot1b = 1; break;
case 1: reportJoystick.rot2b = 1; break;
}
}
}
}
}
void checkButtons() {
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];
}
}
// Only one button for now
if (rbi(PINB, 5) == 0 && buttons[0].detected == 0 && buttons[0].timer == 0) {
buttons[0].detected = 1;
buttons[0].timer = 5;
buttons[0].waitup = 0;
}
if (rbi(PINB, 5) == 1 && buttons[0].waitup == 1) {
buttons[0].detected = 0;
buttons[0].timer = 0;
buttons[0].waitup = 0;
}
if (buttons[0].detected == 1 && buttons[0].timer == 0 && buttons[0].waitup == 0 ) {
xbi(keySelect, 0);
if (keySelect == 0)
sbi(PORTD, PD4);
else
cbi(PORTD, PD4);
buttons[0].waitup = 1;
}
}
void updateLcd() {
usbPoll();
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);
}
}
void millis_tick() {
}
/*
*
* 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 = PINC; break;
case 2: pcInt[pcint].current = PIND; break;
}
pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
pcInt[pcint].last = pcInt[pcint].current;
// For Each rotary:
// Check which pin caused the interrupt. If they both
// equal 0, the pin that interrupted is the direction
// Rotary 0
if (rbi(pcInt[pcint].current, PCINT10) == 0
&& rbi(pcInt[pcint].current, PCINT11) == 0
&& rbi(pcInt[pcint].mask, PCINT11) ) {
if (rotary[0].timer == 0 && rotary[0].direction == 0) {
rotary[0].direction = 1;
rotary[0].timer = ROTARY_ON_TIME;
}
} else if (rbi(pcInt[pcint].current, PCINT10) == 0
&& rbi(pcInt[pcint].current, PCINT11) == 0
&& rbi(pcInt[pcint].mask, PCINT10) ) {
if (rotary[0].timer == 0 && rotary[0].direction == 0) {
rotary[0].direction = 2;
rotary[0].timer = ROTARY_ON_TIME;
}
}
// Rotary 1
if (rbi(pcInt[pcint].current, PCINT16) == 0
&& rbi(pcInt[pcint].current, PCINT17) == 0
&& rbi(pcInt[pcint].mask, PCINT16) ) {
if (rotary[1].timer == 0 && rotary[1].direction == 0) {
rotary[1].direction = 1;
rotary[1].timer = ROTARY_ON_TIME;
}
} else if (rbi(pcInt[pcint].current, PCINT16) == 0
&& rbi(pcInt[pcint].current, PCINT17) == 0
&& rbi(pcInt[pcint].mask, PCINT17) ) {
if (rotary[1].timer == 0 && rotary[1].direction == 0) {
rotary[1].direction = 2;
rotary[1].timer = ROTARY_ON_TIME;
}
}
// Clear the mask so we know we've delth with it
pcInt[pcint].mask = 0;
}
ISR(TIMER0_OVF_vect) {
tmr0_ovf++;
if (tmr0_ovf >= sys_ovf_tick) {
systime++;
tmr0_ovf = 0;
//millis_tick(); // Not working, taking too long to call?
// Decrease our various millisecond timers
if (buttons[0].timer)
buttons[0].timer--;
if (lcdTimer)
lcdTimer--;
uint8_t i = 0;
for (i=0; i<=1; i++) {
if (rotary[i].timer)
rotary[i].timer--;
}
}
}
ISR(PCINT0_vect) {
pcInterrupt(0);
}
ISR(PCINT1_vect) {
pcInterrupt(1);
}
ISR(PCINT2_vect) {
pcInterrupt(2);
}
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;
}