Skip to content

Commit

Permalink
common code elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Dec 8, 2017
1 parent f24b260 commit 93eef59
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 92 deletions.
17 changes: 0 additions & 17 deletions Joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
//================================================================================
// Joystick

void HIDJoystick::sendReport(void){
usb_hid_tx(joystick_Report, sizeof(joystick_Report));

while (usb_hid_is_transmitting() != 0) {
}
/* flush out to avoid having the pc wait for more data */
usb_hid_tx(NULL, 0);
}

HIDJoystick::HIDJoystick(uint8_t reportID) {
joystick_Report[0] = reportID;
}

void HIDJoystick::begin(void){
}

Expand All @@ -29,15 +16,11 @@ void HIDJoystick::setManualReportMode(bool mode) {

void HIDJoystick::safeSendReport() {
if (!manualReport) {
while (usb_hid_is_transmitting() != 0) {
}
sendReport();
}
}

void HIDJoystick::sendManualReport() {
while (usb_hid_is_transmitting() != 0) {
}
sendReport();
}

Expand Down
38 changes: 9 additions & 29 deletions Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,17 @@
//================================================================================
// Keyboard

HIDKeyboard::HIDKeyboard(uint8_t _reportID) {
reportID = _reportID;
}

void HIDKeyboard::sendReport(KeyReport* keys)
void HIDKeyboard::sendKeyReport(KeyReport* keys)
{
//HID_SendReport(2,keys,sizeof(KeyReport));
uint8_t buf[9];//sizeof(_keyReport)+1;
buf[0] = reportID;
buf[1] = _keyReport.modifiers;
buf[2] = _keyReport.reserved;
reportBuffer[1] = _keyReport.modifiers;
reportBuffer[2] = _keyReport.reserved;

uint8_t i;
for(i=0;i<sizeof(_keyReport.keys);i++){
buf[i+3] = _keyReport.keys[i];
reportBuffer[i+3] = _keyReport.keys[i];
}
usb_hid_tx(buf, sizeof(buf));

while (usb_hid_is_transmitting() != 0) {
}
/* flush out to avoid having the pc wait for more data */
usb_hid_tx(NULL, 0);

sendReport();
}

void HIDKeyboard::begin(void){
Expand Down Expand Up @@ -72,10 +61,7 @@ size_t HIDKeyboard::press(uint8_t k)
}
}

while (usb_hid_is_transmitting() != 0) {
}

sendReport(&_keyReport);
sendKeyReport(&_keyReport);
return 1;
}

Expand Down Expand Up @@ -109,10 +95,7 @@ size_t HIDKeyboard::release(uint8_t k)
}
}

while (usb_hid_is_transmitting() != 0) {
}

sendReport(&_keyReport);
sendKeyReport(&_keyReport);
return 1;
}

Expand All @@ -126,10 +109,7 @@ void HIDKeyboard::releaseAll(void)
_keyReport.keys[5] = 0;
_keyReport.modifiers = 0;

while (usb_hid_is_transmitting() != 0) {
}

sendReport(&_keyReport);
sendKeyReport(&_keyReport);
}

size_t HIDKeyboard::write(uint8_t c)
Expand Down
42 changes: 6 additions & 36 deletions Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
//================================================================================
// Mouse

HIDMouse::HIDMouse(uint8_t _reportID) : _buttons(0){
reportID = _reportID;
}

void HIDMouse::begin(void){
}

Expand All @@ -17,60 +13,36 @@ void HIDMouse::end(void){
void HIDMouse::click(uint8_t b)
{
_buttons = b;
while (usb_hid_is_transmitting() != 0) {
}
move(0,0,0);
_buttons = 0;
while (usb_hid_is_transmitting() != 0) {
}
move(0,0,0);
}

void HIDMouse::move(signed char x, signed char y, signed char wheel)
{
uint8_t m[4];
m[0] = _buttons;
m[1] = x;
m[2] = y;
m[3] = wheel;

uint8_t buf[1+sizeof(m)];
buf[0] = reportID;//report id
uint8_t i;
for(i=0;i<sizeof(m);i++){
buf[i+1] = m[i];
}

usb_hid_tx(buf, sizeof(buf));

while (usb_hid_is_transmitting() != 0) {
}
/* flush out to avoid having the pc wait for more data */
usb_hid_tx(NULL, 0);
reportBuffer[1] = _buttons;
reportBuffer[2] = x;
reportBuffer[3] = y;
reportBuffer[4] = wheel;

sendReport();
}

void HIDMouse::buttons(uint8_t b)
{
if (b != _buttons)
{
_buttons = b;
while (usb_hid_is_transmitting() != 0) {
}
move(0,0,0);
}
}

void HIDMouse::press(uint8_t b)
{
while (usb_hid_is_transmitting() != 0) {
}
buttons(_buttons | b);
}

void HIDMouse::release(uint8_t b)
{
while (usb_hid_is_transmitting() != 0) {
}
buttons(_buttons & ~b);
}

Expand All @@ -81,6 +53,4 @@ bool HIDMouse::isPressed(uint8_t b)
return false;
}



HIDMouse Mouse;
19 changes: 9 additions & 10 deletions usb_hid_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ class HIDReporter {
#define MOUSE_MIDDLE 4
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)

class HIDMouse{
class HIDMouse : public HIDReporter {
private:
uint8_t _buttons;
uint8_t reportID;
void buttons(uint8_t b);
uint8_t reportBuffer[5];
public:
HIDMouse(uint8_t _reportID=USB_HID_MOUSE_REPORT_ID);
HIDMouse(uint8_t reportID=USB_HID_MOUSE_REPORT_ID) : HIDReporter(reportBuffer, sizeof(reportBuffer), reportID), _buttons(0) {}
void begin(void);
void end(void);
void click(uint8_t b = MOUSE_LEFT);
Expand Down Expand Up @@ -406,13 +406,13 @@ typedef struct{
uint8_t keys[6];
} KeyReport;

class HIDKeyboard : public Print{
class HIDKeyboard : public Print, public HIDReporter {
private:
KeyReport _keyReport;
uint8_t reportID;
void sendReport(KeyReport* keys);
void sendKeyReport(KeyReport* keys);
uint8_t reportBuffer[9];
public:
HIDKeyboard(uint8_t _reportID=USB_HID_KEYBOARD_REPORT_ID);
HIDKeyboard(uint8_t reportID=USB_HID_KEYBOARD_REPORT_ID) : HIDReporter(reportBuffer, sizeof(reportBuffer), reportID) {}
void begin(void);
void end(void);
virtual size_t write(uint8_t k);
Expand All @@ -426,16 +426,15 @@ class HIDKeyboard : public Print{
//================================================================================
// Joystick

class HIDJoystick{
class HIDJoystick : public HIDReporter {
private:
uint8_t joystick_Report[13] = {3,0,0,0,0,0x0F,0x20,0x80,0x00,0x02,0x08,0x20,0x80};
bool manualReport = false;
void safeSendReport(void);
void sendReport(void);
public:
void sendManualReport(void);
void setManualReportMode(bool manualReport);
HIDJoystick(uint8_t reportID=USB_HID_JOYSTICK_REPORT_ID);
HIDJoystick(uint8_t reportID=USB_HID_JOYSTICK_REPORT_ID) : HIDReporter(joystick_Report, sizeof(joystick_Report), reportID) {}
void begin(void);
void end(void);
void button(uint8_t button, bool val);
Expand Down

0 comments on commit 93eef59

Please sign in to comment.