Skip to content

Absolute mouse support #6331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Update USBHIDMouse.h
  • Loading branch information
tobozo authored Mar 2, 2022
commit 35a0054b6241006f70dcd92a6b4cf81b3cb33f7f
76 changes: 46 additions & 30 deletions libraries/USB/src/USBHIDMouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,67 @@
#define MOUSE_FORWARD 0x10
#define MOUSE_ALL 0x1F

// Relative Mouse

class USBHIDMouse: public USBHIDDevice {
private:
USBHID hid;
uint8_t _buttons;
void buttons(uint8_t b);
bool write(int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
enum MousePositioning_t
{
HID_MOUSE_RELATIVE,
HID_MOUSE_ABSOLUTE
};

struct HIDMouseType_t
{
MousePositioning_t positioning;
const uint8_t* report_descriptor;
size_t descriptor_size;
size_t report_size;
};

extern HIDMouseType_t HIDMouseRel;
extern HIDMouseType_t HIDMouseAbs;


class USBHIDMouseBase: public USBHIDDevice {
public:
USBHIDMouse(void);
USBHIDMouseBase(HIDMouseType_t *type);
void begin(void);
void end(void);

void click(uint8_t b = MOUSE_LEFT);
void move(int8_t x, int8_t y, int8_t wheel = 0, int8_t pan = 0);
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default

template <typename T> bool sendReport(T report) { return hid.SendReport( HID_REPORT_ID_MOUSE, &report, _type->report_size ); };
// internal use
uint16_t _onGetDescriptor(uint8_t* buffer);
void buttons(uint8_t b);
protected:
USBHID hid;
uint8_t _buttons;
HIDMouseType_t *_type;
};


// Absolute Mouse


typedef struct TU_ATTR_PACKED
{
uint8_t buttons = 0;
int16_t x = 0;
int16_t y = 0;
} abs_mouse_report_t;
class USBHIDRelativeMouse: public USBHIDMouseBase {
public:
USBHIDRelativeMouse(void): USBHIDMouseBase(&HIDMouseRel) { }
void move(int8_t x, int8_t y, int8_t wheel = 0, int8_t pan = 0);
void click(uint8_t b = MOUSE_LEFT);
void buttons(uint8_t b);
};


class USBHIDAbsMouse: public USBHIDDevice
{
private:
USBHID hid;
class USBHIDAbsoluteMouse: public USBHIDMouseBase {
public:
USBHIDAbsMouse(void);
void begin(void);
uint16_t _onGetDescriptor(uint8_t* buffer);
bool sendReport(abs_mouse_report_t * value);
void end();
USBHIDAbsoluteMouse(void): USBHIDMouseBase(&HIDMouseAbs) { }
void move(int16_t x, int16_t y, int8_t wheel = 0, int8_t pan = 0);
void click(uint8_t b = MOUSE_LEFT);
void buttons(uint8_t b);
private:
int16_t _lastx = 0;
int16_t _lasty = 0;
};


// don't break examples and old sketches
#define USBHIDMouse USBHIDRelativeMouse;


#endif