├── library.properties ├── src ├── CustomHID.h ├── CustomHID.cpp ├── examples │ └── connect_switch │ │ └── connect_switch.ino ├── SwitchControlLibrary.h └── SwitchControlLibrary.cpp ├── LICENSE ├── .gitignore └── README.md /library.properties: -------------------------------------------------------------------------------- 1 | name=SwitchControlLibrary 2 | version=2.0.2 3 | author=celclow 4 | maintainer=celclow 5 | sentence= 6 | paragraph= 7 | category=Device Control 8 | url=https://github.com/celclow/SwitchControlLibrary 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/CustomHID.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #if defined(USBCON) 6 | 7 | class CustomHID_ : public HID_ 8 | { 9 | public: 10 | CustomHID_(); 11 | int SendReport(const void *data, int len); 12 | }; 13 | 14 | CustomHID_ &CustomHID(); 15 | 16 | #endif /* if defined(USBCON) */ 17 | -------------------------------------------------------------------------------- /src/CustomHID.cpp: -------------------------------------------------------------------------------- 1 | #include "CustomHID.h" 2 | 3 | #if defined(USBCON) 4 | 5 | CustomHID_::CustomHID_() 6 | { 7 | } 8 | 9 | int CustomHID_::SendReport(const void *data, int len) 10 | { 11 | auto ret = USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, len); 12 | return ret; 13 | } 14 | 15 | CustomHID_ &CustomHID() 16 | { 17 | static CustomHID_ obj; 18 | return obj; 19 | } 20 | 21 | #endif /* if defined(USBCON) */ 22 | -------------------------------------------------------------------------------- /src/examples/connect_switch/connect_switch.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() 4 | { 5 | SwitchControlLibrary(); 6 | } 7 | 8 | int loopCount = 0; 9 | void loop() 10 | { 11 | if (100 < loopCount) 12 | loopCount = 0; 13 | 14 | if (loopCount == 50) 15 | { 16 | SwitchControlLibrary().pressButton(Button::L); 17 | SwitchControlLibrary().pressButton(Button::R); 18 | SwitchControlLibrary().sendReport(); 19 | 20 | SwitchControlLibrary().releaseButton(Button::L); 21 | SwitchControlLibrary().releaseButton(Button::R); 22 | SwitchControlLibrary().sendReport(); 23 | } 24 | 25 | if (loopCount == 100) 26 | { 27 | SwitchControlLibrary().pressButton(Button::A); 28 | SwitchControlLibrary().releaseButton(Button::A); 29 | } 30 | 31 | SwitchControlLibrary().sendReport(); 32 | 33 | loopCount++; 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 celclow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### https://raw.github.com/github/gitignore/18654b47c86bef38add5cd091ec2dac7d6e6e37b/C++.gitignore 2 | 3 | # Prerequisites 4 | *.d 5 | 6 | # Compiled Object files 7 | *.slo 8 | *.lo 9 | *.o 10 | *.obj 11 | 12 | # Precompiled Headers 13 | *.gch 14 | *.pch 15 | 16 | # Compiled Dynamic libraries 17 | *.so 18 | *.dylib 19 | *.dll 20 | 21 | # Fortran module files 22 | *.mod 23 | *.smod 24 | 25 | # Compiled Static libraries 26 | *.lai 27 | *.la 28 | *.a 29 | *.lib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | 36 | 37 | ### https://raw.github.com/github/gitignore/18654b47c86bef38add5cd091ec2dac7d6e6e37b/Global/macOS.gitignore 38 | 39 | # General 40 | .DS_Store 41 | .AppleDouble 42 | .LSOverride 43 | 44 | # Icon must end with two \r 45 | Icon 46 | 47 | # Thumbnails 48 | ._* 49 | 50 | # Files that might appear in the root of a volume 51 | .DocumentRevisions-V100 52 | .fseventsd 53 | .Spotlight-V100 54 | .TemporaryItems 55 | .Trashes 56 | .VolumeIcon.icns 57 | .com.apple.timemachine.donotpresent 58 | 59 | # Directories potentially created on remote AFP share 60 | .AppleDB 61 | .AppleDesktop 62 | Network Trash Folder 63 | Temporary Items 64 | .apdisk 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/SwitchControlLibrary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "CustomHID.h" 7 | 8 | struct Button 9 | { 10 | static const uint16_t Y = 0x0001; 11 | static const uint16_t B = 0x0002; 12 | static const uint16_t A = 0x0004; 13 | static const uint16_t X = 0x0008; 14 | static const uint16_t L = 0x0010; 15 | static const uint16_t R = 0x0020; 16 | static const uint16_t ZL = 0x0040; 17 | static const uint16_t ZR = 0x0080; 18 | static const uint16_t MINUS = 0x0100; 19 | static const uint16_t PLUS = 0x0200; 20 | static const uint16_t LCLICK = 0x0400; 21 | static const uint16_t RCLICK = 0x0800; 22 | static const uint16_t HOME = 0x1000; 23 | static const uint16_t CAPTURE = 0x2000; 24 | }; 25 | 26 | struct Hat 27 | { 28 | static const uint8_t UP = 0x00; 29 | static const uint8_t UP_RIGHT = 0x01; 30 | static const uint8_t RIGHT = 0x02; 31 | static const uint8_t DOWN_RIGHT = 0x03; 32 | static const uint8_t DOWN = 0x04; 33 | static const uint8_t DOWN_LEFT = 0x05; 34 | static const uint8_t LEFT = 0x06; 35 | static const uint8_t UP_LEFT = 0x07; 36 | static const uint8_t NEUTRAL = 0x08; 37 | }; 38 | 39 | struct HatButton 40 | { 41 | static const uint8_t UP = 0b0001; 42 | static const uint8_t RIGHT = 0b0010; 43 | static const uint8_t DOWN = 0b0100; 44 | static const uint8_t LEFT = 0b1000; 45 | }; 46 | 47 | struct Stick 48 | { 49 | static const uint8_t MIN = 0; 50 | static const uint8_t NEUTRAL = 128; 51 | static const uint8_t MAX = 255; 52 | }; 53 | 54 | typedef struct 55 | { 56 | uint16_t Button; 57 | uint8_t Hat; 58 | uint8_t LX; 59 | uint8_t LY; 60 | uint8_t RX; 61 | uint8_t RY; 62 | uint8_t VendorSpec; 63 | } USB_JoystickReport_Input_t; 64 | 65 | class HatState 66 | { 67 | private: 68 | std::list _hat_button_state; 69 | 70 | uint8_t getHat(); 71 | 72 | public: 73 | HatState(); 74 | 75 | uint8_t pressHatButton(uint8_t hat_button); 76 | uint8_t releaseHatButton(uint8_t hat_button); 77 | }; 78 | 79 | class SwitchControlLibrary_ 80 | { 81 | private: 82 | USB_JoystickReport_Input_t _joystickInputData; 83 | HatState _hatState; 84 | 85 | public: 86 | SwitchControlLibrary_(); 87 | 88 | void sendReport(); 89 | 90 | void pressButton(uint16_t button); 91 | void releaseButton(uint16_t button); 92 | 93 | void moveHat(uint8_t hat); 94 | void pressHatButton(uint8_t hat_button); 95 | void releaseHatButton(uint8_t hat_button); 96 | 97 | void moveLeftStick(uint8_t lx, uint8_t ly); 98 | void moveRightStick(uint8_t rx, uint8_t ry); 99 | }; 100 | 101 | SwitchControlLibrary_ &SwitchControlLibrary(); 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Switch Control Library 2 | 3 | ## 概要 4 | 5 | Arduino を Switch コントローラーとして認識させます。 6 | 7 | **v2 系 は v1 系とは互換性がありません。** 8 | 過去バージョンは[Release](https://github.com/celclow/SwitchControlLibrary/releases)ページよりダウンロードできます。 9 | 10 | ## 使い方 11 | 12 | VID=0x0f0d, PID=0x0092 へ変更してボードへ書き込んでください。 13 | 14 | 変更方法はボードによって異なりますが、SparkFun Pro Micro の場合、下記ファイルを変更してください。 15 | `~/Library/Arduino15/packages/SparkFun/hardware/avr/1.1.12/boards.txt` 16 | 17 | ## 操作方法 18 | 19 | - ボタンを押す 20 | 21 | - `pressButton(uint8_t button)` 22 | 23 | ``` 24 | SwitchControlLibrary().pressButton(Button::A); // Aボタンを押す 25 | SwitchControlLibrary().pressButton(Button::B); // Bボタンを押す 26 | ``` 27 | 28 | - `releaseButton(uint8_t button)` 29 | 30 | ``` 31 | SwitchControlLibrary().releaseButton(Button::A); // Aボタンを離す 32 | SwitchControlLibrary().releaseButton(Button::B); // Bボタンを離す 33 | ``` 34 | 35 | - `Button` 定義一覧 36 | 37 | ``` 38 | Button::Y 39 | Button::B 40 | Button::A 41 | Button::X 42 | Button::L 43 | Button::R 44 | Button::ZL 45 | Button::ZR 46 | Button::MINUS 47 | Button::PLUS 48 | Button::LCLICK 49 | Button::RCLICK 50 | Button::HOME 51 | Button::CAPTURE 52 | ``` 53 | 54 | - ハット 55 | 56 | - `moveHat(uint8_t hat)` 57 | 58 | ``` 59 | SwitchControlLibrary().moveHat(Hat::UP); // ハットは上 60 | SwitchControlLibrary().moveHat(Hat::RIGHT); // ハットは右 61 | ``` 62 | 63 | - `Hat` 定義一覧 64 | 65 | ``` 66 | Hat::UP 67 | Hat::UP_RIGHT 68 | Hat::RIGHT 69 | Hat::DOWN_RIGHT 70 | Hat::DOWN 71 | Hat::DOWN_LEFT 72 | Hat::LEFT 73 | Hat::UP_LEFT 74 | Hat::NEUTRAL 75 | ``` 76 | 77 | - `pressHat(uint8_t hat_button)` `releaseHat(uint8_t hat_button)` 78 | 79 | ``` 80 | SwitchControlLibrary().pressHat(HatButton::UP) // ハットは上 81 | SwitchControlLibrary().pressHat(HatButton::RIGHT) // ハットは右上 82 | SwitchControlLibrary().releaseHat(HatButton::UP) // ハットは右 83 | SwitchControlLibrary().releaseHat(HatButton::RIGHT) // ハットはニュートラル 84 | ``` 85 | 86 | - `HatButton` 定義一覧 87 | 88 | ``` 89 | HatButton::UP 90 | HatButton::RIGHT 91 | HatButton::DOWN 92 | HatButton::LEFT 93 | ``` 94 | 95 | - スティック 96 | 97 | - `moveLeftStick(uint8_t lx, uint8_t ly)` `moveRightStick(uint8_t rx, uint8_t ry)` 98 | 99 | - `lx` `ly` `rx` `ry` には、0〜255 の値を指定します。 100 | 101 | ``` 102 | SwitchControlLibrary().moveLeftStick(0, 128) // 左スティックは左 103 | SwitchControlLibrary().moveLeftStick(128, 128) // 左スティックはニュートラル 104 | SwitchControlLibrary().moveLeftStick(255, 128) // 左スティックは右 105 | SwitchControlLibrary().moveLeftStick(Stick::MIN, Stick::NEUTRAL) // 左スティックは左 106 | SwitchControlLibrary().moveLeftStick(Stick::MAX, Stick::NEUTRAL) // 左スティックは右 107 | ``` 108 | 109 | - `Stick` 定義一覧 110 | 111 | ``` 112 | Stick::MIN 113 | Stick::NEUTRAL 114 | Stick::MAX 115 | ``` 116 | 117 | - `sendReport()` 118 | 119 | `sendReport()` を実行したタイミングでキーが送信されます。 120 | 121 | ``` 122 | SwitchControlLibrary().pressButton(Button::A); // ニュートラル 123 | SwitchControlLibrary().pressButton(Button::B); // ニュートラル 124 | SwitchControlLibrary().sendReport() // A、Bボタンが同時に送信される 125 | ``` 126 | 127 | ## ライセンス 128 | 129 | [MIT](https://github.com/celclow/SwitchControlLibrary/blob/master/LICENSE) 130 | -------------------------------------------------------------------------------- /src/SwitchControlLibrary.cpp: -------------------------------------------------------------------------------- 1 | #include "SwitchControlLibrary.h" 2 | 3 | #if defined(_USING_HID) 4 | 5 | static const uint8_t _hidReportDescriptor[] PROGMEM = { 6 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 7 | 0x09, 0x05, // USAGE (Game Pad) 8 | 0xa1, 0x01, // COLLECTION (Application) 9 | 0x15, 0x00, // LOGICAL_MINIMUM (0) 10 | 0x25, 0x01, // LOGICAL_MAXIMUM (1) 11 | 0x35, 0x00, // PHYSICAL_MINIMUM (0) 12 | 0x45, 0x01, // PHYSICAL_MAXIMUM (1) 13 | 0x75, 0x01, // REPORT_SIZE (1) 14 | 0x95, 0x10, // REPORT_COUNT (16) 15 | 0x05, 0x09, // USAGE_PAGE (Button) 16 | 0x19, 0x01, // USAGE_MINIMUM (1) 17 | 0x29, 0x10, // USAGE_MAXIMUM (16) 18 | 0x81, 0x02, // INPUT (Data,Var,Abs) 19 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 20 | 0x25, 0x07, // LOGICAL_MAXIMUM (7) 21 | 0x46, 0x3b, 0x01, // PHYSICAL_MAXIMUM (315) 22 | 0x75, 0x04, // REPORT_SIZE (4) 23 | 0x95, 0x01, // REPORT_COUNT (1) 24 | 0x65, 0x14, // UNIT (20) 25 | 0x09, 0x39, // USAGE (Hat Switch) 26 | 0x81, 0x42, // INPUT (Data,Var,Abs) 27 | 0x65, 0x00, // UNIT (0) 28 | 0x95, 0x01, // REPORT_COUNT (1) 29 | 0x81, 0x01, // INPUT (Cnst,Arr,Abs) 30 | 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255) 31 | 0x46, 0xff, 0x00, // PHYSICAL_MAXIMUM (255) 32 | 0x09, 0x30, // USAGE (X) 33 | 0x09, 0x31, // USAGE (Y) 34 | 0x09, 0x32, // USAGE (Z) 35 | 0x09, 0x35, // USAGE (Rz) 36 | 0x75, 0x08, // REPORT_SIZE (8) 37 | 0x95, 0x04, // REPORT_COUNT (4) 38 | 0x81, 0x02, // INPUT (Data,Var,Abs) 39 | 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined 65280) 40 | 0x09, 0x20, // USAGE (32) 41 | 0x95, 0x01, // REPORT_COUNT (1) 42 | 0x81, 0x02, // INPUT (Data,Var,Abs) 43 | 0x0a, 0x21, 0x26, // USAGE (9761) 44 | 0x95, 0x08, // REPORT_COUNT (8) 45 | 0x91, 0x02, // OUTPUT (Data,Var,Abs) 46 | 0xc0 // END_COLLECTION 47 | }; 48 | 49 | uint8_t HatState::getHat() 50 | { 51 | 52 | uint8_t hat_button = 0b0000; 53 | uint8_t i = 0; 54 | for (auto itr = _hat_button_state.begin(); itr != _hat_button_state.end() && i < 2; ++itr, ++i) 55 | { 56 | hat_button |= *itr; 57 | } 58 | 59 | uint8_t hat = Hat::NEUTRAL; 60 | switch (hat_button) 61 | { 62 | case 0b0000: 63 | hat = Hat::NEUTRAL; 64 | break; 65 | case 0b0001: 66 | hat = Hat::UP; 67 | break; 68 | case 0b0010: 69 | hat = Hat::RIGHT; 70 | break; 71 | case 0b0011: 72 | hat = Hat::UP_RIGHT; 73 | break; 74 | case 0b0100: 75 | hat = Hat::DOWN; 76 | break; 77 | case 0b0110: 78 | hat = Hat::DOWN_RIGHT; 79 | break; 80 | case 0b1000: 81 | hat = Hat::LEFT; 82 | break; 83 | case 0b1001: 84 | hat = Hat::UP_LEFT; 85 | break; 86 | case 0b1100: 87 | hat = Hat::DOWN_LEFT; 88 | break; 89 | default: 90 | hat = Hat::NEUTRAL; 91 | break; 92 | } 93 | return hat; 94 | } 95 | 96 | HatState::HatState() 97 | { 98 | } 99 | 100 | uint8_t HatState::pressHatButton(uint8_t hat_button) 101 | { 102 | auto itr = std::find(_hat_button_state.begin(), _hat_button_state.end(), hat_button); 103 | if (itr == _hat_button_state.end()) 104 | { 105 | _hat_button_state.push_front(hat_button); 106 | } 107 | 108 | return getHat(); 109 | } 110 | 111 | uint8_t HatState::releaseHatButton(uint8_t hat_button) 112 | { 113 | auto itr = std::find(_hat_button_state.begin(), _hat_button_state.end(), hat_button); 114 | if (itr != _hat_button_state.end()) 115 | { 116 | _hat_button_state.erase(itr); 117 | } 118 | return getHat(); 119 | } 120 | 121 | SwitchControlLibrary_::SwitchControlLibrary_() 122 | { 123 | static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor)); 124 | CustomHID().AppendDescriptor(&node); 125 | 126 | memset(&_joystickInputData, 0, sizeof(USB_JoystickReport_Input_t)); 127 | _joystickInputData.LX = Stick::NEUTRAL; 128 | _joystickInputData.LY = Stick::NEUTRAL; 129 | _joystickInputData.RX = Stick::NEUTRAL; 130 | _joystickInputData.RY = Stick::NEUTRAL; 131 | _joystickInputData.Hat = Hat::NEUTRAL; 132 | } 133 | 134 | void SwitchControlLibrary_::sendReport() 135 | { 136 | CustomHID().SendReport(&_joystickInputData, sizeof(USB_JoystickReport_Input_t)); 137 | } 138 | 139 | void SwitchControlLibrary_::pressButton(uint16_t button) 140 | { 141 | _joystickInputData.Button |= button; 142 | } 143 | 144 | void SwitchControlLibrary_::releaseButton(uint16_t button) 145 | { 146 | _joystickInputData.Button &= (button ^ 0xffff); 147 | } 148 | 149 | void SwitchControlLibrary_::moveHat(uint8_t hat) 150 | { 151 | _joystickInputData.Hat = hat; 152 | } 153 | 154 | void SwitchControlLibrary_::pressHatButton(uint8_t hat_button) 155 | { 156 | _joystickInputData.Hat = _hatState.pressHatButton(hat_button); 157 | } 158 | 159 | void SwitchControlLibrary_::releaseHatButton(uint8_t hat_button) 160 | { 161 | _joystickInputData.Hat = _hatState.releaseHatButton(hat_button); 162 | } 163 | 164 | void SwitchControlLibrary_::moveLeftStick(uint8_t lx, uint8_t ly) 165 | { 166 | _joystickInputData.LX = lx; 167 | _joystickInputData.LY = ly; 168 | } 169 | 170 | void SwitchControlLibrary_::moveRightStick(uint8_t rx, uint8_t ry) 171 | { 172 | _joystickInputData.RX = rx; 173 | _joystickInputData.RY = ry; 174 | } 175 | 176 | SwitchControlLibrary_ &SwitchControlLibrary() 177 | { 178 | static SwitchControlLibrary_ obj; 179 | return obj; 180 | } 181 | 182 | #endif 183 | --------------------------------------------------------------------------------