Skip to content

Add spectrograph support for SteelSeries Apex M800 #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions KeyboardVisualizerVC/HidUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "HidUtil.h"

#include <cfgmgr32.h>
#include <setupapi.h>

#include <sstream>
#include <string>

#pragma comment(lib, "setupapi.lib")

//==================================================================================================
// Code by http://www.reddit.com/user/chrisgzy
//==================================================================================================
bool IsMatchingDevice(wchar_t *pDeviceID, unsigned int uiVID, unsigned int uiPID, unsigned int uiMI)
{
unsigned int uiLocalVID = 0, uiLocalPID = 0, uiLocalMI = 0;

LPWSTR pszNextToken = 0;
LPWSTR pszToken = wcstok(pDeviceID, L"\\#&");
while (pszToken)
{
std::wstring tokenStr(pszToken);
if (tokenStr.find(L"VID_", 0, 4) != std::wstring::npos)
{
std::wistringstream iss(tokenStr.substr(4));
iss >> std::hex >> uiLocalVID;
}
else if (tokenStr.find(L"PID_", 0, 4) != std::wstring::npos)
{
std::wistringstream iss(tokenStr.substr(4));
iss >> std::hex >> uiLocalPID;
}
else if (tokenStr.find(L"MI_", 0, 3) != std::wstring::npos)
{
std::wistringstream iss(tokenStr.substr(3));
iss >> std::hex >> uiLocalMI;
}

pszToken = wcstok(0, L"\\#&");
}

if (uiVID != uiLocalVID || uiPID != uiLocalPID || uiMI != uiLocalMI)
return false;

return true;
}

//==================================================================================================
// Code by http://www.reddit.com/user/chrisgzy
//==================================================================================================
HANDLE GetDeviceHandle(unsigned int uiVID, unsigned int uiPID, unsigned int uiMI)
{
const GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 };
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_HID, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
return 0;

HANDLE hReturn = 0;

SP_DEVINFO_DATA deviceData = { 0 };
deviceData.cbSize = sizeof(SP_DEVINFO_DATA);

for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i)
{
wchar_t wszDeviceID[MAX_DEVICE_ID_LEN];
if (CM_Get_Device_IDW(deviceData.DevInst, wszDeviceID, MAX_DEVICE_ID_LEN, 0))
continue;

if (!IsMatchingDevice(wszDeviceID, uiVID, uiPID, uiMI))
continue;

SP_INTERFACE_DEVICE_DATA interfaceData = { 0 };
interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

if (!SetupDiEnumDeviceInterfaces(hDevInfo, &deviceData, &GUID_DEVINTERFACE_HID, 0, &interfaceData))
break;

DWORD dwRequiredSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, 0, 0, &dwRequiredSize, 0);

SP_INTERFACE_DEVICE_DETAIL_DATA *pData = (SP_INTERFACE_DEVICE_DETAIL_DATA *)new unsigned char[dwRequiredSize];
pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pData, dwRequiredSize, 0, 0))
{
delete pData;
break;
}

HANDLE hDevice = CreateFile(pData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (hDevice == INVALID_HANDLE_VALUE)
{
delete pData;
break;
}

hReturn = hDevice;
break;
}

SetupDiDestroyDeviceInfoList(hDevInfo);

return hReturn;
}
9 changes: 9 additions & 0 deletions KeyboardVisualizerVC/HidUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef HIDUTIL_H
#define HIDUTIL_H

#include <Windows.h>

bool IsMatchingDevice(wchar_t *pDeviceID, unsigned int uiVID, unsigned int uiPID, unsigned int uiMI);
HANDLE GetDeviceHandle(unsigned int uiVID, unsigned int uiPID, unsigned int uiMI);

