]> git.defcon.no Git - rctxduino/blob - tools/jstt/jstt.cpp
The code is now functional for the 4 first channels, also applied some simple pulldow...
[rctxduino] / tools / jstt / jstt.cpp
1 #include "jstt.h"
2 #include "ui_jstt.h"
3
4 jstt::jstt(QWidget *parent) :
5 QMainWindow(parent),
6 ui(new Ui::jstt)
7 {
8 ui->setupUi(this);
9
10 joystick = NULL;
11 bars[0] = ui->Chan_1_bar;
12 bars[1] = ui->Chan_2_bar;
13 bars[2] = ui->Chan_3_bar;
14 bars[3] = ui->Chan_4_bar;
15 labels[0] = ui->Chan_1_val;
16 labels[1] = ui->Chan_2_val;
17 labels[2] = ui->Chan_3_val;
18 labels[3] = ui->Chan_4_val;
19
20 init();
21 }
22
23 jstt::~jstt()
24 {
25 if( jsIsOpen() ) jsClose();
26
27 SDL_Quit();
28
29 delete ui;
30 //delete joystick; // This causes warnings that i don't understand
31 delete [] bars; // Will this be deleted when i delete ui? Does it matter? We're deleting the whole object anyway
32 delete [] labels; // Same here, will it be deleted when ui is deleted?
33 }
34
35 void jstt::init()
36 {
37 QStringList jsNames;
38
39 if( SDL_Init(SDL_INIT_JOYSTICK) == 0 ) {
40
41 for(int i = 0; i < SDL_NumJoysticks(); i++) {
42 jsNames.append(SDL_JoystickName(i));
43 }
44
45 connect(&jsPullTimer, SIGNAL(timeout()), this, SLOT(processJs()));
46
47 ui->Joysticks->addItems(jsNames);
48 ui->Joysticks->setCurrentIndex(0); // This will trigger on_Joysticks_currentIndexChanged(int)
49 }
50 }
51
52 void jstt::jsOpen(int js)
53 {
54 if( jsIsOpen() ) jsClose();
55
56 joystick = SDL_JoystickOpen(js);
57 if( joystick ) {
58
59 /*
60 TODO:
61
62 numAxes = SDL_JoystickNumAxes(joystick);
63 numButtons = SDL_JoystickNumButtons(joystick);
64 numHats = SDL_JoystickNumHats(joystick);
65 numTrackballs = SDL_JoystickNumBalls(joystick);
66 JoystickTimer.start(eventTimeout);
67
68 When we have found out how many axes, buttons etc, we can expand the GUI further
69 */
70
71 jsPullTimer.start(eventTimeout);
72 return;
73 } else {
74
75 /*
76 One should make sure the program will react on this situation instead of just
77 returning.
78 */
79
80 return;
81 }
82 }
83
84 void jstt::jsClose()
85 {
86 jsPullTimer.stop();
87 if( joystick ) SDL_JoystickClose(joystick);
88 joystick = NULL;
89 }
90
91 void jstt::processJs()
92 {
93 Sint16 value;
94
95 SDL_JoystickUpdate();
96
97 /*
98 This code needs to be fixed so that it iterates through all
99 axes instead of just the four i am setting up now.
100 */
101
102 for(int i = 0; i < 4; i++) {
103 value = SDL_JoystickGetAxis(joystick, i);
104 labels[i]->setNum(value);
105 bars[i]->setValue(percentBar(value));
106 }
107 }
108
109 void jstt::on_actionAbout_triggered()
110 {
111
112 }
113
114 void jstt::on_actionHelp_triggered()
115 {
116
117 }
118
119 void jstt::on_actionQuit_triggered()
120 {
121 close();
122 }