From 1afd96cdb932ccb1df0f0ed7e6662080f2618a26 Mon Sep 17 00:00:00 2001 From: tomaszduda23 Date: Mon, 11 Jul 2022 08:01:57 +0900 Subject: [PATCH] add factory for avr (#98) --- hid/lib/drivers-avr/factory.cpp | 61 +++++++++++++++++++ hid/{src => lib/drivers-avr}/ps2/hid.h | 0 hid/{src => lib/drivers-avr}/ps2/keymap.h | 0 .../drivers-avr}/ps2/keymap.h.mako | 0 hid/{src => lib/drivers-avr}/spi.cpp | 0 hid/{src => lib/drivers-avr}/spi.h | 0 hid/{src => lib/drivers-avr}/usb/hid.h | 4 +- hid/{src => lib/drivers-avr}/usb/keymap.h | 0 .../drivers-avr}/usb/keymap.h.mako | 0 hid/{src => lib/drivers}/aum.h | 0 hid/lib/drivers/factory.h | 33 ++++++++++ hid/lib/drivers/tools.cpp | 30 +++++++++ hid/{src => lib/drivers}/tools.h | 8 +-- hid/platformio.ini | 2 + hid/src/main.cpp | 25 +++----- 15 files changed, 138 insertions(+), 25 deletions(-) create mode 100644 hid/lib/drivers-avr/factory.cpp rename hid/{src => lib/drivers-avr}/ps2/hid.h (100%) rename hid/{src => lib/drivers-avr}/ps2/keymap.h (100%) rename hid/{src => lib/drivers-avr}/ps2/keymap.h.mako (100%) rename hid/{src => lib/drivers-avr}/spi.cpp (100%) rename hid/{src => lib/drivers-avr}/spi.h (100%) rename hid/{src => lib/drivers-avr}/usb/hid.h (99%) rename hid/{src => lib/drivers-avr}/usb/keymap.h (100%) rename hid/{src => lib/drivers-avr}/usb/keymap.h.mako (100%) rename hid/{src => lib/drivers}/aum.h (100%) create mode 100644 hid/lib/drivers/factory.h create mode 100644 hid/lib/drivers/tools.cpp rename hid/{src => lib/drivers}/tools.h (90%) diff --git a/hid/lib/drivers-avr/factory.cpp b/hid/lib/drivers-avr/factory.cpp new file mode 100644 index 00000000..d58e6c96 --- /dev/null +++ b/hid/lib/drivers-avr/factory.cpp @@ -0,0 +1,61 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + +#include "usb/hid.h" +#include "ps2/hid.h" +#include "factory.h" + +namespace DRIVERS +{ + + Keyboard *Factory::makeKeyboard(type _type) { + switch (_type) { + +# ifdef HID_WITH_USB + case USB_KEYBOARD: + return new UsbKeyboard(); +# endif + +# ifdef HID_WITH_PS2 + case PS2_KEYBOARD: + return new Ps2Keyboard(); +# endif + default: + return new Keyboard(DUMMY); + } + } + + Mouse *Factory::makeMouse(type _type) { + switch(_type) + { +# ifdef HID_WITH_USB + case USB_MOUSE_ABSOLUTE: + case USB_MOUSE_ABSOLUTE_WIN98: + return new UsbMouseAbsolute(_type); + case USB_MOUSE_RELATIVE: + return new UsbMouseRelative(); +# endif + default: + return new Mouse(DRIVERS::DUMMY); + } + } + +} diff --git a/hid/src/ps2/hid.h b/hid/lib/drivers-avr/ps2/hid.h similarity index 100% rename from hid/src/ps2/hid.h rename to hid/lib/drivers-avr/ps2/hid.h diff --git a/hid/src/ps2/keymap.h b/hid/lib/drivers-avr/ps2/keymap.h similarity index 100% rename from hid/src/ps2/keymap.h rename to hid/lib/drivers-avr/ps2/keymap.h diff --git a/hid/src/ps2/keymap.h.mako b/hid/lib/drivers-avr/ps2/keymap.h.mako similarity index 100% rename from hid/src/ps2/keymap.h.mako rename to hid/lib/drivers-avr/ps2/keymap.h.mako diff --git a/hid/src/spi.cpp b/hid/lib/drivers-avr/spi.cpp similarity index 100% rename from hid/src/spi.cpp rename to hid/lib/drivers-avr/spi.cpp diff --git a/hid/src/spi.h b/hid/lib/drivers-avr/spi.h similarity index 100% rename from hid/src/spi.h rename to hid/lib/drivers-avr/spi.h diff --git a/hid/src/usb/hid.h b/hid/lib/drivers-avr/usb/hid.h similarity index 99% rename from hid/src/usb/hid.h rename to hid/lib/drivers-avr/usb/hid.h index ca923352..0bc5cb2d 100644 --- a/hid/src/usb/hid.h +++ b/hid/lib/drivers-avr/usb/hid.h @@ -27,9 +27,9 @@ #include "keyboard.h" #include "mouse.h" -#include "../tools.h" +#include "tools.h" #ifdef AUM -# include "../aum.h" +# include "aum.h" #endif #include "keymap.h" diff --git a/hid/src/usb/keymap.h b/hid/lib/drivers-avr/usb/keymap.h similarity index 100% rename from hid/src/usb/keymap.h rename to hid/lib/drivers-avr/usb/keymap.h diff --git a/hid/src/usb/keymap.h.mako b/hid/lib/drivers-avr/usb/keymap.h.mako similarity index 100% rename from hid/src/usb/keymap.h.mako rename to hid/lib/drivers-avr/usb/keymap.h.mako diff --git a/hid/src/aum.h b/hid/lib/drivers/aum.h similarity index 100% rename from hid/src/aum.h rename to hid/lib/drivers/aum.h diff --git a/hid/lib/drivers/factory.h b/hid/lib/drivers/factory.h new file mode 100644 index 00000000..c787347a --- /dev/null +++ b/hid/lib/drivers/factory.h @@ -0,0 +1,33 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + +#pragma once + +#include "keyboard.h" +#include "mouse.h" + +namespace DRIVERS { + + struct Factory { + static Keyboard *makeKeyboard(type _type); + static Mouse *makeMouse(type _type); + }; +} diff --git a/hid/lib/drivers/tools.cpp b/hid/lib/drivers/tools.cpp new file mode 100644 index 00000000..7f0e29ca --- /dev/null +++ b/hid/lib/drivers/tools.cpp @@ -0,0 +1,30 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + +#include + +bool is_micros_timed_out(unsigned long start_ts, unsigned long timeout) { + unsigned long now = micros(); + return ( + (now >= start_ts && now - start_ts > timeout) + || (now < start_ts && ((unsigned long)-1) - start_ts + now > timeout) + ); +} diff --git a/hid/src/tools.h b/hid/lib/drivers/tools.h similarity index 90% rename from hid/src/tools.h rename to hid/lib/drivers/tools.h index 0a798596..0f11c7c3 100644 --- a/hid/src/tools.h +++ b/hid/lib/drivers/tools.h @@ -23,10 +23,4 @@ #pragma once -bool is_micros_timed_out(unsigned long start_ts, unsigned long timeout) { - unsigned long now = micros(); - return ( - (now >= start_ts && now - start_ts > timeout) - || (now < start_ts && ((unsigned long)-1) - start_ts + now > timeout) - ); -} +bool is_micros_timed_out(unsigned long start_ts, unsigned long timeout); diff --git a/hid/platformio.ini b/hid/platformio.ini index aa9202dd..8484ef50 100644 --- a/hid/platformio.ini +++ b/hid/platformio.ini @@ -10,6 +10,8 @@ lib_deps = git+https://github.com/NicoHood/HID#2.8.2 git+https://github.com/Harvie/ps2dev#v0.0.3 digitalWriteFast@1.0.0 + HID@1.0 + drivers-avr extra_scripts = pre:avrdude.py post:patch.py diff --git a/hid/src/main.cpp b/hid/src/main.cpp index 732cbde2..fe030a3a 100644 --- a/hid/src/main.cpp +++ b/hid/src/main.cpp @@ -44,9 +44,8 @@ #ifdef AUM # include "aum.h" #endif -#include "usb/hid.h" -#include "ps2/hid.h" +#include "factory.h" // ----------------------------------------------------------------------------- static DRIVERS::Keyboard *_kbd = nullptr; @@ -110,36 +109,30 @@ static void _initOutputs() { uint8_t kbd = outputs & PROTO::OUTPUTS1::KEYBOARD::MASK; switch (kbd) { -# ifdef HID_WITH_USB case PROTO::OUTPUTS1::KEYBOARD::USB: - _kbd = new UsbKeyboard(); + _kbd = DRIVERS::Factory::makeKeyboard(DRIVERS::USB_KEYBOARD); break; -# endif -# ifdef HID_WITH_PS2 case PROTO::OUTPUTS1::KEYBOARD::PS2: - _kbd = new Ps2Keyboard(); + _kbd = DRIVERS::Factory::makeKeyboard(DRIVERS::PS2_KEYBOARD); break; -# endif default: - _kbd = new DRIVERS::Keyboard(DRIVERS::DUMMY); + _kbd = DRIVERS::Factory::makeKeyboard(DRIVERS::DUMMY); break; } uint8_t mouse = outputs & PROTO::OUTPUTS1::MOUSE::MASK; switch (mouse) { -# ifdef HID_WITH_USB case PROTO::OUTPUTS1::MOUSE::USB_ABS: - _mouse = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE); + _mouse = DRIVERS::Factory::makeMouse(DRIVERS::USB_MOUSE_ABSOLUTE); break; case PROTO::OUTPUTS1::MOUSE::USB_WIN98: - _mouse = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE_WIN98); + _mouse = DRIVERS::Factory::makeMouse(DRIVERS::USB_MOUSE_ABSOLUTE_WIN98); break; case PROTO::OUTPUTS1::MOUSE::USB_REL: - _mouse = new UsbMouseRelative(); + _mouse = DRIVERS::Factory::makeMouse(DRIVERS::USB_MOUSE_RELATIVE); break; -# endif default: - _mouse = new DRIVERS::Mouse(DRIVERS::DUMMY); + _mouse = DRIVERS::Factory::makeMouse(DRIVERS::DUMMY); break; } @@ -255,7 +248,7 @@ static void _sendResponse(uint8_t code) { # endif if (_kbd->getType() != DRIVERS::DUMMY) { response[1] |= (_kbd->isOffline() ? PROTO::PONG::KEYBOARD_OFFLINE : 0); - KeyboardLedsState leds = _kbd->getLeds(); + DRIVERS::KeyboardLedsState leds = _kbd->getLeds(); response[1] |= (leds.caps ? PROTO::PONG::CAPS : 0); response[1] |= (leds.num ? PROTO::PONG::NUM : 0); response[1] |= (leds.scroll ? PROTO::PONG::SCROLL : 0);