#endif
4 changes: 4 additions & 0 deletions KeyboardVisualizerVC/KeyboardVisualizerVC.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,22 @@
<ItemGroup>
<ClCompile Include="chuck_fft.c" />
<ClCompile Include="CorsairCUE.cpp" />
<ClCompile Include="HidUtil.cpp" />
<ClCompile Include="hsv.cpp" />
<ClCompile Include="LEDStrip.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="KeyboardVisDlg.cpp" />
<ClCompile Include="MSIKeyboard.cpp" />
<ClCompile Include="RazerChroma.cpp" />
<ClCompile Include="serial_port.cpp" />
<ClCompile Include="SteelSeriesGameSense.cpp" />
<ClCompile Include="udp_port.cpp" />
<ClCompile Include="Visualizer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="chuck_fft.h" />
<ClInclude Include="CorsairCUE.h" />
<ClInclude Include="HidUtil.h" />
<ClInclude Include="hsv.h" />
<ClInclude Include="KeyboardVisDlg.h" />
<ClInclude Include="LEDStrip.h" />
Expand All @@ -195,6 +198,7 @@
<ClInclude Include="resource.h" />
<ClInclude Include="serial_port.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="SteelSeriesGameSense.h" />
<ClInclude Include="udp_port.h" />
<ClInclude Include="Visualizer.h" />
<ClInclude Include="VisualizerDefines.h" />
Expand Down
97 changes: 2 additions & 95 deletions KeyboardVisualizerVC/MSIKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "MSIKeyboard.h"

#include "HidUtil.h"

static bool init_ok = TRUE;

MSIKeyboard::MSIKeyboard()
Expand All @@ -18,103 +20,8 @@ MSIKeyboard::~MSIKeyboard()
}


#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hid.lib")

//==================================================================================================
// Code by http://www.reddit.com/user/chrisgzy
//==================================================================================================
static bool IsMatchingDevice(wchar_t *pDeviceID, unsigned int uiVID, unsigned int uiPID, unsigned int uiMI)
{
unsigned int uiLocalVID = 0, uiLocalPID = 0, uiLocalMI = 0;

LPWSTR pszNextToken = 0;
LPWSTR pszToken = wcstok(pDeviceID, L"\\#&");
while (pszToken)
{
std::wstring tokenStr(pszToken);
if (tokenStr.find(L"VID_", 0, 4) != std::wstring::npos)
{
std::wistringstream iss(tokenStr.substr(4));
iss >> std::hex >> uiLocalVID;
}
else if (tokenStr.find(L"PID_", 0, 4) != std::wstring::npos)
{
std::wistringstream iss(tokenStr.substr(4));
iss >> std::hex >> uiLocalPID;
}
else if (tokenStr.find(L"MI_", 0, 3) != std::wstring::npos)
{
std::wistringstream iss(tokenStr.substr(3));
iss >> std::hex >> uiLocalMI;
}

pszToken = wcstok(0, L"\\#&");
}

if (uiVID != uiLocalVID || uiPID != uiLocalPID || uiMI != uiLocalMI)
return false;

return true;
}

//==================================================================================================
// Code by http://www.reddit.com/user/chrisgzy
//==================================================================================================
static HANDLE GetDeviceHandle(unsigned int uiVID, unsigned int uiPID, unsigned int uiMI)
{
const GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 };
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_HID, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
return 0;

HANDLE hReturn = 0;

SP_DEVINFO_DATA deviceData = { 0 };
deviceData.cbSize = sizeof(SP_DEVINFO_DATA);

for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i)
{
wchar_t wszDeviceID[MAX_DEVICE_ID_LEN];
if (CM_Get_Device_IDW(deviceData.DevInst, wszDeviceID, MAX_DEVICE_ID_LEN, 0))
continue;

if (!IsMatchingDevice(wszDeviceID, uiVID, uiPID, uiMI))
continue;

SP_INTERFACE_DEVICE_DATA interfaceData = { 0 };
interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

if (!SetupDiEnumDeviceInterfaces(hDevInfo, &deviceData, &GUID_DEVINTERFACE_HID, 0, &interfaceData))
break;

DWORD dwRequiredSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, 0, 0, &dwRequiredSize, 0);

SP_INTERFACE_DEVICE_DETAIL_DATA *pData = (SP_INTERFACE_DEVICE_DETAIL_DATA *)new unsigned char[dwRequiredSize];
pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pData, dwRequiredSize, 0, 0))
{
delete pData;
break;
}

HANDLE hDevice = CreateFile(pData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (hDevice == INVALID_HANDLE_VALUE)
{
delete pData;
break;
}

hReturn = hDevice;
break;
}

SetupDiDestroyDeviceInfoList(hDevInfo);

return hReturn;
}

