Skip to content

Commit

Permalink
add mouse function.
Browse files Browse the repository at this point in the history
  • Loading branch information
yashikno committed Sep 30, 2010
1 parent e7c6839 commit d3b1af9
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*.lst
*.map
*.sym
tags
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ TARGET = mykey

# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c \
keymap.c \
matrix.c \
usb_device.c \
usb.c \
usb_keyboard.c \
usb_mouse.c \
usb_debug.c \
print.c \
jump_bootloader.c
keymap.c \
matrix.c \
jump_bootloader.c \
print.c


# MCU name, you MUST set this to match the board you are using
Expand Down
2 changes: 1 addition & 1 deletion keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define KEYMAP_H 1

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

int get_layer(void);
uint8_t get_keycode(int layer, int row, int col);
Expand Down
5 changes: 5 additions & 0 deletions matrix.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef MATRIX_H
#define MATRIX_H 1

#include <stdbool.h>

extern uint8_t *matrix;
Expand All @@ -8,3 +11,5 @@ uint8_t matrix_scan(void);
bool matrix_is_modified(void);
bool matrix_has_ghost(void);
bool matrix_has_ghost_in_row(uint8_t row);

#endif
127 changes: 54 additions & 73 deletions mykey.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include <avr/interrupt.h>
#include <util/delay.h>

#include "usb_device.h"
#include "usb.h"
#include "usb_keyboard.h"
#include "usb_mouse.h"
#include "print.h"
#include "matrix.h"
#include "keymap.h"
Expand All @@ -50,16 +52,9 @@ uint16_t idle_count=0;

int main(void)
{
bool modified = false;
bool has_ghost = false;
uint8_t key_index = 0;

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

matrix_init();


// 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,
// this will wait forever.
Expand All @@ -78,99 +73,85 @@ int main(void)
TCCR0B = 0x05;
TIMSK0 = (1<<TOIE0);


matrix_init();
print("firmware 0.2 for t.m.k.\n");

bool modified = false;
bool has_ghost = false;
int key_index = 0;
int loop_count = 0;
int layer = 0;
while (1) {
int layer = 0;

matrix_scan();
layer = get_layer();

modified = matrix_is_modified();
has_ghost = matrix_has_ghost();

// doesnt send keys during ghost occurs
if (modified && !has_ghost) {
key_index = 0;
keyboard_modifier_keys = 0;
for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
// print matrix state for debug
if (modified) {
print_matrix();

for (int row = 0; row < MATRIX_ROWS; row++) {
for (int col = 0; col < MATRIX_COLS; col++) {
if (matrix[row] & 1<<col) continue;

uint8_t code = get_keycode(layer, row, col);
if (code == KB_NO) {
continue;
} else if (KB_LCTRL <= code && code <= KB_RGUI) {
// modifier keycode: 0xE0-0xE7
keyboard_modifier_keys |= 1<<(code & 0x07);
} else {
if (key_index < 6)
keyboard_keys[key_index] = code;
key_index++;
}
// LED flush
DDRD |= 1<<PD6;
PORTD |= 1<<PD6;
}

// set matrix state to keyboard_keys, keyboard_modifier_keys
key_index = 0;
keyboard_modifier_keys = 0;
for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
for (int row = 0; row < MATRIX_ROWS; row++) {
for (int col = 0; col < MATRIX_COLS; col++) {
if (matrix[row] & 1<<col) continue;

uint8_t code = get_keycode(layer, row, col);
if (code == KB_NO) {
continue;
} else if (KB_LCTRL <= code && code <= KB_RGUI) {
// modifier keycode: 0xE0-0xE7
keyboard_modifier_keys |= 1<<(code & 0x07);
} else {
if (key_index < 6)
keyboard_keys[key_index] = code;
key_index++;
}
}
}

// run bootloader when 4 left modifier keys down
if (!has_ghost) {
// when 4 left modifier keys down
if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
// cancel all keys
keyboard_modifier_keys = 0;
for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
usb_keyboard_send();

/*
print("jump to bootloader...\n");
_delay_ms(1000);
jump_bootloader();
}
*/

if (key_index > 6) {
//Rollover
}
// mouse
print("usb_mouse_move\n");
usb_mouse_move(10, 0, 0);

usb_keyboard_send();


// variables shared with interrupt routines must be
// accessed carefully so the interrupt routine doesn't
// try to use the variable in the middle of our access
cli();
//idle_count = 0;
sei();
}

// print matrix state for debug
if (modified) {
print_matrix();

// LED flush
DDRD |= 1<<PD6;
PORTD |= 1<<PD6;
}
_delay_ms(100);
continue;
}

/*
// print counts for debug
if ((loop_count % 0x1000) == 0) {
//print(".");
print("idle_count: "); phex((idle_count & 0xFF00) >> 8); phex(idle_count & 0xFF); print("\n");
print("loop_count: "); phex((loop_count & 0xFF00) >> 8); phex(loop_count & 0xFF); print("\n");
print_matrix();
}

// teensy LED flush for debug
if ((loop_count & 0x100) == 0) {
DDRD |= 1<<PD6;
PORTD |= 1<<PD6;
// send keys to host
if (modified) {
if (key_index > 6) {
//Rollover
}
usb_keyboard_send();
}
}
*/

// now the current pins will be the previous, and
// wait a short delay so we're not highly sensitive
// to mechanical "bounce".
_delay_ms(2);
loop_count++;
_delay_ms(2);
}
}

Expand Down
1 change: 0 additions & 1 deletion print.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <avr/io.h>
#include <avr/pgmspace.h>

#include "print.h"

void print_P(const char *s)
Expand Down
4 changes: 2 additions & 2 deletions print.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef print_h__
#define print_h__
#ifndef PRINT_H__
#define PRINT_H__ 1

#include <avr/pgmspace.h>
#include "usb_debug.h"
Expand Down
Loading

0 comments on commit d3b1af9

Please sign in to comment.