]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Add mouse support to ADB
authorMekaniserad Apelsin <mek@pels.in>
Sun, 26 Apr 2015 06:08:54 +0000 (08:08 +0200)
committerMekaniserad Apelsin <mek@pels.in>
Sun, 26 Apr 2015 06:57:00 +0000 (08:57 +0200)
Adding the makefile options ADB_MOUSE_ENABLE and ADB_MOUSE_ACCMAX.
Might have gone overboard with comments, and tried but failed at not
adding more than necessary outside the converter/adb_usb/ folder.

converter/adb_usb/Makefile
converter/adb_usb/Makefile.pjrc
converter/adb_usb/matrix.c
tmk_core/common/keyboard.c
tmk_core/protocol.mk
tmk_core/protocol/adb.c
tmk_core/protocol/adb.h
tmk_core/protocol/pjrc.mk

index a16873dc03c8df1a255d741a62bd1fbd30eff230..2369ee70ac7279562478fc3861f94365f272dfd5 100644 (file)
@@ -122,6 +122,10 @@ CONSOLE_ENABLE = yes       # Console for debug(+400)
 COMMAND_ENABLE = yes    # Commands for debug and configuration
 #SLEEP_LED_ENABLE = yes  # Breathing sleep LED during USB suspend
 #NKRO_ENABLE = yes     # USB Nkey Rollover
+ADB_MOUSE_ENABLE = yes
+
+# ADB Mice need acceleration for todays much bigger screens. 
+OPT_DEFS += -DADB_MOUSE_MAXACC=8
 
 
 # Optimize size but this may cause error "relocation truncated to fit"
index b54ea142c5c1f8985b7000560c164918e4ddddc6..c320c09d255909d6b25986f5be7f8a3bd43cada3 100644 (file)
@@ -57,6 +57,10 @@ CONSOLE_ENABLE = yes    # Console for debug
 COMMAND_ENABLE = yes    # Commands for debug and configuration
 #SLEEP_LED_ENABLE = yes  # Breathing sleep LED during USB suspend
 #NKRO_ENABLE = yes     # USB Nkey Rollover(+500)
+ADB_MOUSE_ENABLE = yes
+
+# ADB Mice need acceleration for todays much bigger screens. 
+OPT_DEFS += -DADB_MOUSE_MAXACC=8
 
 
 # Search Path
index 405391865fe4861b296320726bd18f3f55c07579..6220ee6e1c001418f19b381c309ea6b5b4d88185 100644 (file)
@@ -27,6 +27,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "debug.h"
 #include "adb.h"
 #include "matrix.h"
+#include "report.h"
+#include "host.h"
 
 
 #if (MATRIX_COLS > 16)
@@ -38,6 +40,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 static bool is_modified = false;
+static report_mouse_t mouse_report = {};
 
 // matrix state buffer(1:on, 0:off)
 #if (MATRIX_COLS <= 8)
@@ -86,6 +89,64 @@ void matrix_init(void)
     return;
 }
 
