#include "debug.h"
#include "adb.h"
#include "matrix.h"
+#include "report.h"
+#include "host.h"
#if (MATRIX_COLS > 16)
static bool is_modified = false;
+static report_mouse_t mouse_report = {};
// matrix state buffer(1:on, 0:off)
#if (MATRIX_COLS <= 8)
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
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)
//
// [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();