int __stdcall DLLStart(void) { if (SUCCEEDED(SimConnect_Open(&hSimConnect, "SimCMain", NULL, 0, NULL, 0))) { printf("\nConnected..."); setup(); SimConnect_CallDispatch(hSimConnect, dispatchEvents, NULL); } return 0; }
//Initiate connectino with SimConnect void Connection::Connect(Questions *questions, std::string file_name) { HRESULT hr; //Start a new connection when hSimConnect is still NULL, meaning, no messages from server (SimConnect) have been recieved if (Connection::hSimConnect == NULL) { printf("\nAttempting to connect to Flight Simulator X\n"); //Call SimConnect function to open a new connection. hr = SimConnect_Open(&Connection::hSimConnect, "Situation Awareness Questionare", NULL, 0, NULL, 0); if (hr == S_OK) { //SimConnect_Open returned S_OK printf("\nConnection established with Flight Simulator X\n"); //data definition set up. This sets the FSX simulation variable to a client defined object definition (in this case enum DEFINITION is the client defined data definition) for (int i = 1; i <= questions->getNumberofElements(); i++) { hr = SimConnect_AddToDataDefinition(Connection::hSimConnect, DEFINITION, questions->getQuestionVariable(i).c_str(), questions->getQuestionUnits(i).c_str(), SIMCONNECT_DATATYPE_FLOAT32); } //Requesting FSX event. hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart"); hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_STOP, "SimStop"); //create three arrays to hold question number, question average response time, and question count int *question_number_array = new int[questions->getNumberofElements()]; double *question_average_array = new double[questions->getNumberofElements()]; double *question_time_sum_array = new double[questions->getNumberofElements()]; int *question_count_array = new int[questions->getNumberofElements()]; //Populate question number in the question number array for (int count = 0; count < questions->getNumberofElements(); count++) { question_number_array[count] = count + 1; //add question numbers question_average_array[count] = 0; //fill array with zero question_count_array[count] = 0; //fill array with zero question_time_sum_array[count] = 0; //fill array with zero //std::cout << question_count_array[count] << " "; } while (quit == 0) { skip_loop: if (Connection::started_flag == 1) { //ask question and save answer to a answer buffer //Ask user to press one to ask a questions std::cout << "\nPress 1 to ask a questions or 'Q' to quit: "; std::getline(std::cin, input_string); //read user input //When user enters 1 (input == 1) if (input_string == "1") { old_time_average = new_time_average; //Counter to count how many questios have been asked number_questions_asked += 1; //Write time stamp to file and questions number std::ofstream ofs; if (!ofs.is_open()) { ofs.open(file_name, std::ofstream::app); } //Pick a random questions from database z = rand() % questions->getNumberofElements() + 1; //Playing sound has not worked on C++ due to passing of string via questions pointer. Much easier to do this on C# //play wav of question in databaseth; //PlaySound(TEXT("C:\\Users\\Elite\\Desktop\\test.wav"), NULL, SND_ASYNC | SND_FILENAME); //PlaySound((LPCWSTR)questions->getQuestionWav(z).c_str(), NULL, SND_ASYNC | SND_FILENAME); //display text of questions being asked invalid_input_loop: std::cout << questions->getQuestionText(z).c_str() << std::endl; //start timer start_time_buffer = Connection::startClock(); //write text of questions being ask onto file ofs << Connection::localTime(); ofs << "Questions number: " << number_questions_asked << ". " << questions->getQuestionText(z).c_str() << std::endl; //take in input and save into variable std::getline(std::cin, input_string); //read user input if (std::cin.fail()) { std::cout << "Invalid input. Try again."; std::cin.clear(); std::cin.ignore(256, '\n'); goto invalid_input_loop; } //stop timer and return it time_buffer = Connection::stopClock(start_time_buffer); //write user input to file ofs << "User input: " << input_string; //write time_buffer to file ofs << " Answered in " << time_buffer << " seconds." << std::endl; //Process the next SimConnect messaged recieved thorugh the spcified callback function MyDispatchProc SimConnect_CallDispatch(Connection::hSimConnect, MyDispatchProc, NULL); //Check if questions is asking for heading. If it is then FSX result must be converted from radiants to degrees if (questions->getQuestionText(z).find("heading") != std::string::npos) { //Convert heading from fsx radians to degrees fsx_calculated_answer = Connection::getAnswer() * 57.2957795; } else { //No need to convert fsx results. Pass it to the variable. fsx_calculated_answer = Connection::getAnswer(); } //Calculate answer different result_buffer = abs(std::stof(input_string) - fsx_calculated_answer); //sum all the response times time_buffer_sum = time_buffer_sum + time_buffer; //get the time_buffer average new_time_average = time_buffer_sum / number_questions_asked; //std::cout << new_time_average << " new. " << old_time_average << " old." << std::endl; //store and calculate individual question average and count for (int count = 0; count < questions->getNumberofElements(); count++) { if (question_number_array[count] == z) { //save current question average current_question_average_buffer = question_average_array[count]; //increase count by 1 question_count_array[count] += 1; //Sum time to respond to this question question_time_sum_array[count] += time_buffer; //calculate average and store it to average array question_average_array[count] = question_time_sum_array[count] / question_count_array[count]; //Save location of current question in arrays for calculations int location_of_current_question_in_arrays = count; if ((number_questions_asked > 1) && (question_average_array[location_of_current_question_in_arrays] > (2 * current_question_average_buffer)) && (current_question_average_buffer != 0)) { current_question_increase_multiplier =question_average_array[location_of_current_question_in_arrays] / current_question_average_buffer; ofs << "Average response time for question number " << location_of_current_question_in_arrays << " increased by a factor of " << current_question_increase_multiplier << ". The previews average was " << current_question_average_buffer << " and the new average is " << question_average_array[location_of_current_question_in_arrays] << "." << std::endl; incorrect_count_time += 1; if (incorrect_count_time >= 2) { //Average response time increase twice. Make suggestion //Make a suggestion std::cout << "\n** SUGGESTION 1 - Decrease automation**" << std::endl; ofs << "**Suggestion was made.**" << std::endl; //increase suggestions count suggestion_count += 1; } } else if ((number_questions_asked > 1) && (question_average_array[location_of_current_question_in_arrays] > (current_question_average_buffer))) { improvement_count += 1; if ((improvement_count > 2) && suggestion_count >= 1) { //Improvement is being shown ofs << "Improving answer reaction time. Suggest to increase automation." << std::endl; std::cout << "\n** SUGGESTION 2 - Increase automation **" << std::endl; } } } } //Check to see if the current average and the new average has doubled if ((number_questions_asked > 1) && (new_time_average > (2 * old_time_average)) && (old_time_average != 0)) { average_increase_multiplier = (new_time_average / old_time_average); ofs << "Average response time increase by a factor of " << average_increase_multiplier << ". The previews average was " << old_time_average << " and the new average is " << new_time_average << "." << std::endl; incorrect_count_time += 1; if (incorrect_count_time >=2) { //Average response time increase twice. Make suggestion //Make a suggestion std::cout << "\n** SUGGESTION 1 - Decrease automation**" << std::endl; ofs << "**Suggestion was made.**" << std::endl; //increase suggestions count suggestion_count += 1; } } else if ((number_questions_asked > 1) && (new_time_average < (old_time_average))) { improvement_count += 1; if ((improvement_count > 2) && suggestion_count >= 1) { //Improvement is being shown ofs << "Improving answer reaction time. Suggest to increase automation." << std::endl; std::cout << "\n** SUGGESTION 2 - Increase automation **" << std::endl; } } if (result_buffer > std::stof(questions->getVariableChange(z))) { //incorrect answer ofs << "Incorrect answer number " << incorrect_count_error + 1 << ". FSX result was " << fsx_calculated_answer << ". Difference in answers " << result_buffer << std::endl; std::cout << "\nIncorrect Answer." << std::endl; //count how many incorrect answer were provided incorrect_count_error = incorrect_count_error + 1; //if this is the second incorrect answer if (incorrect_count_error >= 2) { //Make a suggestion std::cout << "\n** SUGGESTION 1 - Decrease automation**" << std::endl; ofs << "**Suggestion was made.**" << std::endl; //increase suggestions count suggestion_count += 1; //reset incorrect_count back to 0 incorrect_count_error = 0; } } else { ofs << "Correct answer. FSX result was " << fsx_calculated_answer << ". Difference in answers was " << result_buffer << std::endl; } ofs << "\n\n"; ofs.close(); } //when user inputs anything besides a 1 (this MAY cause problems when expecting answer as string) else if (input_string == "Q" || input_string == "q") { quit = 1; } else { //Notify it was incorrect entry and clear cin buffer std::cout << "\nInvalid input. Try again!\n" << std::endl; } //Check to see if sim flight is still active if (started_flag != 1) { //If it is not active exit out of this loop and reset the questioning session goto skip_loop; } SimConnect_CallDispatch(Connection::hSimConnect, MyDispatchProc, NULL); } else { SimConnect_CallDispatch(Connection::hSimConnect, MyDispatchProc, NULL); } } //FSX has quit,or ran out of questiosn in the database. disconnect. Connection::Disconnect(); } else { printf("\nUnable to connect to Flight Simulator X\n"); } } Connection::Pause(); }
void testCockpitCamera() { if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Cockpit Camera", NULL, 0, 0, 0))) { cameraConfig = configLoader.load(); SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "title", NULL, SIMCONNECT_DATATYPE_STRINGV); SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart"); SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_AIRCRAFT_CHANGED, "AircraftLoaded"); SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP0, SIMCONNECT_GROUP_PRIORITY_STANDARD); REGISTER_CAMERA_BUTTON(1, "Num_1", "ctrl+Num_1"); REGISTER_CAMERA_BUTTON(2, "Num_2", "ctrl+Num_2"); REGISTER_CAMERA_BUTTON(3, "Num_3", "ctrl+Num_3"); REGISTER_CAMERA_BUTTON(4, "Num_4", "ctrl+Num_4"); REGISTER_CAMERA_BUTTON(5, "Num_5", "ctrl+Num_5"); REGISTER_CAMERA_BUTTON(6, "Num_6", "ctrl+Num_6"); REGISTER_CAMERA_BUTTON(7, "Num_7", "ctrl+Num_7"); REGISTER_CAMERA_BUTTON(8, "Num_8", "ctrl+Num_8"); REGISTER_CAMERA_BUTTON(9, "Num_9", "ctrl+Num_9"); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_LEFT); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_RIGHT); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_UP); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_DOWN); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_FORWARD); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_BACK); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_HEADING_LEFT); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_HEADING_RIGHT); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_CENTER); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_PITCH_UP); SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_PITCH_DOWN); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_LEFT); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_RIGHT); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_UP); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_DOWN); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_FORWARD); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_BACK); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_HEADING_LEFT); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_HEADING_RIGHT); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_CENTER); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_PITCH_UP); SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_PITCH_DOWN); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_4", EVENT_LEFT); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_6", EVENT_RIGHT); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_8", EVENT_UP); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_2", EVENT_DOWN); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_7", EVENT_FORWARD); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_1", EVENT_BACK); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_0", EVENT_HEADING_LEFT); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_Del", EVENT_HEADING_RIGHT); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_5", EVENT_CENTER); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_9", EVENT_PITCH_UP); SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+Num_3", EVENT_PITCH_DOWN); SimConnect_SetInputGroupState(hSimConnect, INPUT0, SIMCONNECT_STATE_ON); while (0 == quit) { SimConnect_CallDispatch(hSimConnect, MyDispatchProcCC, NULL); Sleep(1); } SimConnect_Close(hSimConnect); } }
void testCommunication() { HRESULT hr; printf("Connecting to Flight Simulator!\n"); if (SUCCEEDED(SimConnect_Open(&hSimConnect, "PMDG 777X Test", NULL, 0, 0, 0))) { printf("Connected to Flight Simulator!\n"); // 1) Set up data connection // Associate an ID with the PMDG data area name hr = SimConnect_MapClientDataNameToID(hSimConnect, PMDG_777X_DATA_NAME, PMDG_777X_DATA_ID); // Define the data area structure - this is a required step hr = SimConnect_AddToClientDataDefinition(hSimConnect, PMDG_777X_DATA_DEFINITION, 0, sizeof(PMDG_777X_Data), 0, 0); // Sign up for notification of data change. // SIMCONNECT_CLIENT_DATA_REQUEST_FLAG_CHANGED flag asks for the data to be sent only when some of the data is changed. hr = SimConnect_RequestClientData(hSimConnect, PMDG_777X_DATA_ID, DATA_REQUEST, PMDG_777X_DATA_DEFINITION, SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET, SIMCONNECT_CLIENT_DATA_REQUEST_FLAG_CHANGED, 0, 0, 0); // 2) Set up control connection // First method: control data area Control.Event = 0; Control.Parameter = 0; // Associate an ID with the PMDG control area name hr = SimConnect_MapClientDataNameToID(hSimConnect, PMDG_777X_CONTROL_NAME, PMDG_777X_CONTROL_ID); // Define the control area structure - this is a required step hr = SimConnect_AddToClientDataDefinition(hSimConnect, PMDG_777X_CONTROL_DEFINITION, 0, sizeof(PMDG_777X_Control), 0, 0); // Sign up for notification of control change. hr = SimConnect_RequestClientData(hSimConnect, PMDG_777X_CONTROL_ID, CONTROL_REQUEST, PMDG_777X_CONTROL_DEFINITION, SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET, SIMCONNECT_CLIENT_DATA_REQUEST_FLAG_CHANGED, 0, 0, 0); // Second method: Create event IDs for controls that we are going to operate hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_LOGO_LIGHT_SWITCH, "#69748"); //EVT_OH_LIGHTS_LOGO hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_FLIGHT_DIRECTOR_SWITCH, "#69834"); //EVT_MCP_FD_SWITCH_L // 3) Request current aircraft .air file path hr = SimConnect_RequestSystemState(hSimConnect, AIR_PATH_REQUEST, "AircraftLoaded"); // also request notifications on sim start and aircraft change hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart"); // 4) Assign keyboard shortcuts hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_KEYBOARD_A); hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_KEYBOARD_B); hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_KEYBOARD_C); hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYBOARD, EVENT_KEYBOARD_A); hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYBOARD, EVENT_KEYBOARD_B); hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYBOARD, EVENT_KEYBOARD_C); hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP_KEYBOARD, SIMCONNECT_GROUP_PRIORITY_HIGHEST); hr = SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+ctrl+a", EVENT_KEYBOARD_A); hr = SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+ctrl+b", EVENT_KEYBOARD_B); hr = SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+ctrl+c", EVENT_KEYBOARD_C); hr = SimConnect_SetInputGroupState(hSimConnect, INPUT0, SIMCONNECT_STATE_ON); printf("entering main loop\n"); // 5) Main loop while (quit == 0) { //printf("running main loop\n"); // receive and process the 777X data SimConnect_CallDispatch(hSimConnect, MyDispatchProc, NULL); Sleep(1); } hr = SimConnect_Close(hSimConnect); } else printf("Unable to connect!\n\n"); }