Add PS/2 mouse support to connect TrackPoint Unit.
Change build options: Makefile and config.h. See README.example_keyboards
parent
1ed336a064
commit
2a562a4191
@ -0,0 +1,40 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#define VENDOR_ID 0xFEED
|
||||
#define PRODUCT_ID 0xCAFE
|
||||
#define MANUFACTURER t.m.k.
|
||||
#define PRODUCT HHKB mod
|
||||
#define DESCRIPTION t.m.k. keyboard firmware for HHKB mod
|
||||
|
||||
/* controller */
|
||||
#include "controller_teensy.h"
|
||||
|
||||
/* matrix size */
|
||||
#define MATRIX_ROWS 8
|
||||
#define MATRIX_COLS 8
|
||||
|
||||
/* USB NKey Rollover */
|
||||
#ifdef USB_NKRO_ENABLE
|
||||
#endif
|
||||
|
||||
/* mouse keys */
|
||||
#ifdef MOUSEKEY_ENABLE
|
||||
# define MOUSEKEY_DELAY_TIME 192
|
||||
#endif
|
||||
|
||||
/* PS/2 mouse */
|
||||
#ifdef PS2_MOUSE_ENABLE
|
||||
/*
|
||||
# define PS2_CLOCK_PORT PORTF
|
||||
# define PS2_CLOCK_PIN PINF
|
||||
# define PS2_CLOCK_DDR DDRF
|
||||
# define PS2_CLOCK_BIT 0
|
||||
# define PS2_DATA_PORT PORTF
|
||||
# define PS2_DATA_PIN PINF
|
||||
# define PS2_DATA_DDR DDRF
|
||||
# define PS2_DATA_BIT 1
|
||||
*/
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,12 +0,0 @@
|
||||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H 1
|
||||
|
||||
#include "controller_teensy.h"
|
||||
|
||||
|
||||
/* matrix row size */
|
||||
#define MATRIX_ROWS 8
|
||||
/* matrix column size */
|
||||
#define MATRIX_COLS 8
|
||||
|
||||
#endif
|
@ -0,0 +1,38 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#define VENDOR_ID 0xFEED
|
||||
#define PRODUCT_ID 0xBEEF
|
||||
#define MANUFACTURER t.m.k.
|
||||
#define PRODUCT Macway mod
|
||||
#define DESCRIPTION t.m.k. keyboard firmware for Macway mod
|
||||
|
||||
/* controller */
|
||||
#include "controller_teensy.h"
|
||||
|
||||
/* matrix size */
|
||||
#define MATRIX_ROWS 9
|
||||
#define MATRIX_COLS 8
|
||||
|
||||
/* USB NKey Rollover */
|
||||
#ifdef USB_NKRO_ENABLE
|
||||
#endif
|
||||
|
||||
/* mouse keys */
|
||||
#ifdef MOUSEKEY_ENABLE
|
||||
# define MOUSEKEY_DELAY_TIME 192
|
||||
#endif
|
||||
|
||||
/* PS/2 mouse */
|
||||
#ifdef PS2_MOUSE_ENABLE
|
||||
# define PS2_CLOCK_PORT PORTF
|
||||
# define PS2_CLOCK_PIN PINF
|
||||
# define PS2_CLOCK_DDR DDRF
|
||||
# define PS2_CLOCK_BIT 0
|
||||
# define PS2_DATA_PORT PORTF
|
||||
# define PS2_DATA_PIN PINF
|
||||
# define PS2_DATA_DDR DDRF
|
||||
# define PS2_DATA_BIT 1
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,12 +0,0 @@
|
||||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H 1
|
||||
|
||||
#include "controller_teensy.h"
|
||||
|
||||
|
||||
/* matrix row size */
|
||||
#define MATRIX_ROWS 9
|
||||
/* matrix column size */
|
||||
#define MATRIX_COLS 8
|
||||
|
||||
#endif
|
@ -0,0 +1,74 @@
|
||||
#include <stdint.h>
|
||||
#include <util/delay.h>
|
||||
#include "usb_keycodes.h"
|
||||
#include "usb_mouse.h"
|
||||
#include "mousekey.h"
|
||||
|
||||
|
||||
static int8_t mousekey_x = 0;
|
||||
static int8_t mousekey_y = 0;
|
||||
static int8_t mousekey_v = 0;
|
||||
static int8_t mousekey_h = 0;
|
||||
static uint8_t mousekey_btn = 0;
|
||||
static uint8_t mousekey_btn_prev = 0;
|
||||
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)
|
||||
{
|
||||
return 10 + (mousekey_repeat < 50 ? mousekey_repeat/5 : 10);
|
||||
}
|
||||
|
||||
void mousekey_decode(uint8_t code)
|
||||
{
|
||||
if (code == MS_UP) mousekey_y -= move_unit();
|
||||
else if (code == MS_DOWN) mousekey_y += move_unit();
|
||||
else if (code == MS_LEFT) mousekey_x -= move_unit();
|
||||
else if (code == MS_RGHT) mousekey_x += move_unit();
|
||||
else if (code == MS_BTN1) mousekey_btn |= MOUSE_BTN1;
|
||||
else if (code == MS_BTN2) mousekey_btn |= MOUSE_BTN2;
|
||||
else if (code == MS_BTN3) mousekey_btn |= MOUSE_BTN3;
|
||||
else if (code == MS_BTN4) mousekey_btn |= MOUSE_BTN4;
|
||||
else if (code == MS_BTN5) mousekey_btn |= MOUSE_BTN5;
|
||||
else if (code == MS_WH_U) mousekey_v += 1;
|
||||
else if (code == MS_WH_D) mousekey_v -= 1;
|
||||
else if (code == MS_WH_L) mousekey_h -= 1;
|
||||
else if (code == MS_WH_R) mousekey_h += 1;
|
||||
}
|
||||
|
||||
bool mousekey_changed(void)
|
||||
{
|
||||
return (mousekey_x || mousekey_y || mousekey_v || mousekey_h || mousekey_btn != mousekey_btn_prev);
|
||||
}
|
||||
|
||||
void mousekey_usb_send(void)
|
||||
{
|
||||
if (mousekey_changed()) {
|
||||
mousekey_btn_prev = mousekey_btn;
|
||||
if (mousekey_x && mousekey_y)
|
||||
usb_mouse_send(mousekey_x*0.7, mousekey_y*0.7, mousekey_v, mousekey_h, mousekey_btn);
|
||||
else
|
||||
usb_mouse_send(mousekey_x, mousekey_y, mousekey_v, mousekey_h, mousekey_btn);
|
||||
|
||||
usb_mouse_print(mousekey_x, mousekey_y, mousekey_v, mousekey_h, mousekey_btn);
|
||||
|
||||
_delay_ms(MOUSEKEY_DELAY_TIME >> (mousekey_repeat < 5 ? mousekey_repeat : 4));
|
||||
mousekey_repeat++;
|
||||
} else {
|
||||
mousekey_repeat = 0;
|
||||
}
|
||||
mousekey_x = 0;
|
||||
mousekey_y = 0;
|
||||
mousekey_v = 0;
|
||||
mousekey_h = 0;
|
||||
mousekey_btn = 0;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#ifndef MOUSEKEY_H
|
||||
#define MOUSEKEY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
void mousekey_decode(uint8_t code);
|
||||
bool mousekey_changed(void);
|
||||
void mousekey_usb_send(void);
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,248 @@
|
||||
/*
|
||||
Copyright (c) 2010 Jun WAKO <wakojun@gmail.com>
|
||||
|
||||
This software is licensed with a Modified BSD License.
|
||||
All of this is supposed to be Free Software, Open Source, DFSG-free,
|
||||
GPL-compatible, and OK to use in both free and proprietary applications.
|
||||
Additions and corrections to this file are welcome.
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
* Neither the name of the copyright holders nor the names of
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include "ps2.h"
|
||||
#include "print.h"
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
static inline void clock_lo(void);
|
||||
static inline void clock_hi(void);
|
||||
static inline bool clock_in(void);
|
||||
static inline void data_lo(void);
|
||||
static inline void data_hi(void);
|
||||
static inline bool data_in(void);
|
||||
static inline uint16_t wait_clock_lo(uint16_t us);
|
||||
static inline uint16_t wait_clock_hi(uint16_t us);
|
||||
static inline uint16_t wait_data_lo(uint16_t us);
|
||||
static inline uint16_t wait_data_hi(uint16_t us);
|
||||
|
||||
|
||||
/*
|
||||
Primitive PS/2 Library for AVR
|
||||
==============================
|
||||
Host side is only supported now.
|
||||
|
||||
|
||||
I/O control
|
||||
-----------
|
||||
High state is asserted by input with pull up.
|
||||
|
||||
|
||||
PS/2 References
|
||||
---------------
|
||||
http://www.computer-engineering.org/ps2protocol/
|
||||
http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
|
||||
*/
|
||||
|
||||
|
||||
#define WAIT(stat, us, err) do { \
|
||||
if (!wait_##stat(us)) { \
|
||||
ps2_error = err; \
|
||||
goto ERROR; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define WAIT_NORETRY(stat, us, err) do { \
|
||||
if (!wait_##stat(us)) { \
|
||||
ps2_error = err; \
|
||||
return 0; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
uint8_t ps2_error = PS2_ERR_NONE;
|
||||
|
||||
|
||||
void ps2_host_init(void)
|
||||
{
|
||||
/* inhibit */
|
||||
clock_lo();
|
||||
data_hi();
|
||||
}
|
||||
|
||||
uint8_t ps2_host_send(uint8_t data)
|
||||
{
|
||||
bool parity = true;
|
||||
ps2_error = 0;
|
||||
|
||||
/* request to send */
|
||||
clock_lo();
|
||||
data_lo();
|
||||
_delay_us(100);
|
||||
/* start bit [1] */
|
||||
clock_hi();
|
||||
WAIT(clock_lo, 15000, 1);
|
||||
/* data [2-9] */
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (data&(1<<i)) {
|
||||
parity = !parity;
|
||||
data_hi();
|
||||
} else {
|
||||
data_lo();
|
||||
}
|
||||
WAIT(clock_hi, 50, 2);
|
||||
WAIT(clock_lo, 50, 3);
|
||||
}
|
||||
/* parity [10] */
|
||||
if (parity) { data_hi(); } else { data_lo(); }
|
||||
WAIT(clock_hi, 50, 4);
|
||||
WAIT(clock_lo, 50, 5);
|
||||
/* stop bit [11] */
|
||||
data_hi();
|
||||
/* ack [12] */
|
||||
WAIT(data_lo, 50, 6);
|
||||
WAIT(clock_lo, 50, 7);
|
||||
WAIT(clock_hi, 50, 8);
|
||||
WAIT(data_hi, 50, 9);
|
||||
|
||||
/* inhibit device to send */
|
||||
clock_lo();
|
||||
|
||||
return 1;
|
||||
ERROR:
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t ps2_host_recv(void)
|
||||
{
|
||||
uint8_t data = 0;
|
||||
bool parity = true;
|
||||
ps2_error = 0;
|
||||
|
||||
/* cancel to sync */
|
||||
clock_lo();
|
||||
_delay_us(100);
|
||||
|
||||
/* release lines(idle state) */
|
||||
clock_hi();
|
||||
data_hi();
|
||||
|
||||
/* start bit [1] */
|
||||
WAIT(clock_lo, 20000, 1);
|
||||
WAIT(data_lo, 1, 2);
|
||||
WAIT(clock_hi, 50, 3);
|
||||
|
||||
/* data [2-9] */
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
WAIT(clock_lo, 50, 4);
|
||||
if (data_in()) {
|
||||
parity = !parity;
|
||||
data |= (1<<i);
|
||||
}
|
||||
WAIT(clock_hi, 50, 5);
|
||||
}
|
||||
|
||||
/* parity [10] */
|
||||
WAIT(clock_lo, 50, 6);
|
||||
if (data_in() != parity) {
|
||||
ps2_error = PS2_ERR_PARITY;
|
||||
goto ERROR;
|
||||
}
|
||||
WAIT(clock_hi, 50, 7);
|
||||
|
||||
/* stop bit [11] */
|
||||
WAIT(clock_lo, 50, 8);
|
||||
WAIT(data_hi, 1, 9);
|
||||
WAIT(clock_hi, 50, 10);
|
||||
|
||||
/* inhibit device to send */
|
||||
clock_lo();
|
||||
|
||||
return data;
|
||||
ERROR:
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static inline void clock_lo()
|
||||
{
|
||||
PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
|
||||
PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
|
||||
}
|
||||
static inline void clock_hi()
|
||||
{
|
||||
/* input with pull up */
|
||||
PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
|
||||
PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
|
||||
}
|
||||
static inline bool clock_in()
|
||||
{
|
||||
PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
|
||||
PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
|
||||
return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
|
||||
}
|
||||
static inline void data_lo()
|
||||
{
|
||||
PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
|
||||
PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
|
||||
}
|
||||
static inline void data_hi()
|
||||
{
|
||||
/* input with pull up */
|
||||
PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
|
||||
PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
|
||||
}
|
||||
static inline bool data_in()
|
||||
{
|
||||
PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
|
||||
PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
|
||||
return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
|
||||
}
|
||||
|
||||
static inline uint16_t wait_clock_lo(uint16_t us)
|
||||
{
|
||||
while (clock_in() && us) { asm(""); _delay_us(1); us--; }
|
||||
return us;
|
||||
}
|
||||
static inline uint16_t wait_clock_hi(uint16_t us)
|
||||
{
|
||||
while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
|
||||
return us;
|
||||
}
|
||||
static inline uint16_t wait_data_lo(uint16_t us)
|
||||
{
|
||||
while (data_in() && us) { asm(""); _delay_us(1); us--; }
|
||||
return us;
|
||||
}
|
||||
static inline uint16_t wait_data_hi(uint16_t us)
|
||||
{
|
||||
while (!data_in() && us) { asm(""); _delay_us(1); us--; }
|
||||
return us;
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright (c) 2010 Jun WAKO <wakojun@gmail.com>
|
||||
|
||||
This software is licensed with a Modified BSD License.
|
||||
All of this is supposed to be Free Software, Open Source, DFSG-free,
|
||||
GPL-compatible, and OK to use in both free and proprietary applications.
|
||||
Additions and corrections to this file are welcome.
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
* Neither the name of the copyright holders nor the names of
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef PS2_H
|
||||
#define PS2_H
|
||||
/*
|
||||
* Primitive PS/2 Library for AVR
|
||||
*/
|
||||
|
||||
|
||||
/* port settings for clock and data line */
|
||||
#if !(defined(PS2_CLOCK_PORT) && \
|
||||
defined(PS2_CLOCK_PIN) && \
|
||||
defined(PS2_CLOCK_DDR) && \
|
||||
defined(PS2_CLOCK_BIT))
|
||||
# error "PS/2 clock port setting is required in config.h"
|
||||
#endif
|
||||
|
||||
#if !(defined(PS2_DATA_PORT) && \
|
||||
defined(PS2_DATA_PIN) && \
|
||||
defined(PS2_DATA_DDR) && \
|
||||
defined(PS2_DATA_BIT))
|
||||
# error "PS/2 data port setting is required in config.h"
|
||||
#endif
|
||||
|
||||
#define PS2_ERR_NONE 0
|
||||
#define PS2_ERR_PARITY 0x10
|
||||
|
||||
|
||||
extern uint8_t ps2_error;
|
||||
|
||||
/* host side */
|
||||
void ps2_host_init(void);
|
||||
uint8_t ps2_host_send(uint8_t);
|
||||
uint8_t ps2_host_recv(void);
|
||||
|
||||
/* TODO: device side */
|
||||
|
||||
#endif
|
@ -0,0 +1,161 @@
|
||||
#include <stdbool.h>
|
||||
#include<avr/io.h>
|
||||
#include<util/delay.h>
|
||||
#include "ps2.h"
|
||||
#include "ps2_mouse.h"
|
||||
#include "usb_mouse.h"
|
||||
|
||||
#define PS2_MOUSE_DEBUG
|
||||
#ifdef PS2_MOUSE_DEBUG
|
||||
# include "print.h"
|
||||
# include "debug.h"
|
||||
#else
|
||||
# define print(s)
|
||||
# define phex(h)
|
||||
# define phex16(h)
|
||||
#endif
|
||||
|
||||
/*
|
||||
TODO
|
||||
----
|
||||
- Error handling
|
||||
- Stream mode
|
||||
- Tracpoint command support: needed
|
||||
- Middle button + move = Wheel traslation
|
||||
*/
|
||||
uint8_t ps2_mouse_x = 0;
|
||||
uint8_t ps2_mouse_y = 0;
|
||||
uint8_t ps2_mouse_btn = 0;
|
||||
uint8_t ps2_mouse_error_count = 0;
|
||||
static uint8_t ps2_mouse_btn_prev = 0;
|
||||
|
||||
void ps2_mouse_init(void) {
|
||||
uint8_t rcv;
|
||||
|
||||
// Reset
|
||||
rcv = ps2_host_send(0xFF);
|
||||
print("ps2_mouse_init: send 0xFF: ");
|
||||
phex(ps2_error); print("\n");
|
||||
|
||||
// ACK
|
||||
rcv = ps2_host_recv();
|
||||
print("ps2_mouse_init: read ACK: ");
|
||||
phex(rcv); phex(ps2_error); print("\n");
|
||||
|
||||
// BAT takes some time
|
||||
_delay_ms(100);
|
||||
rcv = ps2_host_recv();
|
||||
print("ps2_mouse_init: read BAT: ");
|
||||
phex(rcv); phex(ps2_error); print("\n");
|
||||
|
||||
// Device ID
|
||||
rcv = ps2_host_recv();
|
||||
print("ps2_mouse_init: read DevID: ");
|
||||
phex(rcv); phex(ps2_error); print("\n");
|
||||
|
||||
// Enable data reporting
|
||||
ps2_host_send(0xF4);
|
||||
print("ps2_mouse_init: send 0xF4: ");
|
||||
phex(ps2_error); print("\n");
|
||||
|
||||
// ACK
|
||||
rcv = ps2_host_recv();
|
||||
print("ps2_mouse_init: read ACK: ");
|
||||
phex(rcv); phex(ps2_error); print("\n");
|
||||
|
||||
// Set Remote mode
|
||||
ps2_host_send(0xF0);
|
||||
print("ps2_mouse_init: send 0xF0: ");
|
||||
phex(ps2_error); print("\n");
|
||||
|
||||
// ACK
|
||||
rcv = ps2_host_recv();
|
||||
print("ps2_mouse_init: read ACK: ");
|
||||
phex(rcv); phex(ps2_error); print("\n");
|
||||
|
||||
if (ps2_error) ps2_mouse_error_count++;
|
||||
}
|
||||
|
||||
/*
|
||||
Data format:
|
||||
bit: 7 6 5 4 3 2 1 0
|
||||
-----------------------------------------------------------------------
|
||||
0 btn: Yovflw Xovflw Ysign Xsign 1 Middle Right Left
|
||||
1 x: X movement(0-255)
|
||||
2 y: Y movement(0-255)
|
||||
*/
|
||||
void ps2_mouse_read(void)
|
||||
{
|
||||
uint8_t rcv;
|
||||
|
||||
ps2_host_send(0xEB);
|
||||
rcv=ps2_host_recv();
|
||||
if(rcv==0xFA) {
|
||||
ps2_mouse_btn = ps2_host_recv();
|
||||
ps2_mouse_x = ps2_host_recv();
|
||||
ps2_mouse_y = ps2_host_recv();
|
||||
}
|
||||
if (ps2_error) ps2_mouse_error_count++;
|
||||
}
|
||||
|
||||
bool ps2_mouse_changed(void)
|
||||
{
|
||||
return (ps2_mouse_x || ps2_mouse_y || (ps2_mouse_btn & PS2_MOUSE_BTN_MASK) != ps2_mouse_btn_prev);
|
||||
}
|
||||
|
||||
#define PS2_MOUSE_SCROLL_BUTTON 0x04
|
||||
void ps2_mouse_usb_send(void)
|
||||
{
|
||||
static bool scrolled = false;
|
||||
if (ps2_mouse_changed()) {
|
||||
int8_t x, y, v, h;
|
||||
x = y = v = h = 0;
|
||||
|
||||
// convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
|
||||
if (ps2_mouse_btn & (1<<PS2_MOUSE_X_SIGN))
|
||||
x = ps2_mouse_x > 128 ? (int8_t)ps2_mouse_x : -127;
|
||||
else
|
||||
x = ps2_mouse_x < 128 ? (int8_t)ps2_mouse_x : 127;
|
||||
|
||||
if (ps2_mouse_btn & (1<<PS2_MOUSE_Y_SIGN))
|
||||
y = ps2_mouse_y > 128 ? (int8_t)ps2_mouse_y : -127;
|
||||
else
|
||||
y = ps2_mouse_y < 128 ? (int8_t)ps2_mouse_y : 127;
|
||||
|
||||
// Y is needed to reverse
|
||||
y = -y;
|
||||
|
||||
if (ps2_mouse_btn & PS2_MOUSE_SCROLL_BUTTON) {
|
||||
// scroll
|
||||
if (x > 0 || x < 0) h = (x > 64 ? 64 : (x < -64 ? -64 :x));
|
||||
if (y > 0 || y < 0) v = (y > 64 ? 64 : (y < -64 ? -64 :y));
|
||||
if (h || v) {
|
||||
scrolled = true;
|
||||
usb_mouse_send(0,0, -v/16, h/16, 0);
|
||||
_delay_ms(100);
|
||||
}
|
||||
} else if (!scrolled && (ps2_mouse_btn_prev & PS2_MOUSE_SCROLL_BUTTON)) {
|
||||
usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON);
|
||||
_delay_ms(100);
|
||||
usb_mouse_send(0,0,0,0, 0);
|
||||
} else {
|
||||
scrolled = false;
|
||||
usb_mouse_send(x, y, 0, 0, ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
|
||||
}
|
||||
|
||||
ps2_mouse_btn_prev = (ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
|
||||
ps2_mouse_print();
|
||||
}
|
||||
ps2_mouse_x = 0;
|
||||
ps2_mouse_y = 0;
|
||||
ps2_mouse_btn = 0;
|
||||
}
|
||||
|
||||
void ps2_mouse_print(void)
|
||||
{
|
||||
if (!debug_mouse) return;
|
||||
print("ps2_mouse[btn|x y]: ");
|
||||
phex(ps2_mouse_btn); print("|");
|
||||
phex(ps2_mouse_x); print(" ");
|
||||
phex(ps2_mouse_y); print("\n");
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#ifndef PS2_MOUSE_H
|
||||
#define PS2_MOUSE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define PS2_MOUSE_BTN_MASK 0x07
|
||||
#define PS2_MOUSE_BTN_LEFT 0
|
||||
#define PS2_MOUSE_BTN_RIGHT 1
|
||||
#define PS2_MOUSE_BTN_MIDDLE 2
|
||||
#define PS2_MOUSE_X_SIGN 4
|
||||
#define PS2_MOUSE_Y_SIGN 5
|
||||
#define PS2_MOUSE_X_OVFLW 6
|
||||
#define PS2_MOUSE_Y_OVFLW 7
|
||||
|
||||
extern uint8_t ps2_mouse_x;
|
||||
extern uint8_t ps2_mouse_y;
|
||||
extern uint8_t ps2_mouse_btn;
|
||||
extern uint8_t ps2_mouse_error_count;
|
||||
|
||||
void ps2_mouse_init(void);
|
||||
void ps2_mouse_read(void);
|
||||
bool ps2_mouse_changed(void);
|
||||
void ps2_mouse_usb_send(void);
|
||||
void ps2_mouse_print(void);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue