-
Notifications
You must be signed in to change notification settings - Fork 0
/
x52.h
143 lines (135 loc) · 7.5 KB
/
x52.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright (C) 2023 Csaba K Molnár
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <boost/property_tree/ptree.hpp>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <windows.h>
#define WSMCMND_API_STATIC
#include <client/WASimClient.h>
#include "SimConnect.h"
#include "x52HID.h"
class X52
{
// VARIABLES
public:
std::ofstream m_cmd_file;
int MAX_MFD_LEN; // Max num of chars written to one MFD row
std::string MFD_ON_JOY[3]; // The currently displayed 3 lines of text on the MFD
double HEARTBEAT_TIME = 0; // Last time of communication with command handler
double X52_RUN_TIME; // Absolute time in the simulator, in seconds, with double precision (5 decimals). Variable name taken from x52luaout.
/// <summary>
/// Contains the name of the current active shift state as a string.
/// </summary>
std::string CUR_SHIFT_STATE;
bool mfd_on, led_on;
bool joybuttonstates[32];
struct LastSentPacket {
DWORD pdwSendID;
std::string message;
};
LastSentPacket lastsentpacket;
enum EVENT_ID {
EVENT_JOYBUTTON_PRESS = 200,
EVENT_JOYBUTTON_RELEASE = 300,
EVENT_CLIENTID = 10000, // First Client Event ID to send a single command/InputEvent
};
int lastClientEventId = EVENT_CLIENTID - 1;
protected:
std::ofstream m_log_file;
struct SingleDataref {
double dataref[1];
};
HANDLE hSimConnect;
WASimCommander::Client::WASimClient* wasimclient;
x52HID* x52hid;
boost::property_tree::ptree* xml_file;
std::map<std::string, std::string> CURRENT_LED_COLOR;
// FUNCTIONS
public:
X52();
~X52();
void logg(std::string status, std::string func, std::string msg);
void set_simconnect_handle(HANDLE handle);
void set_wasimconnect_instance(WASimCommander::Client::WASimClient& client);
void set_x52HID(x52HID&);
void set_xmlfile(boost::property_tree::ptree* xml_file);
void heartbeat();
void heartbeatReset();
void write_to_mfd(std::string line1, std::string line2, std::string line3);
/// <summary>
/// Maintain the led's current color in the CURRENT_LED_COLOR map. Sets a led to a given color, if it is not already that color, according to CURRENT_LED_COLOR.
/// </summary>
/// <param name="led">The name of one of the 11 leds, for example, "t1". See the source for all names.</param>
/// <param name="light">The string "off", "red", "green", "amber" for lights with 2 physical leds. "on" and "off" for lights with 1 physical led, that is fire and throttle, whose color is controlled by the joystick.</param>
/// <returns></returns>
void write_led(std::string led, std::string light);
/// <summary>
/// Sets a led to a color or, if a sequence is used, sets it to the next color in the sequence. Uses X52_RUN_TIME to keep track of where we are in a sequence. This function is not called recursively.
/// </summary>
/// <param name="led">The name of one of the 11 leds, for example, "t1".</param>
/// <param name="light">The name of a color, or "on" or "off", or a sequence.</param>
/// <param name="current_light">The name of the led's current color.</param>
/// <param name="state">Ptree of a state tag</param>
/// <param name="force">True to update the led's color even if it already has that color.</param>
void update_led(std::string led, std::string light, std::string current_light, boost::property_tree::ptree &state, bool force);
/// <summary>
/// Evaluates if the given simvarvalue matches the given operand.
/// </summary>
/// <param name="simvarvalue">A SimVar's value as a double.</param>
/// <param name="op">Four characters. The first two can be == for equality, -- for less than, and ++ for greater than. The last two are a two-digit number.</param>
/// <returns></returns>
bool evaluate_xml_op(double simvarvalue, std::string op);
void execute_button_press(boost::property_tree::ptree &xmltree, int btn);
void execute_button_release(boost::property_tree::ptree &xmltree, int btn);
/// <summary>This method is called recursively to process elements under the assignments tag.</summary>
/// <param name="xmltree">A Property Tree of EITHER all button tags under the assignments tag OR a button tag OR a shifted_button tag under a button tag.</param>
/// <param name="status">Contains the string pressed or released.</param>
/// https://learn.microsoft.com/en-us/cpp/build/reference/summary-visual-cpp?view=msvc-170
bool assignment_button_action(boost::property_tree::ptree &xmltree, int btn, std::string status);
/// <summary>
/// <summary>
/// A recursively called function. Checks if data has changed in MSFS and updates joystick leds.
/// Each led's current color in stored in the led tag's current_light attribute created during runtime. It stores the value of the light
/// attribute from the active state tag.
/// </summary>
/// <param name="tagname">Led or state. Initially an empty string.</param>
/// <param name="xmltree">Initially, immediate children (led tags) of the indicators tag. On recursive calls, a state tag below a led tag or below another state tag.</param>
/// <param name="led">std::string The led's name taken from the id attribute of the led tag</param>
/// <param name="current_light">Used during recursive calls: current color of the currently processed led.</param>
/// <param name="force">Passed on to the update_led function. Force the update of the led's color even if it already has that color.</param>
/// <returns>The light attribute of the first state tag which is true when all state tags are evaluated from inside out.</returns>
std::string dataref_ind_action(std::string tagname, boost::property_tree::ptree &xmltree, std::string led = "", std::string current_light = "", bool force = false);
/// <summary>
/// Handles switching a target on or off. For led on, it starts to operate leds according to XML configuration. For led off, it does nothing. For mfd on, it ???. For mfd off, it only clears the MFD text.
/// </summary>
/// <param name="id">The target's id. Can be "led" or "mfd".</param>
/// <param name="on">True of false.</param>
void all_on(std::string id, bool on);
/// <summary>
/// A recursively called function which, based on joybuttonstates[], finds out what is the currently active shift state.
/// If a shift state was found, its name is stored in CUR_SHIFT_STATE and the joystick's SHIFT indicator is switched on.
/// </summary>
/// <param name="xmltree">Initially, the whole XML configuration file. On recursive calls, only the nested shift_state tag.</param>
/// <returns>True if an active shift state was found. On recursive calls, true if the button in the nested shift_state tag is pressed.</returns>
bool shift_state_active(const boost::property_tree::ptree xmltree);
/// <summary>
/// Initiates the calling of the recursive shift_state_active function and handles the case when no active shift state was found.
/// </summary>
/// <param name="xml_file">Pointer to the whole XML configuration file.</param>
void shift_state_action(const boost::property_tree::ptree xml_file);
void start_command_handler();
bool construct_successful();
};