Hello everyone. I’m new to development when it comes to making a software based on a joystick I’m developing for MSFS and SimConnect itself.
As of now, I’m making a code that detects the Cessna 172s trimwheel and that I want to make sure the trimwheel I have will reciprocate the MSFS Cessna 172s’s trimwheel when I steer it upwards or downwards.
But the problem is, since I’m new, I’m really confused on how to make these setups work. What I’m currently doing is that I’m trying to send a normalized value of my axis to MSFS and the trimwheel in Cessna 172s will act accordingly to the number of it. I got an error that says: -2147467259.
Can anyone help me out? Thanks.
Here’s my code by the way. I’ve already made an initialization of SimConnect through my header file.
// TrimwheelActions.cpp
#include "TrimwheelActions.h"
#include <iostream>
#include <SDL.h>
const DWORD TrimwheelActions::TRIM_DEFINITION_ID;
TrimwheelActions::TrimwheelActions() {
lastAxisValue = 0;
simConnectHandle = nullptr;
}
void TrimwheelActions::handleTrimwheelEvents() {
if (trimwheelConnected && storedProductID != -1) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_JOYAXISMOTION) {
if (event.jaxis.which == 0 && event.jaxis.axis == 0) {
int normalizedValue = getActionForAxis(event.jaxis.value);
if (normalizedValue != -999) {
int lastValue = normalizedValue;
// Print the normalized value
std::cout << "Normalized value: " << normalizedValue << std::endl;
if (lastValue != lastAxisValue) {
lastAxisValue = lastValue;
handleEventFromAxis(lastValue);
}
}
}
}
}
}
}
int TrimwheelActions::getActionForAxis(int axisValue) {
int minAxisValue = -32768;
int maxAxisValue = 32767;
int normalizedMinValue = -16383;
int normalizedMaxValue = 16384;
int normalizedValue = ((axisValue - minAxisValue) * (normalizedMaxValue - normalizedMinValue))
/ (maxAxisValue - minAxisValue) + normalizedMinValue;
if (normalizedValue >= -16383 && normalizedValue <= 16384) {
return normalizedValue; // Return the normalized value
}
return -999;
}
void TrimwheelActions::handleEventFromAxis(int lastValue) {
float elevatorTrimValue = static_cast<float>(lastValue);
sendTrimCommand(elevatorTrimValue);
}
void TrimwheelActions::sendTrimCommand(float trimValue) {
HRESULT hr = SimConnect_SetDataOnSimObject(simConnectHandle, SIMCONNECT_OBJECT_ID_USER, TRIM_DEFINITION_ID, 0, 1, sizeof(float), &trimValue);
if (SUCCEEDED(hr)) {
std::cout << "SimConnect command successful" << std::endl;
}
else {
std::cerr << "SimConnect error: " << hr << std::endl;
}
}
// TrimwheelActions.h
#ifndef TRIMWHEEL_ACTIONS_H
#define TRIMWHEEL_ACTIONS_H
#include "SimConnectManager.h"
#include <iostream>
#include <SDL.h>
#include <unordered_map>
#include <Windows.h>
class TrimwheelActions {
private:
int getActionForAxis(int axisValue);
int lastAxisValue;
HANDLE simConnectHandle;
HRESULT hr;
SimConnectManager simConnectManager;
int storedProductID;
static const DWORD TRIM_DEFINITION_ID = 1000;
void handleEventFromAxis(int lastValue);
void sendTrimCommand(float trimValue);
public:
TrimwheelActions();
void handleTrimwheelEvents();
bool trimwheelConnected = false;
bool isTrimwheelConnected() const { return trimwheelConnected; }
void setTrimwheelProductID(int productID) { storedProductID = productID; }
};
#endif // TRIMWHEEL_ACTIONS_H