void MSIKeyboard::send_usb_msg(char * data_pkt)
{
Expand Down
125 changes: 125 additions & 0 deletions KeyboardVisualizerVC/SteelSeriesGameSense.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*---------------------------------------------------------*\
| Processing Code for SteelSeries Apex M800 |
| |
| saltybot@gmail.com, 12/13/2016 |
\*---------------------------------------------------------*/

#include "SteelSeriesGameSense.h"

#pragma comment(lib, "hid.lib")

extern "C" {
#include <hidsdi.h>
}

#include "HidUtil.h"

namespace
{

const int ROWS = 6;
const int COLS = 23;

const unsigned char KeyMap[ROWS][COLS] = {
0xe8, 0x29, 0xff, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0xff, 0x00, 0xff, 0xff,
0xe9, 0x35, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0xff, 0x2a, 0x49, 0x4a, 0x4b, 0x53, 0x54, 0x55, 0x56,
0xea, 0x2b, 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c, 0x12, 0x13, 0x2f, 0x30, 0xff, 0x31, 0x4c, 0x4d, 0x4e, 0x5f, 0x60, 0x61, 0x57,
0xeb, 0x39, 0x04, 0x16, 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33, 0x34, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0x5c, 0x5d, 0x5e, 0xff,
0xec, 0xe1, 0xff, 0x1d, 0x1b, 0x06, 0x19, 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0xff, 0xff, 0xff, 0x52, 0xff, 0x59, 0x5a, 0x5b, 0x58,
0xed, 0xe0, 0xe3, 0xff, 0xe2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe6, 0xe7, 0x65, 0xef, 0xe4, 0x50, 0x51, 0x4f, 0x62, 0xff, 0x63, 0xff
};

}

SteelSeriesGameSense::SteelSeriesGameSense()
:Dev(INVALID_HANDLE_VALUE)
,FeatureReportBuf()
{
}

SteelSeriesGameSense::~SteelSeriesGameSense()
{
unsigned char buf[33];
memset(buf, 0, sizeof(buf));
buf[0] = 0x00;
buf[1] = 0x0d;
buf[2] = 0x00;
buf[3] = 0x00;

HidD_SetOutputReport(Dev, buf, sizeof(buf));
}

void SteelSeriesGameSense::Initialize()
{
Dev = GetDeviceHandle(0x1038, 0x1600, 0x0000);

SetMode(0x02);
}

bool SteelSeriesGameSense::SetLEDs(COLORREF pixels[64][256])
{
memset(FeatureReportBuf, 0, sizeof(FeatureReportBuf));
FeatureReportBuf[0] = 0x00;
FeatureReportBuf[1] = 0x0c;
FeatureReportBuf[2] = 0x00;
FeatureReportBuf[3] = 0x6e;
FeatureReportBuf[4] = 0x00;

int i = 5;
int yStep = 64 / ROWS;
int xStep = 256 / COLS;

for (int y = 0; y < ROWS; ++y)
{
for (int x = 0; x < COLS; ++x)
{
unsigned char key = KeyMap[y][x];
if (key != 0xff)
{
COLORREF color = pixels[y * yStep][x * xStep];
FeatureReportBuf[i++] = key;
FeatureReportBuf[i++] = GetRValue(color);
FeatureReportBuf[i++] = GetGValue(color);
FeatureReportBuf[i++] = GetBValue(color);
}
}
}

return (HidD_SetFeature(Dev, FeatureReportBuf, sizeof(FeatureReportBuf))) ? true : false;
}

void SteelSeriesGameSense::SetMode(unsigned char mode)
{
if (Dev != INVALID_HANDLE_VALUE)
{
memset(FeatureReportBuf, 0, sizeof(FeatureReportBuf));
FeatureReportBuf[0] = 0x00;
FeatureReportBuf[1] = 0x0e;
FeatureReportBuf[2] = 0x00;
FeatureReportBuf[3] = 0x2a;
FeatureReportBuf[4] = 0x00;

int stride = 12;
for (int y = 0; y < ROWS; ++y)
{
int i = 5;
memset(&FeatureReportBuf[i], 0, COLS * stride);

for (int x = 0; x < COLS; ++x)
{
unsigned char key = KeyMap[y][x];
if (key != 0xff)
{
FeatureReportBuf[i + 9] = mode;
FeatureReportBuf[i + 11] = key;
i += stride;
}
}

FeatureReportBuf[3] = i / stride;

HidD_SetFeature(Dev, FeatureReportBuf, sizeof(FeatureReportBuf));
Sleep(10);
}
}
}
Loading