refactor keyboard.h, host.h

This commit is contained in:
tmk 2011-02-10 15:51:30 +09:00
parent acc974c64b
commit 2b8cd88ab1
21 changed files with 522 additions and 360 deletions

View file

@ -23,6 +23,12 @@
# define MOUSEKEY_DELAY_TIME 255
#endif
#define IS_COMMAND() ( \
keyboard_report->mods == (BIT_LSHIFT | BIT_RSHIFT) || \
keyboard_report->mods == (BIT_LCTRL | BIT_RSHIFT) \
)
/* PS/2 lines */
#define PS2_CLOCK_PORT PORTD
#define PS2_CLOCK_PIN PIND
@ -39,6 +45,7 @@
EICRA |= ((1<<ISC11) | (0<<ISC10)); \
EIFR |= (1<<INTF1); \
} while (0)
#define PS2_INT_DISABLE() do { \
EIMSK &= ~(1<<INT1); \
} while (0)
@ -50,6 +57,7 @@
PCICR |= (1<<PCIE2); \
PCIFR |= (1<<PCIF2); \
} while (0)
#define PS2_INT_DISABLE() do { \
PCMSK2 &= ~(1<<PCINT22); \
PCICR &= ~(1<<PCIE); \

View file

@ -1,18 +1,111 @@
#include "usbdrv.h"
#include "usbconfig.h"
#include "keyboard.h"
#include "print.h"
#include "usb_keycodes.h"
#include "host.h"
#include "host_vusb.h"
#include "debug.h"
static report_keyboard_t report0;
static report_keyboard_t report1;
report_keyboard_t *keyboard_report = &report0;
report_keyboard_t *keyboard_report_prev = &report1;
static uint8_t keyboard_led = 0;
static uchar idleRate = 0;
uint8_t host_keyboard_led(void)
{
return keyboard_led;
}
/*------------------------------------------------------------------*
* Keyboard report operations
*------------------------------------------------------------------*/
void host_add_key(uint8_t code)
{
int8_t i = 0;
int8_t empty = -1;
for (; i < REPORT_KEYS; i++) {
if (keyboard_report_prev->keys[i] == code) {
keyboard_report->keys[i] = code;
break;
}
if (empty == -1 && keyboard_report_prev->keys[i] == KB_NO && keyboard_report->keys[i] == KB_NO) {
empty = i;
}
}
if (i == REPORT_KEYS && empty != -1) {
keyboard_report->keys[empty] = code;
}
}
void host_add_mod_bit(uint8_t mod)
{
keyboard_report->mods |= mod;
}
void host_set_mods(uint8_t mods)
{
keyboard_report->mods = mods;
}
void host_add_code(uint8_t code)
{
if (IS_MOD(code)) {
host_add_mod_bit(MOD_BIT(code));
} else {
host_add_key(code);
}
}
void host_swap_keyboard_report(void)
{
report_keyboard_t *tmp = keyboard_report_prev;
keyboard_report_prev = keyboard_report;
keyboard_report = tmp;
}
void host_clear_keyboard_report(void)
{
keyboard_report->mods = 0;
for (int8_t i = 0; i < REPORT_KEYS; i++) {
keyboard_report->keys[i] = 0;
}
}
uint8_t host_has_anykey(void)
{
uint8_t cnt = 0;
for (int i = 0; i < REPORT_KEYS; i++) {
if (keyboard_report->keys[i])
cnt++;
}
return cnt;
}
uint8_t *host_get_keys(void)
{
return keyboard_report->keys;
}
uint8_t host_get_mods(void)
{
return keyboard_report->mods;
}
/*------------------------------------------------------------------*
* Keyboard report send buffer
*------------------------------------------------------------------*/
#define KBUF_SIZE 16
static report_keyboard_t kbuf[KBUF_SIZE];
static uint8_t kbuf_head = 0;
static uint8_t kbuf_tail = 0;
void host_vusb_keyboard_send()
void host_vusb_keyboard_send(void)
{
while (usbInterruptIsReady() && kbuf_head != kbuf_tail) {
usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
@ -20,30 +113,32 @@ void host_vusb_keyboard_send()
}
}
void host_keyboard_send(report_keyboard_t *report)
void host_send_keyboard_report(void)
{
uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
if (next != kbuf_tail) {
kbuf[kbuf_head] = *report;
kbuf[kbuf_head] = *keyboard_report;
kbuf_head = next;
print("kbuf: "); phex(kbuf_head); phex(kbuf_tail); print("\n");
} else {
print("kbuf: full\n");
debug("kbuf: full\n");
}
}
void host_mouse_send(report_mouse_t *report)
{
if (usbInterruptIsReady3()) {
usbSetInterrupt3((void *)report, sizeof(*report));
} else {
print("Int3 not ready\n");
debug("Int3 not ready\n");
}
}
/*------------------------------------------------------------------*
* Request from host *
*------------------------------------------------------------------*/
static struct {
uint16_t len;
enum {
@ -52,47 +147,36 @@ static struct {
} kind;
} last_req;
uint8_t host_keyboard_led = 0;
static uchar idleRate;
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
//print("Setup: ");
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
/*
print("CLASS: ");
phex(rq->bRequest); print(" ");
phex16(rq->wValue.word); print(" ");
phex16(rq->wIndex.word); print(" ");
phex16(rq->wLength.word); print(" ");
*/
if(rq->bRequest == USBRQ_HID_GET_REPORT){
print(" GET_REPORT");
debug(" GET_REPORT");
/* we only have one report type, so don't look at wValue */
usbMsgPtr = (void *)keyboard_report;
return sizeof(*keyboard_report);
}else if(rq->bRequest == USBRQ_HID_GET_IDLE){
print(" GET_IDLE: ");
phex(idleRate);
debug(" GET_IDLE: ");
debug_hex(idleRate);
usbMsgPtr = &idleRate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
print(" SET_IDLE: ");
phex(idleRate);
debug(" SET_IDLE: ");
debug_hex(idleRate);
}else if(rq->bRequest == USBRQ_HID_SET_REPORT){
//print(" SET_REPORT: ");
//debug(" SET_REPORT: ");
if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
last_req.kind = SET_LED;
last_req.len = rq->wLength.word;
}
return USB_NO_MSG; // to get data in usbFunctionWrite
}
print("\n");
debug("\n");
}else{
print("VENDOR\n");
debug("VENDOR\n");
/* no vendor specific requests implemented */
}
return 0; /* default for not implemented requests: return no data back to host */
@ -105,8 +189,8 @@ uchar usbFunctionWrite(uchar *data, uchar len)
}
switch (last_req.kind) {
case SET_LED:
//print("SET_LED\n");
host_keyboard_led = data[0];
//debug("SET_LED\n");
keyboard_led = data[0];
last_req.len = 0;
return 1;
break;
@ -119,6 +203,16 @@ uchar usbFunctionWrite(uchar *data, uchar len)
}
/*------------------------------------------------------------------*
* Descriptors *
*------------------------------------------------------------------*/
/*
* Report Descriptor for keyboard
*
* from an example in HID spec appendix
*/
PROGMEM uchar keyboard_hid_report[] = {
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x06, // Usage (Keyboard),
@ -154,10 +248,14 @@ PROGMEM uchar keyboard_hid_report[] = {
0xc0 // End Collection
};
// Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
// http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
// http://www.keil.com/forum/15671/
// http://www.microsoft.com/whdc/device/input/wheel.mspx
/*
* Report Descriptor for mouse
*
* Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
* http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
* http://www.keil.com/forum/15671/
* http://www.microsoft.com/whdc/device/input/wheel.mspx
*/
PROGMEM uchar mouse_hid_report[] = {
/* from HID 1.11 spec example */
0x05, 0x01, // Usage Page (Generic Desktop),
@ -261,7 +359,11 @@ PROGMEM uchar mouse_hid_report[] = {
};
/* Descriptor for compite device: Keyboard + Mouse */
/*
* Descriptor for compite device: Keyboard + Mouse
*
* contains: device, interface, HID and endpoint descriptors
*/
#if USB_CFG_DESCR_PROPS_CONFIGURATION
PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
@ -343,16 +445,17 @@ PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor
};
#endif
USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
{
usbMsgLen_t len = 0;
print("usbFunctionDescriptor: ");
phex(rq->bmRequestType); print(" ");
phex(rq->bRequest); print(" ");
phex16(rq->wValue.word); print(" ");
phex16(rq->wIndex.word); print(" ");
phex16(rq->wLength.word); print("\n");
debug("usbFunctionDescriptor: ");
debug_hex(rq->bmRequestType); debug(" ");
debug_hex(rq->bRequest); debug(" ");
debug_hex16(rq->wValue.word); debug(" ");
debug_hex16(rq->wIndex.word); debug(" ");
debug_hex16(rq->wLength.word); debug("\n");
switch (rq->wValue.bytes[1]) {
#if USB_CFG_DESCR_PROPS_CONFIGURATION
@ -379,6 +482,6 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
}
break;
}
print("desc len: "); phex(len); print("\n");
debug("desc len: "); debug_hex(len); debug("\n");
return len;
}