+#ifdef ADB_MOUSE_ENABLE
+
+#ifdef MAX
+#undef MAX
+#endif
+#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
+
+void adb_mouse_task(void)
+{
+    uint16_t codes;
+    int16_t x, y;
+    static int8_t mouseacc; 
+    _delay_ms(12);  // delay for preventing overload of poor ADB keyboard controller
+    codes = adb_host_mouse_recv();
+    // If nothing received reset mouse acceleration, and quit. 
+    if (!codes) {
+        mouseacc = 1;
+        return;
+    };
+    // Bit sixteen is button.
+    if (~codes & (1 << 15))
+        mouse_report.buttons |= MOUSE_BTN1;
+    if (codes & (1 << 15))
+        mouse_report.buttons &= ~MOUSE_BTN1;
+    // lower seven bits are movement, as signed int_7. 
+    // low byte is X-axis, high byte is Y. 
+    y = (codes>>8 & 0x3F);
+    x = (codes>>0 & 0x3F);
+    // bit seven and fifteen is negative
+    // usb does not use int_8, but int_7 (measuring distance) with sign-bit. 
+    if (codes & (1 << 6))
+          x = (x-0x40);
+    if (codes & (1 << 14))
+         y = (y-0x40);
+    // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
+    x *= mouseacc;
+    y *= mouseacc;
+    // Cap our two bytes per axis to one byte. 
+    // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
+        // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
+    mouse_report.x = -MAX(-MAX(x, -127), -127);
+    mouse_report.y = -MAX(-MAX(y, -127), -127);
+    if (debug_mouse) {
+            print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
+            print("adb_mouse raw: [");
+            phex(mouseacc); print(" ");
+            phex(mouse_report.buttons); print("|");
+            print_decs(mouse_report.x); print(" ");
+            print_decs(mouse_report.y); print("]\n");
+    }
+    // Send result by usb. 
+    host_mouse_send(&mouse_report);
+    // increase acceleration of mouse
+    mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
+    return;
+}
+#endif
+
 uint8_t matrix_scan(void)
 {
     /* extra_key is volatile and more convoluted than necessary because gcc refused
index f0ead604eccb87567c4984a67adf2bb7efa10434..b03b124d76a98af7bc41e32aa4a09b055c8bf392 100644 (file)
@@ -39,6 +39,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifdef SERIAL_MOUSE_ENABLE
 #include "serial_mouse.h"
 #endif
+#ifdef ADB_MOUSE_ENABLE
+#include "adb.h"
+#endif
 
 
 #ifdef MATRIX_HAS_GHOST
@@ -69,6 +72,9 @@ void keyboard_init(void)
 #ifdef SERIAL_MOUSE_ENABLE
     serial_mouse_init();
 #endif
+#ifdef ADB_MOUSE_ENABLE
+    adb_mouse_init();
+#endif
 
 
 #ifdef BOOTMAGIC_ENABLE
@@ -147,6 +153,10 @@ MATRIX_LOOP_END:
         serial_mouse_task();
 #endif
 
+#ifdef ADB_MOUSE_ENABLE
+        adb_mouse_task();
+#endif
+
     // update LED
     if (led_status != host_keyboard_leds()) {
         led_status = host_keyboard_leds();
index 726f658a0422df5ab9096469aaa1f5a240a4d57b..54913329e69ff0e9c9dcfde2445ad476d455b9c1 100644 (file)
@@ -46,5 +46,9 @@ ifdef SERIAL_MOUSE_USE_UART
     SRC += $(PROTOCOL_DIR)/serial_uart.c
 endif
 
+ifdef ADB_MOUSE_ENABLE
+        OPT_DEFS += -DADB_MOUSE_ENABLE -DMOUSE_ENABLE
+endif
+
 # Search Path
 VPATH += $(TMK_DIR)/protocol
index bbff66df03613bdd3d54232a53ad7a9dee64eb28..5c6c99b4fcc225fd4b5293eeafe1459271608a9c 100644 (file)
@@ -60,6 +60,7 @@ static inline void place_bit1(void);
 static inline void send_byte(uint8_t data);
 static inline uint16_t wait_data_lo(uint16_t us);
 static inline uint16_t wait_data_hi(uint16_t us);
+static inline uint16_t adb_host_dev_recv(uint8_t device);
 
 
 void adb_host_init(void)
@@ -121,12 +122,33 @@ bool adb_host_psw(void)
 //
 // [from Apple IIgs Hardware Reference Second Edition]
 
+enum {
+    ADDR_KEYB  = 0x20,
+    ADDR_MOUSE = 0x30
+};
+
 uint16_t adb_host_kbd_recv(void)
+{
+    return adb_host_dev_recv(ADDR_KEYB);
+}
+
+#ifdef ADB_MOUSE_ENABLE
+void adb_mouse_init(void) {
+           return;
+}
+
+uint16_t adb_host_mouse_recv(void)
+{
+    return adb_host_dev_recv(ADDR_MOUSE);
+}
+#endif
+
+static inline uint16_t adb_host_dev_recv(uint8_t device)
 {
     uint16_t data = 0;
     cli();
     attention();
-    send_byte(0x2C);            // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
+    send_byte(device|0x0C);     // Addr:Keyboard(0010)/Mouse(0011), Cmd:Talk(11), Register0(00)
     place_bit0();               // Stopbit(0)
     if (!wait_data_hi(500)) {    // Service Request(310us Adjustable Keyboard): just ignored
         sei();
index bfe598bbf1a1bba7f470d03f5b553b40086d7613..b4b3633cf450a5bd6ba37a3a952f692ce3e313a1 100644 (file)
@@ -56,7 +56,11 @@ POSSIBILITY OF SUCH DAMAGE.
 void     adb_host_init(void);
 bool     adb_host_psw(void);
 uint16_t adb_host_kbd_recv(void);
+uint16_t adb_host_mouse_recv(void);
 void     adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l);
 void     adb_host_kbd_led(uint8_t led);
+void     adb_mouse_task(void);
+void     adb_mouse_init(void);
+
 
 #endif
index 36585de7dfbf182b620b29b7b7acc5b60e8308fb..2b1ba2cbfbb9f4a79bda474eacfd7268fda555aa 100644 (file)
@@ -11,6 +11,10 @@ ifdef MOUSEKEY_ENABLE
     SRC += $(PJRC_DIR)/usb_mouse.c
 endif
 
+ifdef ADB_MOUSE_ENABLE
+    SRC += $(PJRC_DIR)/usb_mouse.c
+endif
+
 ifdef PS2_MOUSE_ENABLE
     SRC += $(PJRC_DIR)/usb_mouse.c
 endif