Skip to content

Commit

Permalink
matrix scan & default keymap
Browse files Browse the repository at this point in the history
  • Loading branch information
yashikno committed Aug 22, 2010
1 parent 20711b5 commit 8cfb371
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 38 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TARGET = mykey

# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c \
keymap.c \
usb_keyboard_debug.c \
print.c

Expand Down
27 changes: 27 additions & 0 deletions keymap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "keymap.h"

/*
* keymap for modified macway keyboard
*/
#define MATRIX_ROWS 9
#define MATRIX_COLS 8

const uint8_t Keymap[MATRIX_COLS][MATRIX_ROWS] = {
{ KB_LALT, KB_1, KB_2, KB_3, KB_4, KB_7, KB_8, KB_9, KB_0 },
{ KB_NO, KB_ESCAPE, KB_RALT, KB_NO, KB_5, KB_6, KB_EQUAL, KB_NO, KB_MINUS },
{ KB_BSPACE, KB_TAB, KB_LGUI, KB_RSHIFT, KB_T, KB_Y, KB_RBRACKET, KB_NO, KB_LBRACKET },
{ KB_NO, KB_Q, KB_W, KB_E, KB_R, KB_U, KB_I, KB_O, KB_P },
{ KB_BSLASH, KB_A, KB_S, KB_D, KB_F, KB_J, KB_K, KB_L, KB_SCOLON },
{ KB_NO, KB_LCTRL, KB_NO, KB_UP, KB_G, KB_H, KB_NO, KB_GRAVE, KB_QUOTE },
{ KB_ENTER, KB_Z, KB_X, KB_C, KB_V, KB_M, KB_COMMA, KB_DOWN, KB_NO },
{ KB_SPACE, KB_DOWN, KB_RIGHT, KB_LEFT, KB_B, KB_N, KB_LSHIFT, KB_NO, KB_SLASH }
};

uint8_t get_keycode(uint8_t row, uint8_t col)
{
if (row >= MATRIX_ROWS)
return KB_NO;
if (col >= MATRIX_COLS)
return KB_NO;
return Keymap[col][row];
}
9 changes: 9 additions & 0 deletions keymap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef KEYMAP_H
#define KEYMAP_H 1

#include <stdint.h>
#include "usbkeycodes.h"

uint8_t get_keycode(uint8_t row, uint8_t col);

#endif
186 changes: 150 additions & 36 deletions mykey.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <util/delay.h>
#include "usb_keyboard_debug.h"
#include "print.h"
#include "keymap.h"

#define LED_CONFIG (DDRD |= (1<<6))
#define LED_ON (PORTD &= ~(1<<6))
Expand All @@ -38,21 +39,127 @@ uint8_t number_keys[10]=

uint16_t idle_count=0;



//
// scan matrix
//
uint8_t MAX_ROW = 9;

// initialize ports for matrix
void port_setup(void)
{
// Column: input w/pullup
DDRB = 0x00;
PORTB = 0xFF;

// Row: Hi-Z(unselected)
// PD:0,1,2,3,6,7
// PC:6,7
// PF:7
DDRD = 0x00;
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
}

// select a row of matrix for read
void select_row(uint8_t row)
{
switch (row) {
case 0:
DDRD = (1<<0);
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 1:
DDRD = (1<<1);
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 2:
DDRD = (1<<2);
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 3:
DDRD = (1<<3);
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 4:
DDRD = (1<<6);
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 5:
DDRD = (1<<7);
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 6:
DDRD = 0x00;
PORTD = 0x00;
DDRC = (1<<6);
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 7:
DDRD = 0x00;
PORTD = 0x00;
DDRC = (1<<7);
PORTC = 0x00;
DDRF = 0x00;
PORTF = 0x00;
break;
case 8:
DDRD = 0x00;
PORTD = 0x00;
DDRC = 0x00;
PORTC = 0x00;
DDRF = (1<<7);
PORTF = 0x00;
break;
}
}

uint8_t read_col(void)
{
return PINB;
}

int main(void)
{
uint8_t b, d, mask, i, reset_idle;
uint8_t b_prev=0xFF, d_prev=0xFF;
uint8_t i, reset_idle;
uint8_t prev_state[MAX_ROW];
for (int i=0; i < MAX_ROW; i++) prev_state[i] = 0xFF;

// set for 16 MHz clock
CPU_PRESCALE(0);

// Configure all port B and port D pins as inputs with pullup resistors.
// See the "Using I/O Pins" page for details.
// http://www.pjrc.com/teensy/pins.html
DDRD = 0x00;
DDRB = 0x00;
PORTB = 0xFF;
PORTD = 0xFF;
port_setup();


// Initialize the USB, and then wait for the host to set configuration.
// If the Teensy is powered without a PC connected to the USB port,
Expand All @@ -76,32 +183,40 @@ int main(void)
print("All Port B or Port D pins are inputs with pullup resistors.\n");
print("Any connection to ground on Port B or D pins will result in\n");
print("keystrokes sent to the PC (and debug messages here).\n");

uint8_t col;
uint8_t code;
while (1) {
// read all port B and port D pins
b = PINB;
d = PIND;
// check if any pins are low, but were high previously
mask = 1;
reset_idle = 0;
for (i=0; i<8; i++) {
if (((b & mask) == 0) && (b_prev & mask) != 0) {
//usb_keyboard_press(KEY_B, KEY_SHIFT);
//usb_keyboard_press(number_keys[i], 0);
print("Port B, bit ");
phex(i);
print("\n");
reset_idle = 1;
}
if (((d & mask) == 0) && (d_prev & mask) != 0) {
//usb_keyboard_press(KEY_D, KEY_SHIFT);
//usb_keyboard_press(number_keys[i], 0);
print("Port D, bit ");
phex(i);
print("\n");
reset_idle = 1;
}
mask = mask << 1;
}
reset_idle = 0;

for (uint8_t r=0; r < MAX_ROW; r++) {
select_row(r);

// without this read unstable value.
_delay_us(30);

col = read_col();
if (col != prev_state[r]) {
prev_state[r] = col;
phex(r);
print(": ");
pbin(col);
print("\n");

for (int c = 0; c < 8; c++) {
if (col & 1<<c) continue;
code = get_keycode(r, c);
phex(code);
print("\n");
usb_keyboard_press(code, 0);
}

reset_idle = 1;
}
}



// if any keypresses were detected, reset the idle counter
if (reset_idle) {
// variables shared with interrupt routines must be
Expand All @@ -111,11 +226,10 @@ int main(void)
idle_count = 0;
sei();
}

// now the current pins will be the previous, and
// wait a short delay so we're not highly sensitive
// to mechanical "bounce".
b_prev = b;
d_prev = d;
_delay_ms(2);
}
}
Expand Down
8 changes: 6 additions & 2 deletions print.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ void phex16(unsigned int i)
}




void pbin(unsigned char c)
{
for (int i=7; i>=0; i--) {
usb_debug_putchar((c & (1<<i)) ? '1' : '0');
}
}
1 change: 1 addition & 0 deletions print.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
void print_P(const char *s);
void phex(unsigned char c);
void phex16(unsigned int i);
void pbin(unsigned char c);

#endif
Loading

0 comments on commit 8cfb371

Please sign in to comment.