簡介
集成羅技G29方向盤SDK,獲取羅技G29方向盤的轉角、油門、剎車、擋位等數據
SDK下載
地址:https://www.logitechg.com.cn/zh-cn/innovation/developer-lab.html
進入之后選擇下載第一個:方向盤SDK
SDK說明
解壓后文件夾中有:
Demo(一個編譯好的MFC例子,用於測試羅技G29的驅動是否安裝好,是否與電腦連接成功)
Doc(SDK說明文檔)
Include(.h頭文件)
Lib(.lib .dll庫文件,包含x86和x64)
Samples(三個MFC工程示例,使用Visual Studio 2017加載工程,配置項目屬性即可編譯成功)
SDK中MFC示例使用說明
用於檢測羅技G29方向盤是否能正常工作
1、下載安裝羅技驅動lghub_installer.exe,地址:https://support.logi.com/hc/zh-cn/articles/360025298133
2、把羅技G29方向盤的USB接入電腦
3、雙擊Demo中SteeringWheelSDKDemo.exe(對應Sample中第一個,第二個是各個接口函數,第三個不用管)
4、點擊 Init 初始化之后,可以看到 Device 0 和 Device 1哪個有數據index=哪個,且有數據證明正常使用
SDK代碼接入
方式一:
說明:因為最初不知道Windows下編譯的庫怎么導入Qt中使用,所以用該方法把Qt嵌入到Visual Studio中
開發環境:Windows10
開發工具:Visual Studio2017+Qt5.14.2
需要文件:LogitechSteeringWheelLib.h、LogitechSteeringWheelLib.lib
參考博客:https://blog.csdn.net/qq_41250354/article/details/104652071
1、工程屬性中添加附加包含目錄(SDK頭文件目錄):工程→屬性→配置屬性→C/C++→常規→附加包含目錄
2、添加附加庫目錄(SDK靜態庫目錄):工程→屬性→配置屬性→鏈接器→常規→附加庫目錄
3、添加附加依賴項(SDK靜態庫文件):工程→屬性→配置屬性→鏈接器→輸入→附加依賴項
4、代碼中加入,邏輯代碼方法二中再說
#pragma comment(lib, "LogitechSteeringWheelLib.lib") #include "LogitechSteeringWheelLib.h"
方式二:
說明:該方法可以直接加載dll動態庫,並使用動態庫中的函數,即使用Qt MinGW 和 MSVC 編譯器都可以
開發環境:Windows10
開發工具:Qt5.14.2
需要文件:LogitechSteeringWheelLib.h、LogitechSteeringWheelEnginesWrapper.dll
注:Qt調用dll方法
法一使用Win32 API(調用LoadLibrary、GetProcAddress、FreeLibrary)
法二使用Qt API(定義函數指針、QLibrary、resolve)
法三使用靜態鏈接方式(需要lib和dll):Qt pro文件中LIBS += -L路徑 -l庫名,其中引用庫名格式為xxx.a,由於羅技官方沒有提供,直接Pass
1、沒有方法一中繁瑣的步驟,這就是Qt的魅力,上代碼(下面貼的代碼使用的是法二加載dll庫)
我是使用定時器去獲取羅技方向盤數據的,定時器的寫法相信大家都會,我這里就沒有貼出來了
Logitech.h #include "LogitechSteeringWheelLib.h" class Logitech : public QObject { Q_OBJECT public: explicit Logitech(QObject *parent = nullptr); ~Logitech(); bool init(HWND hwnd); //初始化羅技G29 QTimer* m_timer; //獲取數據定時器 public slots: void onGetLogitechData(); //獲取羅技G29數據 private: int m_index; //羅技方向盤設備節點 };
Logitech.cpp #include "Logitech.h" //bool LogiSteeringInitializeWithWindow(bool ignoreXInputControllers, HWND hwnd); typedef bool (*Fun0)(bool ignoreXInputControllers, HWND hwnd); //bool LogiUpdate(); typedef bool (*Fun1)(); //bool LogiIsConnected(const int index); typedef bool (*Fun2)(const int index); //DIJOYSTATE2* LogiGetState(const int index); typedef DIJOYSTATE2* (*Fun3)(const int index); Fun0 initLogitech; //初始化羅技SDK,會關聯一個窗口 Fun1 updateLogitech; //更新設備狀態 Fun2 connectedLogitech; //檢查設備索引是否連接 Fun3 getStateLogitech; //以基本的方法獲取設備狀態 Logitech::Logitech(QObject *parent) : QObject(parent) { qDebug() << "thread 1: " << QThread::currentThreadId(); m_index = 0; m_timer = new QTimer(this); } Logitech::~Logitech() {} bool Logitech::init(HWND hwnd) { qDebug() << "thread 2: " << QThread::currentThreadId(); qDebug() << "Logitech init"; //加載dll QLibrary qLib("LogitechSteeringWheelEnginesWrapper"); if(qLib.load()) { qDebug() << "QLibrary load success!"; initLogitech = (Fun0)qLib.resolve("LogiSteeringInitializeWithWindow"); updateLogitech = (Fun1)qLib.resolve("LogiUpdate"); connectedLogitech = (Fun2)qLib.resolve("LogiIsConnected"); getStateLogitech = (Fun3)qLib.resolve("LogiGetState"); if(initLogitech && updateLogitech && connectedLogitech && getStateLogitech) { qDebug() << "resolve success!"; //初始化羅技方向盤 bool ret = initLogitech(true, hwnd); if (ret) { connect(m_timer, &QTimer::timeout, this, &Logitech::onGetLogitechData); } return ret; } else { qDebug() << "resolve failed!"; return false; } } else { qDebug() << "QLibrary load failed!"; return false; } } void Logitech::onGetLogitechData() { if (updateLogitech() && connectedLogitech(m_index)) { DIJOYSTATE2* logitechData = getStateLogitech(m_index); qDebug("%6d %6d %6d %d", logitechData->lX, //方向盤角度 logitechData->lY, //油門 logitechData->lRz, //剎車 logitechData->rgbButtons[12/13/14] == 128);//擋位 } else { qDebug() << "Logitech getLogitechData failed"; } }