├── .gitattributes ├── LICENSE ├── ModeSwitch ├── C │ ├── devicepath.dat │ ├── modeswitch.exe │ └── src │ │ ├── CMakeLists.txt │ │ └── modeswitch.cpp ├── ahk │ └── src │ │ ├── AHKHID.ahk │ │ └── modeswitch.ahk └── readme.md ├── README.md ├── ezusb ├── ezusb-popn15.dll ├── ezusb.dll ├── readme.md └── src │ ├── CMakeLists.txt │ ├── ezusb.cpp │ ├── ezusb.h │ └── lib │ └── hid.lib ├── pinout.png ├── pinout_leonardo.png ├── pinout_leonardo_psx.png ├── popnController ├── POPNHID.cpp ├── POPNHID.h ├── popnController.ino ├── ps2.c └── ps2.h └── shield.png /.gitattributes: -------------------------------------------------------------------------------- 1 | ModeSwitch/* linguist-vendored=true -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /ModeSwitch/C/devicepath.dat: -------------------------------------------------------------------------------- 1 | \\?\HID#VID_2341&PID_003E&MI_02#7&3156e204&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030} 2 | \\?\HID#VID_2341&PID_8036&MI_02#7&63200bf&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030} -------------------------------------------------------------------------------- /ModeSwitch/C/modeswitch.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/ModeSwitch/C/modeswitch.exe -------------------------------------------------------------------------------- /ModeSwitch/C/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(popn_modeswitch) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc") 6 | 7 | include_directories(${popn_modeswitch_SOURCE_DIR}) 8 | link_directories(${PROJECT_SOURCE_DIR}/include) 9 | 10 | add_executable(modeswitch modeswitch.cpp) -------------------------------------------------------------------------------- /ModeSwitch/C/src/modeswitch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | HANDLE g_hidHandle; 5 | 6 | /** 7 | * Initialize the g_hidHandle global variable 8 | * It will attempt to read paths from devicepath.dat file if it exists until it finds one which works. 9 | * If nothing is found, as a last resort it will try the two default paths from the Due and Leonardo 10 | * versions of the firmware. 11 | * 12 | * @return 0 on success, -1 on error 13 | */ 14 | static int controller_init(){ 15 | FILE *file; 16 | char path[256]; 17 | 18 | file = fopen("devicepath.dat", "r"); 19 | if (file == NULL) 20 | { 21 | goto last_resort; 22 | } 23 | 24 | while ( fgets(path,256,file) != NULL ) 25 | { 26 | path[strcspn(path, "\r\n")] = 0; 27 | g_hidHandle = CreateFile(path, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 28 | if ( g_hidHandle != INVALID_HANDLE_VALUE ) 29 | break; 30 | } 31 | fclose(file); 32 | 33 | if ( g_hidHandle == INVALID_HANDLE_VALUE ) 34 | { 35 | goto last_resort; 36 | } 37 | return 0; 38 | 39 | last_resort: 40 | g_hidHandle = CreateFile("\\\\?\\HID#VID_2341&PID_003E&MI_02#7&3156e204&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}", 41 | GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 42 | if ( g_hidHandle != INVALID_HANDLE_VALUE ) 43 | return 0; 44 | 45 | g_hidHandle = CreateFile("\\\\?\\HID#VID_2341&PID_8036&MI_02#7&63200bf&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}", 46 | GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 47 | if ( g_hidHandle != INVALID_HANDLE_VALUE ) 48 | return 0; 49 | 50 | printf("Couldn't open device. Make sure devicepath.dat exists and contains the correct path.\r\n"); 51 | return -1; 52 | } 53 | 54 | int main(int argc, char* argv[]) 55 | { 56 | char OutputReport[5]; 57 | int res; 58 | int mode; 59 | DWORD BytesWritten = 0; 60 | 61 | if ( argc < 2 ) 62 | { 63 | printf("Usage: %s mode\r\n",argv[0]); 64 | return 1; 65 | } 66 | 67 | mode = atoi(argv[1]); 68 | if ( mode < 0 || mode > 4 ) 69 | { 70 | printf("Invalid mode value %d\r\n", mode); 71 | return 2; 72 | } 73 | 74 | if ( controller_init() == -1 ) 75 | { 76 | printf("Couldn't open device. Make sure devicepath.dat contains the correct path.\r\n"); 77 | return 3; 78 | } 79 | 80 | OutputReport[0] = 0x05; //report ID 81 | OutputReport[1] = 0x00; 82 | OutputReport[2] = 0x00; 83 | OutputReport[3] = 0x00; 84 | OutputReport[4] = mode+16; //mode+magicbit 85 | 86 | WriteFile(g_hidHandle, OutputReport, 5, &BytesWritten, NULL); 87 | if ( BytesWritten == 5 ) 88 | { 89 | res = 0; 90 | printf("Succesfully switched to mode %u ", mode); 91 | switch(mode) 92 | { 93 | case 0: 94 | printf("(reactive)\r\n"); 95 | break; 96 | case 1: 97 | printf("(HID)\r\n"); 98 | break; 99 | case 2: 100 | printf("(mixed)\r\n"); 101 | break; 102 | case 3: 103 | printf("(combined)\r\n"); 104 | break; 105 | case 4: 106 | printf("(combined invert)\r\n"); 107 | } 108 | } 109 | else 110 | { 111 | printf("Error sending HID report (%u bytes written)\r\n",BytesWritten); 112 | res = 4; 113 | } 114 | 115 | CloseHandle(g_hidHandle); 116 | return res; 117 | } 118 | -------------------------------------------------------------------------------- /ModeSwitch/ahk/src/AHKHID.ahk: -------------------------------------------------------------------------------- 1 | /*! TheGood 2 | AHKHID - An AHK implementation of the HID functions. 3 | Last updated: August 22nd, 2010 4 | 5 | USING THE CONSTANTS: 6 | 7 | If you explicitly #include AHKHID in your script, you will have all the constants available to you. Otherwise, if AHKHID is 8 | in your library folder and you do not wish to explicitly #include it, you can call AHKHID_UseConstants() to have the 9 | constants available to you. 10 | 11 | FUNCTION LIST: 12 | _____________________ 13 | AHKHID_UseConstants() 14 | 15 | See the section above titled "USING THE CONSTANTS" 16 | ___________________________________ 17 | AHKHID_Initialize(bRefresh = False) 18 | 19 | You don't have to call this function manually. It is automatically called by other functions to get the pointer of the 20 | RAWINPUTDEVICELIST struct array. However, if a new device is plugged in, you will have to refresh the listing by calling it 21 | with bRefresh = True. Returns -1 on error (with error message in ErrorLevel). 22 | ____________________ 23 | AHKHID_GetDevCount() 24 | 25 | Returs the number of HID devices connected to this computer. 26 | Returns -1 on error (with error message in ErrorLevel). 27 | ______________________ 28 | AHKHID_GetDevHandle(i) 29 | 30 | Returns the handle of device i (starts at 1). 31 | Mostly used internally for API calls. 32 | __________________________ 33 | AHKHID_GetDevIndex(Handle) 34 | 35 | Returns the index (starts at 1) of the device in the enumeration with matching handle. 36 | Returns 0 if not found. 37 | ______________________________________ 38 | AHKHID_GetDevType(i, IsHandle = False) 39 | 40 | Returns the type of the device. See the RIM_ constants for possible values. 41 | If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration. 42 | Otherwise it is the handle of the device. 43 | ______________________________________ 44 | AHKHID_GetDevName(i, IsHandle = False) 45 | 46 | Returns the name of the device (or empty string on error, with error message in ErrorLevel). 47 | If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration. 48 | Otherwise it is the handle of the device. 49 | ____________________________________________ 50 | AHKHID_GetDevInfo(i, Flag, IsHandle = False) 51 | 52 | Retrieves info from the RID_DEVICE_INFO struct. To retrieve a member, simply use the corresponding flag. A list of flags 53 | can be found at the top of the script (the constants starting with DI_). Each flag corresponds to a member in the struct. 54 | If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration. Otherwise it is the 55 | handle of the device. Returns -1 on error (with error message in ErrorLevel). 56 | 57 | See Example 1 for an example on how to use it. 58 | _______________________________________________________________________________ 59 | AHKHID_AddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) 60 | 61 | Allows you to queue up RAWINPUTDEVICE structures before doing the registration. To use it, you first need to initialize the 62 | variable by calling AHKHID_AddRegister(iNumberOfElements). To then add to the stack, simply call it with the parameters you 63 | want (eg. AHKHID_AddRegister(1,6,MyGuiHandle) for keyboards). When you're finally done, you just have to call 64 | AHKHID_Register() with no parameters. The function returns -1 if the struct is full. Redimensioning the struct will erase 65 | all previous structs added. On success, it returns the address of the array of structs (if you'd rather manipulate it 66 | yourself). 67 | 68 | See Example 2 for an example on how to use it. 69 | 70 | You will need to do this if you want to use advance features of the RAWINPUTDEVICE flags. For example, if you want to 71 | register all devices using Usage Page 1 but would like to exclude devices of Usage Page 1 using Usage 2 (keyboards), then 72 | you need to place two elements in the array. The first one is AHKHID_AddRegister(1,0,MyGuiHandle,RIDEV_PAGEONLY) and the 73 | second one is AHKHID_AddRegister(1,2,MyGuiHandle,RIDEV_EXCLUDE). 74 | 75 | Tip: Have a look at all the flags you can use (see the constants starting with RIDEV_). The most useful is RIDEV_INPUTSINK. 76 | Tip: Set Handle to 0 if you want the WM_INPUT messages to go to the window with keyboard focus. 77 | Tip: To unregister, use the flag RIDEV_REMOVE. Note that you also need to use the RIDEV_PAGEONLY flag if the TLC was 78 | registered with it. 79 | ____________________________________________________________________________ 80 | AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0) 81 | 82 | This function can be used in two ways. If no parameters are specified, it will use the RAWINPUTDEVICE array created through 83 | AHKHID_AddRegister() and register. Otherwise, it will register only the specified parameters. For example, if you just want 84 | to register the mouse, you can simply do AHKHID_Register(1,2,MyGuiHandle). Returns 0 on success, returns -1 on error (with 85 | error message in ErrorLevel). 86 | 87 | See Example 2 for an example on how to use it with the RAWINPUTDEVICE. 88 | See Example 3 for an example on how to use it only with the specified parameters. 89 | ____________________________________ 90 | AHKHID_GetRegisteredDevs(ByRef uDev) 91 | 92 | This function allows you to get an array of the TLCs that have already been registered. 93 | It fills uDev with an array of RAWINPUTDEVICE and returns the number of elements in the array. 94 | Returns -1 on error (with error message in ErrorLevel). 95 | 96 | See Example 2 for an example on how to use it. 97 | ______________________________________ 98 | AHKHID_GetInputInfo(InputHandle, Flag) 99 | 100 | This function is used to retrieve the data upon receiving WM_INPUT messages. By passing the lParam of the WM_INPUT (0xFF00) 101 | messages, it can retrieve all the members of the RAWINPUT structure, except the raw data coming from HID devices (use 102 | AHKHID_GetInputData for that). To retrieve a member, simply specify the flag corresponding to the member you want, and call 103 | the function. A list of all the flags can be found at the top of this script (the constants starting with II_). Returns -1 104 | on error (with error message in ErrorLevel). 105 | 106 | See Example 2 for an example on how to use it to retrieve each member of the structure. 107 | See Example 3 for an example on how to interpret members which represent flags. 108 | 109 | Tip: You have to use Critical in your message function or you might get invalid handle errors. 110 | Tip: You can check the value of wParam to know if the application was in the foreground upon reception (see RIM_INPUT). 111 | _____________________________________________ 112 | AHKHID_GetInputData(InputHandle, ByRef uData) 113 | 114 | This function is used to retrieve the data sent by HID devices of type RIM_TYPEHID (ie. neither keyboard nor mouse) upon 115 | receiving WM_INPUT messages. CAUTION: it does not check if the device is indeed of type HID. It is up to you to do so (you 116 | can use GetInputInfo for that). Specify the lParam of the WM_INPUT (0xFF00) message and the function will put in uData the 117 | raw data received from the device. It will then return the size (number of bytes) of uData. Returns -1 on error (with error 118 | message in ErrorLevel). 119 | 120 | See Example 2 for an example on how to use it (although you need an HID device of type RIM_TYPEHID to test it). 121 | 122 | */ 123 | 124 | AHKHID_Included := True 125 | AHKHID_SetConstants: 126 | ;______________________________________ 127 | ;Flags you can use in AHKHID_GetDevInfo 128 | ;http://msdn.microsoft.com/en-us/library/ms645581 129 | DI_DEVTYPE := 4 ;Type of the device. See RIM_ constants. 130 | 131 | DI_MSE_ID := 8 ;ID for the mouse device. 132 | DI_MSE_NUMBEROFBUTTONS := 12 ;Number of buttons for the mouse. 133 | DI_MSE_SAMPLERATE := 16 ;Number of data points per second. This information may not be applicable for every 134 | ;mouse device. 135 | DI_MSE_HASHORIZONTALWHEEL := 20 ;Vista and later only: TRUE if the mouse has a wheel for horizontal scrolling; 136 | ;otherwise, FALSE. 137 | 138 | DI_KBD_TYPE := 8 ;Type of the keyboard. 139 | DI_KBD_SUBTYPE := 12 ;Subtype of the keyboard. 140 | DI_KBD_KEYBOARDMODE := 16 ;Scan code mode. 141 | DI_KBD_NUMBEROFFUNCTIONKEYS := 20 ;Number of function keys on the keyboard. 142 | DI_KBD_NUMBEROFINDICATORS := 24 ;Number of LED indicators on the keyboard. 143 | DI_KBD_NUMBEROFKEYSTOTAL := 28 ;Total number of keys on the keyboard. 144 | 145 | DI_HID_VENDORID := 8 ;Vendor ID for the HID. 146 | DI_HID_PRODUCTID := 12 ;Product ID for the HID. 147 | DI_HID_VERSIONNUMBER := 16 ;Version number for the HID. 148 | DI_HID_USAGEPAGE := 20 | 0x0100 ;Top-level collection Usage Page for the device. 149 | DI_HID_USAGE := 22 | 0x0100 ;Top-level collection Usage for the device. 150 | ;________________________________________ 151 | ;Flags you can use in AHKHID_GetInputInfo 152 | ;http://msdn.microsoft.com/en-us/library/ms645562 153 | II_DEVTYPE := 0 ;Type of the device generating the raw input data. See RIM_ constants. 154 | II_DEVHANDLE := 8 ;Handle to the device generating the raw input data. 155 | 156 | II_MSE_FLAGS := (08+A_PtrSize*2) | 0x0100 ;Mouse state. This member can be any reasonable combination of the 157 | ;following values -> see MOUSE constants. 158 | II_MSE_BUTTONFLAGS := (12+A_PtrSize*2) | 0x0100 ;Transition state of the mouse buttons. This member can be one or more of 159 | ;the following values -> see RI_MOUSE constants. 160 | II_MSE_BUTTONDATA := (14+A_PtrSize*2) | 0x1100 ;If usButtonFlags is RI_MOUSE_WHEEL, this member is a signed value that 161 | ;specifies the wheel delta. 162 | II_MSE_RAWBUTTONS := (16+A_PtrSize*2) ;Raw state of the mouse buttons. 163 | II_MSE_LASTX := (20+A_PtrSize*2) | 0x1000 ;Motion in the X direction. This is signed relative motion or absolute 164 | ;motion, depending on the value of usFlags. 165 | II_MSE_LASTY := (24+A_PtrSize*2) | 0x1000 ;Motion in the Y direction. This is signed relative motion or absolute 166 | ;motion, depending on the value of usFlags. 167 | II_MSE_EXTRAINFO := (28+A_PtrSize*2) ;Device-specific additional information for the event. 168 | 169 | II_KBD_MAKECODE := (08+A_PtrSize*2) | 0x0100 ;Scan code from the key depression. The scan code for keyboard overrun is 170 | ;KEYBOARD_OVERRUN_MAKE_CODE. 171 | II_KBD_FLAGS := (10+A_PtrSize*2) | 0x0100 ;Flags for scan code information. It can be one or more of the following 172 | ;values -> see RI_KEY constants. 173 | II_KBD_VKEY := (14+A_PtrSize*2) | 0x0100 ;Microsoft Windows message compatible virtual-key code. 174 | II_KBD_MSG := (16+A_PtrSize*2) ;Corresponding window message, for example WM_KEYDOWN, WM_SYSKEYDOWN, and 175 | ;so forth. 176 | II_KBD_EXTRAINFO := (20+A_PtrSize*2) ;Device-specific additional information for the event. 177 | 178 | II_HID_SIZE := (08+A_PtrSize*2) ;Size, in bytes, of each HID input in bRawData. 179 | II_HID_COUNT := (12+A_PtrSize*2) ;Number of HID inputs in bRawData. 180 | 181 | ;DO NOT USE WITH AHKHID_GetInputInfo. Use AHKHID_GetInputData instead to retrieve the raw data. 182 | II_HID_DATA := (16+A_PtrSize*2) ;Raw input data as an array of bytes. 183 | ;__________________________________________________________________________________ 184 | ;Device type values returned by AHKHID_GetDevType as well as DI_DEVTYPE and II_DEVTYPE 185 | ;http://msdn.microsoft.com/en-us/library/ms645568 186 | RIM_TYPEMOUSE := 0 ;The device is a mouse. 187 | RIM_TYPEKEYBOARD := 1 ;The device is a keyboard. 188 | RIM_TYPEHID := 2 ;The device is an Human Interface Device (HID) that is not a keyboard and not a mouse. 189 | ;_______________________________________________________________________________________________ 190 | ;Different flags for RAWINPUTDEVICE structure (to be used with AHKHID_AddRegister and AHKHID_Register) 191 | ;http://msdn.microsoft.com/en-us/library/ms645565 192 | RIDEV_REMOVE := 0x00000001 ;If set, this removes the top level collection from the inclusion list. This tells the 193 | ;operating system to stop reading from a device which matches the top level collection. 194 | RIDEV_EXCLUDE := 0x00000010 ;If set, this specifies the top level collections to exclude when reading a complete 195 | ;usage page. This flag only affects a TLC whose usage page is already specified with 196 | ;RIDEV_PAGEONLY. 197 | RIDEV_PAGEONLY := 0x00000020 ;If set, this specifies all devices whose top level collection is from the specified 198 | ;usUsagePage. Note that usUsage must be zero. To exclude a particular top level 199 | ;collection, use RIDEV_EXCLUDE. 200 | RIDEV_NOLEGACY := 0x00000030 ;If set, this prevents any devices specified by usUsagePage or usUsage from generating 201 | ;legacy messages. This is only for the mouse and keyboard. See Remarks. 202 | RIDEV_INPUTSINK := 0x00000100 ;If set, this enables the caller to receive the input even when the caller is not in 203 | ;the foreground. Note that hwndTarget must be specified. 204 | RIDEV_CAPTUREMOUSE := 0x00000200 ;If set, the mouse button click does not activate the other window. 205 | RIDEV_NOHOTKEYS := 0x00000200 ;If set, the application-defined keyboard device hotkeys are not handled. However, the 206 | ;system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default, 207 | ;all keyboard hotkeys are handled. RIDEV_NOHOTKEYS can be specified even if 208 | ;RIDEV_NOLEGACY is not specified and hwndTarget is NULL. 209 | RIDEV_APPKEYS := 0x00000400 ;Microsoft Windows XP Service Pack 1 (SP1): If set, the application command keys are 210 | ;handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a 211 | ;keyboard device. 212 | RIDEV_EXINPUTSINK := 0x00001000 ;Vista and later only: If set, this enables the caller to receive input in the 213 | ;background only if the foreground application does not process it. In other words, if 214 | ;the foreground application is not registered for raw input, then the background 215 | ;application that is registered will receive the input. 216 | RIDEV_DEVNOTIFY := 0x00002000 ;Vista and later only: If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE 217 | ;notifications for device arrival and device removal. 218 | ;__________________________________________________ 219 | ;Different values of wParam in the WM_INPUT message 220 | ;http://msdn.microsoft.com/en-us/library/ms645590 221 | RIM_INPUT := 0 ;Input occurred while the application was in the foreground. The application must call 222 | ;DefWindowProc so the system can perform cleanup. 223 | RIM_INPUTSINK := 1 ;Input occurred while the application was not in the foreground. The application must call 224 | ;DefWindowProc so the system can perform the cleanup. 225 | ;__________________________________ 226 | ;Flags for GetRawInputData API call 227 | ;http://msdn.microsoft.com/en-us/library/ms645596 228 | RID_INPUT := 0x10000003 ;Get the raw data from the RAWINPUT structure. 229 | RID_HEADER := 0x10000005 ;Get the header information from the RAWINPUT structure. 230 | ;_____________________________________ 231 | ;Flags for RAWMOUSE (part of RAWINPUT) 232 | ;http://msdn.microsoft.com/en-us/library/ms645578 233 | 234 | ;Flags for the II_MSE_FLAGS member 235 | MOUSE_MOVE_RELATIVE := 0 ;Mouse movement data is relative to the last mouse position. 236 | MOUSE_MOVE_ABSOLUTE := 1 ;Mouse movement data is based on absolute position. 237 | MOUSE_VIRTUAL_DESKTOP := 0x02 ;Mouse coordinates are mapped to the virtual desktop (for a multiple monitor system) 238 | MOUSE_ATTRIBUTES_CHANGED := 0x04 ;Mouse attributes changed; application needs to query the mouse attributes. 239 | 240 | ;Flags for the II_MSE_BUTTONFLAGS member 241 | RI_MOUSE_LEFT_BUTTON_DOWN := 0x0001 ;Self-explanatory 242 | RI_MOUSE_LEFT_BUTTON_UP := 0x0002 ;Self-explanatory 243 | RI_MOUSE_RIGHT_BUTTON_DOWN := 0x0004 ;Self-explanatory 244 | RI_MOUSE_RIGHT_BUTTON_UP := 0x0008 ;Self-explanatory 245 | RI_MOUSE_MIDDLE_BUTTON_DOWN := 0x0010 ;Self-explanatory 246 | RI_MOUSE_MIDDLE_BUTTON_UP := 0x0020 ;Self-explanatory 247 | RI_MOUSE_BUTTON_4_DOWN := 0x0040 ;XBUTTON1 changed to down. 248 | RI_MOUSE_BUTTON_4_UP := 0x0080 ;XBUTTON1 changed to up. 249 | RI_MOUSE_BUTTON_5_DOWN := 0x0100 ;XBUTTON2 changed to down. 250 | RI_MOUSE_BUTTON_5_UP := 0x0200 ;XBUTTON2 changed to up. 251 | RI_MOUSE_WHEEL := 0x0400 ;Raw input comes from a mouse wheel. The wheel delta is stored in usButtonData. 252 | ;____________________________________________ 253 | ;Flags for the RAWKEYBOARD (part of RAWINPUT) 254 | ;http://msdn.microsoft.com/en-us/library/ms645575 255 | 256 | ;Flag for the II_KBD_MAKECODE member in the event of a keyboard overrun 257 | KEYBOARD_OVERRUN_MAKE_CODE := 0xFF 258 | 259 | ;Flags for the II_KBD_FLAGS member 260 | RI_KEY_MAKE := 0 261 | RI_KEY_BREAK := 1 262 | RI_KEY_E0 := 2 263 | RI_KEY_E1 := 4 264 | RI_KEY_TERMSRV_SET_LED := 8 265 | RI_KEY_TERMSRV_SHADOW := 0x10 266 | ;____________________________________ 267 | ;AHKHID FUNCTIONS 268 | 269 | If Not AHKHID_Included 270 | Return 271 | 272 | AHKHID_UseConstants() { 273 | Global ;To make the constants global 274 | AHKHID_Included := False 275 | Gosub, AHKHID_SetConstants 276 | } 277 | 278 | AHKHID_Initialize(bRefresh = False) { 279 | Static uHIDList, bInitialized := False 280 | 281 | If bInitialized And Not bRefresh 282 | Return &uHIDList 283 | 284 | ;Get the device count 285 | r := DllCall("GetRawInputDeviceList", "Ptr", 0, "UInt*", iCount, "UInt", A_PtrSize * 2) 286 | 287 | ;Check for error 288 | If (r = -1) Or ErrorLevel { 289 | ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 290 | Return -1 291 | } 292 | 293 | ;Prep var 294 | VarSetCapacity(uHIDList, iCount * (A_PtrSize * 2)) 295 | r := DllCall("GetRawInputDeviceList", "Ptr", &uHIDList, "UInt*", iCount, "UInt", A_PtrSize * 2) 296 | If (r = -1) Or ErrorLevel { 297 | ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 298 | Return -1 299 | } 300 | 301 | bInitialized := True 302 | Return &uHIDList 303 | } 304 | 305 | AHKHID_GetDevCount() { 306 | 307 | ;Get the device count 308 | r := DllCall("GetRawInputDeviceList", "Ptr", 0, "UInt*", iCount, "UInt", A_PtrSize * 2) 309 | 310 | ;Check for error 311 | If (r = -1) Or ErrorLevel { 312 | ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 313 | Return -1 314 | } Else Return iCount 315 | } 316 | 317 | AHKHID_GetDevHandle(i) { 318 | Return NumGet(AHKHID_Initialize(), (i - 1) * (A_PtrSize * 2)) 319 | } 320 | 321 | AHKHID_GetDevIndex(Handle) { 322 | Loop % AHKHID_GetDevCount() 323 | If (NumGet(AHKHID_Initialize(), (A_Index - 1) * (A_PtrSize * 2)) = Handle) 324 | Return A_Index 325 | Return 0 326 | } 327 | 328 | AHKHID_GetDevType(i, IsHandle = False) { 329 | Return Not IsHandle ? NumGet(AHKHID_Initialize(), ((i - 1) * (A_PtrSize * 2)) + A_PtrSize, "UInt") 330 | : NumGet(AHKHID_Initialize(), ((AHKHID_GetDevIndex(i) - 1) * (A_PtrSize * 2)) + A_PtrSize, "UInt") 331 | } 332 | 333 | AHKHID_GetDevName(i, IsHandle = False) { 334 | 335 | ;Get index if i is handle 336 | h := IsHandle ? i : AHKHID_GetDevHandle(i) 337 | 338 | ;Get device name length. RIDI_DEVICENAME 339 | r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x20000007, "Ptr", 0, "UInt*", iLength) 340 | If (r = -1) Or ErrorLevel { 341 | ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 342 | Return "" 343 | } 344 | 345 | ;Get device name. 346 | VarSetCapacity(s, (iLength + 1) * 2) ;RIDI_DEVICENAME 347 | r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x20000007, "Str", s, "UInt*", iLength) 348 | If (r = -1) Or ErrorLevel { 349 | ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 350 | Return "" 351 | } 352 | 353 | Return s 354 | } 355 | 356 | AHKHID_GetDevInfo(i, Flag, IsHandle = False) { 357 | Static uInfo, iLastHandle := 0 358 | 359 | ;Get index if i is handle 360 | h := IsHandle ? i : AHKHID_GetDevHandle(i) 361 | 362 | ;Check if the handle changed 363 | If (h = iLastHandle) ;It's the same device. No need to call again 364 | Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt") 365 | Else { 366 | 367 | ;Get device info buffer size. RIDI_DEVICEINFO 368 | r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x2000000b, "Ptr", 0, "UInt*", iLength) 369 | If (r = -1) Or ErrorLevel { 370 | ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 371 | Return -1 372 | } 373 | 374 | ;Get device info 375 | VarSetCapacity(uInfo, iLength) 376 | NumPut(iLength, uInfo, 0, "UInt") ;Put length in struct RIDI_DEVICEINFO 377 | r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x2000000b, "Ptr", &uInfo, "UInt*", iLength) 378 | If (r = -1) Or ErrorLevel { 379 | ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 380 | Return -1 381 | } 382 | 383 | ;Successful. Keep handle. 384 | iLastHandle := h 385 | 386 | ;Retrieve data 387 | Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt") 388 | } 389 | 390 | Return 0 391 | } 392 | 393 | AHKHID_AddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) { 394 | Static uDev, iIndex := 0, iCount := 0 395 | 396 | ;Check if we just want the address 397 | If Not (UsagePage Or Usage Or Handle Or Flags) 398 | Return &uDev 399 | ;Check if we just want the count 400 | Else If (UsagePage = "Count") 401 | Return iCount 402 | ;Check if we're dimensioning the struct 403 | Else If UsagePage And Not (Usage Or Handle Or Flags) { 404 | iCount := UsagePage 405 | iIndex := 0 406 | VarSetCapacity(uDev, iCount * (8 + A_PtrSize)) 407 | Return &uDev 408 | } 409 | 410 | ;Check if there's space before adding data to struct 411 | If (iIndex = iCount) 412 | Return -1 ;Full capacity 413 | 414 | ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE 415 | Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle 416 | 417 | ;Put in struct 418 | NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort") 419 | NumPut(Usage, uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort") 420 | NumPut(Flags, uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt") 421 | NumPut(Handle, uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr") 422 | 423 | ;Move to next slot 424 | iIndex += 1 425 | 426 | Return &uDev 427 | } 428 | 429 | AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0) { 430 | 431 | ;Check if we're using the AddRegister array or only a single struct 432 | If Not (UsagePage Or Usage Or Handle Or Flags) { 433 | 434 | ;Call 435 | r := DllCall("RegisterRawInputDevices", "Ptr", AHKHID_AddRegister(), "UInt", AHKHID_AddRegister("Count"), "UInt", 8 + A_PtrSize) 436 | 437 | ;Check for error 438 | If Not r { 439 | ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 440 | Return -1 441 | } 442 | 443 | ;Build struct and call 444 | } Else { 445 | 446 | ;Prep var 447 | VarSetCapacity(uDev, (8 + A_PtrSize), 0) 448 | 449 | ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE 450 | Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle 451 | 452 | NumPut(UsagePage, uDev, 0, "UShort") 453 | NumPut(Usage, uDev, 2, "UShort") 454 | NumPut(Flags, uDev, 4, "UInt") 455 | NumPut(Handle, uDev, 8, "Ptr") 456 | 457 | ;Call 458 | r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize) 459 | 460 | ;Check for error 461 | If Not r Or ErrorLevel { 462 | ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 463 | Return -1 464 | } 465 | } 466 | 467 | Return 0 468 | } 469 | 470 | AHKHID_GetRegisteredDevs(ByRef uDev) { 471 | 472 | ;Get length 473 | VarSetCapacity(iCount, 4, 0) 474 | r := DllCall("GetRegisteredRawInputDevices", "Ptr", 0, "UInt*", iCount, "UInt", 8 + A_PtrSize) 475 | If ErrorLevel { 476 | ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 477 | Return -1 478 | } 479 | 480 | If (iCount > 0) { 481 | 482 | ;Prep var 483 | VarSetCapacity(uDev, iCount * (8 + A_PtrSize)) 484 | 485 | ;Call 486 | r := DllCall("GetRegisteredRawInputDevices", "Ptr", &uDev, "UInt*", iCount, "UInt", 8 + A_PtrSize) 487 | If (r = -1) Or ErrorLevel { 488 | ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 489 | Return -1 490 | } 491 | } 492 | 493 | Return iCount 494 | } 495 | 496 | AHKHID_GetInputInfo(InputHandle, Flag) { 497 | Static uRawInput, iLastHandle := 0 498 | 499 | ;Check if it's the same handle 500 | If (InputHandle = iLastHandle) ;We can retrieve the data without having to call again 501 | Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ? (AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int" : (Flag = 8 ? "Ptr" : "UInt"))) 502 | Else { ;We need to get a fresh copy 503 | 504 | ;Get raw data size RID_INPUT 505 | r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2) 506 | If (r = -1) Or ErrorLevel { 507 | ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 508 | Return -1 509 | } 510 | 511 | ;Prep var 512 | VarSetCapacity(uRawInput, iSize) 513 | 514 | ;Get raw data RID_INPUT 515 | r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2) 516 | If (r = -1) Or ErrorLevel { 517 | ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 518 | Return -1 519 | } Else If (r <> iSize) { 520 | ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize% 521 | Return -1 522 | } 523 | 524 | ;Keep handle reference of current uRawInput 525 | iLastHandle := InputHandle 526 | 527 | ;Retrieve data 528 | Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ? (AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int" : (Flag = 8 ? "Ptr" : "UInt"))) 529 | } 530 | 531 | Return 0 532 | } 533 | 534 | AHKHID_GetInputData(InputHandle, ByRef uData) { 535 | 536 | ;Get raw data size RID_INPUT 537 | r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2) 538 | If (r = -1) Or ErrorLevel { 539 | ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 540 | Return -1 541 | } 542 | 543 | ;Prep var 544 | VarSetCapacity(uRawInput, iSize) 545 | 546 | ;Get raw data RID_INPUT 547 | r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2) 548 | If (r = -1) Or ErrorLevel { 549 | ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 550 | Return -1 551 | } Else If (r <> iSize) { 552 | ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize% 553 | Return -1 554 | } 555 | 556 | ;Get the size of each HID input and the number of them 557 | iSize := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE 558 | iCount := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT 559 | 560 | ;Allocate memory 561 | VarSetCapacity(uData, iSize * iCount) 562 | 563 | ;Copy bytes 564 | DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount) 565 | 566 | Return (iSize * iCount) 567 | } 568 | 569 | ;Internal use only 570 | AHKHID_NumIsShort(ByRef Flag) { 571 | If (Flag & 0x0100) { 572 | Flag ^= 0x0100 ;Remove it 573 | Return True 574 | } Return False 575 | } 576 | 577 | ;Internal use only 578 | AHKHID_NumIsSigned(ByRef Flag) { 579 | If (Flag & 0x1000) { 580 | Flag ^= 0x1000 ;Remove it 581 | Return True 582 | } Return False 583 | } 584 | -------------------------------------------------------------------------------- /ModeSwitch/ahk/src/modeswitch.ahk: -------------------------------------------------------------------------------- 1 | #SingleInstance Force 2 | #Persistent 3 | #include AHKHID.ahk 4 | 5 | MsgBox Sorry, this script is currently broken (I now need to get it to send a proper feature report instead of an output report) 6 | ExitApp 7 | 8 | hArd := HID_Open("1ccf","1000") 9 | while (hLED = -1) { 10 | hArd := HID_Open("1ccf","1000") 11 | Sleep, 5000 12 | } 13 | if 0 < 1 ; The left side of a non-expression if-statement is always the name of a variable. 14 | { 15 | MsgBox This script requires a parameter 16 | ExitApp 17 | } 18 | 19 | param := %A_Index% 20 | mode := %param% + 16 21 | if (mode > 21) or (mode < 16){ 22 | MsgBox, Incorrect value %mode% 23 | ExitApp 24 | } 25 | changeMode(hArd,mode) 26 | HID_Close(hArd) 27 | ExitApp 28 | 29 | changeMode(hArd,Num){ 30 | ;MsgBox, sending %Num% 31 | TestString = "aa" 32 | NumPut(5,TestString,0,"UInt") 33 | NumPut(0,TestString,1, "UInt") 34 | NumPut(0,TestString,2, "UInt") 35 | NumPut(0,TestString,3, "UInt") 36 | NumPut(Num,TestString,4, "UInt") 37 | ;outputdebug teststring %TestString% 38 | HID_Write(hArd,TestString,5) 39 | } 40 | 41 | ;DLL CALLS 42 | HID_Open(VID,PID){ 43 | str = VID_%VID%`&PID_%PID% 44 | iCount := AHKHID_GetDevCount() 45 | Loop , %icount% 46 | { 47 | HID0 += 1 48 | name := AHKHID_GetDevName(HID0) 49 | type := AHKHID_GetDevType(HID0) 50 | ;outputdebug str to find is %str% 51 | if type = 2 52 | { 53 | if name contains %str% 54 | { 55 | ;outputdebug found %VID%:%PID% under name: %name% 56 | return HandleFromName(name) 57 | } 58 | } 59 | } 60 | ;outputdebug (%VID%:%PID% not found) 61 | return -1 62 | } 63 | 64 | HandleFromName(FileName){ 65 | GENERIC_READ = 0x80000000 ; Open the file for reading rather than writing. 66 | GENERIC_WRITE = 0x40000000 67 | OPEN_EXISTING = 3 ; This mode indicates that the file to be opened must already exist. 68 | FILE_SHARE_READ = 0x1 ; This and the next are whether other processes can open the file while we have it open. 69 | FILE_SHARE_WRITE = 0x2 70 | ;outputdebug Calling Createfile on %FileName% 71 | hFile := DllCall("CreateFile", Str, FileName, UInt, GENERIC_READ|GENERIC_WRITE, UInt, FILE_SHARE_READ|FILE_SHARE_WRITE, Ptr, 0, UInt, OPEN_EXISTING, UInt, 0, Ptr, 0) 72 | If (hFile = -1) Or ErrorLevel 73 | { 74 | ErrorLevel = CreateFile call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 75 | outputdebug (%A_LineNumber%) %errorlevel% 76 | Return -1 77 | } 78 | ;outputdebug file handle %hFile% 79 | return hFile 80 | } 81 | 82 | HID_Write(handle,data,len=-1){ 83 | BytesToWrite := (len>0) ? len : VarSetCapacity(data, -1) 84 | ;outputdebug length %BytesToWrite% 85 | r := DllCall("WriteFile", Ptr, handle, Str, data, UInt, BytesToWrite, UIntP, BytesActuallyWritten, Ptr, 0) 86 | If (r = -1) Or ErrorLevel 87 | { 88 | ErrorLevel = WriteFile call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError% 89 | outputdebug (%A_LineNumber%) %errorlevel% 90 | Return -1 91 | } 92 | ;outputdebug writefile %r% 93 | return BytesActuallyWritten 94 | } 95 | 96 | HID_Close(handle){ 97 | DllCall("CloseHandle", Ptr, handle) ; Close the file. 98 | } 99 | -------------------------------------------------------------------------------- /ModeSwitch/readme.md: -------------------------------------------------------------------------------- 1 | # NOTE: ModeSwitch is broken for now, fixing is low priority as long as nobody needs it, so open an issue if needed. 2 | 3 | # ModeSwitch for the Pop'n Ultimate Controller 4 | 5 | This is a binary meant to set the LightMode used by the Pop'n Controller. It is mostly useful for multi-boot frontends as it allows to switch to the correct mode before launching a game. 6 | 7 | For ease of use I included a pre-compiled binary working on WinXP Embedded (for official Pop'n Music cabinet). 8 | 9 | While the autohotkey version works perfectly on my computer I couldn't make it work on the BemaniPC. I'm still including it here as it provides a good basis for HID light control with simple scripts. 10 | 11 | ## Usage 12 | 13 | ```modeswitch.exe ``` 14 | 15 | Where ```` can take the value 16 | 17 | 0 : Simulated mode (Reactive buttons + AC lighting simulation for emulators) 18 | 19 | 1 : HID only (original AC mode) 20 | 21 | 2 : Mixed (AC mode combined with lights from any button pressed) 22 | 23 | 3 : Invert (Like Mixed except ON is OFF and vice versa) 24 | 25 | ## How it works 26 | 27 | On the Pop'n Ultimate Controller firmware, HID report ID 5 is a 32bit bitfield : 28 | 29 | - bits 0 to 17 are the HID lamps. 30 | - bit 28 is the magic lightMode change request bit 31 | - bits 24 and 25 encode the requested lightMode if bit 28 is set 32 | 33 | This binary opens the USB device whose path is written in ```devicepath.dat``` and sends the correct HID report. 34 | 35 | The autohotkey version finds the path automatically based on VID=2341 and PID=003E. 36 | 37 | ## How to retrieve devicepath (for the C version) 38 | 39 | The included devicepath.dat should work with both Due and Leonardo versions out of the box, but in case you get the message ``Couldn't open device. Make sure devicepath.dat contains the correct path.`` and you are sure the device is plugged in, you can either go into windows device manager and look for the device path in the advanced properties of your arduino device, or the easiest way is to look at the value inside spicetools or bemanitools configuration files. 40 | 41 | devicepath.dat file should contain the device path on its own on a single line. Multiple paths on multiple lines should work too in which case it will open the first valid path it finds. 42 | 43 | If no valid path is found or if there is no `devicepath.dat` file, it will attempt to open the default paths from the Due and Leonardo versions (same as the ones found in this included devicepath file). 44 | 45 | ### spicetools 46 | 47 | open ```%appdata%/spicetools.xml```, look for a "devid" value starting with ```\\?\HID#VID_2341&PID_003E``` (Due) or ```\\?\HID#VID_2341&PID_8036``` (Leonardo) 48 | 49 | Note that you'll have to replace "```&```" occurrences by "```&```". 50 | 51 | ### bemanitools 52 | 53 | open ```%appdata%/DJHACKERS/pnm_v4_25.bin``` and look for a string starting with ```\\?\HID#VID_2341&PID_003E``` (Due) or ```\\?\HID#VID_2341&PID_8036``` (Leonardo) 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/donate?hosted_button_id=WT735CX4UMZ9U) 2 | 3 | # Ultimate Pop'n Controller 4 | 5 | USB HID controller with 13 inputs (9 buttons, coin, test, reset, service), 20 named outputs (18 lights, coin blocker, coin counter), 4 features (dip switches), 12-key matrix numpad, high polling rate, custom dll for full cabinet compatibility, Pop'n beMouse and Lively native compatibility, and nice programmable features. 6 | 7 | The goal was to replace the official IO Board from my pop'n cabinet with a DIY controller instead, for stability reasons (Konami IO boards are prone to failure, most Pop'n Music cabinets in the wild have broken lamps...) but also for QoL improvements (this allows me to use my panel system-wide and not only in-game, which means I can control a multiboot menu, play with emulators etc..). 8 | 9 | In combination with the PN5180-cardio project, the whole IO from a Pop'n Music cabinet can be replaced pretty cheaply :) 10 | 11 | # Leonardo version 12 | 13 | This code was originally written for Arduino Due but is also compatible with Leonardo without any change required. 14 | 15 | Due to lack of gpio, in this case there's only 11 buttons and 9 lights, no keypad support, and reactive mode won't include AC light simulation (there's no side or top lamps, but you might want to have a look at the ambilight branch for ws2812b side/top lamps). However, there is **Playstation compatibility** as well. 16 | 17 | I'm also taking advantage of the Leonardo EEPROM. On manually switching, the resulting lightmode is stored in the EEPROM so that it persists on controller disconnect/reconnect. 18 | 19 | You only need to select "Leonardo" as your board type in Arduino IDE before flashing. 20 | 21 | # Demo 22 | 23 | https://www.instagram.com/p/CKE9HCQFCYM/ 24 | 25 | https://www.instagram.com/p/CBWgSMklvvT/ 26 | 27 | # Acknowledgments 28 | 29 | Other pop'n controller codes like Knuckleslee's https://github.com/knuckleslee/RhythmCodes/ or 4yn's https://github.com/4yn/iivx/tree/master/leoo/leoo are widely popular. 30 | 31 | They are usually based on mon's https://github.com/mon/Arduino-HID-Lighting which is based on NicoHood's https://github.com/NicoHood/HID/blob/master/src/SingleReport/RawHID.cpp 32 | 33 | As I needed more gpio than usual, this project was my first time working with Arduino Due and unfortunately the USB HID definitions are not compatible with Leonardo code so a lot of changes had to be made. 34 | 35 | The keypad code uses the Keypad library by Mark Stanley and Alexander Brevig. 36 | 37 | Switch debouncing is done with Bounce2 library by Thomas O Fredericks. 38 | 39 | Playstation compatibility uses parts of progmem's excellent https://github.com/progmem/re-usbemani/ project 40 | 41 | # Supported devices and requirements 42 | 43 | This code was designed for Arduino Due. It will compile for Leonardo as well but some features are stripped due to lack of gpio (only 11 buttons and 9 lights, no keypad, lower polling rate). 44 | Code is updated automatically depending on the selected target board, no changes are needed. 45 | 46 | The controller code requires the Bounce2 library by Thomas O Fredericks. It can be installed from the Arduino IDE Library manager. 47 | 48 | The keypad code requires the Keypad library by Mark Stanley and Alexander Brevig. It can be installed from the Arduino IDE Library manager. 49 | 50 | # Features 51 | 52 | ## I/O 53 | 54 | This controller has 13 buttons (9 buttons + coin + service + reset + test), 4 dip-switches (only DIP4 is used in cabinets to select between 15 or 31kHz monitor resolution on boot), 18 lights (9 buttons + 5 top neon + 4 side pillar), 2 coin outputs (coin blocker, coin counter). 55 | 56 | It also has 7 pins for the 12-key numpad, following the original cabinet pinout. It is recognized as a separate keyboard, and it is mapped to the keyboard toprow rather than the numpad (due to lack of stability when sending the numlock command to the BemaniPC). 57 | 58 | The 00 key is mapped to comma (for direct compatibility with spicetools) and the originally unused bottom right key is mapped to the default card scan key. 59 | 60 | It also has a cool light animation on boot which you can easily adapt to your liking :) 61 | 62 | ## ezusb driver 63 | 64 | The I/O is HID so it can be mapped with the usual IO emulation tools, and even has named outputs for ease of use, but it can also be used just like an official IOBoard, provided you replace the original `ezusb.dll` file with the one from this repo. 65 | 66 | This way the firmware is fully compatible with anything that works on an official cabinet (PopnForwarder (a bit silly but why not :D), DJMame, ezPSXe pad plugin...) 67 | 68 | ## pop'n beMouse 69 | 70 | The controller will enumerate in a way that is directly compatible with the old Pop'n beMouse software (it also directly works with Pop'n Lively). 71 | 72 | ## Light modes 73 | 74 | There are 5 different modes : 75 | 76 | ### Reactive mode 77 | 78 | This mode roughly simulates AC lighting and was meant to be used for emulators or other games with no HID light support. 79 | 80 | Pressing a button will trigger blinking lights from the side pillars, blue most of the time, red with 1/7 probability, and purple with 1/20 probability. 81 | 82 | The top neons follow a fill-empty pattern whose speed is dynamically adjusted with the rate at which you press buttons (might need more work tweaking values, but looks good enough already). 83 | 84 | ### HID mode 85 | 86 | This is the original IO board mode where only messages from the game can control the lamps. 87 | 88 | ### Mixed mode (default mode) 89 | 90 | This mode behaves like HID mode as soon as messages are received. If 3 seconds elapse without any received message, the firmware switches to reactive behavior (and will switch back to HID as soon as new HID messages are received). 91 | 92 | ### Combined mode 93 | 94 | This combines the HID messages with button presses for instant lighting (you don't have to wait for the game to register the input and send a message back to light the lamp, and as a bonus it also allows you to play with the lights while the cabinet is booting ;) ). 95 | 96 | ### Invert mode 97 | 98 | This is like the Mixed mode except ON and OFF states are switched around just for the fun of it. Might be even more fun to use with colored leds and nothing but white buttons. 99 | 100 | ## Switching lightmodes 101 | 102 | You can either press button 2 (left yellow button) while holding service to switch between modes, or you can request a mode change with an HID message, which is incredibly useful in a multiboot environment as you can set the proper lightmode from the commandline before launching a game. 103 | 104 | I included pre-compiled binaries and sources in the "ModeSwitch" folder. Refer to readme.md inside that folder for more details. 105 | 106 | # Pinout (DUE) 107 | 108 | The Arduino DUE has 3.3v logic whereas the Pop'n Music cabinet outputs use 12V. 109 | 110 | Therefore I'm using transistors to do level shifting (the parts I used were three ULN2003APG or TBD62003APG chips). 111 | 112 | You can also buy pre-made level shifters such as this one https://www.tindie.com/products/ddebeer/12-channel-level-shifter-and-buffer/ (you'd need two of them since there are 20 outputs to control for a Pop'n cabinet). 113 | 114 | Refer to ```pinout.png``` to see how it is all wired to a Pop'n Music cabinet. 115 | 116 | ![pinout](https://github.com/CrazyRedMachine/UltimatePopnController/blob/master/pinout.png?raw=true) 117 | 118 | ## Pop'n IOShield 119 | 120 | I've also built a custom arduino shield with integrated level-shifting circuitry. This allows you to simply connect a Pop'n Music cabinet through 2 connectors present in your cab (unplug CN7 and CN8 from the power distribution board and plug them on this shield instead, then splice 12V from CN63 connector and feed it to the shield 12V in connector). You can contact me for more information. 121 | 122 | ![shield](https://github.com/CrazyRedMachine/UltimatePopnController/blob/master/shield.png?raw=true) 123 | 124 | Demo : https://www.instagram.com/p/CKE9HCQFCYM/ 125 | 126 | # Pinout (Leonardo) 127 | 128 | Arduino Leonardo has 5V logic therefore one can directly connect 5V leds to it. 129 | 130 | ![pinout](https://github.com/CrazyRedMachine/UltimatePopnController/blob/master/pinout_leonardo.png?raw=true) 131 | 132 | ## Playstation compatibility 133 | 134 | Arduino Leonardo version is also compatible with Playstation and Playstation 2 (it can be made to be plugged directly to the controller port, using the following pinout). 135 | 136 | ![pinout_psx](https://github.com/CrazyRedMachine/UltimatePopnController/blob/master/pinout_leonardo_psx.png?raw=true) 137 | 138 | For ACK (TXLED aka PD5) and SS (RXLED aka PB0) you have to solder new headers or cables directly on the leonardo PCB (or you can use an Arduino Micro (not pro micro) which has everything broken out). 139 | 140 | LEDs will be dimmer due to 3.3v power. Using the 7V rumble motor line to Vin instead, and using NPN transistors like 2N2222A on MISO and ACK lines to prevent backfeeding voltage into the console will solve the issue (set INVERT_CIPO and INVERT_ACK to 1 in `ps2.c`). 141 | 142 | **BEWARE: DO NOT PLUG USB AND PSX AT THE SAME TIME, THIS CAN DAMAGE YOUR CONSOLE** 143 | 144 | # Donation 145 | 146 | If this project helps you and you want to give back, you can help me with my future projects. 147 | 148 | While not necessary, donations are much appreciated and will only go towards funding future github projects (arcade hardware ain't cheap :( ). 149 | 150 | Of course you'll also receive my gratitude and I'll remember you if you post a feature request ;) 151 | 152 | [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/donate?hosted_button_id=WT735CX4UMZ9U) 153 | -------------------------------------------------------------------------------- /ezusb/ezusb-popn15.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/ezusb/ezusb-popn15.dll -------------------------------------------------------------------------------- /ezusb/ezusb.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/ezusb/ezusb.dll -------------------------------------------------------------------------------- /ezusb/readme.md: -------------------------------------------------------------------------------- 1 | # ezusb.dll for the Pop'n Ultimate Controller 2 | 3 | This is a dll meant to seamlessly replace the original one from the different games (and compatible with both the PopnForwarder and DJMame, or really anything else meant to be used with ezusb.dll). 4 | 5 | For ease of use I included pre-compiled dll working on WinXP Embedded (for official Pop'n Music cabinets). 6 | 7 | ## Usage 8 | 9 | Replace the game ezusb.dll file with this one and the game will read input and send lights to your Arduino as if it were the original IOBoard. 10 | 11 | Pop'n Music Adventure used a different dll format so you have to set a flag in `ezusb.h` for the dll to compile in a way that will work with this game. 12 | 13 | The dll makes the lights turn on for 1 sec on load so you'll know if the game managed to load your device. 14 | 15 | ## How to retrieve devicepath 16 | 17 | The dll will attempt to open the device automatically based on the custom VID/PID. In case it doesn't work you can put a `devicepath.dat` file in the same folder as the dll. -------------------------------------------------------------------------------- /ezusb/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(UltimatePopnController-ezusb) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | include_directories(${ezusb_SOURCE_DIR}) 7 | link_directories(${PROJECT_SOURCE_DIR}/include) 8 | link_directories(${PROJECT_SOURCE_DIR}/lib) 9 | 10 | add_library(ezusb SHARED ezusb.cpp ezusb.h) 11 | target_link_libraries(ezusb hid) 12 | SET_TARGET_PROPERTIES(ezusb PROPERTIES PREFIX "") 13 | -------------------------------------------------------------------------------- /ezusb/src/ezusb.cpp: -------------------------------------------------------------------------------- 1 | // dllmain.cpp : Defines the entry point for the DLL application. 2 | #include "stdafx.h" 3 | #include "ezusb.h" 4 | extern "C" { 5 | // This file is in the Windows DDK available from Microsoft. 6 | #include 7 | #include 8 | } 9 | #include 10 | #include 11 | #include "ezusb.h" 12 | 13 | //#define DEBUG 14 | //#define DEBUG_LAMP 15 | 16 | HANDLE g_hid_handle; 17 | uint8_t g_dip_state = 0; 18 | // GUID_DEVCLASS_HIDCLASS 19 | static GUID hidclass_guid = {0x745a17a0, 0x74d3, 0x11d0, 0xb6, 0xfe, 0x00, 0xa0, 0xc9, 0x0f, 0x57, 0xda}; 20 | 21 | BOOLEAN get_device_path(wchar_t *lPath) 22 | { 23 | 24 | const GUID *guid = &hidclass_guid; 25 | HidD_GetHidGuid(&hidclass_guid); 26 | // Get device interface info set handle 27 | // for all devices attached to system 28 | HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // Function class devices. 29 | if(hDevInfo == INVALID_HANDLE_VALUE) 30 | return FALSE; 31 | 32 | // Retrieve a context structure for a device interface of a device information set. 33 | BYTE buf[1024]; 34 | PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buf; 35 | SP_DEVICE_INTERFACE_DATA spdid; 36 | SP_DEVINFO_DATA spdd; 37 | DWORD dwSize; 38 | 39 | spdid.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 40 | 41 | // Iterate through all the interfaces and try to match one based on 42 | // the device number. 43 | DWORD device_index = 0; 44 | 45 | for(DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL,guid, i, &spdid); i++) 46 | { 47 | // Get the device path. 48 | dwSize = 0; 49 | SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 0, &dwSize, NULL); 50 | if(dwSize == 0 || dwSize > sizeof(buf)) 51 | continue; 52 | 53 | pspdidd->cbSize = sizeof(*pspdidd); 54 | ZeroMemory((PVOID)&spdd, sizeof(spdd)); 55 | spdd.cbSize = sizeof(spdd); 56 | if(!SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd, 57 | dwSize, &dwSize, &spdd)) 58 | continue; 59 | 60 | /* check if the device contains our wanted vid/pid */ 61 | if ( wcsstr( pspdidd->DevicePath, L"vid_1ccf&pid_1000&mi_02" ) == NULL ) 62 | { 63 | continue; 64 | } 65 | #ifdef DEBUG 66 | printf("\r\nDevice found at %S\r\n", &pspdidd->DevicePath); 67 | #endif 68 | //copy devpath into lPath 69 | wcscpy(lPath, pspdidd->DevicePath); 70 | SetupDiDestroyDeviceInfoList(hDevInfo); 71 | return TRUE; 72 | } 73 | SetupDiDestroyDeviceInfoList(hDevInfo); 74 | return FALSE; 75 | } 76 | 77 | /** 78 | * Initialize the g_hid_handle global variable 79 | * It will attempt to read paths from devicepath.dat file if it exists until it finds one which works. 80 | * If nothing is found, as a last resort it will try the two default paths from the Due and Leonardo 81 | * versions of the firmware. 82 | * 83 | * @return 0 on success, -1 on error 84 | */ 85 | static int controller_init(){ 86 | static uint8_t err_count = 0; 87 | wchar_t lPath[256]; 88 | char path[256]; 89 | FILE *file; 90 | 91 | /* first check if there's a devicepath.dat override */ 92 | file = fopen("devicepath.dat", "r"); 93 | if (file != NULL) 94 | { 95 | while ( fgets(path,256,file) != NULL ) 96 | { 97 | path[strcspn(path, "\r\n")] = 0; 98 | g_hid_handle = CreateFileA(path, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 99 | if ( g_hid_handle != INVALID_HANDLE_VALUE ) 100 | { 101 | fclose(file); 102 | return 0; 103 | } 104 | } 105 | fclose(file); 106 | } 107 | 108 | /* auto detect device path */ 109 | if (!get_device_path(lPath)) 110 | { 111 | printf("\r\nPOPN MUSIC USB not detected.\r\n"); 112 | err_count++; 113 | if (err_count > 2){ 114 | printf("Could not init device after multiple attempts. Exiting.\r\n"); 115 | exit(1); 116 | } 117 | return -1; 118 | } 119 | 120 | g_hid_handle = CreateFile(lPath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 121 | 122 | if ( g_hid_handle == INVALID_HANDLE_VALUE ) 123 | { 124 | printf("Could not open detected device (err = %x).\r\n", GetLastError()); 125 | return -1; 126 | } 127 | return 0; 128 | } 129 | 130 | /** 131 | * Read buttons state from the board and convert them into Pop'n Music format 132 | * 133 | * Pop'n Music processes button states as a uint32_t bitfield with the following format : 134 | * bits 0 (LSB) to 5 : nothing 135 | * bit 6 : service 136 | * bit 7 : test 137 | * bits 8 to 16 : buttons 1 to 9 138 | * bit 22 : coin mech 139 | * bits 24 to 27 : dip 1 to 4 140 | * 141 | * @param pad_bits[out] button state bitfield 142 | * @return 0 on success, -1 on error 143 | */ 144 | static int controller_read_buttons(uint32_t *pad_bits){ 145 | DWORD bytesRead = 0; 146 | uint8_t res[4]; 147 | unsigned char buf[6]; // gamepad report length is 3 bytes in firmware, doubled because NumInputBuffer is set to 2 148 | 149 | buf[0] = 0x04; // gamepad report ID is 4 in firmware 150 | *pad_bits = 0; 151 | 152 | ReadFile(g_hid_handle, buf, 6, &bytesRead, NULL); 153 | // bytesRead should either be 6 (if it successfully read 2 reports) or 3 (only one) 154 | if ( bytesRead != 6 && bytesRead != 3 ) 155 | { 156 | #ifdef DEBUG 157 | printf("HID read error (got %u bytes)\n",bytesRead); 158 | #endif 159 | return -1; 160 | } 161 | 162 | /* HID read ok, convert latest report bytes to pop'n bitfield */ 163 | res[3] = g_dip_state; // dip switches 164 | res[2] = (buf[bytesRead-1]<<3 | buf[bytesRead-1]) & 0x41; // button 9 (0x01) and coin mech (0x40) 165 | res[1] = buf[bytesRead-2]; // button 1 to 8 166 | res[0] = 0; 167 | buf[bytesRead-1] >>= 1; 168 | if ( buf[bytesRead-1]&1 ) res[0] |= 0x80; // test 169 | buf[bytesRead-1] >>= 1; 170 | if ( buf[bytesRead-1]&1 ) res[0] |= 0x40; // service 171 | *pad_bits = *(uint32_t *)res; 172 | 173 | #ifdef DEBUG 174 | for (int i = 0; i<32; i++){ 175 | printf("%c",( ((uint32_t )*pad_bits>>(31-i))&1)?'1':'0'); 176 | if (((i+1)%4)==0) printf(" "); 177 | } 178 | printf("\n"); 179 | if (*pad_bits & 0x8000000) printf("31kHz mode\r\n"); 180 | #endif 181 | return 0; 182 | } 183 | 184 | /** 185 | * Read lights state and send the corresponding HID report to the Arduino 186 | * 187 | * Pop'n Music sends lights state as a int32_t bitfield with the following format : 188 | * bits 0 (LSB) to 4 : neons from top to bottom 189 | * bits 8 to 11 : side lamps (left blue, left red, right blue, right red) 190 | * bits 16 to 19 : coin blocker (0x0 for on, 0xF for off) 191 | * bits 23 to 31 : button lamps from left to right (note: technically game sets the whole 20-23 to 0xF rather than 0x8 for lamp 1) 192 | * 193 | * @param lamp_bits[in] lamp state bitfield 194 | * @return 0 on success, -1 on error 195 | */ 196 | static int controller_write_leds(int32_t lamp_bits){ 197 | DWORD bytesWritten = 0; 198 | uint8_t *src = (uint8_t*) &lamp_bits; //cast as uint8_t array for fast conversion 199 | uint8_t buf[5]; // HID lights report length is 5 bytes in Arduino firmware 200 | 201 | #ifdef DEBUG_LAMP 202 | for (int i = 0; i<32; i++){ 203 | printf("%c",( ((uint32_t )lamp_bits>>(31-i))&1)?'1':'0'); 204 | if (((i+1)%4)==0) printf(" "); 205 | } 206 | printf("\n"); 207 | #endif 208 | 209 | /* convert bitfield into HID Report format */ 210 | buf[4] = 0; // hid report padding 211 | buf[3] = ((src[1]&0x0F) >> 2); // right lamps 212 | if ( !(src[2] & 0x0F) ) buf[3] |= 0x04; // coin blocker 213 | if ( src[1] & 0xF0 ) buf[3] |= 0x08; // coin counter? 214 | buf[2] = (src[1] << 6) | (src[0] << 1) | (src[3] >> 7); // button 9, top and left lamps 215 | buf[1] = (src[3] << 1) | (src[2] >> 7); // button lamps 1-8 216 | buf[0] = 0x05; // HID lights report id 217 | 218 | /* send HID Report */ 219 | WriteFile(g_hid_handle, buf, 5, &bytesWritten, NULL); 220 | if ( bytesWritten == 5 ) 221 | return 0; 222 | 223 | #ifdef DEBUG_LAMP 224 | printf("Error sending HID report (%u bytes written)\r\n",bytesWritten); 225 | #endif 226 | return -1; 227 | } 228 | 229 | 230 | /* EZUSB EXPORTS */ 231 | 232 | #ifdef POPN15_FORMAT 233 | extern "C" { 234 | #endif 235 | 236 | __declspec(dllexport) int __cdecl usbCheckAlive() { 237 | return 1; 238 | } 239 | 240 | __declspec(dllexport) int __cdecl usbCheckSecurityNew(int i) { 241 | return 0; 242 | } 243 | 244 | __declspec(dllexport) int __cdecl usbCoinGet(int i) { 245 | return 0; 246 | } 247 | 248 | __declspec(dllexport) int __cdecl usbCoinMode(int i) { 249 | return 0; 250 | } 251 | 252 | __declspec(dllexport) int __cdecl usbEnd() { 253 | CloseHandle(g_hid_handle); 254 | return 0; 255 | } 256 | 257 | __declspec(dllexport) int __cdecl usbFirmResult() { 258 | return 0; 259 | } 260 | 261 | __declspec(dllexport) int __cdecl usbGetKEYID(unsigned char *a, int i) { 262 | return 0; 263 | } 264 | 265 | __declspec(dllexport) int __cdecl usbGetSecurity(int i, unsigned char *a) { 266 | return 0; 267 | } 268 | 269 | __declspec(dllexport) int __cdecl usbLamp(int32_t lamp_bits) { 270 | return controller_write_leds(lamp_bits); 271 | } 272 | 273 | __declspec(dllexport) int __cdecl usbPadRead(unsigned long *pad_bits) { 274 | return controller_read_buttons((uint32_t *)pad_bits); 275 | } 276 | 277 | __declspec(dllexport) int __cdecl usbPadReadLast(uint8_t *a1) { 278 | /* This memset is what is being done by the usual hooks. 279 | * However, it breaks Pop'n 15~18 compatibility and seems 280 | * to have no effect on later Pop'n so I removed it... */ 281 | //memset(a1, 0, 40); 282 | return 0; 283 | } 284 | 285 | __declspec(dllexport) int __cdecl usbSecurityInit() { 286 | return 0; 287 | } 288 | 289 | __declspec(dllexport) int __cdecl usbSecurityInitDone() { 290 | return 0; 291 | } 292 | 293 | __declspec(dllexport) int __cdecl usbSecuritySearch() { 294 | return 0; 295 | } 296 | 297 | __declspec(dllexport) int __cdecl usbSecuritySearchDone() { 298 | return 0; 299 | } 300 | 301 | __declspec(dllexport) int __cdecl usbSecuritySelect(int i) { 302 | return 0; 303 | } 304 | 305 | __declspec(dllexport) int __cdecl usbSecuritySelectDone() { 306 | return 0; 307 | } 308 | 309 | __declspec(dllexport) int __cdecl usbSetExtIo(int i) { 310 | return 0; 311 | } 312 | 313 | __declspec(dllexport) int __cdecl usbStart(int i) { 314 | if (controller_init() == -1) 315 | { 316 | printf("Could not init device.\r\n"); 317 | return -1; 318 | } 319 | 320 | BOOLEAN hidres = HidD_SetNumInputBuffers( 321 | g_hid_handle, 322 | 2); 323 | if (!hidres) 324 | { 325 | printf("Error %d setnuminputbuff\r\n",GetLastError()); 326 | return -1; 327 | } 328 | 329 | /* read dip switches */ 330 | uint8_t feat[2] = {0x06, 0x00}; 331 | if (!HidD_GetFeature(g_hid_handle, &feat, 2)) 332 | { 333 | printf("Cannot read dipswitches (error %d)\r\n",GetLastError()); 334 | }; 335 | g_dip_state = feat[1]; 336 | 337 | #ifdef FORCE_DIP4 338 | g_dip_state |= 0x08; 339 | #endif 340 | /* light everything for 1 sec to get visual confirmation 341 | * the device is working */ 342 | controller_write_leds((uint32_t) 0xFFFFFFFF); 343 | Sleep(1000); 344 | controller_write_leds((uint32_t) 0x00000000); 345 | 346 | #ifdef DEBUG 347 | printf("Managed to open device %x\n",g_hid_handle); 348 | printf("Dip state is %x\n", g_dip_state); 349 | #endif 350 | return 0; 351 | } 352 | 353 | __declspec(dllexport) int __cdecl usbWdtReset() { 354 | return 0; 355 | } 356 | 357 | __declspec(dllexport) int __cdecl usbWdtStart(int i) { 358 | return 0; 359 | } 360 | 361 | __declspec(dllexport) int __cdecl usbWdtStartDone() { 362 | return 0; 363 | } 364 | 365 | #ifdef POPN15_FORMAT 366 | } 367 | #endif 368 | 369 | 370 | BOOL APIENTRY DllMain( HMODULE hModule, 371 | DWORD fdwReason, 372 | LPVOID lpReserved 373 | ) { 374 | if (fdwReason == 1) { 375 | //https://blogs.msdn.microsoft.com/larryosterman/2004/06/03/little-known-win32-apis-disablethreadlibrarycalls/ 376 | DisableThreadLibraryCalls(hModule); 377 | } 378 | 379 | return 1; 380 | } 381 | 382 | -------------------------------------------------------------------------------- /ezusb/src/ezusb.h: -------------------------------------------------------------------------------- 1 | #ifndef EZUSB_UPC_EZUSB_H 2 | #define EZUSB_UPC_EZUSB_H 3 | 4 | /* Pop'n Music Adventure ezusb.dll was compiled in C and thus used unmangled function names 5 | * set this POPN15_FORMAT to 1 to compile a compatible version */ 6 | #define POPN15_FORMAT 0 7 | 8 | #if POPN15_FORMAT == 1 9 | extern "C" { 10 | #endif 11 | __declspec(dllexport) int __cdecl usbCheckAlive(); 12 | __declspec(dllexport) int __cdecl usbCheckSecurityNew(int i); 13 | __declspec(dllexport) int __cdecl usbCoinGet(int i); 14 | __declspec(dllexport) int __cdecl usbCoinMode(int i); 15 | __declspec(dllexport) int __cdecl usbEnd(); 16 | __declspec(dllexport) int __cdecl usbFirmResult(); 17 | __declspec(dllexport) int __cdecl usbGetKEYID(unsigned char *a, int i); 18 | __declspec(dllexport) int __cdecl usbGetSecurity(int i, unsigned char *a); 19 | __declspec(dllexport) int __cdecl usbLamp(int32_t lamp_bits); 20 | __declspec(dllexport) int __cdecl usbPadRead(unsigned long *pad_bits); 21 | __declspec(dllexport) int __cdecl usbPadReadLast(uint8_t *a1); 22 | __declspec(dllexport) int __cdecl usbSecurityInit(); 23 | __declspec(dllexport) int __cdecl usbSecurityInitDone(); 24 | __declspec(dllexport) int __cdecl usbSecuritySearch(); 25 | __declspec(dllexport) int __cdecl usbSecuritySearchDone(); 26 | __declspec(dllexport) int __cdecl usbSecuritySelect(int i); 27 | __declspec(dllexport) int __cdecl usbSecuritySelectDone(); 28 | __declspec(dllexport) int __cdecl usbSetExtIo(int i); 29 | __declspec(dllexport) int __cdecl usbStart(int i); 30 | __declspec(dllexport) int __cdecl usbWdtReset(); 31 | __declspec(dllexport) int __cdecl usbWdtStart(int i); 32 | __declspec(dllexport) int __cdecl usbWdtStartDone(); 33 | #if POPN15_FORMAT == 1 34 | } 35 | #endif 36 | 37 | #endif //EZUSB_UPC_EZUSB_H -------------------------------------------------------------------------------- /ezusb/src/lib/hid.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/ezusb/src/lib/hid.lib -------------------------------------------------------------------------------- /pinout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/pinout.png -------------------------------------------------------------------------------- /pinout_leonardo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/pinout_leonardo.png -------------------------------------------------------------------------------- /pinout_leonardo_psx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/pinout_leonardo_psx.png -------------------------------------------------------------------------------- /popnController/POPNHID.cpp: -------------------------------------------------------------------------------- 1 | /* Arduino-HID-Lighting-Library 2 | * 3 | * This Arduino-HID-Lighting-Library is derived from Arduino-HID-Lighting, whose copyriht owner is mon. 4 | * More information about Arduino-HID-Lighting you can find under: 5 | * 6 | * mon's Arduino-HID-Lighting 7 | * https://github.com/mon/Arduino-HID-Lighting 8 | * 9 | * 2018 (C) Arduino-HID-Lighting-Library, Knuckleslee 10 | */ 11 | #include "POPNHID.h" 12 | 13 | byte extern LightPins[]; 14 | byte extern DipPins[]; 15 | 16 | /* HID DESCRIPTOR */ 17 | static const byte PROGMEM _hidReportPOPN[] = { 18 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 19 | 0x09, 0x05, /* USAGE (Game Pad) */ 20 | 0xa1, 0x01, /* COLLECTION (Application) */ 21 | 22 | /*Buttons */ 23 | 0x85, 0x04, /* REPORT_ID 4 */ 24 | 0x05, 0x09, /* USAGE_PAGE (Button) */ 25 | 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ 26 | 0x29, 0x0d, /* USAGE_MAXIMUM (Button 13)*/ 27 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 28 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 29 | #if defined(ARDUINO_ARCH_SAM) 30 | 0x95, 0x0d, /* REPORT_COUNT (13) */ 31 | #else 32 | 0x95, 0x0b, /* REPORT_COUNT (11) */ 33 | #endif 34 | 0x75, 0x01, /* REPORT_SIZE (1) */ 35 | 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 36 | /* Reserved bits */ 37 | 0x95, 0x01, /* REPORT_COUNT (1) */ 38 | #if defined(ARDUINO_ARCH_SAM) 39 | 0x75, 0x03, /* REPORT_SIZE (3) */ 40 | #else 41 | 0x75, 0x05, /* REPORT_SIZE (5) */ 42 | #endif 43 | 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */ 44 | 45 | /*Lights */ 46 | 0x85, 0x05, /* REPORT_ID 5*/ 47 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 48 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 49 | /*Led 1 */ 50 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 51 | 0x09, 0x01, /* USAGE (Instance 1) */ 52 | 0xa1, 0x02, /* COLLECTION (Logical) */ 53 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 54 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 55 | 0x79, 0x04, /* STRING INDEX (4) */ 56 | 0x75, 0x01, /* REPORT_SIZE (1) */ 57 | 0x95, 0x01, /* REPORT_COUNT (1) */ 58 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 59 | 0xc0, /* END_COLLECTION */ 60 | /*Led 2 */ 61 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 62 | 0x09, 0x02, /* USAGE (Instance 2) */ 63 | 0xa1, 0x02, /* COLLECTION (Logical) */ 64 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 65 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 66 | 0x79, 0x05, 67 | 0x75, 0x01, /* REPORT_SIZE (1) */ 68 | 0x95, 0x01, /* REPORT_COUNT (1) */ 69 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 70 | 0xc0, /* END_COLLECTION */ 71 | /*Led 3 */ 72 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 73 | 0x09, 0x03, /* USAGE (Instance 3) */ 74 | 0xa1, 0x02, /* COLLECTION (Logical) */ 75 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 76 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 77 | 0x79, 0x06, 78 | 0x75, 0x01, /* REPORT_SIZE (1) */ 79 | 0x95, 0x01, /* REPORT_COUNT (1) */ 80 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 81 | 0xc0, /* END_COLLECTION */ 82 | /*Led 4 */ 83 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 84 | 0x09, 0x04, /* USAGE (Instance 4) */ 85 | 0xa1, 0x02, /* COLLECTION (Logical) */ 86 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 87 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 88 | 0x79, 0x07, 89 | 0x75, 0x01, /* REPORT_SIZE (1) */ 90 | 0x95, 0x01, /* REPORT_COUNT (1) */ 91 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 92 | 0xc0, /* END_COLLECTION */ 93 | /*Led 5 */ 94 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 95 | 0x09, 0x05, /* USAGE (Instance 5) */ 96 | 0xa1, 0x02, /* COLLECTION (Logical) */ 97 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 98 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 99 | 0x79, 0x08, 100 | 0x75, 0x01, /* REPORT_SIZE (1) */ 101 | 0x95, 0x01, /* REPORT_COUNT (1) */ 102 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 103 | 0xc0, /* END_COLLECTION */ 104 | /*Led 6 */ 105 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 106 | 0x09, 0x06, /* USAGE (Instance 6) */ 107 | 0xa1, 0x02, /* COLLECTION (Logical) */ 108 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 109 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 110 | 0x79, 0x09, 111 | 0x75, 0x01, /* REPORT_SIZE (1) */ 112 | 0x95, 0x01, /* REPORT_COUNT (1) */ 113 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 114 | 0xc0, /* END_COLLECTION */ 115 | /*Led 7 */ 116 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 117 | 0x09, 0x07, /* USAGE (Instance 7) */ 118 | 0xa1, 0x02, /* COLLECTION (Logical) */ 119 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 120 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 121 | 0x79, 0x0a, 122 | 0x75, 0x01, /* REPORT_SIZE (1) */ 123 | 0x95, 0x01, /* REPORT_COUNT (1) */ 124 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 125 | 0xc0, /* END_COLLECTION */ 126 | /*Led 8 */ 127 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 128 | 0x09, 0x08, /* USAGE (Instance 8) */ 129 | 0xa1, 0x02, /* COLLECTION (Logical) */ 130 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 131 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 132 | 0x79, 0x0b, 133 | 0x75, 0x01, /* REPORT_SIZE (1) */ 134 | 0x95, 0x01, /* REPORT_COUNT (1) */ 135 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 136 | 0xc0, /* END_COLLECTION */ 137 | /*Led 9 */ 138 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 139 | 0x09, 0x09, /* USAGE (Instance 9) */ 140 | 0xa1, 0x02, /* COLLECTION (Logical) */ 141 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 142 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 143 | 0x79, 0x0c, 144 | 0x75, 0x01, /* REPORT_SIZE (1) */ 145 | 0x95, 0x01, /* REPORT_COUNT (1) */ 146 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 147 | 0xc0, /* END_COLLECTION */ 148 | #if defined(ARDUINO_ARCH_SAM) 149 | /*Led 10 */ 150 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 151 | 0x09, 0x0a, /* USAGE (Instance 10) */ 152 | 0xa1, 0x02, /* COLLECTION (Logical) */ 153 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 154 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 155 | 0x79, 0x0d, 156 | 0x75, 0x01, /* REPORT_SIZE (1) */ 157 | 0x95, 0x01, /* REPORT_COUNT (1) */ 158 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 159 | 0xc0, /* END_COLLECTION */ 160 | /*Led 11 */ 161 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 162 | 0x09, 0x0b, /* USAGE (Instance 11) */ 163 | 0xa1, 0x02, /* COLLECTION (Logical) */ 164 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 165 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 166 | 0x79, 0x0e, 167 | 0x75, 0x01, /* REPORT_SIZE (1) */ 168 | 0x95, 0x01, /* REPORT_COUNT (1) */ 169 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 170 | 0xc0, /* END_COLLECTION */ 171 | /*Led 12 */ 172 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 173 | 0x09, 0x0c, /* USAGE (Instance 12) */ 174 | 0xa1, 0x02, /* COLLECTION (Logical) */ 175 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 176 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 177 | 0x79, 0x0f, 178 | 0x75, 0x01, /* REPORT_SIZE (1) */ 179 | 0x95, 0x01, /* REPORT_COUNT (1) */ 180 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 181 | 0xc0, /* END_COLLECTION */ 182 | /*Led 13 */ 183 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 184 | 0x09, 0x0d, /* USAGE (Instance 13) */ 185 | 0xa1, 0x02, /* COLLECTION (Logical) */ 186 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 187 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 188 | 0x79, 0x10, 189 | 0x75, 0x01, /* REPORT_SIZE (1) */ 190 | 0x95, 0x01, /* REPORT_COUNT (1) */ 191 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 192 | 0xc0, /* END_COLLECTION */ 193 | /*Led 14 */ 194 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 195 | 0x09, 0x0e, /* USAGE (Instance 14) */ 196 | 0xa1, 0x02, /* COLLECTION (Logical) */ 197 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 198 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 199 | 0x79, 0x11, 200 | 0x75, 0x01, /* REPORT_SIZE (1) */ 201 | 0x95, 0x01, /* REPORT_COUNT (1) */ 202 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 203 | 0xc0, /* END_COLLECTION */ 204 | /*Led 15 */ 205 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 206 | 0x09, 0x0f, /* USAGE (Instance 15) */ 207 | 0xa1, 0x02, /* COLLECTION (Logical) */ 208 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 209 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 210 | 0x79, 0x12, 211 | 0x75, 0x01, /* REPORT_SIZE (1) */ 212 | 0x95, 0x01, /* REPORT_COUNT (1) */ 213 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 214 | 0xc0, /* END_COLLECTION */ 215 | /*Led 16 */ 216 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 217 | 0x09, 0x10, /* USAGE (Instance 16) */ 218 | 0xa1, 0x02, /* COLLECTION (Logical) */ 219 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 220 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 221 | 0x79, 0x13, 222 | 0x75, 0x01, /* REPORT_SIZE (1) */ 223 | 0x95, 0x01, /* REPORT_COUNT (1) */ 224 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 225 | 0xc0, /* END_COLLECTION */ 226 | /*Led 17 */ 227 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 228 | 0x09, 0x11, /* USAGE (Instance 17) */ 229 | 0xa1, 0x02, /* COLLECTION (Logical) */ 230 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 231 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 232 | 0x79, 0x14, 233 | 0x75, 0x01, /* REPORT_SIZE (1) */ 234 | 0x95, 0x01, /* REPORT_COUNT (1) */ 235 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 236 | 0xc0, /* END_COLLECTION */ 237 | /*Led 18 */ 238 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 239 | 0x09, 0x12, /* USAGE (Instance 18) */ 240 | 0xa1, 0x02, /* COLLECTION (Logical) */ 241 | 0x05, 0x08, /* USAGE_PAGE (LEDs) */ 242 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 243 | 0x79, 0x15, 244 | 0x75, 0x01, /* REPORT_SIZE (1) */ 245 | 0x95, 0x01, /* REPORT_COUNT (1) */ 246 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 247 | 0xc0, /* END_COLLECTION */ 248 | /* Coin blocker */ 249 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 250 | 0x09, 0x13, /* USAGE (Instance 19) */ 251 | 0xa1, 0x02, /* COLLECTION (Logical) */ 252 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 253 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 254 | 0x79, 0x16, 255 | 0x75, 0x01, /* REPORT_SIZE (1) */ 256 | 0x95, 0x01, /* REPORT_COUNT (1) */ 257 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 258 | 0xc0, /* END_COLLECTION */ 259 | /* Coin counter */ 260 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 261 | 0x09, 0x14, /* USAGE (Instance 20) */ 262 | 0xa1, 0x02, /* COLLECTION (Logical) */ 263 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 264 | 0x09, 0x4b, /* USAGE (Generic Indicator 1) */ 265 | 0x79, 0x17, 266 | 0x75, 0x01, /* REPORT_SIZE (1) */ 267 | 0x95, 0x01, /* REPORT_COUNT (1) */ 268 | 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ 269 | 0xc0, /* END_COLLECTION */ 270 | /* Reserved 12 bits */ 271 | 0x95, 0x01, /* REPORT_COUNT (1) */ 272 | 0x75, 0x0C, /* REPORT_SIZE (12) */ 273 | 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ 274 | #else 275 | /* Reserved 23 bits */ 276 | 0x95, 0x01, /* REPORT_COUNT (1) */ 277 | 0x75, 0x17, /* REPORT_SIZE (23) */ 278 | 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ 279 | #endif 280 | 281 | #if defined(ARDUINO_ARCH_SAM) 282 | /*Dip switches*/ 283 | 0x85, 0x06, /* REPORT_ID 6 */ 284 | 0x05, 0x09, /* USAGE_PAGE (Button) */ 285 | 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ 286 | 0x29, 0x04, /* USAGE_MAXIMUM (Button 4)*/ 287 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 288 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 289 | 0x95, 0x04, /* REPORT_COUNT (4) */ 290 | 0x75, 0x01, /* REPORT_SIZE (1) */ 291 | 0xb1, 0x02, /* FEATURE (Data,Var,Abs) */ 292 | /* Reserved bits */ 293 | 0x95, 0x01, /* REPORT_COUNT (1) */ 294 | 0x75, 0x04, /* REPORT_SIZE (4) */ 295 | 0xb1, 0x03, /* FEATURE (Cnst,Var,Abs) */ 296 | #endif 297 | 298 | /*Light mode switch*/ 299 | 0x85, 0x07, /* REPORT_ID 7 */ 300 | 0x05, 0x0a, /* USAGE_PAGE (Ordinals) */ 301 | 0x19, 0x00, /* USAGE_MINIMUM (Button 1) */ 302 | 0x29, 0x04, /* USAGE_MAXIMUM (Button 4)*/ 303 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 304 | 0x25, 0x04, /* LOGICAL_MAXIMUM (4) */ 305 | 0x95, 0x01, /* REPORT_COUNT (1) */ 306 | 0x75, 0x04, /* REPORT_SIZE (4) */ 307 | 0xb1, 0x02, /* FEATURE (Data,Var,Abs) */ 308 | /* Reserved bits */ 309 | 0x95, 0x01, /* REPORT_COUNT (1) */ 310 | 0x75, 0x04, /* REPORT_SIZE (4) */ 311 | 0xb1, 0x03, /* FEATURE (Cnst,Var,Abs) */ 312 | 313 | /*Footer */ 314 | 0xc0 /* END_COLLECTION */ 315 | }; 316 | 317 | static bool SendControl(uint8_t d) 318 | { 319 | return USB_SendControl(0, &d, 1) == 1; 320 | } 321 | 322 | static bool USB_SendStringDescriptor(const char *string_P, uint8_t string_len, uint8_t flags) { 323 | SendControl(2 + string_len * 2); 324 | SendControl(3); 325 | bool pgm = flags & TRANSFER_PGM; 326 | for(uint8_t i = 0; i < string_len; i++) { 327 | bool r = SendControl(pgm ? pgm_read_byte(&string_P[i]) : string_P[i]); 328 | r &= SendControl(0); // high byte 329 | if(!r) { 330 | return false; 331 | } 332 | } 333 | return true; 334 | } 335 | 336 | /* HID Light descriptor strings */ 337 | const char* const PROGMEM String_Manufacturer = "CrazyRedMachine"; 338 | const char* const PROGMEM String_Product = "Pop'n controller"; 339 | const char* const PROGMEM String_Serial = "POPNHID"; 340 | 341 | const char* const PROGMEM LEDString_00 = "Button 1"; 342 | const char* const PROGMEM LEDString_01 = "Button 2"; 343 | const char* const PROGMEM LEDString_02 = "Button 3"; 344 | const char* const PROGMEM LEDString_03 = "Button 4"; 345 | const char* const PROGMEM LEDString_04 = "Button 5"; 346 | const char* const PROGMEM LEDString_05 = "Button 6"; 347 | const char* const PROGMEM LEDString_06 = "Button 7"; 348 | const char* const PROGMEM LEDString_07 = "Button 8"; 349 | const char* const PROGMEM LEDString_08 = "Button 9"; 350 | const char* const PROGMEM LEDString_09 = "Hi Lamp 1"; 351 | const char* const PROGMEM LEDString_10 = "Hi Lamp 2"; 352 | const char* const PROGMEM LEDString_11 = "Hi Lamp 3"; 353 | const char* const PROGMEM LEDString_12 = "Hi Lamp 4"; 354 | const char* const PROGMEM LEDString_13 = "Hi Lamp 5"; 355 | const char* const PROGMEM LEDString_14 = "Left Lamp 1"; 356 | const char* const PROGMEM LEDString_15 = "Left Lamp 2"; 357 | const char* const PROGMEM LEDString_16 = "Right Lamp 1"; 358 | const char* const PROGMEM LEDString_17 = "Right Lamp 2"; 359 | const char* const PROGMEM BlockerString = "Coin blocker"; 360 | const char* const PROGMEM CounterString = "Coin counter"; 361 | 362 | const char* LEDString_indiv[] = {LEDString_00,LEDString_01,LEDString_02,LEDString_03,LEDString_04,LEDString_05,LEDString_06,LEDString_07,LEDString_08,LEDString_09,LEDString_10,LEDString_11,LEDString_12,LEDString_13,LEDString_14,LEDString_15,LEDString_16,LEDString_17,BlockerString,CounterString}; 363 | 364 | #if defined(ARDUINO_ARCH_SAM) 365 | uint8_t STRING_ID_LED_Count = 20; 366 | #else 367 | uint8_t STRING_ID_LED_Count = 9; 368 | #endif 369 | 370 | const DeviceDescriptor PROGMEM USB_DeviceDescriptorIAD = 371 | D_DEVICE(0xEF,0x02,0x01,64,0x1ccf,0x1000,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); 372 | 373 | /* PluggableUSBModule IMPLEMENTATION */ 374 | 375 | POPNHID_::POPNHID_(void) : PluggableUSBModule(1, 1, epType) { 376 | epType[0] = EP_TYPE_INTERRUPT_IN; 377 | PluggableUSB().plug(this); 378 | } 379 | 380 | int POPNHID_::getInterface(byte* interfaceCount) { 381 | *interfaceCount += 1; // uses 1 382 | HIDDescriptor hidInterface = { 383 | D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE), 384 | D_HIDREPORT(sizeof(_hidReportPOPN)), 385 | D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, USB_EP_BINTERVAL) 386 | }; 387 | return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); 388 | } 389 | 390 | int POPNHID_::getDescriptor(USBSetup& setup) 391 | { 392 | if(setup.wValueH == USB_DEVICE_DESCRIPTOR_TYPE) { 393 | return USB_SendControl(TRANSFER_PGM, (const uint8_t*)&USB_DeviceDescriptorIAD, sizeof(USB_DeviceDescriptorIAD)); 394 | } 395 | if(setup.wValueH == USB_STRING_DESCRIPTOR_TYPE) { 396 | if (setup.wValueL == IPRODUCT) { 397 | return USB_SendStringDescriptor(String_Product, strlen(String_Product), 0); 398 | } 399 | else if (setup.wValueL == IMANUFACTURER) { 400 | return USB_SendStringDescriptor(String_Manufacturer, strlen(String_Manufacturer), 0); 401 | } 402 | else if (setup.wValueL == ISERIAL) { 403 | return USB_SendStringDescriptor(String_Serial, strlen(String_Serial), 0); 404 | } 405 | else if(setup.wValueL >= STRING_ID_LED_Base && setup.wValueL < (STRING_ID_LED_Base + STRING_ID_LED_Count)) { 406 | return USB_SendStringDescriptor(LEDString_indiv[setup.wValueL - STRING_ID_LED_Base], strlen(LEDString_indiv[setup.wValueL - STRING_ID_LED_Base]), 0); 407 | } 408 | } 409 | // Check if this is a HID Class Descriptor request 410 | if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; } 411 | if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; } 412 | 413 | // In a HID Class Descriptor wIndex contains the interface number 414 | if (setup.wIndex != pluggedInterface) { return 0; } 415 | 416 | return USB_SendControl(TRANSFER_PGM, _hidReportPOPN, sizeof(_hidReportPOPN)); 417 | } 418 | 419 | bool POPNHID_::setup(USBSetup& setup) 420 | { 421 | if (pluggedInterface != setup.wIndex) { 422 | return false; 423 | } 424 | 425 | byte request = setup.bRequest; 426 | byte requestType = setup.bmRequestType; 427 | 428 | if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) 429 | { 430 | #if defined(ARDUINO_ARCH_SAM) 431 | /* dip switches */ 432 | if (setup.wValueH == HID_REPORT_TYPE_FEATURE && setup.wValueL == 6) { 433 | uint8_t dip_data[2] = {6,0x00}; 434 | for(int i=0; i<4; i++) 435 | { 436 | if (digitalRead(DipPins[i]) == LOW) 437 | { 438 | dip_data[1] |= 1< 4)) { 493 | effective_mode = 2; 494 | } 495 | lightMode = effective_mode; 496 | mode_data = lightMode; 497 | return lightMode; 498 | } 499 | 500 | void POPNHID_::updateLightMode(){ 501 | uint8_t mode = mode_data; 502 | if (mode != lightMode) { 503 | mode_data = setLightMode(mode); 504 | } 505 | } 506 | 507 | void POPNHID_::updateLeds(uint32_t buttonsState, bool invert){ 508 | uint32_t* bitfield = (uint32_t*)&(led_data[1]); 509 | uint32_t leds = (*bitfield|buttonsState); 510 | if (invert) 511 | leds = ~leds; 512 | for(int i = 0; i < 9; i++) { 513 | if (leds>>i&1) 514 | digitalWrite(LightPins[i],HIGH); 515 | else 516 | digitalWrite(LightPins[i],LOW); 517 | } 518 | #if defined(ARDUINO_ARCH_SAM) 519 | for(int i = 9; i < 20; i++) { 520 | if (leds>>i&1) 521 | digitalWrite(LightPins[i],HIGH); 522 | else 523 | digitalWrite(LightPins[i],LOW); 524 | } 525 | #endif 526 | 527 | } 528 | 529 | int POPNHID_::sendState(uint32_t buttonsState){ 530 | uint8_t data[3]; 531 | data[0] = (uint8_t) 4; //report id 532 | data[1] = (uint8_t) (buttonsState & 0xFF); 533 | data[2] = (uint8_t) (buttonsState >> 8) & 0xFF; 534 | return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, 3); 535 | } 536 | -------------------------------------------------------------------------------- /popnController/POPNHID.h: -------------------------------------------------------------------------------- 1 | #include "HID.h" 2 | 3 | #define USB_EP_BINTERVAL 1 4 | 5 | #if defined(ARDUINO_ARCH_AVR) 6 | 7 | #define EPTYPE_DESCRIPTOR_SIZE uint8_t 8 | 9 | #elif defined(ARDUINO_ARCH_SAM) 10 | 11 | #define EPTYPE_DESCRIPTOR_SIZE uint32_t 12 | #define USB_EP_SIZE 64 13 | #define TRANSFER_PGM 0x80 14 | #define USB_SendControl USBD_SendControl 15 | #define USB_RecvControl USBD_RecvControl 16 | #define USB_Recv USBD_Recv 17 | #define USB_Send USBD_Send 18 | #define USB_Flush USBD_Flush 19 | #define HID_REPORT_TYPE_INPUT 1 20 | #define HID_REPORT_TYPE_OUTPUT 2 21 | #define HID_REPORT_TYPE_FEATURE 3 22 | 23 | #else 24 | 25 | #error "Unsupported architecture" 26 | 27 | #endif 28 | 29 | #define STRING_ID_LED_Base 4 30 | 31 | class POPNHID_ : public PluggableUSBModule { 32 | 33 | public: 34 | POPNHID_(void); 35 | 36 | /** 37 | * Updates the led status based on led_data (HID report received) and button states 38 | * param[in] buttonState bitfield with currently pressed buttons (used to force additional lights for mixed mode) 39 | * param[in] invert set to true to invert on/off status (used for invert lightmode) 40 | */ 41 | void updateLeds(uint32_t buttonsState, bool invert); 42 | 43 | /** 44 | * Sends the gamepad button states to the PC as an HID report 45 | * param[in] buttonsState bitfield with currently pressed buttons 46 | * return USB_Send() return value 47 | */ 48 | int sendState(uint32_t buttonsState); 49 | 50 | /** 51 | * Changes the lightMode if a received HID report asks for it 52 | */ 53 | void updateLightMode(); 54 | 55 | /** 56 | * getter and setter for lightMode protected field. 57 | */ 58 | uint8_t getLightMode(); 59 | uint8_t setLightMode(uint8_t mode); 60 | 61 | /** 62 | * getter for lastHidUpdate protected field. 63 | */ 64 | unsigned long getLastHidUpdate(); 65 | 66 | protected: 67 | /* current lightMode (0 = reactive, 1 = HID only, 2 = mixed (HID+reactive auto-switch), 3 = combined (HID+button presses), 4 = combined invert) */ 68 | uint8_t lightMode = 2; 69 | /* timestamp of last received HID report for lightMode 3 */ 70 | unsigned long lastHidUpdate = 0; 71 | /* byte array to receive HID reports from the PC */ 72 | byte led_data[5]; 73 | byte mode_data; 74 | 75 | /* Implementation of the PUSBListNode */ 76 | EPTYPE_DESCRIPTOR_SIZE epType[1]; 77 | uint8_t protocol; 78 | uint8_t idle; 79 | int getInterface(uint8_t* interfaceCount); 80 | int getDescriptor(USBSetup& setup); 81 | bool setup(USBSetup& setup); 82 | }; 83 | 84 | extern POPNHID_ POPNHID; 85 | -------------------------------------------------------------------------------- /popnController/popnController.ino: -------------------------------------------------------------------------------- 1 | #define BOUNCE_WITH_PROMPT_DETECTION 2 | #include 3 | #if defined(ARDUINO_ARCH_SAM) 4 | #include 5 | #include 6 | #else 7 | #include 8 | 9 | #define WITH_PSX 0 //enable PSX compatibility 10 | #if WITH_PSX == 1 11 | #include "ps2.h" 12 | #endif 13 | 14 | #endif 15 | 16 | #include "POPNHID.h" 17 | 18 | #if defined(ARDUINO_ARCH_SAM) 19 | /* 1 frame (as declared in POPNHID.cpp) on highspeed USB spec is 125µs */ 20 | #define REPORT_DELAY 120 21 | #else 22 | /* 1 frame (as declared in POPNHID.cpp) on fullspeed USB spec is 1ms */ 23 | #define REPORT_DELAY 995 24 | #endif 25 | 26 | #define MILLIDEBOUNCE 5 27 | POPNHID_ POPNHID; 28 | 29 | /* Buttons + Lights declarations */ 30 | #if defined(ARDUINO_ARCH_SAM) 31 | byte LightPins[] = {A7, CANTX, A8, A9, CANRX, A10, DAC1, A11, DAC0, 14, 15, 16, 17, 18, A6, A5, A4, A3, 19, 20}; 32 | byte ButtonPins[] = {5, 4, 3, 2, 1, 6, 7, 8, 9, 11, 13, 10, 12}; 33 | byte DipPins[] = {22, 24, 26, 28}; 34 | #else 35 | uint8_t LightPins[] = {11,12,13,23,22,21,20,19,18}; 36 | uint8_t ButtonPins[] = {0,1,2,3,4,5,6,7,8,9,10}; 37 | #endif 38 | 39 | const byte ButtonCount = sizeof(ButtonPins) / sizeof(ButtonPins[0]); 40 | const byte LightCount = sizeof(LightPins) / sizeof(LightPins[0]); 41 | Bounce buttons[ButtonCount]; 42 | 43 | uint16_t buttonsState = 0; 44 | 45 | #if defined(ARDUINO_ARCH_SAM) 46 | /* Keypad declarations */ 47 | const byte ROWS = 4; 48 | const byte COLS = 3; 49 | 50 | /* 51 | // To use the keypad as the numpad keys (will require to send numlock for it to work) 52 | char numpad[ROWS][COLS] = { 53 | {'\347', '\350', '\351'}, 54 | {'\344', '\345', '\346'}, 55 | {'\341', '\342', '\343'}, 56 | {'\352', ',', '\337'} 57 | }; 58 | */ 59 | 60 | /* This is to use the toprow keys instead */ 61 | char numpad[ROWS][COLS] = { 62 | {'7', '8', '9'}, 63 | {'4', '5', '6'}, 64 | {'1', '2', '3'}, 65 | {'0', ',', '\337'} 66 | }; 67 | 68 | /* This follows the Pop'n Music cabinet numpad pins order */ 69 | //byte rowPins[ROWS] = {46, 44, 42, 40}; //connect to the row pinouts of the keypad 70 | //byte colPins[COLS] = {48, 50, 52}; //connect to the column pinouts of the keypad 71 | 72 | /* For mini keypad 73 | */ 74 | byte rowPins[ROWS] = {50, 40, 42, 46}; //connect to the row pinouts of the keypad 75 | byte colPins[COLS] = {48, 52, 44}; //connect to the column pinouts of the keypad 76 | 77 | Keypad kpd = Keypad( makeKeymap(numpad), rowPins, colPins, ROWS, COLS ); 78 | #endif 79 | 80 | /* SETUP */ 81 | void setup() { 82 | 83 | // setup I/O for pins 84 | for (int i = 0; i < ButtonCount; i++) { 85 | buttons[i] = Bounce(); 86 | buttons[i].attach(ButtonPins[i], INPUT_PULLUP); 87 | buttons[i].interval(MILLIDEBOUNCE); 88 | } 89 | 90 | for (int i = 0; i < LightCount; i++) { 91 | pinMode(LightPins[i], OUTPUT); 92 | } 93 | 94 | #if defined(ARDUINO_ARCH_SAM) 95 | for (int i = 0; i < 4; i++) { 96 | pinMode(DipPins[i], INPUT_PULLUP); 97 | } 98 | kpd.setDebounceTime(30); 99 | Keyboard.begin(); 100 | 101 | /* activate numlock if you are not using the toprow keys */ 102 | /* delay(2000); 103 | Keyboard.press(136 + 83); 104 | delay(500); 105 | Keyboard.release(136+83); 106 | */ 107 | POPNHID.setLightMode(2); 108 | #else 109 | uint8_t lightMode; 110 | EEPROM.get(0, lightMode); 111 | if (lightMode > 4) 112 | lightMode = 2; 113 | POPNHID.setLightMode(lightMode); 114 | 115 | #if WITH_PSX == 1 116 | PS2_MapInput(&buttonsState, (1<<0), PS2_TRIANGLE); 117 | PS2_MapInput(&buttonsState, (1<<1), PS2_CIRCLE); 118 | PS2_MapInput(&buttonsState, (1<<2), PS2_R1); 119 | PS2_MapInput(&buttonsState, (1<<3), PS2_CROSS); 120 | PS2_MapInput(&buttonsState, (1<<4), PS2_L1); 121 | PS2_MapInput(&buttonsState, (1<<5), PS2_SQUARE); 122 | PS2_MapInput(&buttonsState, (1<<6), PS2_R2); 123 | PS2_MapInput(&buttonsState, (1<<7), PS2_UP); 124 | PS2_MapInput(&buttonsState, (1<<8), PS2_L2); 125 | PS2_MapInput(&buttonsState, (1<<9), PS2_SELECT); 126 | PS2_MapInput(&buttonsState, (1<<10), PS2_START); 127 | PS2_AlwaysInput(PS2_LEFT|PS2_DOWN|PS2_RIGHT); 128 | 129 | PS2_Init(); 130 | #endif 131 | 132 | #endif 133 | //boot animation 134 | uint16_t anim[] = {1, 4, 16, 64, 256, 128, 32, 8, 2}; 135 | animate(anim, 9, 100); 136 | animate(anim, 9, 100); 137 | uint16_t anim2[] = {1 + 4 + 16 + 64 + 256, 2 + 8 + 32 + 128}; 138 | animate(anim2, 2, 500); 139 | animate(anim2, 2, 500); 140 | } 141 | 142 | /* LOOP */ 143 | unsigned long lastReport = 0; 144 | uint16_t prevButtonsState = 0; 145 | bool modeChanged = false; 146 | void loop() { 147 | /* BUTTONS */ 148 | buttonsState = 0; 149 | for (int i = 0; i < ButtonCount; i++) { 150 | buttons[i].update(); 151 | int value = buttons[i].read(); 152 | if (value != HIGH){ 153 | buttonsState |= (uint16_t)1 << i; 154 | } else { 155 | buttonsState &= ~((uint16_t)1 << i); 156 | } 157 | } 158 | 159 | #if defined(ARDUINO_ARCH_AVR) 160 | #if WITH_PSX == 1 161 | PS2_Task(); 162 | #endif 163 | #endif 164 | 165 | /* USB DATA */ 166 | if ( ( (micros() - lastReport) >= REPORT_DELAY) ) 167 | { 168 | POPNHID.sendState(buttonsState); 169 | lastReport = micros(); 170 | prevButtonsState = buttonsState; 171 | 172 | //check for HID-requested lightmode change 173 | POPNHID.updateLightMode(); 174 | } 175 | 176 | /* LAMPS */ 177 | uint8_t mode = POPNHID.getLightMode(); 178 | /* mixed mode will behave sometimes like HID, sometimes like reactive */ 179 | if (mode == 2){ 180 | if ((millis()-POPNHID.getLastHidUpdate()) > 3000) 181 | mode = 0; 182 | else 183 | mode = 1; 184 | } 185 | switch (mode) 186 | { 187 | /* Reactive mode, locally determined lamp data */ 188 | case 0: 189 | but_lights(buttonsState & 0x1ff); 190 | #if defined(ARDUINO_ARCH_SAM) 191 | reactive_neon(buttonsState & 0x1ff); 192 | #endif 193 | break; 194 | /* HID mode, only based on received HID data */ 195 | case 1: 196 | POPNHID.updateLeds(0, false); 197 | break; 198 | /* Combined inverse mode, received HID data and button state are combined then inverted */ 199 | case 4: 200 | POPNHID.updateLeds(buttonsState & 0x1ff, true); 201 | break; 202 | /* Combined mode, received HID data and button state are combined */ 203 | case 3: 204 | POPNHID.updateLeds(buttonsState & 0x1ff, false); 205 | break; 206 | default: 207 | break; 208 | } 209 | 210 | #if defined(ARDUINO_ARCH_SAM) 211 | /* KEYPAD */ 212 | if (kpd.getKeys()) 213 | { 214 | for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list. 215 | { 216 | if ( kpd.key[i].stateChanged ) // Only find keys that have changed state. 217 | { 218 | switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED 219 | case PRESSED: 220 | Keyboard.press(kpd.key[i].kchar); 221 | break; 222 | case HOLD: 223 | 224 | break; 225 | case RELEASED: 226 | Keyboard.release(kpd.key[i].kchar); 227 | break; 228 | case IDLE: 229 | break; 230 | } 231 | 232 | } 233 | } 234 | } 235 | #endif 236 | 237 | /* MANUAL LIGHTMODE UPDATE */ 238 | if ( buttonsState & 1024 ) { 239 | if ( (buttonsState & 2) && (modeChanged == false)) { 240 | modeChanged = true; 241 | uint8_t mode = POPNHID.getLightMode()+1; 242 | if (mode > 4) mode = 0; 243 | POPNHID.setLightMode(mode); 244 | #if defined(ARDUINO_ARCH_AVR) 245 | EEPROM.put(0, mode); 246 | #endif 247 | } 248 | else if (!(buttonsState&2)) { 249 | modeChanged = false; 250 | } 251 | } 252 | } 253 | 254 | /* Light up button lights according to bitfield */ 255 | void but_lights(uint16_t lightDesc) { 256 | for (int i = 0; i < 9; i++) { 257 | if ((lightDesc >> i) & 1) { 258 | digitalWrite(LightPins[i], HIGH); 259 | } else { 260 | digitalWrite(LightPins[i], LOW); 261 | } 262 | } 263 | } 264 | 265 | /* Light up pillars and top neons according to bitfield */ 266 | void neon_lights(uint16_t lightDesc) { 267 | for (int i = 0; i < 9; i++) { 268 | if ((lightDesc >> i) & 1) { 269 | digitalWrite(LightPins[i + 9], HIGH); 270 | } else { 271 | digitalWrite(LightPins[i + 9], LOW); 272 | } 273 | } 274 | } 275 | 276 | /* Display animation on the cab according to a bitfield array */ 277 | void animate(uint16_t* tab, uint8_t n, int mswait) { 278 | for (int i = 0; i < n; i++) { 279 | but_lights(tab[i]); 280 | #if defined(ARDUINO_ARCH_SAM) 281 | neon_lights(tab[i]); 282 | #endif 283 | delay(mswait); 284 | } 285 | } 286 | 287 | /* ARDUINO DUE ONLY FUNCTIONS */ 288 | 289 | #if defined(ARDUINO_ARCH_SAM) 290 | /* Manage pillars and top neons in reactive mode */ 291 | uint16_t neon_anim[] = {16, 24, 28, 30, 31, 30, 28, 24}; 292 | int neon_anim_index = 0; 293 | uint16_t pillar_state[] = {0, 0x140, 0xA0, 0x1E0}; 294 | int pillar_state_index = 0; 295 | bool pillar_lit = false; 296 | uint16_t prevState = 0; 297 | unsigned long lastBlink = 0; 298 | unsigned long lastNeonUpdate = 0; 299 | unsigned long lastButtonAction = 0; 300 | unsigned long actionRate = 0; 301 | unsigned long neonRate = 200; 302 | void reactive_neon(uint16_t buttonsState) { 303 | uint16_t neons = 0; 304 | unsigned long currTime = millis(); 305 | 306 | /* 307 | * SIDE PILLARS 308 | * When pressing any button the side pillars will blink for half a second 309 | * The color is randomly chosen with blue being predominant, red rare and purple super rare 310 | */ 311 | if ( buttonsState != prevState ) 312 | { 313 | if ( buttonsState != 0 ){ 314 | long randNumber = random(21); 315 | if (randNumber == 0) 316 | { 317 | pillar_state_index = 3; 318 | } 319 | else if (randNumber < 3) 320 | { 321 | pillar_state_index = 1; 322 | } else { 323 | pillar_state_index = 2; 324 | } 325 | pillar_lit = true; 326 | if (buttonsState != 0) lastButtonAction = currTime; 327 | } 328 | prevState = buttonsState; 329 | } else { /* no state change, continue to blink for 50ms */ 330 | if (currTime - lastBlink > 50) { 331 | pillar_lit = !pillar_lit; 332 | lastBlink = currTime; 333 | } 334 | if (currTime - lastButtonAction > 500) { 335 | pillar_state_index = 0; 336 | } 337 | } 338 | 339 | if (pillar_lit) 340 | neons |= pillar_state[pillar_state_index]; 341 | 342 | /* 343 | * Adjusting top neon animation speed (should go faster when you hit buttons quickly) 344 | */ 345 | actionRate = currTime - lastButtonAction; 346 | 347 | if (15*actionRate < neonRate) 348 | neonRate *= 0.99995; 349 | else if (actionRate > 10*neonRate){ 350 | neonRate = neonRate*1.01; 351 | if (neonRate < 100) neonRate++; 352 | } 353 | if (neonRate > 400) 354 | neonRate = 400; 355 | 356 | if (neonRate < 40) 357 | neonRate = 40; 358 | 359 | /* 360 | * Cycling through the top neon animation 361 | */ 362 | if ((currTime - lastNeonUpdate) > neonRate) 363 | { 364 | neon_anim_index++; 365 | if (neon_anim_index > 7) neon_anim_index = 0; 366 | lastNeonUpdate = currTime; 367 | } 368 | 369 | neons |= neon_anim[neon_anim_index]; 370 | 371 | /* 372 | * Light the leds 373 | */ 374 | neon_lights(neons); 375 | } 376 | #endif 377 | -------------------------------------------------------------------------------- /popnController/ps2.c: -------------------------------------------------------------------------------- 1 | #include "ps2.h" 2 | 3 | #if defined(ARDUINO_ARCH_AVR) 4 | /* USER CUSTOMIZABLE SETTINGS */ 5 | #define ACK_PORT PORTD 6 | #define ACK_DDR DDRD 7 | #define ACK_PIN 5 // PD5 (TXLED) 8 | 9 | /* RECOMMENDED DO NOT CHANGE */ 10 | #define INVERT_CIPO 0 // Set to 1 if CIPO is open-drain via transistor (recommended) 11 | #define INVERT_ACK 0 // Set to 1 if ACK is open-drain via transistor (recommended) 12 | 13 | /* END OF USER CUSTOMIZABLE SETTINGS */ 14 | 15 | // Stores a constructed packet for the PS2. 16 | uint16_t Data = 0; 17 | // List of available PS2 inputs. 18 | PS2_InputList_t *PS2Input = NULL; 19 | // Current PS2 state. 20 | void (*PS2Handler)(uint8_t) = NULL; 21 | 22 | 23 | 24 | void PS2_Acknowledge(void) { 25 | // Burn a few cycles before acknowledging 26 | asm volatile( 27 | "nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n" 28 | ); 29 | #if INVERT_ACK == 0 30 | ACK_DDR |= (1<input || (*map->input & map->mask)) 201 | new_data |= map->buttons; 202 | 203 | map = map->parent; 204 | } 205 | 206 | Data = ~new_data; 207 | } 208 | 209 | void PS2_MapInput(uint16_t *input, uint16_t mask, PS2_INPUT buttons) { 210 | PS2_InputList_t *child = calloc(1, sizeof(PS2_InputList_t)); 211 | 212 | child->input = input, 213 | child->mask = mask, 214 | child->buttons = buttons, 215 | child->parent = PS2Input; 216 | 217 | PS2Input = child; 218 | } 219 | 220 | void PS2_AlwaysInput(PS2_INPUT buttons) { 221 | PS2_InputList_t *child = calloc(1, sizeof(PS2_InputList_t)); 222 | 223 | child->input = NULL, 224 | child->mask = 0, 225 | child->buttons = buttons, 226 | child->parent = PS2Input; 227 | 228 | PS2Input = child; 229 | } 230 | 231 | // When a transfer is complete, determine what to do next 232 | ISR(SPI_STC_vect) { 233 | uint8_t input = SPDR; 234 | if (input == 0x01 && (!memory_card_timeout)) PS2Handler = PS2_Listen; 235 | PS2Handler(SPDR); 236 | } 237 | #endif 238 | -------------------------------------------------------------------------------- /popnController/ps2.h: -------------------------------------------------------------------------------- 1 | #if defined(ARDUINO_ARCH_AVR) 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef enum { 8 | PS2_NC = 0, 9 | PS2_SELECT = (1 << 0), 10 | PS2_L3 = (1 << 1), 11 | PS2_R3 = (1 << 2), 12 | PS2_START = (1 << 3), 13 | PS2_UP = (1 << 4), 14 | PS2_RIGHT = (1 << 5), 15 | PS2_DOWN = (1 << 6), 16 | PS2_LEFT = (1 << 7), 17 | PS2_L2 = (1 << 8), 18 | PS2_R2 = (1 << 9), 19 | PS2_L1 = (1 << 10), 20 | PS2_R1 = (1 << 11), 21 | PS2_TRIANGLE = (1 << 12), 22 | PS2_CIRCLE = (1 << 13), 23 | PS2_CROSS = (1 << 14), 24 | PS2_SQUARE = (1 << 15), 25 | } PS2_INPUT; 26 | 27 | typedef struct PS2_InputList_t PS2_InputList_t; 28 | 29 | struct PS2_InputList_t { 30 | uint16_t *input; // Input source; NULL is always true 31 | uint16_t mask; // Mask to check; (*input & mask) 32 | PS2_INPUT buttons; // OR the following if true 33 | PS2_InputList_t *parent; 34 | }; 35 | 36 | #ifdef __cplusplus 37 | extern "C"{ 38 | #endif 39 | 40 | void PS2_Init(void); 41 | void PS2_Task(void); 42 | 43 | void PS2_MapInput(uint16_t *input, uint16_t mask, PS2_INPUT buttons); 44 | void PS2_AlwaysInput(PS2_INPUT buttons); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /shield.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrazyRedMachine/UltimatePopnController/2c6f4758b636515b8db7742fda2c6eb1c0c88b56/shield.png --------------------------------------------------------------------------------