initial
This commit is contained in:
@@ -0,0 +1 @@
|
||||
\\?\HID#VID_2341&PID_003E&MI_02#7&3156e204&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
|
||||
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(popn_modeswitch)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc")
|
||||
|
||||
include_directories(${popn_modeswitch_SOURCE_DIR})
|
||||
link_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
|
||||
# STATIC LINK HID
|
||||
find_library(HIDDLL NAMES hid PATHS ${CMAKE_SOURCE_DIR}/lib)
|
||||
message(STATUS "find_library for hid returned ${HIDDLL}")
|
||||
add_library(hid MODULE IMPORTED)
|
||||
set_property(TARGET hid PROPERTY IMPORTED_LOCATION "${HIDDLL}")
|
||||
|
||||
add_executable(modeswitch modeswitch.cpp)
|
||||
target_link_libraries(modeswitch hid)
|
||||
@@ -0,0 +1,78 @@
|
||||
/* TO BE COMPILED WITH MINGW */
|
||||
/* Visual C++ compilation didn't work for me on the BemaniPC (invalid win32 application) */
|
||||
#include <windows.h>
|
||||
#include <fstream>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
FILE *file;
|
||||
char path[256];
|
||||
HANDLE hidHandle;
|
||||
char OutputReport[5];
|
||||
int res;
|
||||
int mode;
|
||||
DWORD BytesWritten = 0;
|
||||
|
||||
if ( argc < 2 )
|
||||
{
|
||||
printf("Usage: %s mode\r\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
mode = atoi(argv[1]);
|
||||
if ( mode < 0 || mode > 3 )
|
||||
{
|
||||
printf("Invalid mode value %d\r\n", mode);
|
||||
return 2;
|
||||
}
|
||||
|
||||
file = fopen("devicepath.dat", "r");
|
||||
while ( fgets(path,256,file) != NULL )
|
||||
{
|
||||
hidHandle = CreateFile(path, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if ( hidHandle != INVALID_HANDLE_VALUE )
|
||||
break;
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
if ( hidHandle == INVALID_HANDLE_VALUE )
|
||||
{
|
||||
printf("Couldn't open device. Make sure devicepath.dat contains the correct path.\r\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
OutputReport[0] = 0x05; //report ID
|
||||
OutputReport[1] = 0x00;
|
||||
OutputReport[2] = 0x00;
|
||||
OutputReport[3] = 0x00;
|
||||
OutputReport[4] = mode+16; //mode+magicbit
|
||||
|
||||
WriteFile(hidHandle, OutputReport, 5, &BytesWritten, NULL);
|
||||
if ( BytesWritten == 5 )
|
||||
{
|
||||
res = 0;
|
||||
printf("Succesfully switched to mode %u ", mode);
|
||||
switch(mode)
|
||||
{
|
||||
case 0:
|
||||
printf("(reactive)\r\n");
|
||||
break;
|
||||
case 1:
|
||||
printf("(HID)\r\n");
|
||||
break;
|
||||
case 2:
|
||||
printf("(mixed)\r\n");
|
||||
break;
|
||||
case 3:
|
||||
printf("(invert)\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error sending HID report (%u bytes written)\r\n",BytesWritten);
|
||||
res = 4;
|
||||
}
|
||||
|
||||
CloseHandle(hidHandle);
|
||||
return res;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,583 @@
|
||||
/*! TheGood
|
||||
AHKHID - An AHK implementation of the HID functions.
|
||||
Last updated: August 22nd, 2010
|
||||
|
||||
USING THE CONSTANTS:
|
||||
|
||||
If you explicitly #include AHKHID in your script, you will have all the constants available to you. Otherwise, if AHKHID is
|
||||
in your library folder and you do not wish to explicitly #include it, you can call AHKHID_UseConstants() to have the
|
||||
constants available to you.
|
||||
|
||||
FUNCTION LIST:
|
||||
_____________________
|
||||
AHKHID_UseConstants()
|
||||
|
||||
See the section above titled "USING THE CONSTANTS"
|
||||
___________________________________
|
||||
AHKHID_Initialize(bRefresh = False)
|
||||
|
||||
You don't have to call this function manually. It is automatically called by other functions to get the pointer of the
|
||||
RAWINPUTDEVICELIST struct array. However, if a new device is plugged in, you will have to refresh the listing by calling it
|
||||
with bRefresh = True. Returns -1 on error (with error message in ErrorLevel).
|
||||
____________________
|
||||
AHKHID_GetDevCount()
|
||||
|
||||
Returs the number of HID devices connected to this computer.
|
||||
Returns -1 on error (with error message in ErrorLevel).
|
||||
______________________
|
||||
AHKHID_GetDevHandle(i)
|
||||
|
||||
Returns the handle of device i (starts at 1).
|
||||
Mostly used internally for API calls.
|
||||
__________________________
|
||||
AHKHID_GetDevIndex(Handle)
|
||||
|
||||
Returns the index (starts at 1) of the device in the enumeration with matching handle.
|
||||
Returns 0 if not found.
|
||||
______________________________________
|
||||
AHKHID_GetDevType(i, IsHandle = False)
|
||||
|
||||
Returns the type of the device. See the RIM_ constants for possible values.
|
||||
If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration.
|
||||
Otherwise it is the handle of the device.
|
||||
______________________________________
|
||||
AHKHID_GetDevName(i, IsHandle = False)
|
||||
|
||||
Returns the name of the device (or empty string on error, with error message in ErrorLevel).
|
||||
If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration.
|
||||
Otherwise it is the handle of the device.
|
||||
____________________________________________
|
||||
AHKHID_GetDevInfo(i, Flag, IsHandle = False)
|
||||
|
||||
Retrieves info from the RID_DEVICE_INFO struct. To retrieve a member, simply use the corresponding flag. A list of flags
|
||||
can be found at the top of the script (the constants starting with DI_). Each flag corresponds to a member in the struct.
|
||||
If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration. Otherwise it is the
|
||||
handle of the device. Returns -1 on error (with error message in ErrorLevel).
|
||||
|
||||
See Example 1 for an example on how to use it.
|
||||
_______________________________________________________________________________
|
||||
AHKHID_AddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0)
|
||||
|
||||
Allows you to queue up RAWINPUTDEVICE structures before doing the registration. To use it, you first need to initialize the
|
||||
variable by calling AHKHID_AddRegister(iNumberOfElements). To then add to the stack, simply call it with the parameters you
|
||||
want (eg. AHKHID_AddRegister(1,6,MyGuiHandle) for keyboards). When you're finally done, you just have to call
|
||||
AHKHID_Register() with no parameters. The function returns -1 if the struct is full. Redimensioning the struct will erase
|
||||
all previous structs added. On success, it returns the address of the array of structs (if you'd rather manipulate it
|
||||
yourself).
|
||||
|
||||
See Example 2 for an example on how to use it.
|
||||
|
||||
You will need to do this if you want to use advance features of the RAWINPUTDEVICE flags. For example, if you want to
|
||||
register all devices using Usage Page 1 but would like to exclude devices of Usage Page 1 using Usage 2 (keyboards), then
|
||||
you need to place two elements in the array. The first one is AHKHID_AddRegister(1,0,MyGuiHandle,RIDEV_PAGEONLY) and the
|
||||
second one is AHKHID_AddRegister(1,2,MyGuiHandle,RIDEV_EXCLUDE).
|
||||
|
||||
Tip: Have a look at all the flags you can use (see the constants starting with RIDEV_). The most useful is RIDEV_INPUTSINK.
|
||||
Tip: Set Handle to 0 if you want the WM_INPUT messages to go to the window with keyboard focus.
|
||||
Tip: To unregister, use the flag RIDEV_REMOVE. Note that you also need to use the RIDEV_PAGEONLY flag if the TLC was
|
||||
registered with it.
|
||||
____________________________________________________________________________
|
||||
AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0)
|
||||
|
||||
This function can be used in two ways. If no parameters are specified, it will use the RAWINPUTDEVICE array created through
|
||||
AHKHID_AddRegister() and register. Otherwise, it will register only the specified parameters. For example, if you just want
|
||||
to register the mouse, you can simply do AHKHID_Register(1,2,MyGuiHandle). Returns 0 on success, returns -1 on error (with
|
||||
error message in ErrorLevel).
|
||||
|
||||
See Example 2 for an example on how to use it with the RAWINPUTDEVICE.
|
||||
See Example 3 for an example on how to use it only with the specified parameters.
|
||||
____________________________________
|
||||
AHKHID_GetRegisteredDevs(ByRef uDev)
|
||||
|
||||
This function allows you to get an array of the TLCs that have already been registered.
|
||||
It fills uDev with an array of RAWINPUTDEVICE and returns the number of elements in the array.
|
||||
Returns -1 on error (with error message in ErrorLevel).
|
||||
|
||||
See Example 2 for an example on how to use it.
|
||||
______________________________________
|
||||
AHKHID_GetInputInfo(InputHandle, Flag)
|
||||
|
||||
This function is used to retrieve the data upon receiving WM_INPUT messages. By passing the lParam of the WM_INPUT (0xFF00)
|
||||
messages, it can retrieve all the members of the RAWINPUT structure, except the raw data coming from HID devices (use
|
||||
AHKHID_GetInputData for that). To retrieve a member, simply specify the flag corresponding to the member you want, and call
|
||||
the function. A list of all the flags can be found at the top of this script (the constants starting with II_). Returns -1
|
||||
on error (with error message in ErrorLevel).
|
||||
|
||||
See Example 2 for an example on how to use it to retrieve each member of the structure.
|
||||
See Example 3 for an example on how to interpret members which represent flags.
|
||||
|
||||
Tip: You have to use Critical in your message function or you might get invalid handle errors.
|
||||
Tip: You can check the value of wParam to know if the application was in the foreground upon reception (see RIM_INPUT).
|
||||
_____________________________________________
|
||||
AHKHID_GetInputData(InputHandle, ByRef uData)
|
||||
|
||||
This function is used to retrieve the data sent by HID devices of type RIM_TYPEHID (ie. neither keyboard nor mouse) upon
|
||||
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
|
||||
can use GetInputInfo for that). Specify the lParam of the WM_INPUT (0xFF00) message and the function will put in uData the
|
||||
raw data received from the device. It will then return the size (number of bytes) of uData. Returns -1 on error (with error
|
||||
message in ErrorLevel).
|
||||
|
||||
See Example 2 for an example on how to use it (although you need an HID device of type RIM_TYPEHID to test it).
|
||||
|
||||
*/
|
||||
|
||||
AHKHID_Included := True
|
||||
AHKHID_SetConstants:
|
||||
;______________________________________
|
||||
;Flags you can use in AHKHID_GetDevInfo
|
||||
;http://msdn.microsoft.com/en-us/library/ms645581
|
||||
DI_DEVTYPE := 4 ;Type of the device. See RIM_ constants.
|
||||
|
||||
DI_MSE_ID := 8 ;ID for the mouse device.
|
||||
DI_MSE_NUMBEROFBUTTONS := 12 ;Number of buttons for the mouse.
|
||||
DI_MSE_SAMPLERATE := 16 ;Number of data points per second. This information may not be applicable for every
|
||||
;mouse device.
|
||||
DI_MSE_HASHORIZONTALWHEEL := 20 ;Vista and later only: TRUE if the mouse has a wheel for horizontal scrolling;
|
||||
;otherwise, FALSE.
|
||||
|
||||
DI_KBD_TYPE := 8 ;Type of the keyboard.
|
||||
DI_KBD_SUBTYPE := 12 ;Subtype of the keyboard.
|
||||
DI_KBD_KEYBOARDMODE := 16 ;Scan code mode.
|
||||
DI_KBD_NUMBEROFFUNCTIONKEYS := 20 ;Number of function keys on the keyboard.
|
||||
DI_KBD_NUMBEROFINDICATORS := 24 ;Number of LED indicators on the keyboard.
|
||||
DI_KBD_NUMBEROFKEYSTOTAL := 28 ;Total number of keys on the keyboard.
|
||||
|
||||
DI_HID_VENDORID := 8 ;Vendor ID for the HID.
|
||||
DI_HID_PRODUCTID := 12 ;Product ID for the HID.
|
||||
DI_HID_VERSIONNUMBER := 16 ;Version number for the HID.
|
||||
DI_HID_USAGEPAGE := 20 | 0x0100 ;Top-level collection Usage Page for the device.
|
||||
DI_HID_USAGE := 22 | 0x0100 ;Top-level collection Usage for the device.
|
||||
;________________________________________
|
||||
;Flags you can use in AHKHID_GetInputInfo
|
||||
;http://msdn.microsoft.com/en-us/library/ms645562
|
||||
II_DEVTYPE := 0 ;Type of the device generating the raw input data. See RIM_ constants.
|
||||
II_DEVHANDLE := 8 ;Handle to the device generating the raw input data.
|
||||
|
||||
II_MSE_FLAGS := (08+A_PtrSize*2) | 0x0100 ;Mouse state. This member can be any reasonable combination of the
|
||||
;following values -> see MOUSE constants.
|
||||
II_MSE_BUTTONFLAGS := (12+A_PtrSize*2) | 0x0100 ;Transition state of the mouse buttons. This member can be one or more of
|
||||
;the following values -> see RI_MOUSE constants.
|
||||
II_MSE_BUTTONDATA := (14+A_PtrSize*2) | 0x1100 ;If usButtonFlags is RI_MOUSE_WHEEL, this member is a signed value that
|
||||
;specifies the wheel delta.
|
||||
II_MSE_RAWBUTTONS := (16+A_PtrSize*2) ;Raw state of the mouse buttons.
|
||||
II_MSE_LASTX := (20+A_PtrSize*2) | 0x1000 ;Motion in the X direction. This is signed relative motion or absolute
|
||||
;motion, depending on the value of usFlags.
|
||||
II_MSE_LASTY := (24+A_PtrSize*2) | 0x1000 ;Motion in the Y direction. This is signed relative motion or absolute
|
||||
;motion, depending on the value of usFlags.
|
||||
II_MSE_EXTRAINFO := (28+A_PtrSize*2) ;Device-specific additional information for the event.
|
||||
|
||||
II_KBD_MAKECODE := (08+A_PtrSize*2) | 0x0100 ;Scan code from the key depression. The scan code for keyboard overrun is
|
||||
;KEYBOARD_OVERRUN_MAKE_CODE.
|
||||
II_KBD_FLAGS := (10+A_PtrSize*2) | 0x0100 ;Flags for scan code information. It can be one or more of the following
|
||||
;values -> see RI_KEY constants.
|
||||
II_KBD_VKEY := (14+A_PtrSize*2) | 0x0100 ;Microsoft Windows message compatible virtual-key code.
|
||||
II_KBD_MSG := (16+A_PtrSize*2) ;Corresponding window message, for example WM_KEYDOWN, WM_SYSKEYDOWN, and
|
||||
;so forth.
|
||||
II_KBD_EXTRAINFO := (20+A_PtrSize*2) ;Device-specific additional information for the event.
|
||||
|
||||
II_HID_SIZE := (08+A_PtrSize*2) ;Size, in bytes, of each HID input in bRawData.
|
||||
II_HID_COUNT := (12+A_PtrSize*2) ;Number of HID inputs in bRawData.
|
||||
|
||||
;DO NOT USE WITH AHKHID_GetInputInfo. Use AHKHID_GetInputData instead to retrieve the raw data.
|
||||
II_HID_DATA := (16+A_PtrSize*2) ;Raw input data as an array of bytes.
|
||||
;__________________________________________________________________________________
|
||||
;Device type values returned by AHKHID_GetDevType as well as DI_DEVTYPE and II_DEVTYPE
|
||||
;http://msdn.microsoft.com/en-us/library/ms645568
|
||||
RIM_TYPEMOUSE := 0 ;The device is a mouse.
|
||||
RIM_TYPEKEYBOARD := 1 ;The device is a keyboard.
|
||||
RIM_TYPEHID := 2 ;The device is an Human Interface Device (HID) that is not a keyboard and not a mouse.
|
||||
;_______________________________________________________________________________________________
|
||||
;Different flags for RAWINPUTDEVICE structure (to be used with AHKHID_AddRegister and AHKHID_Register)
|
||||
;http://msdn.microsoft.com/en-us/library/ms645565
|
||||
RIDEV_REMOVE := 0x00000001 ;If set, this removes the top level collection from the inclusion list. This tells the
|
||||
;operating system to stop reading from a device which matches the top level collection.
|
||||
RIDEV_EXCLUDE := 0x00000010 ;If set, this specifies the top level collections to exclude when reading a complete
|
||||
;usage page. This flag only affects a TLC whose usage page is already specified with
|
||||
;RIDEV_PAGEONLY.
|
||||
RIDEV_PAGEONLY := 0x00000020 ;If set, this specifies all devices whose top level collection is from the specified
|
||||
;usUsagePage. Note that usUsage must be zero. To exclude a particular top level
|
||||
;collection, use RIDEV_EXCLUDE.
|
||||
RIDEV_NOLEGACY := 0x00000030 ;If set, this prevents any devices specified by usUsagePage or usUsage from generating
|
||||
;legacy messages. This is only for the mouse and keyboard. See Remarks.
|
||||
RIDEV_INPUTSINK := 0x00000100 ;If set, this enables the caller to receive the input even when the caller is not in
|
||||
;the foreground. Note that hwndTarget must be specified.
|
||||
RIDEV_CAPTUREMOUSE := 0x00000200 ;If set, the mouse button click does not activate the other window.
|
||||
RIDEV_NOHOTKEYS := 0x00000200 ;If set, the application-defined keyboard device hotkeys are not handled. However, the
|
||||
;system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default,
|
||||
;all keyboard hotkeys are handled. RIDEV_NOHOTKEYS can be specified even if
|
||||
;RIDEV_NOLEGACY is not specified and hwndTarget is NULL.
|
||||
RIDEV_APPKEYS := 0x00000400 ;Microsoft Windows XP Service Pack 1 (SP1): If set, the application command keys are
|
||||
;handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a
|
||||
;keyboard device.
|
||||
RIDEV_EXINPUTSINK := 0x00001000 ;Vista and later only: If set, this enables the caller to receive input in the
|
||||
;background only if the foreground application does not process it. In other words, if
|
||||
;the foreground application is not registered for raw input, then the background
|
||||
;application that is registered will receive the input.
|
||||
RIDEV_DEVNOTIFY := 0x00002000 ;Vista and later only: If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE
|
||||
;notifications for device arrival and device removal.
|
||||
;__________________________________________________
|
||||
;Different values of wParam in the WM_INPUT message
|
||||
;http://msdn.microsoft.com/en-us/library/ms645590
|
||||
RIM_INPUT := 0 ;Input occurred while the application was in the foreground. The application must call
|
||||
;DefWindowProc so the system can perform cleanup.
|
||||
RIM_INPUTSINK := 1 ;Input occurred while the application was not in the foreground. The application must call
|
||||
;DefWindowProc so the system can perform the cleanup.
|
||||
;__________________________________
|
||||
;Flags for GetRawInputData API call
|
||||
;http://msdn.microsoft.com/en-us/library/ms645596
|
||||
RID_INPUT := 0x10000003 ;Get the raw data from the RAWINPUT structure.
|
||||
RID_HEADER := 0x10000005 ;Get the header information from the RAWINPUT structure.
|
||||
;_____________________________________
|
||||
;Flags for RAWMOUSE (part of RAWINPUT)
|
||||
;http://msdn.microsoft.com/en-us/library/ms645578
|
||||
|
||||
;Flags for the II_MSE_FLAGS member
|
||||
MOUSE_MOVE_RELATIVE := 0 ;Mouse movement data is relative to the last mouse position.
|
||||
MOUSE_MOVE_ABSOLUTE := 1 ;Mouse movement data is based on absolute position.
|
||||
MOUSE_VIRTUAL_DESKTOP := 0x02 ;Mouse coordinates are mapped to the virtual desktop (for a multiple monitor system)
|
||||
MOUSE_ATTRIBUTES_CHANGED := 0x04 ;Mouse attributes changed; application needs to query the mouse attributes.
|
||||
|
||||
;Flags for the II_MSE_BUTTONFLAGS member
|
||||
RI_MOUSE_LEFT_BUTTON_DOWN := 0x0001 ;Self-explanatory
|
||||
RI_MOUSE_LEFT_BUTTON_UP := 0x0002 ;Self-explanatory
|
||||
RI_MOUSE_RIGHT_BUTTON_DOWN := 0x0004 ;Self-explanatory
|
||||
RI_MOUSE_RIGHT_BUTTON_UP := 0x0008 ;Self-explanatory
|
||||
RI_MOUSE_MIDDLE_BUTTON_DOWN := 0x0010 ;Self-explanatory
|
||||
RI_MOUSE_MIDDLE_BUTTON_UP := 0x0020 ;Self-explanatory
|
||||
RI_MOUSE_BUTTON_4_DOWN := 0x0040 ;XBUTTON1 changed to down.
|
||||
RI_MOUSE_BUTTON_4_UP := 0x0080 ;XBUTTON1 changed to up.
|
||||
RI_MOUSE_BUTTON_5_DOWN := 0x0100 ;XBUTTON2 changed to down.
|
||||
RI_MOUSE_BUTTON_5_UP := 0x0200 ;XBUTTON2 changed to up.
|
||||
RI_MOUSE_WHEEL := 0x0400 ;Raw input comes from a mouse wheel. The wheel delta is stored in usButtonData.
|
||||
;____________________________________________
|
||||
;Flags for the RAWKEYBOARD (part of RAWINPUT)
|
||||
;http://msdn.microsoft.com/en-us/library/ms645575
|
||||
|
||||
;Flag for the II_KBD_MAKECODE member in the event of a keyboard overrun
|
||||
KEYBOARD_OVERRUN_MAKE_CODE := 0xFF
|
||||
|
||||
;Flags for the II_KBD_FLAGS member
|
||||
RI_KEY_MAKE := 0
|
||||
RI_KEY_BREAK := 1
|
||||
RI_KEY_E0 := 2
|
||||
RI_KEY_E1 := 4
|
||||
RI_KEY_TERMSRV_SET_LED := 8
|
||||
RI_KEY_TERMSRV_SHADOW := 0x10
|
||||
;____________________________________
|
||||
;AHKHID FUNCTIONS
|
||||
|
||||
If Not AHKHID_Included
|
||||
Return
|
||||
|
||||
AHKHID_UseConstants() {
|
||||
Global ;To make the constants global
|
||||
AHKHID_Included := False
|
||||
Gosub, AHKHID_SetConstants
|
||||
}
|
||||
|
||||
AHKHID_Initialize(bRefresh = False) {
|
||||
Static uHIDList, bInitialized := False
|
||||
|
||||
If bInitialized And Not bRefresh
|
||||
Return &uHIDList
|
||||
|
||||
;Get the device count
|
||||
r := DllCall("GetRawInputDeviceList", "Ptr", 0, "UInt*", iCount, "UInt", A_PtrSize * 2)
|
||||
|
||||
;Check for error
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Prep var
|
||||
VarSetCapacity(uHIDList, iCount * (A_PtrSize * 2))
|
||||
r := DllCall("GetRawInputDeviceList", "Ptr", &uHIDList, "UInt*", iCount, "UInt", A_PtrSize * 2)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
bInitialized := True
|
||||
Return &uHIDList
|
||||
}
|
||||
|
||||
AHKHID_GetDevCount() {
|
||||
|
||||
;Get the device count
|
||||
r := DllCall("GetRawInputDeviceList", "Ptr", 0, "UInt*", iCount, "UInt", A_PtrSize * 2)
|
||||
|
||||
;Check for error
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
} Else Return iCount
|
||||
}
|
||||
|
||||
AHKHID_GetDevHandle(i) {
|
||||
Return NumGet(AHKHID_Initialize(), (i - 1) * (A_PtrSize * 2))
|
||||
}
|
||||
|
||||
AHKHID_GetDevIndex(Handle) {
|
||||
Loop % AHKHID_GetDevCount()
|
||||
If (NumGet(AHKHID_Initialize(), (A_Index - 1) * (A_PtrSize * 2)) = Handle)
|
||||
Return A_Index
|
||||
Return 0
|
||||
}
|
||||
|
||||
AHKHID_GetDevType(i, IsHandle = False) {
|
||||
Return Not IsHandle ? NumGet(AHKHID_Initialize(), ((i - 1) * (A_PtrSize * 2)) + A_PtrSize, "UInt")
|
||||
: NumGet(AHKHID_Initialize(), ((AHKHID_GetDevIndex(i) - 1) * (A_PtrSize * 2)) + A_PtrSize, "UInt")
|
||||
}
|
||||
|
||||
AHKHID_GetDevName(i, IsHandle = False) {
|
||||
|
||||
;Get index if i is handle
|
||||
h := IsHandle ? i : AHKHID_GetDevHandle(i)
|
||||
|
||||
;Get device name length. RIDI_DEVICENAME
|
||||
r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x20000007, "Ptr", 0, "UInt*", iLength)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return ""
|
||||
}
|
||||
|
||||
;Get device name.
|
||||
VarSetCapacity(s, (iLength + 1) * 2) ;RIDI_DEVICENAME
|
||||
r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x20000007, "Str", s, "UInt*", iLength)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return ""
|
||||
}
|
||||
|
||||
Return s
|
||||
}
|
||||
|
||||
AHKHID_GetDevInfo(i, Flag, IsHandle = False) {
|
||||
Static uInfo, iLastHandle := 0
|
||||
|
||||
;Get index if i is handle
|
||||
h := IsHandle ? i : AHKHID_GetDevHandle(i)
|
||||
|
||||
;Check if the handle changed
|
||||
If (h = iLastHandle) ;It's the same device. No need to call again
|
||||
Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt")
|
||||
Else {
|
||||
|
||||
;Get device info buffer size. RIDI_DEVICEINFO
|
||||
r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x2000000b, "Ptr", 0, "UInt*", iLength)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Get device info
|
||||
VarSetCapacity(uInfo, iLength)
|
||||
NumPut(iLength, uInfo, 0, "UInt") ;Put length in struct RIDI_DEVICEINFO
|
||||
r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x2000000b, "Ptr", &uInfo, "UInt*", iLength)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Successful. Keep handle.
|
||||
iLastHandle := h
|
||||
|
||||
;Retrieve data
|
||||
Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt")
|
||||
}
|
||||
|
||||
Return 0
|
||||
}
|
||||
|
||||
AHKHID_AddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
|
||||
Static uDev, iIndex := 0, iCount := 0
|
||||
|
||||
;Check if we just want the address
|
||||
If Not (UsagePage Or Usage Or Handle Or Flags)
|
||||
Return &uDev
|
||||
;Check if we just want the count
|
||||
Else If (UsagePage = "Count")
|
||||
Return iCount
|
||||
;Check if we're dimensioning the struct
|
||||
Else If UsagePage And Not (Usage Or Handle Or Flags) {
|
||||
iCount := UsagePage
|
||||
iIndex := 0
|
||||
VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
|
||||
Return &uDev
|
||||
}
|
||||
|
||||
;Check if there's space before adding data to struct
|
||||
If (iIndex = iCount)
|
||||
Return -1 ;Full capacity
|
||||
|
||||
;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
|
||||
Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
|
||||
|
||||
;Put in struct
|
||||
NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
|
||||
NumPut(Usage, uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
|
||||
NumPut(Flags, uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
|
||||
NumPut(Handle, uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
|
||||
|
||||
;Move to next slot
|
||||
iIndex += 1
|
||||
|
||||
Return &uDev
|
||||
}
|
||||
|
||||
AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
|
||||
|
||||
;Check if we're using the AddRegister array or only a single struct
|
||||
If Not (UsagePage Or Usage Or Handle Or Flags) {
|
||||
|
||||
;Call
|
||||
r := DllCall("RegisterRawInputDevices", "Ptr", AHKHID_AddRegister(), "UInt", AHKHID_AddRegister("Count"), "UInt", 8 + A_PtrSize)
|
||||
|
||||
;Check for error
|
||||
If Not r {
|
||||
ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Build struct and call
|
||||
} Else {
|
||||
|
||||
;Prep var
|
||||
VarSetCapacity(uDev, (8 + A_PtrSize), 0)
|
||||
|
||||
;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
|
||||
Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
|
||||
|
||||
NumPut(UsagePage, uDev, 0, "UShort")
|
||||
NumPut(Usage, uDev, 2, "UShort")
|
||||
NumPut(Flags, uDev, 4, "UInt")
|
||||
NumPut(Handle, uDev, 8, "Ptr")
|
||||
|
||||
;Call
|
||||
r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
|
||||
|
||||
;Check for error
|
||||
If Not r Or ErrorLevel {
|
||||
ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
}
|
||||
|
||||
Return 0
|
||||
}
|
||||
|
||||
AHKHID_GetRegisteredDevs(ByRef uDev) {
|
||||
|
||||
;Get length
|
||||
VarSetCapacity(iCount, 4, 0)
|
||||
r := DllCall("GetRegisteredRawInputDevices", "Ptr", 0, "UInt*", iCount, "UInt", 8 + A_PtrSize)
|
||||
If ErrorLevel {
|
||||
ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
If (iCount > 0) {
|
||||
|
||||
;Prep var
|
||||
VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
|
||||
|
||||
;Call
|
||||
r := DllCall("GetRegisteredRawInputDevices", "Ptr", &uDev, "UInt*", iCount, "UInt", 8 + A_PtrSize)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
}
|
||||
|
||||
Return iCount
|
||||
}
|
||||
|
||||
AHKHID_GetInputInfo(InputHandle, Flag) {
|
||||
Static uRawInput, iLastHandle := 0
|
||||
|
||||
;Check if it's the same handle
|
||||
If (InputHandle = iLastHandle) ;We can retrieve the data without having to call again
|
||||
Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ? (AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int" : (Flag = 8 ? "Ptr" : "UInt")))
|
||||
Else { ;We need to get a fresh copy
|
||||
|
||||
;Get raw data size RID_INPUT
|
||||
r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Prep var
|
||||
VarSetCapacity(uRawInput, iSize)
|
||||
|
||||
;Get raw data RID_INPUT
|
||||
r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
} Else If (r <> iSize) {
|
||||
ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Keep handle reference of current uRawInput
|
||||
iLastHandle := InputHandle
|
||||
|
||||
;Retrieve data
|
||||
Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ? (AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int" : (Flag = 8 ? "Ptr" : "UInt")))
|
||||
}
|
||||
|
||||
Return 0
|
||||
}
|
||||
|
||||
AHKHID_GetInputData(InputHandle, ByRef uData) {
|
||||
|
||||
;Get raw data size RID_INPUT
|
||||
r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Prep var
|
||||
VarSetCapacity(uRawInput, iSize)
|
||||
|
||||
;Get raw data RID_INPUT
|
||||
r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
|
||||
If (r = -1) Or ErrorLevel {
|
||||
ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
Return -1
|
||||
} Else If (r <> iSize) {
|
||||
ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
|
||||
Return -1
|
||||
}
|
||||
|
||||
;Get the size of each HID input and the number of them
|
||||
iSize := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
|
||||
iCount := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
|
||||
|
||||
;Allocate memory
|
||||
VarSetCapacity(uData, iSize * iCount)
|
||||
|
||||
;Copy bytes
|
||||
DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
|
||||
|
||||
Return (iSize * iCount)
|
||||
}
|
||||
|
||||
;Internal use only
|
||||
AHKHID_NumIsShort(ByRef Flag) {
|
||||
If (Flag & 0x0100) {
|
||||
Flag ^= 0x0100 ;Remove it
|
||||
Return True
|
||||
} Return False
|
||||
}
|
||||
|
||||
;Internal use only
|
||||
AHKHID_NumIsSigned(ByRef Flag) {
|
||||
If (Flag & 0x1000) {
|
||||
Flag ^= 0x1000 ;Remove it
|
||||
Return True
|
||||
} Return False
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#SingleInstance Force
|
||||
#Persistent
|
||||
#include AHKHID.ahk
|
||||
|
||||
hArd := HID_Open("2341","003E")
|
||||
while (hLED = -1) {
|
||||
hArd := HID_Open("2341","003E")
|
||||
Sleep, 5000
|
||||
}
|
||||
if 0 < 1 ; The left side of a non-expression if-statement is always the name of a variable.
|
||||
{
|
||||
MsgBox This script requires a parameter
|
||||
ExitApp
|
||||
}
|
||||
|
||||
param := %A_Index%
|
||||
mode := %param% + 16
|
||||
if (mode > 19) or (mode < 16){
|
||||
MsgBox, Incorrect value %mode%
|
||||
ExitApp
|
||||
}
|
||||
changeMode(hArd,mode)
|
||||
HID_Close(hArd)
|
||||
ExitApp
|
||||
|
||||
changeMode(hArd,Num){
|
||||
;MsgBox, sending %Num%
|
||||
TestString = "aa"
|
||||
NumPut(5,TestString,0,"UInt")
|
||||
NumPut(0,TestString,1, "UInt")
|
||||
NumPut(0,TestString,2, "UInt")
|
||||
NumPut(0,TestString,3, "UInt")
|
||||
NumPut(Num,TestString,4, "UInt")
|
||||
;outputdebug teststring %TestString%
|
||||
HID_Write(hArd,TestString,5)
|
||||
}
|
||||
|
||||
;DLL CALLS
|
||||
HID_Open(VID,PID){
|
||||
str = VID_%VID%`&PID_%PID%
|
||||
iCount := AHKHID_GetDevCount()
|
||||
Loop , %icount%
|
||||
{
|
||||
HID0 += 1
|
||||
name := AHKHID_GetDevName(HID0)
|
||||
type := AHKHID_GetDevType(HID0)
|
||||
;outputdebug str to find is %str%
|
||||
if type = 2
|
||||
{
|
||||
if name contains %str%
|
||||
{
|
||||
;outputdebug found %VID%:%PID% under name: %name%
|
||||
return HandleFromName(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
;outputdebug (%VID%:%PID% not found)
|
||||
return -1
|
||||
}
|
||||
|
||||
HandleFromName(FileName){
|
||||
GENERIC_READ = 0x80000000 ; Open the file for reading rather than writing.
|
||||
GENERIC_WRITE = 0x40000000
|
||||
OPEN_EXISTING = 3 ; This mode indicates that the file to be opened must already exist.
|
||||
FILE_SHARE_READ = 0x1 ; This and the next are whether other processes can open the file while we have it open.
|
||||
FILE_SHARE_WRITE = 0x2
|
||||
;outputdebug Calling Createfile on %FileName%
|
||||
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)
|
||||
If (hFile = -1) Or ErrorLevel
|
||||
{
|
||||
ErrorLevel = CreateFile call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
outputdebug (%A_LineNumber%) %errorlevel%
|
||||
Return -1
|
||||
}
|
||||
;outputdebug file handle %hFile%
|
||||
return hFile
|
||||
}
|
||||
|
||||
HID_Write(handle,data,len=-1){
|
||||
BytesToWrite := (len>0) ? len : VarSetCapacity(data, -1)
|
||||
;outputdebug length %BytesToWrite%
|
||||
r := DllCall("WriteFile", Ptr, handle, Str, data, UInt, BytesToWrite, UIntP, BytesActuallyWritten, Ptr, 0)
|
||||
If (r = -1) Or ErrorLevel
|
||||
{
|
||||
ErrorLevel = WriteFile call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
|
||||
outputdebug (%A_LineNumber%) %errorlevel%
|
||||
Return -1
|
||||
}
|
||||
;outputdebug writefile %r%
|
||||
return BytesActuallyWritten
|
||||
}
|
||||
|
||||
HID_Close(handle){
|
||||
DllCall("CloseHandle", Ptr, handle) ; Close the file.
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
# ModeSwitch for the Pop'n Ultimate Controller
|
||||
|
||||
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.
|
||||
|
||||
For ease of use I included a pre-compiled binary working on WinXP Embedded (for official Pop'n Music cabinet).
|
||||
|
||||
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.
|
||||
|
||||
## Usage
|
||||
|
||||
modeswitch.exe <num>
|
||||
|
||||
Where <num> can take the values
|
||||
|
||||
0 : Reactive mode (AC lighting simulation for emulators)
|
||||
|
||||
1 : HID only (original AC mode)
|
||||
|
||||
2 : Mixed (AC mode with lights from any button pressed)
|
||||
|
||||
3 : Invert (Like Mixed except ON is OFF and vice versa)
|
||||
|
||||
## How it works
|
||||
|
||||
On the Pop'n Ultimate Controller firmware, HID report ID 5 is a 32bit bitfield :
|
||||
|
||||
- bits 0 to 17 are the HID lamps.
|
||||
- bit 28 is the magic lightMode change request bit
|
||||
- bits 24 and 25 encode the requested lightMode if bit 28 is set
|
||||
|
||||
This binary opens the USB device whose path is written in devicepath.dat and sends the correct HID report.
|
||||
|
||||
The autohotkey version finds the path automatically based on VID=2341 and PID=003E.
|
||||
|
||||
## How to retrieve devicepath (for the C version)
|
||||
|
||||
You can either go into windows device manager and look for the device path in the Arduino Due advanced properties, or the easiest way is to look at the value inside spicetools or bemanitools configuration files.
|
||||
|
||||
### spicetools
|
||||
|
||||
open %appdata%/spicetools.xml, look for a "devid" value starting with \\?\HID#VID_2341&PID_003E
|
||||
|
||||
Note that you'll have to replace "&" occurrences by "&".
|
||||
|
||||
### bemanitools
|
||||
|
||||
open %appdata%/DJHACKERS/pnm_v4_25.bin and look for a string starting with \\?\HID#VID_2341&PID_003E
|
||||
@@ -1,2 +1,78 @@
|
||||
# UltimatePopnController
|
||||
Pop'n Music Controller for official cabinets
|
||||
# Ultimate Pop'n Controller
|
||||
|
||||
USB HID controller with 12 buttons, 18 lights, 12-key matrix numpad and nice programmable features.
|
||||
|
||||
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..).
|
||||
|
||||
In combination with the PN5180-cardio project, the whole IO from a Pop'n Music cabinet can be replaced pretty cheaply :)
|
||||
|
||||
# Acknowledgments
|
||||
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
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.
|
||||
|
||||
# Supported devices
|
||||
|
||||
This code has been tested on Arduino Due. It should still compile for Leonardo provided you redefine the pinout.
|
||||
|
||||
# Features
|
||||
|
||||
## I/O
|
||||
|
||||
This controller has 12 buttons (9 buttons + coin + service + test) and 18 lights (9 buttons + 5 top neon + 4 side pillar).
|
||||
|
||||
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 toprow rather than the numpad (due to lack of stability when sending the numlock command to the BemaniPC).
|
||||
|
||||
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.
|
||||
|
||||
It also has a cool light animation on boot which you can easily adapt to your liking :)
|
||||
|
||||
## Light modes
|
||||
|
||||
There are 4 different modes :
|
||||
|
||||
### Reactive mode
|
||||
|
||||
This mode roughly simulates AC lighting and was meant to be used for emulators or other games with no HID light support.
|
||||
|
||||
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.
|
||||
|
||||
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).
|
||||
|
||||
### HID mode
|
||||
|
||||
This is the original IO board mode where only messages from the game can control the lamps.
|
||||
|
||||
### Mixed mode (default mode)
|
||||
|
||||
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 ;) ).
|
||||
|
||||
### Invert mode
|
||||
|
||||
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.
|
||||
|
||||
## Switching lightmodes
|
||||
|
||||
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.
|
||||
|
||||
I included pre-compiled binaries and sources in the "ModeSwitch" folder. Refer to readme.md inside that folder for more details.
|
||||
|
||||
# Pinout
|
||||
|
||||
The Arduino DUE has 3.3v logic whereas the Pop'n Music cabinet lamps use 12V. Therefore I'm using mosfet transistors to do level shifting (the parts I used were three ULN2003APG chips).
|
||||
|
||||
Refer to pinout.png to see how it is all wired to a Pop'n Music cabinet.
|
||||
|
||||
LightPins 36 to 52 = Button lights 1 to 9
|
||||
LightPins 37 to 53 = Top Neon 1 to 5, Left 1 (blue) 2 (red), Right 1 (blue) 2 (red)
|
||||
connect pin to mosfet gate then mosfet drain to - terminal of LED
|
||||
connect ground to mosfet ground (daisychain)
|
||||
connect +12V to + terminal of LED (daisychain)
|
||||
ButtonPins 5 to 13 = Button input 1 to 9
|
||||
ButtonPins 4 3 2 = Test, Service, Coin
|
||||
connect button pin to ground to trigger button press
|
||||
|
||||
|
||||
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 664 KiB |
@@ -0,0 +1,334 @@
|
||||
/* Arduino-HID-Lighting-Library
|
||||
*
|
||||
* This Arduino-HID-Lighting-Library is derived from Arduino-HID-Lighting, whose copyriht owner is mon.
|
||||
* More information about Arduino-HID-Lighting you can find under:
|
||||
*
|
||||
* mon's Arduino-HID-Lighting
|
||||
* https://github.com/mon/Arduino-HID-Lighting
|
||||
*
|
||||
* 2018 (C) Arduino-HID-Lighting-Library, Knuckleslee
|
||||
*/
|
||||
#include "POPNHID.h"
|
||||
|
||||
byte extern LightPins[];
|
||||
|
||||
/* HID DESCRIPTOR */
|
||||
static const byte PROGMEM _hidReportPOPN[] = {
|
||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
|
||||
0x09, 0x05, /* USAGE (Game Pad) */
|
||||
0xa1, 0x01, /* COLLECTION (Application) */
|
||||
|
||||
/*Buttons */
|
||||
0x85, 0x04, /* REPORT_ID 4 */
|
||||
0x05, 0x09, /* USAGE_PAGE (Button) */
|
||||
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
|
||||
0x29, 0x0c, /* USAGE_MAXIMUM (Button 12)*/
|
||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
|
||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
|
||||
0x95, 0x0c, /* REPORT_COUNT (12) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */
|
||||
/* Reserved 4 bits */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x75, 0x04, /* REPORT_SIZE (4) */
|
||||
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
|
||||
|
||||
/*Lights */
|
||||
0x85, 0x05, /* REPORT_ID 5*/
|
||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
|
||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
|
||||
/*Led 1 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x01, /* USAGE (Instance 1) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 2 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x02, /* USAGE (Instance 2) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 3 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x03, /* USAGE (Instance 3) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 4 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x04, /* USAGE (Instance 4) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 5 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x05, /* USAGE (Instance 5) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 6 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x06, /* USAGE (Instance 6) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 7 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x07, /* USAGE (Instance 7) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 8 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x08, /* USAGE (Instance 8) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 9 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x09, /* USAGE (Instance 9) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 10 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x0a, /* USAGE (Instance 10) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 11 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x0b, /* USAGE (Instance 11) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 12 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x0c, /* USAGE (Instance 12) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 13 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x0d, /* USAGE (Instance 13) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 14 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x0e, /* USAGE (Instance 14) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 15 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x0f, /* USAGE (Instance 15) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 16 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x10, /* USAGE (Instance 16) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 17 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x11, /* USAGE (Instance 17) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/*Led 18 */
|
||||
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
|
||||
0x09, 0x12, /* USAGE (Instance 18) */
|
||||
0xa1, 0x02, /* COLLECTION (Logical) */
|
||||
0x05, 0x08, /* USAGE_PAGE (LEDs) */
|
||||
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
0xc0, /* END_COLLECTION */
|
||||
/* Reserved 14 bits */
|
||||
0x95, 0x01, /* REPORT_COUNT (1) */
|
||||
0x75, 0x0E, /* REPORT_SIZE (14) */
|
||||
0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
|
||||
/*Footer */
|
||||
0xc0 /* END_COLLECTION */
|
||||
};
|
||||
|
||||
/* PluggableUSBModule IMPLEMENTATION */
|
||||
|
||||
POPNHID_::POPNHID_(void) : PluggableUSBModule(1, 1, epType) {
|
||||
epType[0] = EP_TYPE_INTERRUPT_IN;
|
||||
PluggableUSB().plug(this);
|
||||
}
|
||||
|
||||
int POPNHID_::getInterface(byte* interfaceCount) {
|
||||
*interfaceCount += 1; // uses 1
|
||||
HIDDescriptor hidInterface = {
|
||||
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
|
||||
D_HIDREPORT(sizeof(_hidReportPOPN)),
|
||||
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 16)
|
||||
};
|
||||
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
|
||||
}
|
||||
|
||||
int POPNHID_::getDescriptor(USBSetup& setup)
|
||||
{
|
||||
// Check if this is a HID Class Descriptor request
|
||||
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
|
||||
if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
|
||||
|
||||
// In a HID Class Descriptor wIndex contains the interface number
|
||||
if (setup.wIndex != pluggedInterface) { return 0; }
|
||||
|
||||
return USB_SendControl(TRANSFER_PGM, _hidReportPOPN, sizeof(_hidReportPOPN));
|
||||
}
|
||||
|
||||
bool POPNHID_::setup(USBSetup& setup)
|
||||
{
|
||||
if (pluggedInterface != setup.wIndex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
byte request = setup.bRequest;
|
||||
byte requestType = setup.bmRequestType;
|
||||
|
||||
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE) {
|
||||
if (request == HID_SET_REPORT) {
|
||||
if(setup.wValueH == HID_REPORT_TYPE_OUTPUT && setup.wLength == 5){
|
||||
USB_RecvControl(led_data, 5);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t POPNHID_::getShortName(char *name)
|
||||
{
|
||||
name[0] = 'P';
|
||||
name[1] = 'O';
|
||||
name[2] = 'P';
|
||||
name[3] = 'N';
|
||||
return 4;
|
||||
}
|
||||
|
||||
/* CUSTOM POPN FUNCTIONS */
|
||||
|
||||
uint8_t POPNHID_::getLightMode(){
|
||||
return lightMode;
|
||||
}
|
||||
|
||||
void POPNHID_::setLightMode(uint8_t mode){
|
||||
if ((mode > 3) || (mode < 0)) {
|
||||
lightMode = 2;
|
||||
return;
|
||||
}
|
||||
lightMode = mode;
|
||||
}
|
||||
|
||||
void POPNHID_::updateLightMode(){
|
||||
uint32_t* bitfield = (uint32_t*)&(led_data[1]);
|
||||
if (*bitfield>>28&1){
|
||||
uint8_t mode = (*bitfield>>24) & 0x0F;
|
||||
setLightMode(mode);
|
||||
*bitfield &= ~(0xFF<<24);
|
||||
}
|
||||
}
|
||||
|
||||
void POPNHID_::updateLeds(uint32_t buttonsState, bool invert){
|
||||
uint32_t* bitfield = (uint32_t*)&(led_data[1]);
|
||||
uint32_t leds = (*bitfield|buttonsState);
|
||||
if (invert)
|
||||
leds = ~leds;
|
||||
for(int i = 0; i < 18; i++) {
|
||||
if (leds>>i&1)
|
||||
digitalWrite(LightPins[i],HIGH);
|
||||
else
|
||||
digitalWrite(LightPins[i],LOW);
|
||||
}
|
||||
}
|
||||
|
||||
int POPNHID_::sendState(uint32_t buttonsState){
|
||||
uint8_t data[3];
|
||||
data[0] = (uint8_t) 4; //report id
|
||||
data[1] = (uint8_t) (buttonsState & 0xFF);
|
||||
data[2] = (uint8_t) (buttonsState >> 8) & 0xFF;
|
||||
return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, 3);
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "HID.h"
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
|
||||
#define EPTYPE_DESCRIPTOR_SIZE uint8_t
|
||||
|
||||
#elif defined(ARDUINO_ARCH_SAM)
|
||||
|
||||
#define EPTYPE_DESCRIPTOR_SIZE uint32_t
|
||||
#define USB_EP_SIZE 64
|
||||
#define TRANSFER_PGM 0x80
|
||||
#define USB_SendControl USBD_SendControl
|
||||
#define USB_RecvControl USBD_RecvControl
|
||||
#define USB_Recv USBD_Recv
|
||||
#define USB_Send USBD_Send
|
||||
#define USB_Flush USBD_Flush
|
||||
#define HID_REPORT_TYPE_OUTPUT 2
|
||||
#define HID_REPORT_TYPE_INPUT 1
|
||||
|
||||
#else
|
||||
|
||||
#error "Unsupported architecture"
|
||||
|
||||
#endif
|
||||
|
||||
class POPNHID_ : public PluggableUSBModule {
|
||||
|
||||
public:
|
||||
POPNHID_(void);
|
||||
|
||||
/**
|
||||
* Updates the led status based on led_data (HID report received) and button states
|
||||
* param[in] buttonState bitfield with currently pressed buttons (used to force additional lights for mixed mode)
|
||||
* param[in] invert set to true to invert on/off status (used for invert lightmode)
|
||||
*/
|
||||
void updateLeds(uint32_t buttonsState, bool invert);
|
||||
|
||||
/**
|
||||
* Sends the gamepad button states to the PC as an HID report
|
||||
* param[in] buttonsState bitfield with currently pressed buttons
|
||||
* return USB_Send() return value
|
||||
*/
|
||||
int sendState(uint32_t buttonsState);
|
||||
|
||||
/**
|
||||
* Changes the lightMode if a received HID report asks for it
|
||||
*/
|
||||
void updateLightMode();
|
||||
|
||||
/**
|
||||
* getter and setter for lightMode protected field.
|
||||
*/
|
||||
uint8_t getLightMode();
|
||||
void setLightMode(uint8_t mode);
|
||||
|
||||
protected:
|
||||
/* current lightMode (0 = reactive, 1 = HID only, 2 = mixed (HID+button presses), 3 = mixed invert) */
|
||||
uint8_t lightMode = 2;
|
||||
/* byte array to receive HID reports from the PC */
|
||||
byte led_data[5];
|
||||
|
||||
/* Implementation of the PUSBListNode */
|
||||
EPTYPE_DESCRIPTOR_SIZE epType[1];
|
||||
uint8_t protocol;
|
||||
uint8_t idle;
|
||||
int getInterface(uint8_t* interfaceCount);
|
||||
int getDescriptor(USBSetup& setup);
|
||||
bool setup(USBSetup& setup);
|
||||
uint8_t getShortName(char *name);
|
||||
};
|
||||
|
||||
extern POPNHID_ POPNHID;
|
||||
@@ -0,0 +1,291 @@
|
||||
#define BOUNCE_WITH_PROMPT_DETECTION
|
||||
#include <Bounce2.h>
|
||||
#include <Keypad.h>
|
||||
#include <Keyboard.h>
|
||||
#include "POPNHID.h"
|
||||
/* 125µs delay is 1 frame on highspeed USB spec */
|
||||
#define REPORT_DELAY 125
|
||||
#define MILLIDEBOUNCE 15
|
||||
POPNHID_ POPNHID;
|
||||
|
||||
/* Buttons + Lights declarations */
|
||||
byte LightPins[] = {36, 38, 40, 42, 44, 46, 48, 50, 52, 37, 39, 41, 43, 45, 47, 49, 51, 53};
|
||||
byte ButtonPins[] = {5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 3, 2};
|
||||
const byte ButtonCount = sizeof(ButtonPins) / sizeof(ButtonPins[0]);
|
||||
const byte LightCount = sizeof(LightPins) / sizeof(LightPins[0]);
|
||||
Bounce buttons[ButtonCount];
|
||||
|
||||
/* Keypad declarations */
|
||||
const byte ROWS = 4;
|
||||
const byte COLS = 3;
|
||||
|
||||
/*
|
||||
// To use the keypad as the numpad keys (will require to send numlock for it to work)
|
||||
char numpad[ROWS][COLS] = {
|
||||
{'\347', '\350', '\351'},
|
||||
{'\344', '\345', '\346'},
|
||||
{'\341', '\342', '\343'},
|
||||
{'\352', ',', '\337'}
|
||||
};
|
||||
*/
|
||||
|
||||
/* This is to use the toprow keys instead */
|
||||
char numpad[ROWS][COLS] = {
|
||||
{'7', '8', '9'},
|
||||
{'4', '5', '6'},
|
||||
{'1', '2', '3'},
|
||||
{'0', ',', '\337'}
|
||||
};
|
||||
|
||||
/* This follows the Pop'n Music cabinet numpad pins order */
|
||||
byte rowPins[ROWS] = {A3, A2, A1, A0}; //connect to the row pinouts of the keypad
|
||||
byte colPins[COLS] = {A4, A5, A6}; //connect to the column pinouts of the keypad
|
||||
|
||||
/* For mini keypad
|
||||
byte rowPins[ROWS] = {A5, A0, A1, A3}; //connect to the row pinouts of the keypad
|
||||
byte colPins[COLS] = {A4, A6, A2}; //connect to the column pinouts of the keypad
|
||||
*/
|
||||
|
||||
Keypad kpd = Keypad( makeKeymap(numpad), rowPins, colPins, ROWS, COLS );
|
||||
|
||||
/* SETUP */
|
||||
void setup() {
|
||||
// setup I/O for pins
|
||||
for (int i = 0; i < ButtonCount; i++) {
|
||||
buttons[i] = Bounce();
|
||||
buttons[i].attach(ButtonPins[i], INPUT_PULLUP);
|
||||
buttons[i].interval(MILLIDEBOUNCE);
|
||||
}
|
||||
|
||||
for (int i = 0; i < LightCount; i++) {
|
||||
pinMode(LightPins[i], OUTPUT);
|
||||
}
|
||||
|
||||
kpd.setDebounceTime(30);
|
||||
Keyboard.begin();
|
||||
|
||||
/* activate numlock if you are not using the toprow keys */
|
||||
/* delay(2000);
|
||||
Keyboard.press(136 + 83);
|
||||
delay(500);
|
||||
Keyboard.release(136+83);
|
||||
*/
|
||||
|
||||
//boot animation
|
||||
uint16_t anim[] = {1, 4, 16, 64, 256, 128, 32, 8, 2};
|
||||
animate(anim, 9, 100);
|
||||
animate(anim, 9, 100);
|
||||
uint16_t anim2[] = {1 + 4 + 16 + 64 + 256, 2 + 8 + 32 + 128};
|
||||
animate(anim2, 2, 500);
|
||||
animate(anim2, 2, 500);
|
||||
}
|
||||
|
||||
/* LOOP */
|
||||
unsigned long lastReport = 0;
|
||||
uint32_t prevButtonsState = 0;
|
||||
bool modeChanged = false;
|
||||
void loop() {
|
||||
/* BUTTONS */
|
||||
uint32_t buttonsState = 0;
|
||||
for (int i = 0; i < ButtonCount; i++) {
|
||||
buttons[i].update();
|
||||
int value = buttons[i].read();
|
||||
if (value != HIGH){
|
||||
buttonsState |= (uint32_t)1 << i;
|
||||
} else {
|
||||
buttonsState &= ~((uint32_t)1 << i);
|
||||
}
|
||||
}
|
||||
|
||||
/* USB DATA */
|
||||
if ( ( (micros() - lastReport) >= REPORT_DELAY) )//&& (buttonsState != prevButtonsState) )
|
||||
{
|
||||
POPNHID.sendState(buttonsState);
|
||||
lastReport = micros();
|
||||
prevButtonsState = buttonsState;
|
||||
|
||||
//check for HID-requested lightmode change
|
||||
POPNHID.updateLightMode();
|
||||
}
|
||||
|
||||
/* LAMPS */
|
||||
switch (POPNHID.getLightMode())
|
||||
{
|
||||
/* Reactive mode, locally determined lamp data */
|
||||
case 0:
|
||||
but_lights(buttonsState & 0x1ff);
|
||||
reactive_neon(buttonsState & 0x1ff);
|
||||
break;
|
||||
/* HID mode, only based on received HID data */
|
||||
case 1:
|
||||
POPNHID.updateLeds(0, false);
|
||||
break;
|
||||
/* Mixed inverse mode, received HID data and button state are combined then inverted */
|
||||
case 3:
|
||||
POPNHID.updateLeds(buttonsState & 0x1ff, true);
|
||||
break;
|
||||
/* Mixed mode, received HID data and button state are combined */
|
||||
default:
|
||||
POPNHID.updateLeds(buttonsState & 0x1ff, false);
|
||||
break;
|
||||
}
|
||||
|
||||
/* KEYPAD */
|
||||
if (kpd.getKeys())
|
||||
{
|
||||
for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
|
||||
{
|
||||
if ( kpd.key[i].stateChanged ) // Only find keys that have changed state.
|
||||
{
|
||||
switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
|
||||
case PRESSED:
|
||||
Keyboard.press(kpd.key[i].kchar);
|
||||
break;
|
||||
case HOLD:
|
||||
|
||||
break;
|
||||
case RELEASED:
|
||||
Keyboard.release(kpd.key[i].kchar);
|
||||
break;
|
||||
case IDLE:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* MANUAL LIGHTMODE UPDATE */
|
||||
if ( buttonsState & 1024 ) {
|
||||
if ( (buttonsState & 2) && (modeChanged == false)) {
|
||||
modeChanged = true;
|
||||
uint8_t mode = POPNHID.getLightMode()+1;
|
||||
if (mode > 3) mode = 0;
|
||||
POPNHID.setLightMode(mode);
|
||||
}
|
||||
else if (!(buttonsState&2)) {
|
||||
modeChanged = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Manage pillars and top neons in reactive mode */
|
||||
uint16_t neon_anim[] = {16, 24, 28, 30, 31, 30, 28, 24};
|
||||
int neon_anim_index = 0;
|
||||
uint16_t pillar_state[] = {0, 0x140, 0xA0, 0x1E0};
|
||||
int pillar_state_index = 0;
|
||||
bool pillar_lit = false;
|
||||
uint16_t prevState = 0;
|
||||
unsigned long lastBlink = 0;
|
||||
unsigned long lastNeonUpdate = 0;
|
||||
unsigned long lastButtonAction = 0;
|
||||
unsigned long actionRate = 0;
|
||||
unsigned long neonRate = 200;
|
||||
void reactive_neon(uint16_t buttonsState) {
|
||||
uint16_t neons = 0;
|
||||
unsigned long currTime = millis();
|
||||
|
||||
/*
|
||||
* SIDE PILLARS
|
||||
* When pressing any button the side pillars will blink for half a second
|
||||
* The color is randomly chosen with blue being predominant, red rare and purple super rare
|
||||
*/
|
||||
if ( buttonsState != prevState )
|
||||
{
|
||||
if ( buttonsState != 0 ){
|
||||
long randNumber = random(21);
|
||||
if (randNumber == 0)
|
||||
{
|
||||
pillar_state_index = 3;
|
||||
}
|
||||
else if (randNumber < 3)
|
||||
{
|
||||
pillar_state_index = 1;
|
||||
} else {
|
||||
pillar_state_index = 2;
|
||||
}
|
||||
pillar_lit = true;
|
||||
if (buttonsState != 0) lastButtonAction = currTime;
|
||||
}
|
||||
prevState = buttonsState;
|
||||
} else { /* no state change, continue to blink for 50ms */
|
||||
if (currTime - lastBlink > 50) {
|
||||
pillar_lit = !pillar_lit;
|
||||
lastBlink = currTime;
|
||||
}
|
||||
if (currTime - lastButtonAction > 500) {
|
||||
pillar_state_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (pillar_lit)
|
||||
neons |= pillar_state[pillar_state_index];
|
||||
|
||||
/*
|
||||
* Adjusting top neon animation speed (should go faster when you hit buttons quickly)
|
||||
*/
|
||||
actionRate = currTime - lastButtonAction;
|
||||
|
||||
if (15*actionRate < neonRate)
|
||||
neonRate *= 0.99995;
|
||||
else if (actionRate > 10*neonRate){
|
||||
neonRate = neonRate*1.01;
|
||||
if (neonRate < 100) neonRate++;
|
||||
}
|
||||
if (neonRate > 400)
|
||||
neonRate = 400;
|
||||
|
||||
if (neonRate < 40)
|
||||
neonRate = 40;
|
||||
|
||||
/*
|
||||
* Cycling through the top neon animation
|
||||
*/
|
||||
if ((currTime - lastNeonUpdate) > neonRate)
|
||||
{
|
||||
neon_anim_index++;
|
||||
if (neon_anim_index > 7) neon_anim_index = 0;
|
||||
lastNeonUpdate = currTime;
|
||||
}
|
||||
|
||||
neons |= neon_anim[neon_anim_index];
|
||||
|
||||
/*
|
||||
* Light the leds
|
||||
*/
|
||||
neon_lights(neons);
|
||||
}
|
||||
|
||||
/* Light up button lights according to bitfield */
|
||||
void but_lights(uint16_t lightDesc) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if ((lightDesc >> i) & 1) {
|
||||
digitalWrite(LightPins[i], HIGH);
|
||||
} else {
|
||||
digitalWrite(LightPins[i], LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Light up pillars and top neons according to bitfield */
|
||||
void neon_lights(uint16_t lightDesc) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if ((lightDesc >> i) & 1) {
|
||||
digitalWrite(LightPins[i + 9], HIGH);
|
||||
} else {
|
||||
digitalWrite(LightPins[i + 9], LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Display animation on the cab according to a bitfield array */
|
||||
void animate(uint16_t* tab, uint8_t n, int mswait) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
but_lights(tab[i]);
|
||||
neon_lights(tab[i]);
|
||||
delay(mswait);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user