Kilian Kurt Hofmann 14c6361fbd Correct serial speed
On ground check
Reset of arduino on ac change
send only if changed
2017-01-15 15:18:32 +01:00

536 lines
25 KiB
C++

// Mode mapping via config file in aircraft folder
#include "stdafx.h"
// Things for execution
bool quit = 0;
HANDLE hSimConnect = NULL;
char *throttleQuadrantCFG;
initialization init;
uint8_t numEngines = 0;
SerialPort *arduino;
values potValues;
int sizeOfDataToSendToFSX = 0;
bool ready = false;
bool hasPropeller = false;
bool onGround = true;
engineAll eAOne;
engineAll eATwo;
engineAll eAThree;
engineAll eAFour;
engineAll lastSaveOne;
engineAll lastSaveTwo;
engineAll lastSaveThree;
engineAll lastSaveFour;
void setupDefinitions() {
printf("\nSetting up ... ");
HRESULT hr;
hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE);
hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO);
hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE);
hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR);
FILE *fp = fopen(throttleQuadrantCFG, "r");
char * line = NULL;
bool hasMixture = false;
if (fp == NULL) {
printf("\nThe application cannot run without a valid tq.cfg.");
arduino->writeSerialPort("q", 1);
exit(EXIT_FAILURE);
}
while ((line = readLine(fp)) != NULL) {
// Parse
char *assign = strstr(line, "=");
char *id = (char *)calloc(13, 1);
memcpy(id, line, ((long)assign - (long)line));
if (strcmp(trimwhitespace(id), "numModes") == 0)
init.numModes = atoi(assign + 1);
else if (strcmp(trimwhitespace(id), "hasPropeller") == 0)
hasPropeller = atoi(assign + 1);
else if (strcmp(trimwhitespace(id), "hasMixture") == 0)
hasMixture = atoi(assign + 1);
else if (strcmp(trimwhitespace(id), "mode1") == 0)
strcpy(init.modes[0], strrev(assign + 1));
else if (strcmp(trimwhitespace(id), "mode2") == 0)
strcpy(init.modes[1], strrev(assign + 1));
else if (strcmp(trimwhitespace(id), "mode3") == 0)
strcpy(init.modes[2], strrev(assign + 1));
free(line);
free(id);
}
fclose(fp);
// Setup Data definitions
printf("AC has %i eng, prop: %d, mix: %d ... ", numEngines, hasPropeller, hasMixture);
if (!hasPropeller && !hasMixture)
{
switch (numEngines) {
case 1:
printf(" set one eng t mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
break;
case 2:
printf(" set two eng t mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
break;
case 3:
printf(" set three eng t mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
break;
case 4:
printf(" set four eng t mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG THROTTLE LEVER POSITION:4", "percent");
break;
default:
break;
}
sizeOfDataToSendToFSX = sizeof(double);
}
else if (hasPropeller && !hasMixture)
{
switch (numEngines) {
case 1:
printf(" set one eng tp mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
break;
case 2:
printf(" set two eng tp mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG PROPELLER LEVER POSITION:2", "percent");
break;
case 3:
printf(" set three eng tp mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG PROPELLER LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG PROPELLER LEVER POSITION:3", "percent");
break;
case 4:
printf(" set four eng tp mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG THROTTLE LEVER POSITION:4", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG PROPELLER LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG PROPELLER LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG PROPELLER LEVER POSITION:4", "percent");
break;
default:
break;
}
sizeOfDataToSendToFSX = 2*sizeof(double);
}
else if (!hasPropeller && hasMixture)
{
switch (numEngines) {
case 1:
printf(" set one eng tm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
break;
case 2:
printf(" set two eng tm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG MIXTURE LEVER POSITION:2", "percent");
break;
case 3:
printf(" set three eng tm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG MIXTURE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG MIXTURE LEVER POSITION:3", "percent");
break;
case 4:
printf(" set four eng tm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG THROTTLE LEVER POSITION:4", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG MIXTURE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG MIXTURE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG MIXTURE LEVER POSITION:4", "percent");
break;
default:
break;
}
sizeOfDataToSendToFSX = 2*sizeof(double);
}
else if (hasPropeller && hasMixture)
{
switch (numEngines) {
case 1:
printf(" set one eng tpm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
break;
case 2:
printf(" set two eng tpm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG PROPELLER LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG MIXTURE LEVER POSITION:2", "percent");
break;
case 3:
printf(" set three eng tpm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG PROPELLER LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG PROPELLER LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG MIXTURE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG MIXTURE LEVER POSITION:3", "percent");
break;
case 4:
printf(" set four eng tpm mode ... ");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG THROTTLE LEVER POSITION:4", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG PROPELLER LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG PROPELLER LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG PROPELLER LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG PROPELLER LEVER POSITION:4", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_TWO, "GENERAL ENG MIXTURE LEVER POSITION:2", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_THREE, "GENERAL ENG MIXTURE LEVER POSITION:3", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_FOUR, "GENERAL ENG MIXTURE LEVER POSITION:4", "percent");
break;
default:
break;
}
sizeOfDataToSendToFSX = sizeof(engineAll);
}
printf("done!");
arduino->writeSerialPort("r", 1);
arduino->writeSerialPort("i", 1);
arduino->writeSerialPort((char *)&init, sizeof(initialization));
// Request AC_ON_GROUND data every frame for rev engagement
hr = SimConnect_RequestDataOnSimObject(hSimConnect, AC_ON_GROUND_REQUEST, DEFINITION_AC_ON_GROUND, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_SIM_FRAME);
ready = true;
}
// SimConnect callback
void CALLBACK MyDispatchProcTC(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
{
HRESULT hr;
switch (pData->dwID)
{
case SIMCONNECT_RECV_ID_SIMOBJECT_DATA: {
SIMCONNECT_RECV_SIMOBJECT_DATA *data = (SIMCONNECT_RECV_SIMOBJECT_DATA *)pData;
switch (data->dwRequestID) {
case AC_ON_GROUND_REQUEST:
{
printf("\nOn ground: %f", &data->dwData);
onGround = (int)&data->dwData;
break;
}
case AC_DATA_REQUEST:
{
printf("Aircraft data retrieved.");
init.maxRev = (int8_t)((aircraftData *)&data->dwData)->lowerThrottleLimit;
numEngines = (uint8_t)((aircraftData *)&data->dwData)->numEngines;
setupDefinitions();
break;
}
default:
break;
}
break;
}
case SIMCONNECT_RECV_ID_SYSTEM_STATE:
{
SIMCONNECT_RECV_SYSTEM_STATE *state = (SIMCONNECT_RECV_SYSTEM_STATE *)pData;
switch (state->dwRequestID)
{
// Receive loaded aircraft name, set path to tq.cfg
case REQUEST_SYSTEM_STATE:
{
char *path = strrev(strstr(strrev(state->szString), "\\"));
throttleQuadrantCFG = (char *)calloc(strlen(path) + 6, 1);
strcpy(throttleQuadrantCFG, path);
strcat(throttleQuadrantCFG, "tq.cfg");
printf("%s loaded.", throttleQuadrantCFG);
printf("\nRetrieving aicraft data ... ");
hr = SimConnect_RequestDataOnSimObject(hSimConnect, AC_DATA_REQUEST, DEFINITION_AC_DATA, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_ONCE);
break;
}
default:
break;
}
break;
}
case SIMCONNECT_RECV_ID_EVENT:
{
SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT *)pData;
switch (evt->uEventID)
{
case EVENT_SIM_START:
{
// Send this request to get the user aircraft id
printf("\nRequesting loaded AC path ... ");
hr = SimConnect_RequestSystemState(hSimConnect, REQUEST_SYSTEM_STATE, "AircraftLoaded");
}
break;
default:
break;
}
break;
}
case SIMCONNECT_RECV_ID_QUIT:
{
quit = 1;
break;
}
default:
break;
}
}
void throttleControl()
{
HRESULT hr;
if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Throttle Control", NULL, 0, 0, 0)))
{
printf("\nConnected to Flight Simulator!");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_AC_DATA, "NUMBER OF ENGINES", "number");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_AC_DATA, "THROTTLE LOWER LIMIT", "percent");
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_AC_ON_GROUND, "SIM ON GROUND", "bool");
// Request a simulation started event
hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");
while (0 == quit)
{
SimConnect_CallDispatch(hSimConnect, MyDispatchProcTC, NULL);
if (ready) {
arduino->writeSerialPort("s", 1);
arduino->readSerialPort((char *)&potValues, sizeof(values));
// Set Values
int currentEngine = 1;
char lastSymbol;
for (int i = 3; i >= 0; i--) {
if (init.modes[potValues.mode][i] != lastSymbol)
currentEngine = 1;
if (init.modes[potValues.mode][i] == 'T')
{
lastSymbol = 'T';
switch (currentEngine) {
case 1:
lastSaveOne.throttlePercent = eAOne.throttlePercent;
eAOne.throttlePercent = (double)potValues.leverValuesInPercent[3 - i];
break;
case 2:
lastSaveTwo.throttlePercent = eATwo.throttlePercent;
eATwo.throttlePercent = (double)potValues.leverValuesInPercent[3 - i];
break;
case 3:
lastSaveThree.throttlePercent = eAThree.throttlePercent;
eAThree.throttlePercent = (double)potValues.leverValuesInPercent[3 - i];
break;
case 4:
lastSaveFour.throttlePercent = eAFour.throttlePercent;
eAFour.throttlePercent = (double)potValues.leverValuesInPercent[3 - i];
break;
}
currentEngine++;
}
else if (init.modes[potValues.mode][i] == 'P')
{
lastSymbol = 'P';
switch (currentEngine) {
case 1:
lastSaveOne.propellerPrecent = eAOne.propellerPrecent;
eAOne.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
break;
case 2:
lastSaveTwo.propellerPrecent = eATwo.propellerPrecent;
eATwo.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
break;
case 3:
lastSaveThree.propellerPrecent = eAThree.propellerPrecent;
eAThree.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
break;
case 4:
lastSaveFour.propellerPrecent = eAFour.propellerPrecent;
eAFour.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
break;
}
currentEngine++;
}
else if (init.modes[potValues.mode][i] == 'M')
{
lastSymbol = 'M';
switch (currentEngine) {
case 1:
if (hasPropeller) {
lastSaveOne.mixturePercent = eAOne.mixturePercent;
eAOne.mixturePercent = (double)potValues.leverValuesInPercent[3 - i];
}
else {
lastSaveOne.mixturePercent = eAOne.propellerPrecent;
eAOne.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
}
break;
case 2:
if (hasPropeller) {
lastSaveTwo.mixturePercent = eATwo.mixturePercent;
eATwo.mixturePercent = (double)potValues.leverValuesInPercent[3 - i];
}
else {
lastSaveTwo.mixturePercent = eATwo.propellerPrecent;
eATwo.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
}
break;
case 3:
if (hasPropeller) {
lastSaveThree.mixturePercent = eAThree.mixturePercent;
eAThree.mixturePercent = (double)potValues.leverValuesInPercent[3 - i];
}
else {
lastSaveThree.mixturePercent = eAThree.propellerPrecent;
eAThree.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
}
break;
case 4:
if (hasPropeller) {
lastSaveFour.mixturePercent = eAFour.mixturePercent;
eAFour.mixturePercent = (double)potValues.leverValuesInPercent[3 - i];
}
else {
lastSaveFour.mixturePercent = eAFour.mixturePercent;
eAFour.propellerPrecent = (double)potValues.leverValuesInPercent[3 - i];
}
break;
}
currentEngine++;
}
}
// Send to sim
switch (numEngines)
{
case 1:
if (lastSaveOne.throttlePercent != eAOne.throttlePercent ||
(hasPropeller && (lastSaveOne.propellerPrecent != eAOne.propellerPrecent || lastSaveOne.mixturePercent != eAOne.mixturePercent)) ||
(!hasPropeller && (lastSaveOne.mixturePercent != eAOne.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_ONE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAOne);
break;
case 2:
if (lastSaveOne.throttlePercent != eAOne.throttlePercent ||
(hasPropeller && (lastSaveOne.propellerPrecent != eAOne.propellerPrecent || lastSaveOne.mixturePercent != eAOne.mixturePercent)) ||
(!hasPropeller && (lastSaveOne.mixturePercent != eAOne.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_ONE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAOne);
if (lastSaveTwo.throttlePercent != eATwo.throttlePercent ||
(hasPropeller && (lastSaveTwo.propellerPrecent != eATwo.propellerPrecent || lastSaveTwo.mixturePercent != eATwo.mixturePercent)) ||
(!hasPropeller && (lastSaveTwo.mixturePercent != eATwo.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_TWO, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eATwo);
break;
case 3:
if (lastSaveOne.throttlePercent != eAOne.throttlePercent ||
(hasPropeller && (lastSaveOne.propellerPrecent != eAOne.propellerPrecent || lastSaveOne.mixturePercent != eAOne.mixturePercent)) ||
(!hasPropeller && (lastSaveOne.mixturePercent != eAOne.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_ONE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAOne);
if (lastSaveTwo.throttlePercent != eATwo.throttlePercent ||
(hasPropeller && (lastSaveTwo.propellerPrecent != eATwo.propellerPrecent || lastSaveTwo.mixturePercent != eATwo.mixturePercent)) ||
(!hasPropeller && (lastSaveTwo.mixturePercent != eATwo.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_TWO, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eATwo);
if (lastSaveThree.throttlePercent != eAThree.throttlePercent ||
(hasPropeller && (lastSaveThree.propellerPrecent != eAThree.propellerPrecent || lastSaveThree.mixturePercent != eAThree.mixturePercent)) ||
(!hasPropeller && (lastSaveThree.mixturePercent != eAThree.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_THREE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAThree);
break;
case 4:
if (lastSaveOne.throttlePercent != eAOne.throttlePercent ||
(hasPropeller && (lastSaveOne.propellerPrecent != eAOne.propellerPrecent || lastSaveOne.mixturePercent != eAOne.mixturePercent)) ||
(!hasPropeller && (lastSaveOne.mixturePercent != eAOne.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_ONE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAOne);
if (lastSaveTwo.throttlePercent != eATwo.throttlePercent ||
(hasPropeller && (lastSaveTwo.propellerPrecent != eATwo.propellerPrecent || lastSaveTwo.mixturePercent != eATwo.mixturePercent)) ||
(!hasPropeller && (lastSaveTwo.mixturePercent != eATwo.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_TWO, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eATwo);
if (lastSaveThree.throttlePercent != eAThree.throttlePercent ||
(hasPropeller && (lastSaveThree.propellerPrecent != eAThree.propellerPrecent || lastSaveThree.mixturePercent != eAThree.mixturePercent)) ||
(!hasPropeller && (lastSaveThree.mixturePercent != eAThree.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_THREE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAThree);
if (lastSaveFour.throttlePercent != eAFour.throttlePercent ||
(hasPropeller && (lastSaveFour.propellerPrecent != eAFour.propellerPrecent || lastSaveFour.mixturePercent != eAFour.mixturePercent)) ||
(!hasPropeller && (lastSaveFour.mixturePercent != eAFour.propellerPrecent)))
hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ENGINE_FOUR, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeOfDataToSendToFSX, &eAFour);
break;
default:
break;
}
}
Sleep(10);
}
hr = SimConnect_Close(hSimConnect);
}
}
int __cdecl _tmain(int argc, _TCHAR* argv[])
{
// Open com to arduino
char* portName = "\\\\.\\COM3";
arduino = new SerialPort(portName);
if (arduino->isConnected()) {
// Calibrate
printf("\nCalibration ... ");
arduino->writeSerialPort("c", 1);
printf("done.");
char ready;
int hasRead = arduino->readSerialPort(&ready, 1);
while (hasRead != 1 && ready != 'r')
{
// EMPTY WAIT FOR CALIBRATION
}
// Start TQ
throttleControl();
}
return 0;
}