Skip to content
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

Consolidated commits of Matesaktesak 3d mouse implementation #226

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/HID-APIs/D3MouseAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <Arduino.h>
#include "HID-Settings.h"

typedef union ATTRIBUTE_PACKED {
uint8_t whole8[0];
uint16_t whole16[0];
uint32_t whole32[0];

struct ATTRIBUTE_PACKED
{
uint8_t button0 : 1;
uint8_t button1 : 1;
uint8_t button2 : 1;
int16_t xAxis : 15;
int16_t yAxis : 15;
int16_t zAxis : 15;
int16_t xRot : 15;
int16_t yRot : 15;
int16_t zRot : 15;
};
} HID_D3MouseReport_Data_t;

class D3MouseAPI
{
public:
inline D3MouseAPI(void);
inline void begin(void);
inline void end(void);
inline void click(int button);
inline void rotate(String axis, int16_t rotation);
inline void move(String axis, int16_t len);
inline void press(int button);
inline void release(int button);
inline void releaseAll(void);
inline bool isPressed(int button);
inline void update(void);

virtual void SendReport(void *data, int length) = 0;

protected:
bool _button0;
inline void button0(bool b);

bool _button1;
inline void button1(bool b);

bool _button2;
inline void button2(bool b);

int16_t _xRot;
inline void xRot(int16_t rotation);

int16_t _yRot;
inline void yRot(int16_t rotation);

int16_t _zRot;
inline void zRot(int16_t rotation);

int16_t _x;
inline void xAxis(int16_t pos);

int16_t _y;
inline void yAxis(int16_t pos);

int16_t _z;
inline void zAxis(int16_t pos);
};

#include "D3MouseAPI.hpp"
168 changes: 168 additions & 0 deletions src/HID-APIs/D3MouseAPI.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#pragma once

D3MouseAPI::D3MouseAPI(void) : _button0(false)
{
// Empty
}

void D3MouseAPI::begin(void)
{
end();
}

void D3MouseAPI::end(void)
{
_button0 = false;
_button1 = false;
_button2 = false;
rotate("x", 0);
rotate("y", 0);
rotate("z", 0);
move("x", 0);
move("y", 0);
move("z", 0);
}

void D3MouseAPI::click(int button)
{
switch (button)
{
case 0:
_button0 = true;
update();
_button0 = false;
update();
break;
case 1:
_button1 = true;
update();
_button1 = false;
update();
break;
case 2:
_button2 = true;
update();
_button2 = false;
update();
break;
}
}

void D3MouseAPI::rotate(String axis, int16_t rotation)
{
if (axis == "x")
_xRot = rotation;
if (axis == "y")
_yRot = rotation;
if (axis == "z")
_zRot = rotation;
}

void D3MouseAPI::move(String axis, int16_t pos)
{
if (axis == "x")
_x = pos;
if (axis == "y")
_y = pos;
if (axis == "z")
_z = pos;
}

void D3MouseAPI::update(void)
{
HID_D3MouseReport_Data_t report;
report.button0 = _button0;
report.button1 = _button1;
report.button2 = _button2;
report.xRot = _xRot;
report.yRot = _yRot;
report.zRot = _zRot;
report.xAxis = _x;
report.yAxis = _y;
report.zAxis = _z;

SendReport(&report, sizeof(report));
}

void D3MouseAPI::button0(bool b)
{
if (b != _button0)
{
_button0 = b;
update();
}
}

void D3MouseAPI::button1(bool b)
{
if (b != _button1)
{
_button1 = b;
update();
}
}

void D3MouseAPI::button2(bool b)
{
if (b != _button2)
{
_button2 = b;
update();
}
}

void D3MouseAPI::press(int button)
{
switch (button)
{
case 0:
button0(true);
break;
case 1:
button1(true);
break;
case 2:
button2(true);
break;
}
}

void D3MouseAPI::release(int button)
{
switch (button)
{
case 0:
button0(false);
break;
case 1:
button1(false);
break;
case 2:
button2(false);
break;
}
}

void D3MouseAPI::releaseAll(void)
{
_button0 = false;
_button1 = false;
_button2 = false;
update();
}

bool D3MouseAPI::isPressed(int button)
{
switch (button)
{
case 0:
return _button0;
break;
case 1:
return _button1;
break;
case 2:
return _button2;
break;
}
}
43 changes: 43 additions & 0 deletions src/MultiReport/D3Mouse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "D3Mouse.h"

static const uint8_t _hidMultiReportDescriptorD3Mouse[] PROGMEM = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x08, // USAGE (Multiaxis)
0xa1, 0x01, // COLLECTION (Application)
0xa1, 0x00, // COLLECTION (Physical)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x03, // REPORT_COUNT (3)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x32, // USAGE (Z)
0x09, 0x33, // USAGE (Rx)
0x09, 0x34, // USAGE (Ry)
0x09, 0x35, // USAGE (Rz)
0x16, 0x00, 0xfe, // LOGICAL_MINIMUM (-512)
0x26, 0x00, 0x02, // LOGICAL_MAXIMUM (512)
0x75, 0x0a, // REPORT_SIZE (10)
0x95, 0x06, // REPORT_COUNT (6)
0x81, 0x06, // INPUT (Data,Var,Rel)
0xc0, // END_COLLECTION
0xc0 // END_COLLECTION
};

D3Mouse_ ::D3Mouse_(void)
{
static HIDSubDescriptor node(_hidMultiReportDescriptorD3Mouse, sizeof(_hidMultiReportDescriptorD3Mouse));
HID().AppendDescriptor(&node);
}

void D3Mouse_ ::SendReport(void *data, int length)
{
HID().SendReport(HID_REPORTID_D3MOUSE, data, length);
}

D3Mouse_ D3Mouse;
16 changes: 16 additions & 0 deletions src/MultiReport/D3Mouse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <Arduino.h>
#include "HID.h"
#include "HID-Settings.h"
#include "../HID-APIs/D3MouseAPI.h"

class D3Mouse_ : public D3MouseAPI
{
public:
D3Mouse_(void);

protected:
virtual inline void SendReport(void *data, int length) override;
};
extern D3Mouse_ D3Mouse;