View file

@ -5,7 +5,7 @@
#include <stdbool.h>
#include <avr/pgmspace.h>
#include "usb_keycodes.h"
#include "keyboard.h"
#include "host.h"
#include "print.h"
#include "debug.h"
#include "util.h"
@ -185,5 +185,5 @@ uint8_t keymap_fn_keycode(uint8_t fn_bits)
// define a condition to enter special function mode
bool keymap_is_special_mode(uint8_t fn_bits)
{
return keyboard_get_mods() == (BIT_LSHIFT | BIT_RSHIFT) || keyboard_get_mods() == (BIT_LCTRL | BIT_RSHIFT);
return host_get_mods() == (BIT_LSHIFT | BIT_RSHIFT) || host_get_mods() == (BIT_LCTRL | BIT_RSHIFT);
}

View file

@ -19,7 +19,6 @@
#include "matrix_skel.h"
#include "keymap_skel.h"
#include "mousekey.h"
#include "keyboard.h"
#include "layer.h"
#include "print.h"
#include "debug.h"
@ -27,11 +26,13 @@
#include "host.h"
#include "host_vusb.h"
#include "timer.h"
#include "led.h"
#include "keyboard.h"
#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
#define DEBUGP(x) do { PORTC = x; } while (0)
static uint8_t last_led = 0;
//static uint8_t last_led = 0;
int main(void)
{
DEBUGP_INIT();
@ -49,8 +50,7 @@ int main(void)
print_enable = true;
//debug_enable = true;
timer_init();
matrix_init();
keyboard_init();
/* enforce re-enumeration, do this while interrupts are disabled! */
usbDeviceDisconnect();
@ -62,7 +62,7 @@ int main(void)
usbDeviceConnect();
sei();
uint8_t fn_bits = 0;
//uint8_t fn_bits = 0;
while (1) { /* main event loop */
DEBUGP(0x01);
wdt_reset();
@ -70,10 +70,13 @@ int main(void)
host_vusb_keyboard_send();
DEBUGP(0x02);
keyboard_proc();
DEBUGP(0x03);
/*
matrix_scan();
fn_bits = 0;
keyboard_swap_report();
keyboard_clear_report();
host_swap_keyboard_report();
host_clear_keyboard_report();
mousekey_clear_report();
for (int row = 0; row < matrix_rows(); row++) {
for (int col = 0; col < matrix_cols(); col++) {
@ -84,10 +87,10 @@ int main(void)
// do nothing
}
else if (IS_MOD(code)) {
keyboard_add_mod_bit(MOD_BIT(code));
host_add_mod_bit(MOD_BIT(code));
}
else if (IS_KEY(code)) {
keyboard_add_key(code);
host_add_key(code);
}
else if (IS_FN(code)) {
fn_bits |= FN_BIT(code);
@ -103,13 +106,14 @@ int main(void)
DEBUGP(0x03);
layer_switching(fn_bits);
if (matrix_is_modified()) {
keyboard_send();
host_send_keyboard_report();
}
mousekey_send();
if (last_led != host_keyboard_led) {
keyboard_set_led(host_keyboard_led);
last_led = host_keyboard_led;
if (last_led != host_keyboard_led()) {
led_set(host_keyboard_led());
last_led = host_keyboard_led();
}
*/
}
}

