From cb733f03f709949d8605e2960b6ce6d101fb55a9 Mon Sep 17 00:00:00 2001 From: Kilian Kurt Hofmann Date: Fri, 13 Jan 2017 22:30:06 +0100 Subject: [PATCH] Prepare for ARDUINO --- TQInterface/SerialPort.cpp | 97 +++++++++ TQInterface/SerialPort.h | 35 +++ TQInterface/StringAdditions.cpp | 61 ++++++ TQInterface/TQInterface.cpp | 271 ++++++++++++------------ TQInterface/TQInterface.vcxproj | 4 +- TQInterface/TQInterface.vcxproj.filters | 10 +- TQInterface/stdafx.cpp | 8 - TQInterface/stdafx.h | 53 ++++- 8 files changed, 392 insertions(+), 147 deletions(-) create mode 100644 TQInterface/SerialPort.cpp create mode 100644 TQInterface/SerialPort.h create mode 100644 TQInterface/StringAdditions.cpp delete mode 100644 TQInterface/stdafx.cpp diff --git a/TQInterface/SerialPort.cpp b/TQInterface/SerialPort.cpp new file mode 100644 index 0000000..4d749bc --- /dev/null +++ b/TQInterface/SerialPort.cpp @@ -0,0 +1,97 @@ +/* +* Author: Manash Kumar Mandal +* Modified Library introduced in Arduino Playground which does not work +* This works perfectly +* LICENSE: MIT +*/ + +#include "SerialPort.h" + +SerialPort::SerialPort(char *portName) +{ + this->connected = false; + + this->handler = CreateFileA(static_cast(portName), + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + if (this->handler == INVALID_HANDLE_VALUE){ + if (GetLastError() == ERROR_FILE_NOT_FOUND){ + printf("ERROR: Handle was not attached. Reason: %s not available\n", portName); + } + else + { + printf("ERROR!!!"); + } + } + else { + DCB dcbSerialParameters = {0}; + + if (!GetCommState(this->handler, &dcbSerialParameters)) { + printf("failed to get current serial parameters"); + } + else { + dcbSerialParameters.BaudRate = CBR_9600; + dcbSerialParameters.ByteSize = 8; + dcbSerialParameters.StopBits = ONESTOPBIT; + dcbSerialParameters.Parity = NOPARITY; + dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE; + + if (!SetCommState(handler, &dcbSerialParameters)) + { + printf("ALERT: could not set Serial port parameters\n"); + } + else { + this->connected = true; + PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR); + Sleep(ARDUINO_WAIT_TIME); + } + } + } +} + +SerialPort::~SerialPort() +{ + if (this->connected){ + this->connected = false; + CloseHandle(this->handler); + } +} + +int SerialPort::readSerialPort(char *buffer, unsigned int buf_size) +{ + DWORD bytesRead; + unsigned int toRead; + + ClearCommError(this->handler, &this->errors, &this->status); + + if (this->status.cbInQue > 0){ + if (this->status.cbInQue > buf_size){ + toRead = buf_size; + } + else toRead = this->status.cbInQue; + } + + if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL)) return bytesRead; + + return 0; +} + +bool SerialPort::writeSerialPort(char *buffer, unsigned int buf_size) +{ + DWORD bytesSend; + + if (!WriteFile(this->handler, (void*) buffer, buf_size, &bytesSend, 0)){ + ClearCommError(this->handler, &this->errors, &this->status); + return false; + } + else return true; +} + +bool SerialPort::isConnected() +{ + return this->connected; +} diff --git a/TQInterface/SerialPort.h b/TQInterface/SerialPort.h new file mode 100644 index 0000000..486d686 --- /dev/null +++ b/TQInterface/SerialPort.h @@ -0,0 +1,35 @@ +/* +* Author: Manash Kumar Mandal +* Modified Library introduced in Arduino Playground which does not work +* This works perfectly +* LICENSE: MIT +*/ + + +#ifndef SERIALPORT_H +#define SERIALPORT_H + +#define ARDUINO_WAIT_TIME 2000 +#define MAX_DATA_LENGTH 255 + +#include +#include +#include + +class SerialPort +{ +private: + HANDLE handler; + bool connected; + COMSTAT status; + DWORD errors; +public: + SerialPort(char *portName); + ~SerialPort(); + + int readSerialPort(char *buffer, unsigned int buf_size); + bool writeSerialPort(char *buffer, unsigned int buf_size); + bool isConnected(); +}; + +#endif // SERIALPORT_H diff --git a/TQInterface/StringAdditions.cpp b/TQInterface/StringAdditions.cpp new file mode 100644 index 0000000..8640d70 --- /dev/null +++ b/TQInterface/StringAdditions.cpp @@ -0,0 +1,61 @@ +#include "stdafx.h" + +char *trimwhitespace(char *str) +{ + char *end; + + // Trim leading space + while (isspace((unsigned char)*str)) str++; + + if (*str == 0) // All spaces? + return str; + + // Trim trailing space + end = str + strlen(str) - 1; + while (end > str && isspace((unsigned char)*end)) end--; + + // Write new null terminator + *(end + 1) = 0; + + return str; +} + +char *readLine(FILE *file) { + + if (file == NULL) { + printf("\nError: file pointer is null."); + exit(1); + } + + int maximumLineLength = 128; + char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength); + + if (lineBuffer == NULL) { + printf("\nError allocating memory for line buffer."); + exit(1); + } + + char ch = getc(file); + int count = 0; + + while ((ch != '\n') && (ch != EOF)) { + if (count == maximumLineLength) { + maximumLineLength += 128; + lineBuffer = (char *)realloc(lineBuffer, maximumLineLength); + if (lineBuffer == NULL) { + printf("\nError reallocating space for line buffer."); + exit(1); + } + } + lineBuffer[count] = ch; + count++; + + ch = getc(file); + } + + lineBuffer[count] = '\0'; + if (count > 0) + return lineBuffer; + else + return NULL; +} \ No newline at end of file diff --git a/TQInterface/TQInterface.cpp b/TQInterface/TQInterface.cpp index c9af578..8466bd9 100644 --- a/TQInterface/TQInterface.cpp +++ b/TQInterface/TQInterface.cpp @@ -9,123 +9,13 @@ char *throttleQuadrantCFG; initialization init; uint8_t numEngines = 0; -// Event to catch beginning -static enum EVENT_ID { - EVENT_SIM_START, - REQUEST_SYSTEM_STATE, -}; - -// Definitions for levers, dynamically added to SimConnect -static enum DATA_DEFINE_ID { - DEFINITION_AC_DATA, - DEFINITION_LEVER_ONE, - DEFINITION_LEVER_TWO, - DEFINITION_LEVER_THREE, - DEFINITION_LEVER_FOUR, -}; - -// Request ID -static enum REQUEST_ID { - AC_DATA_REQUEST, -}; - -// Struct for ac data -struct aircraftData { - double numEngines; - double lowerThrottleLimit; -}; - -// Structs for levers -struct engineJustThrottle { - double throttlePercent; -}; - -struct engineThrottleAndPropeller { - double throttlePercent; - double propellerPercent; -}; - -struct engineThrottleAndMixture { - double throttlePercent; - double mixturePercent; -}; - -struct engineAll { - double throttlePercent; - double propellerPrecent; - double mixturePercent; -}; - -//TESTING PURPOSE -engineJustThrottle tc; - -char *trimwhitespace(char *str) -{ - char *end; - - // Trim leading space - while (isspace((unsigned char)*str)) str++; - - if (*str == 0) // All spaces? - return str; - - // Trim trailing space - end = str + strlen(str) - 1; - while (end > str && isspace((unsigned char)*end)) end--; - - // Write new null terminator - *(end + 1) = 0; - - return str; -} - -char *readLine(FILE *file) { - - if (file == NULL) { - printf("\nError: file pointer is null."); - exit(1); - } - - int maximumLineLength = 128; - char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength); - - if (lineBuffer == NULL) { - printf("\nError allocating memory for line buffer."); - exit(1); - } - - char ch = getc(file); - int count = 0; - - while ((ch != '\n') && (ch != EOF)) { - if (count == maximumLineLength) { - maximumLineLength += 128; - lineBuffer = (char *)realloc(lineBuffer, maximumLineLength); - if (lineBuffer == NULL) { - printf("\nError reallocating space for line buffer."); - exit(1); - } - } - lineBuffer[count] = ch; - count++; - - ch = getc(file); - } - - lineBuffer[count] = '\0'; - if (count > 0) - return lineBuffer; - else - return NULL ; -} - void setupDefinitions() { - printf("\nSetting up..."); + printf("\nSetting up ... "); HRESULT hr; - hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_LEVER_ONE); - hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_LEVER_TWO); - hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_LEVER_THREE); - hr = SimConnect_ClearDataDefinition(hSimConnect, DEFINITION_LEVER_FOUR); + 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; uint8_t numEngines = 0; @@ -170,27 +60,143 @@ void setupDefinitions() { { switch (numEngines) { case 1: - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); + hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); break; case 2: - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent"); + 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: - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent"); - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent"); + 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: - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:2", "percent"); - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:3", "percent"); - hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_LEVER_ONE, "GENERAL ENG THROTTLE LEVER POSITION:4", "percent"); + 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; + } + } + else if (hasPropeller && !hasMixture) + { + switch (numEngines) { + case 1: + 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: + 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: + 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: + 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; } } + else if (!hasPropeller && hasMixture) + { + switch (numEngines) { + case 1: + hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); + hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent"); + break; + case 2: + 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: + 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: + 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; + } + } + else if (hasPropeller && hasMixture) + { + switch (numEngines) { + case 1: + hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG THROTTLE LEVER POSITION:1", "percent"); + hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent"); + hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ENGINE_ONE, "GENERAL ENG MIXTURE LEVER POSITION:1", "percent"); + break; + case 2: + 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: + 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: + 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; + } + } + printf("done!"); } // SimConnect callback @@ -207,7 +213,7 @@ void CALLBACK MyDispatchProcTC(SIMCONNECT_RECV* pData, DWORD cbData, void *pCont switch (data->dwRequestID) { case AC_DATA_REQUEST: { - printf("\Aircraft data retrieved"); + printf("Aircraft data retrieved."); init.maxRev = (int8_t)((aircraftData *)&data->dwData)->lowerThrottleLimit; numEngines = (uint8_t)((aircraftData *)&data->dwData)->numEngines; setupDefinitions(); @@ -234,8 +240,8 @@ void CALLBACK MyDispatchProcTC(SIMCONNECT_RECV* pData, DWORD cbData, void *pCont throttleQuadrantCFG = (char *)calloc(strlen(path) + 6, 1); strcpy(throttleQuadrantCFG, path); strcat(throttleQuadrantCFG, "tq.cfg"); - printf("\n%s loaded.", throttleQuadrantCFG); - printf("\nRetrieving aicraft data..."); + 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; } @@ -255,7 +261,7 @@ void CALLBACK MyDispatchProcTC(SIMCONNECT_RECV* pData, DWORD cbData, void *pCont case EVENT_SIM_START: { // Send this request to get the user aircraft id - printf("\n\nRequesting loaded AC path..."); + printf("\nRequesting loaded AC path ... "); hr = SimConnect_RequestSystemState(hSimConnect, REQUEST_SYSTEM_STATE, "AircraftLoaded"); } break; @@ -277,7 +283,7 @@ void CALLBACK MyDispatchProcTC(SIMCONNECT_RECV* pData, DWORD cbData, void *pCont } } -void testThrottleControl() +void throttleControl() { HRESULT hr; @@ -287,8 +293,6 @@ void testThrottleControl() hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_AC_DATA, "NUMBER OF ENGINES", "number"); hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_AC_DATA, "THROTTLE LOWER LIMIT", "percent"); - - // Set up a data definition for the throttle control //hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_LEVER_ONE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(tc), &tc); @@ -307,7 +311,6 @@ void testThrottleControl() int __cdecl _tmain(int argc, _TCHAR* argv[]) { - testThrottleControl(); - + throttleControl(); return 0; } diff --git a/TQInterface/TQInterface.vcxproj b/TQInterface/TQInterface.vcxproj index 9b6262a..e16f105 100644 --- a/TQInterface/TQInterface.vcxproj +++ b/TQInterface/TQInterface.vcxproj @@ -149,11 +149,13 @@ + - + + diff --git a/TQInterface/TQInterface.vcxproj.filters b/TQInterface/TQInterface.vcxproj.filters index 141f797..2178f5a 100644 --- a/TQInterface/TQInterface.vcxproj.filters +++ b/TQInterface/TQInterface.vcxproj.filters @@ -24,12 +24,18 @@ Header Files + + Header Files + - + Source Files - + + Source Files + + Source Files diff --git a/TQInterface/stdafx.cpp b/TQInterface/stdafx.cpp deleted file mode 100644 index 8016b6c..0000000 --- a/TQInterface/stdafx.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// stdafx.cpp : source file that includes just the standard includes -// TQInterface.pch will be the pre-compiled header -// stdafx.obj will contain the pre-compiled type information - -#include "stdafx.h" - -// TODO: reference any additional headers you need in STDAFX.H -// and not in this file diff --git a/TQInterface/stdafx.h b/TQInterface/stdafx.h index d067b78..accc6ad 100644 --- a/TQInterface/stdafx.h +++ b/TQInterface/stdafx.h @@ -5,8 +5,6 @@ #pragma once -#include "targetver.h" - #include #include #include @@ -15,6 +13,8 @@ #include #include "SimConnect.h" +#include "SerialPort.h" +#include "targetver.h" #pragma comment(lib, "SimConnect.lib") @@ -29,3 +29,52 @@ typedef struct values { int8_t leverValuesInPercent[4]; } values; +char *trimwhitespace(char *str); +char *readLine(FILE *file); + +// Event IDs +static enum EVENT_ID { + EVENT_SIM_START, + REQUEST_SYSTEM_STATE, +}; + +// Data definition IDs, engines dynamically added +static enum DATA_DEFINE_ID { + DEFINITION_AC_DATA, + DEFINITION_ENGINE_ONE, + DEFINITION_ENGINE_TWO, + DEFINITION_ENGINE_THREE, + DEFINITION_ENGINE_FOUR, +}; + +// Request IDs +static enum REQUEST_ID { + AC_DATA_REQUEST, +}; + +// Struct for ac data +struct aircraftData { + double numEngines; + double lowerThrottleLimit; +}; + +// Structs for engines +struct engineJustThrottle { + double throttlePercent; +}; + +struct engineThrottleAndPropeller { + double throttlePercent; + double propellerPercent; +}; + +struct engineThrottleAndMixture { + double throttlePercent; + double mixturePercent; +}; + +struct engineAll { + double throttlePercent; + double propellerPrecent; + double mixturePercent; +};