Marine systems simulation
SConsole.h
1#ifndef SCONSOLE_H
2#define SCONSOLE_H
3#include "Joystick.h"
4#include "ui_SConsole.h"
5#include "qwt_dial_needle.h"
6#include "qwt_slider.h"
7#include "qwt_knob.h"
8#include "qpushbutton.h"
9#include "qpalette.h"
10#include "qwt_analog_clock.h"
11
12
16
17
18
19
20class ShipConsoleChild :public QWidget, public Ui::ShipConsole{
21 Q_OBJECT
22
23public:
24 ShipConsoleChild(QWidget *Parent=0):QWidget(Parent){
25
26 // Initialization
27 for(int i=0; i<3; i++){
28 ThrusterActive[i] = 0;
29 ThrusterUp[i] = 0;
30 ThrusterDown[i] = 0;
31 ThrusterValue[i] = 0;
32 }
33
34
35
36 for(int i=0; i<32; i++){
37 permval.rgbButtons[i] = 0;
38 }
39
40 }
41
42 void SetSocket( int _fd )
43 {
44 sockfd = _fd;
45 }
46
47 void SetMSPanelID( long long id)
48 {
49 MSPanelID = id;
50 }
51
52 void SetShipConsoleID( long long id)
53 {
54 ShipConsoleID = id;
55 }
56
57 void setupWidgets()
58 {
59
60 QwtDialSimpleNeedle *needle = new QwtDialSimpleNeedle(QwtDialSimpleNeedle::Arrow, true, Qt::red, QColor(Qt::gray).light(130));
61 MainEngineRight->setRange(-1.0,1.,0.01);
62 MainEngineRight->setNeedle(needle);
63 MainEngineLeft->setRange(-1.0,1.,0.01);
64 MainEngineLeft->setNeedle(needle);
65 Rudder->setRange(-1.0,1.,0.01);
66 Rudder->setNeedle(needle);
67
68 MainEngineRight->setScaleArc(-150.0, 150.0);
69 MainEngineLeft->setScaleArc(-150.0, 150.0);
70 Rudder->setScaleArc(-150.0, 150);
71
72 EngineButtonLeft->setCheckable(true);
73 EngineButtonRight->setCheckable(true);
74
75 EngineButtonLeft->setChecked(false);
76 EngineButtonLeft->setStyleSheet("* { background-color: rgb(255,0,0) }");
77 EngineButtonLeft->setText("Off");
78
79 EngineButtonRight->setChecked(false);
80 EngineButtonRight->setStyleSheet("* { background-color: rgb(255,0,0) }");
81 EngineButtonRight->setText("Off");
82
83 DiffLeft->setRange(-1.0, 1.0, 0.01);
84 DiffRight->setRange(-1.0, 1.0, 0.01);
85 DiffRudder->setRange(-1.0, 1.0, 0.01);
86
87 ThrusterSlider1->setHandleSize( 30, 16 );
88 ThrusterSlider1->setRange(-1.0, 1.0, 1.0, 0); // paging disabled
89 ThrusterSlider2->setHandleSize( 30, 16 );
90 ThrusterSlider2->setRange(-1.0, 1.0, 1.0, 0); // paging disabled
91 ThrusterSlider3->setHandleSize( 30, 16 );
92 ThrusterSlider3->setRange(-1.0, 1.0, 1.0, 0); // paging disabled
93
94 ThrusterButton1->setCheckable(true);
95 ThrusterButton2->setCheckable(true);
96 ThrusterButton3->setCheckable(true);
97
98 ThrusterButton1->setChecked(false);
99 ThrusterButton1->setStyleSheet("* { background-color: rgb(255,0,0) }");
100 ThrusterButton1->setText("Off");
101
102 ThrusterButton2->setChecked(false);
103 ThrusterButton2->setStyleSheet("* { background-color: rgb(255,0,0) }");
104 ThrusterButton2->setText("Off");
105
106 ThrusterButton3->setChecked(false);
107 ThrusterButton3->setStyleSheet("* { background-color: rgb(255,0,0) }");
108 ThrusterButton3->setText("Off");
109
110 }
111
112signals:
113 void JoystickUpdated( long long id, joyData_t data);
114private slots:
115
116 void updateDials(struct myOutputStruct FHSimData)
117 {
118
119 if(EngineButtonLeft->isChecked())
120 MainEngineLeft->setValue(FHSimData.mainengine[0]);
121 else MainEngineLeft->setValue(0.0);
122
123 if(EngineButtonRight->isChecked())
124 MainEngineRight->setValue( FHSimData.mainengine[1] );
125 else MainEngineRight->setValue(0.0);
126
127
128 Rudder->setValue(FHSimData.mainengine[2] );
129
130
131 // Differential value from joystick to feedback
132 DiffLeft->setValue(permval.rotAxis[2]/1000.0-FHSimData.mainengine[0]);
133 DiffRight->setValue(permval.rglSlider[0]/1000.0-FHSimData.mainengine[1]);
134 DiffRudder->setValue(permval.rotAxis[0]/1000.0-FHSimData.mainengine[2]);
135
136 }
137
138 void UpdateAndSend( )
139 {
140 std::map<long long,joyData_t>::iterator it;
141 std::map<long long,myStruct_t>::iterator it2;
142 UpdateInputState(this->winId());
143
144 //send
145 for( it = Joysticks.begin(); it != Joysticks.end(); it++)
146 {
147 LPDIRECTINPUTDEVICE8 dev = it->second.dev;
148 //if(send(sockfd,(const char *) &it->second.data, sizeof(myStruct), 0)==-1)
149 //printf( "error");
150 emit JoystickUpdated(it->first,it->second);
151
152 }
153
154 //send(sockfd, (const char *) &permval, sizeof(permval), 0);
155
156 }
157
158
159 void UpdateJoystickData(long long id,joyData_t data)
160 {
161 if( id == ShipConsoleID){
162
163permval.GuidData1 = data.data.GuidData1;
164 for(int i=0; i<3; i++)
165 permval.rotAxis[i]= data.data.rotAxis[i];
166 for(int i=0; i<2; i++)
167 permval.rglSlider[i] = data.data.rglSlider[i];
168
169
170
171 for(int i=0; i<32; i++){
172
173 if(data.data.rgbButtons[i]){
174 permval.rgbButtons[i] = 128*(!permval.rgbButtons[i]);
175 }
176 }
177
181
182 for(int i=0; i<3; i++)
183 ThrusterActive[i] = data.data.rgbButtons[i+19];
184
185 ThrusterUp[0] = data.data.rgbButtons[3];
186 ThrusterUp[1] = data.data.rgbButtons[5];
187 ThrusterUp[2] = data.data.rgbButtons[9];
188
189 ThrusterDown[0] = data.data.rgbButtons[2];
190 ThrusterDown[1] = data.data.rgbButtons[4];
191 ThrusterDown[2] = data.data.rgbButtons[10];
192
193 for(int i=0; i<3; i++){
194
195 if(ThrusterUp[i] == 128)
196 ThrusterValue[i]+=0.05;
197
198
199 if(ThrusterDown[i] == 128)
200 ThrusterValue[i]-=0.05;
201
202 }
203
204 if(data.data.rgbButtons[19]/128 == 1){
205
206 if(ThrusterButton1->isChecked()){
207 ThrusterButton1->setChecked(false);
208 ThrusterButton1->setStyleSheet("* { background-color: rgb(255,0,0) }");
209 ThrusterButton1->setText("Off");
210
211 }
212 else {
213 ThrusterButton1->setChecked(true);
214 ThrusterButton1->setStyleSheet("* { background-color: rgb(0,255,0) }");
215 ThrusterButton1->setText("On");
216 }
217 }
218
219
220 if(data.data.rgbButtons[20]/128 == 1){
221
222 if(ThrusterButton2->isChecked()){
223 ThrusterButton2->setChecked(false);
224 ThrusterButton2->setStyleSheet("* { background-color: rgb(255,0,0) }");
225 ThrusterButton2->setText("Off");
226
227 }
228 else {
229 ThrusterButton2->setChecked(true);
230 ThrusterButton2->setStyleSheet("* { background-color: rgb(0,255,0) }");
231 ThrusterButton2->setText("On");
232
233 }
234 }
235
236
237 if(data.data.rgbButtons[21]/128 == 1){
238
239 if(ThrusterButton3->isChecked()){
240 ThrusterButton3->setChecked(false);
241 ThrusterButton3->setStyleSheet("* { background-color: rgb(255,0,0) }");
242 ThrusterButton3->setText("Off");
243
244 }
245 else {
246 ThrusterButton3->setChecked(true);
247 ThrusterButton3->setStyleSheet("* { background-color: rgb(0,255,0) }");
248 ThrusterButton3->setText("On");
249
250 }
251
252 }
253
254 if(ThrusterButton1->isChecked())
255 {
256 ThrusterSlider1->setValue(ThrusterValue[0]);
257 permval.Thruster[0] = ThrusterValue[0];
258 }
259 else
260 {
261 ThrusterSlider1->setValue(0.0);
262 permval.Thruster[0] = 0.0;
263 }
264
265
266
267 if(ThrusterButton2->isChecked())
268 {
269 ThrusterSlider2->setValue(ThrusterValue[1]);
270 permval.Thruster[1] = ThrusterValue[1];
271 }
272 else
273 {
274 ThrusterSlider2->setValue(0.0);
275 permval.Thruster[1] = 0.0;
276 }
277
278
279
280 if(ThrusterButton3->isChecked())
281 {
282 ThrusterSlider3->setValue(ThrusterValue[2]);
283 permval.Thruster[2] = ThrusterValue[2];
284 }
285 else
286 {
287 ThrusterSlider3->setValue(0.0);
288 permval.Thruster[2] = 0.0;
289 }
290
291
295
296 if(data.data.rgbButtons[6]/128 == 1){
297
298 if(EngineButtonLeft->isChecked()){
299 EngineButtonLeft->setChecked(false);
300 EngineButtonLeft->setStyleSheet("* { background-color: rgb(255,0,0) }");
301 EngineButtonLeft->setText("Off");
302
303 }
304 else {
305 EngineButtonLeft->setChecked(true);
306 EngineButtonLeft->setStyleSheet("* { background-color: rgb(0,255,0) }");
307 EngineButtonLeft->setText("On");
308 }
309 }
310
311
312 if(data.data.rgbButtons[7]/128 == 1){
313
314 if(EngineButtonRight->isChecked()){
315 EngineButtonRight->setChecked(false);
316 EngineButtonRight->setStyleSheet("* { background-color: rgb(255,0,0) }");
317 EngineButtonRight->setText("Off");
318
319 }
320 else {
321 EngineButtonRight->setChecked(true);
322 EngineButtonRight->setStyleSheet("* { background-color: rgb(0,255,0) }");
323 EngineButtonRight->setText("On");
324 }
325 }
326
330
331 send(sockfd,(const char *) &permval, sizeof(myStruct), 0);
332 }
333 }
334
335private:
336 int sockfd;
337 long long ShipConsoleID;
338 long long MSPanelID;
339 double ThrusterActive[3], ThrusterUp[3], ThrusterDown[3], ThrusterValue[3];
340
341 //myStruct_t updateval; // Holds the current joystick values
342 myStruct_t permval; // Holds permanent values for the "hold" buttons
343
344};
345
346
347#endif
Definition: program2.h:14
Definition: Joystick.h:78
Definition: myOutputStruct.h:5
Definition: myStruct.h:5