依然话不多说,直接上代码,有需要直接拿去使用!!!!!
#pragma once
#include <cstdint>
#include <cstring>
#include <string>
#include <sstream>
#include <fcntl.h>
#include <functional>
#include <vector>
#include <unistd.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
#include <stdlib.h>
#include "Thread.h"
enum class F710_EVENT_TYPE
{
Button = 1,
Axis = 2,
ButtonHeartBeat = 129,
AxisHeartBeat = 130,
};
enum class F710KeyD : uint8_t
{
LeftThumbX = 0,
LeftThumbY = 1,
RightThumbX = 2,
RightThumbY = 3,
PadX = 4,
PadY = 5,
ButtonX = 0,
ButtonA = 1,
ButtonB = 2,
ButtonY = 3,
LeftBumper = 4,
RightBumper = 5,
LeftTrigger = 6,
RightTrigger = 7,
ButtonBack = 8,
ButtonStart = 9,
LeftStick = 10,
RighttStick = 11
};
enum class F710KeyX : uint8_t
{
LeftThumbX = 0,
LeftThumbY = 1,
LeftTrigger = 2,
RightThumbX = 3,
RightThumbY = 4,
RightTrigger = 5,
PadX = 6,
PadY = 7,
ButtonA = 0,
ButtonB = 1,
ButtonX = 2,
ButtonY = 3,
LeftBumper = 4,
RightBumper = 5,
ButtonBack = 6,
ButtonStart = 7,
ButtonHome = 8,
LeftStick = 9,
RighttStick = 10
};
class F710MCD
{
public:
~F710MCD();
bool init() override;
void addMCDCallback(const ChasisCodeCallback& chasisCallback, const OperatorCodeCallback& opCallback) override;
private:
void f710Process();
bool checkF710(const std::string& dirName);
bool openF710();
timespec timespecFromMs(uint32_t millis);
bool waitReadable(uint32_t timeout);
void callbackHandleDMode(js_event inputKey);
void callbackHandleXMode(js_event inputKey);
std::vector<ChasisCodeCallback> m_chasisCodeCallback;
std::vector<OperatorCodeCallback> m_operatorCodeCallback;
OperatorCodeMsg m_operatorCodeMsg;
ChasisCodeMsg m_chasisCodeMsg;
vcs::common::thread::Thread m_f710MCDThread;
int32_t m_keysFd;
bool m_findF710Flag = false;
bool m_isXmode;
bool m_isFrontButtonDown = false;
bool m_isBackButtonDown = false;
bool m_leftMoveAcc = false;
bool m_rightMoveAcc = false;
int32_t m_RightThumbXValue = 0;
int32_t m_RightThumbYValue = 0;
bool m_useMCD = true;
};
#include "F710MCD.h"
#include <string>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <errno.h>
const int32_t CHECK_F710_TIME = 5;
const double PI = 3.1415926;
F710MCD::~F710MCD()
{
m_f710MCDThread.stop();
}
bool F710MCD::init()
{
m_f710MCDThread.assignTask(std::bind(&F710MCD::f710Process, this));
m_f710MCDThread.start();
return true;
}
void F710MCD::addMCDCallback(const ChasisCodeCallback& chasisCallback,
const OperatorCodeCallback& opCallback)
{
if (chasisCallback)
{
m_chasisCodeCallback.emplace_back(chasisCallback);
}
if (opCallback)
{
m_operatorCodeCallback.emplace_back(opCallback);
}
}
bool F710MCD::checkF710(const std::string& dirName)
{
DIR* p_Dir;
bool isCheckF710 = false;
struct dirent* p_Dirent;
const char* dirStr = dirName.c_str();
p_Dir = opendir(dirStr);
if (p_Dir == NULL)
{
AERROR_EVERY(500) << " open dev " + dirName + " false";
isCheckF710 = false;
}
while ((p_Dirent = readdir(p_Dir)) != NULL)
{
if (strcmp(p_Dirent->d_name, "js0") == 0)
{
isCheckF710 = true;
}
}
if (!isCheckF710)
{
AERROR_EVERY(500) << " find F710 faile ";
}
closedir(p_Dir);
return