View file

@ -9,7 +9,6 @@
#include "util.h"
#include "debug.h"
#include "ps2.h"
#include "keyboard.h"
#include "matrix_skel.h"

View file

@ -1,102 +0,0 @@
#include <stdint.h>
#include <util/delay.h>
#include "usb_keycodes.h"
#include "host.h"
#include "timer.h"
#include "print.h"
#include "mousekey.h"
static report_mouse_t report;
static report_mouse_t report_prev;
static uint8_t mousekey_repeat = 0;
/*
* TODO: fix acceleration algorithm
* see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
*/
#ifndef MOUSEKEY_DELAY_TIME
# define MOUSEKEY_DELAY_TIME 255
#endif
static inline uint8_t move_unit(void)
{
uint8_t unit = (10 + (mousekey_repeat));
return unit > 127 ? 127 : unit;
}
void mousekey_decode(uint8_t code)
{
if (code == KB_MS_UP) report.y -= move_unit();
else if (code == KB_MS_DOWN) report.y += move_unit();
else if (code == KB_MS_LEFT) report.x -= move_unit();
else if (code == KB_MS_RIGHT) report.x += move_unit();
else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
/*
else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
else if (code == KB_MS_WH_UP) report.v += 1;
else if (code == KB_MS_WH_DOWN) report.v -= 1;
else if (code == KB_MS_WH_LEFT) report.h -= 1;
else if (code == KB_MS_WH_RIGHT)report.h += 1;
*/
}
bool mousekey_changed(void)
{
return (report.buttons != report_prev.buttons ||
report.x != report_prev.x ||
report.y != report_prev.y ||
report.x || report.y);
//return (report.buttons != report_prev.buttons || report.x || report.y);
}
void mousekey_send(void)
{
static uint16_t last_timer = 0;
if (!mousekey_changed()) {
mousekey_repeat = 0;
return;
}
// send immediately when buttun state is changed
if (report.buttons == report_prev.buttons) {
// TODO: delay parameter setting
if ((timer_elapsed(last_timer) < (mousekey_repeat == 1 ? 20 : 5))) {
return;
}
}
if (report.x && report.y) {
report.x *= 0.7;
report.y *= 0.7;
}
/*
print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
print("timer: "); phex16(timer_read()); print("\n");
print("last_timer: "); phex16(last_timer); print("\n");
print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
*/
host_mouse_send(&report);
report_prev.buttons = report.buttons;
report_prev.x = report.x;
report_prev.y = report.y;
if (mousekey_repeat != 0xFF) mousekey_repeat++;
last_timer = timer_read();
mousekey_clear_report();
}
void mousekey_clear_report(void)
{
report.buttons = 0;
report.x = 0;
report.y = 0;
}

View file

@ -1,12 +0,0 @@
#ifndef MOUSEKEY_H
#define MOUSEKEY_H
#include <stdbool.h>
#include "host.h"
void mousekey_decode(uint8_t code);
bool mousekey_changed(void);
void mousekey_send(void);
void mousekey_clear_report(void);
#endif