Created
December 26, 2024 02:25
-
-
Save chainq/995a66581c10c219264f51fcbad98b2d to your computer and use it in GitHub Desktop.
X68000 to USB Mouse adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sharp X68000 to USB mouse port adapter | |
// by Chain-Q | |
// | |
// based on: | |
// Sharp X68000 to USB keyboard and mouse converter | |
// by Zuofu | |
// | |
// Supports true USB protocol (even through hub), so all types mice may be used. | |
// | |
// Provided free of charge and without warranty by Zuofu and Chain-Q, you may use | |
// this code for either non-commercial or commercial purposes (e.g. sell on eBay), | |
// but please give credit. | |
// | |
// Requirements | |
// -Arduino Mini Pro (3.3V/8MHz): https://smile.amazon.com/dp/B004RF9LB8 (or equivalent) | |
// -Mini USB Host: https://smile.amazon.com/dp/B01EWW9R1E (or equivalent) | |
// note: above USB host board should have USB VBUS->3.3V connection removed and replaced with a connection from VBUS to RAW for full 5V USB compatibility | |
// -Mini DIN-6 connector | |
// -Some way to program above Arduino (typically a USB->Serial adapter) | |
// -Add USB Host Shield 2.0 library to Arduino (Tools->Manage Libraries...) | |
#include <hidboot.h> | |
#include <usbhub.h> | |
#include <SoftwareSerial.h> | |
//#define debugonserial 1 | |
// Satisfy IDE, which only needs to see the include statment in the ino. | |
#ifdef dobogusinclude | |
#include <spi4teensy3.h> | |
#endif | |
#include <SPI.h> | |
//Adjust this accordingly to your mouse DPI if you have issues, 4 is a good number for 300/600 modern mice, may need to increase | |
//if using a high-DPI 'gamer' mouse | |
#define MOUSE_DIVIDER 0x04 | |
SoftwareSerial x68kSerMouse (4, 5); // RX, TX (RX unused) | |
uint8_t rxbyte, last_rxbyte; | |
uint8_t mouse_left, mouse_right; | |
uint8_t leftGUI = 0; | |
int16_t mouse_dx, mouse_dy; | |
class MouseRptParser : public MouseReportParser | |
{ | |
protected: | |
void OnMouseMove(MOUSEINFO *mi); | |
void OnLeftButtonUp(MOUSEINFO *mi); | |
void OnLeftButtonDown(MOUSEINFO *mi); | |
void OnRightButtonUp(MOUSEINFO *mi); | |
void OnRightButtonDown(MOUSEINFO *mi); | |
void OnMiddleButtonUp(MOUSEINFO *mi); | |
void OnMiddleButtonDown(MOUSEINFO *mi); | |
}; | |
void MouseRptParser::OnMouseMove(MOUSEINFO *mi) | |
{ | |
mouse_dx += (mi->dX)/MOUSE_DIVIDER; | |
mouse_dy += (mi->dY)/MOUSE_DIVIDER; | |
}; | |
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) | |
{ | |
mouse_left = 0; | |
}; | |
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) | |
{ | |
mouse_left = 1; | |
}; | |
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi) | |
{ | |
mouse_right = 0; | |
}; | |
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi) | |
{ | |
mouse_right = 1; | |
}; | |
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) | |
{ | |
//Middle mouse unused | |
}; | |
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) | |
{ | |
//Middle mouse unused | |
}; | |
USB Usb; | |
USBHub Hub(&Usb); | |
HIDBoot<USB_HID_PROTOCOL_MOUSE> HidComposite(&Usb); | |
HIDBoot<USB_HID_PROTOCOL_MOUSE> HidMouse(&Usb); | |
MouseRptParser MousePrs; | |
uint8_t p_mouse_ctrl; | |
void setup() | |
{ | |
pinMode(3,INPUT_PULLUP); | |
Serial.begin(2400); | |
#if !defined(__MIPSEL__) | |
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection | |
#endif | |
x68kSerMouse.begin(4800); | |
if (Usb.Init() == -1) { | |
#ifdef debugonserial | |
Serial.println("OSC did not start.") | |
#endif | |
} | |
delay( 200 ); | |
HidComposite.SetReportParser(0, &MousePrs); | |
HidMouse.SetReportParser(0, &MousePrs); | |
#ifdef debugonserial | |
Serial.println("Init ok."); | |
#endif | |
} | |
void loop() | |
{ | |
uint8_t mouse_ctrl = digitalRead(3); | |
Usb.Task(); //Poll USB | |
if ((p_mouse_ctrl != mouse_ctrl) && (mouse_ctrl == 0)) //MSCRTL Toggle H->L | |
{ | |
uint8_t mouse_packet[3]; | |
uint8_t x_ovp, x_ovn, y_ovp, y_ovn = 0; | |
//detect overflow conditions | |
if (mouse_dx > 127) | |
x_ovp = 1; | |
if (mouse_dx < -128) | |
x_ovn = 1; | |
if (mouse_dy > 127) | |
y_ovp = 1; | |
if (mouse_dy < -128) | |
y_ovp = 1; | |
mouse_packet[0] = (y_ovn << 7) | (y_ovp << 6) | (x_ovn << 5) | (x_ovp << 4) | (mouse_right << 1) | mouse_left; | |
mouse_packet[1] = mouse_dx; | |
mouse_packet[2] = mouse_dy; | |
mouse_dx = 0; | |
mouse_dy = 0; | |
for (int i=0; i < 3; i++) | |
x68kSerMouse.write(mouse_packet[i]); | |
#ifdef debugonserial | |
Serial.print("Sending packet: "); | |
Serial.print(mouse_packet[0],HEX); | |
Serial.print(" "); | |
Serial.print(mouse_packet[1],DEC); | |
Serial.print(" "); | |
Serial.println(mouse_packet[2],DEC); | |
#endif | |
} | |
p_mouse_ctrl = mouse_ctrl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment