Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2105 - 1
#include <linux/joystick.h>
2
#include "main.h"
3
#include "gamepad.h"
4
#include "frame.h"
5
 
6
volatile int *Axis = NULL;
7
volatile char *Button = NULL;
8
volatile float Sensitivity = 1.0;
9
int NumberOfAxesAvailiable;
10
int NumberOfButtonsAvailiable;
11
char GamepadConnected[30];
12
u8 IsActive = 0;
13
u8 ProgramSelect = 0;
14
 
15
int recordingButtonTemp = 125;
16
int padFd;
17
struct js_event js;
18
 
19
struct str_ExternControl externControl;                 //struct for controlling MikroKopter
20
struct str_ExternControl externControlGier;             //struct for controlling Yaw (Gier) only
21
 
22
u8 gamepad_values_convert_for_cam(int tmpvalue){
23
        u8 tmp;
24
        double tmpdouble = tmpvalue;
25
        tmp = ((((tmpdouble/(MaxAxisValue))-1)*125)-131)*-1;
26
        if(tmp == 0){tmp = 255;}
27
        return(tmp);
28
}
29
 
30
//>> Converting gamepad values to values readable by a MikroKopter
31
//------------------------------------------------------------------------------------------------------
32
u8 gamepad_values_convert_for_kopter(int tmpvalue){
33
        u8 tmp;
34
        double tmpdouble = tmpvalue;
35
        tmp = ((((tmpdouble/(MaxAxisValue*Sensitivity))-1)*125)-131)*-1;
36
        /*Increase values exponentially
37
        if(tmpvalue < 0){
38
                tmpdouble = tmpvalue*-1;
39
        }else{tmpdouble = tmpvalue;}
40
        tmp = ((30497*tmpdouble*tmpdouble*tmpdouble)/11187226521286200+(10667778*tmpdouble*tmpdouble)/532725072442200+(2592340312*tmpdouble)/11187226521286);
41
        if(tmpvalue < 0){
42
                tmp = (tmp*-1)+255;
43
        }
44
        */
45
        return(tmp);
46
}
47
 
48
u8 gamepad_values_convert_for_kopter_gas(int tmpvalue){
49
        u8 tmp;
50
        double tmpdouble = tmpvalue;
51
        tmp = ((((tmpdouble/(MaxAxisValue))-1)*125)-131)*-1;
52
        return(tmp);
53
}
54
 
55
int check_if_button_pushed(){
56
        read_gamepad_data();
57
        int buttonpushed = -1;
58
        for (int i = 0; i < NumberOfButtonsAvailiable; i++)
59
        {
60
                if(Button[i] != 0){
61
                        buttonpushed = i;
62
                }
63
        }
64
        return(buttonpushed);
65
}
66
 
67
//>> Check if controls are active
68
//------------------------------------------------------------------------------------------------------
69
void check_if_controls_active(){
70
        if(check_if_button_pushed() == GamepadLayout[17] && IsActive == 0){
71
                IsActive = 1;
72
                while(1){if(!value_sum(0)){break;};}
73
        }else if(check_if_button_pushed() == GamepadLayout[17] && IsActive == 1){
74
                IsActive = 0;
75
                while(1){if(!value_sum(0)){break;};}
76
        }
77
}
78
//>> Check if program has been switched
79
//------------------------------------------------------------------------------------------------------
80
void check_program_switch(){
81
        if(check_if_button_pushed() == GamepadLayout[11]){
82
                frame_print("", 1, 0);
83
                ProgramSelect = 1;
84
        }else if(check_if_button_pushed() == GamepadLayout[12]){
85
                frame_print("", 1, 0);
86
                ProgramSelect = 2;
87
        }
88
}
89
 
90
//>> Check if sensitivity of the controls has been adjusted
91
//------------------------------------------------------------------------------------------------------
92
void get_sensitivity(){
93
        if(check_if_button_pushed() == GamepadLayout[15] && Sensitivity <= 2.0){
94
                if(Sensitivity < 2){buzzer_short(1);}
95
                Sensitivity += 0.2;
96
                while(1){if(!value_sum(0)){break;};}
97
        }else if(check_if_button_pushed() == GamepadLayout[16] && Sensitivity > 1.0){
98
                if(Sensitivity > 1){buzzer_short(1);}
99
                Sensitivity -= 0.2;
100
                while(1){if(!value_sum(0)){break;};}
101
        }
102
}
103
 
104
//>> Preparing Data for remotely controlling a Camera-Gimbal
105
//------------------------------------------------------------------------------------------------------
106
/*
107
void create_data_camera(serial_data_struct* pdata_package_camera_control, serial_data_struct* pdata_package_camera_control_gier){
108
        pdata_package_camera_control->data[0] = gamepad_values_convert_for_cam(Axis[GamepadLayout[0]]);                                                                                         //Nick
109
        pdata_package_camera_control->data[1] = gamepad_values_convert_for_cam(Axis[GamepadLayout[1]]);                                                                                         //Roll
110
        pdata_package_camera_control->data[2] = gamepad_values_convert_for_cam(Axis[GamepadLayout[3]]);                                                                                         //
111
        if(Button[GamepadLayout[8]] > 0){recordingButtonTemp = 131;}else if(Button[GamepadLayout[9]] > 0){recordingButtonTemp = 125;}
112
        pdata_package_camera_control->data[3] = recordingButtonTemp;
113
        if(Button[GamepadLayout[10]]){pdata_package_camera_control->data[4] = 131;}else{pdata_package_camera_control->data[4] = 125;}
114
        int i = 5;
115
        while(i < 12){
116
                pdata_package_camera_control->data[i] = 0;
117
                i++;
118
        }
119
        //Add Yaw (Gier)-funcionality
120
        externControlGier.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[2]]);
121
        externControlGier.Config = EC_VALID | EC_GAS_ADD;
122
        externControlGier.Frame = 'z';                                                                          //character for echo
123
        u8 *tmpData = (unsigned char *) &externControlGier;
124
        for (int i = 0; i < pdata_package_camera_control_gier->cmdLength; ++i)
125
        {
126
                pdata_package_camera_control_gier->data[i] = tmpData[i];
127
        }
128
 
129
        //Print out values on screen
130
        char tmpbuffer[255];
131
        sprintf(tmpbuffer, "Nick: %d    \n     Roll: %d     \n     Gier: %d     \n     Zoom: %d     \n     Aufnahme: %d    \n     Auslösung: %d", pdata_package_camera_control->data[0], pdata_package_camera_control->data[1], pdata_package_camera_control_gier->data[2], pdata_package_camera_control->data[2], pdata_package_camera_control->data[3], pdata_package_camera_control->data[4]);
132
        frame_values("VALUES", 5, 2);
133
        frame_values(tmpbuffer, 5, 4);
134
}
135
*/
136
 
137
//>> Preparing Data for remotely controlling a Camera-Gimbal (CAM CONTROL ONLY)
138
//------------------------------------------------------------------------------------------------------
139
void create_data_camera(serial_data_struct* pdata_package_camera_control, serial_data_struct* pdata_package_camera_control_gier){
140
        pdata_package_camera_control->data[0] = gamepad_values_convert_for_cam(Axis[GamepadLayout[0]]);                                                                                         //Nick
141
        pdata_package_camera_control->data[1] = gamepad_values_convert_for_cam(Axis[GamepadLayout[1]]);                                                                                         //Roll
142
        pdata_package_camera_control->data[2] = gamepad_values_convert_for_cam(Axis[GamepadLayout[3]]);                                                                                         //
143
        if(Button[GamepadLayout[4]] > 0){recordingButtonTemp = 131;}else if(Button[GamepadLayout[5]] > 0){recordingButtonTemp = 125;}
144
        pdata_package_camera_control->data[3] = recordingButtonTemp;
145
        if(Button[GamepadLayout[6]]){pdata_package_camera_control->data[4] = 131;}else{pdata_package_camera_control->data[4] = 125;}
146
        int i = 5;
147
        while(i < 12){
148
                pdata_package_camera_control->data[i] = 0;
149
                i++;
150
        }
151
        //Add Yaw (Gier)-funcionality
152
        externControlGier.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[2]]);
153
        externControlGier.Config = EC_VALID | EC_GAS_ADD;
154
        externControlGier.Frame = 'z';                                                                          //character for echo
155
        u8 *tmpData = (unsigned char *) &externControlGier;
156
        for (int i = 0; i < pdata_package_camera_control_gier->cmdLength; ++i)
157
        {
158
                pdata_package_camera_control_gier->data[i] = tmpData[i];
159
        }
160
 
161
        //Print out values on screen
162
        char tmpbuffer[255];
163
        sprintf(tmpbuffer, "Nick: %d    \n     Roll: %d     \n     Gier: %d     \n     Zoom: %d     \n     Aufnahme: %d    \n     Auslösung: %d", pdata_package_camera_control->data[0], pdata_package_camera_control->data[1], pdata_package_camera_control_gier->data[2], pdata_package_camera_control->data[2], pdata_package_camera_control->data[3], pdata_package_camera_control->data[4]);
164
        frame_values("VALUES", 5, 2);
165
        frame_values(tmpbuffer, 5, 4);
166
}
167
 
168
//>> Preparing Data for remotely controlling a MikroKopter
169
//------------------------------------------------------------------------------------------------------
170
void create_data_kopter(serial_data_struct* pdata_package_kopter_control){
171
        if(check_if_button_pushed() == GamepadLayout[13]){                              //Motor start
172
                externControl.Nick = 131;
173
                externControl.Roll = 131;
174
                externControl.Gier = 131;
175
                externControl.Gas = 131;       
176
        }else if(check_if_button_pushed() == GamepadLayout[14]){                //Motor stop
177
                externControl.Nick = 131;
178
                externControl.Roll = 125;
179
                externControl.Gier = 125;
180
                externControl.Gas = 131;
181
        }else{
182
                externControl.Nick = gamepad_values_convert_for_kopter(Axis[GamepadLayout[4]]);
183
                externControl.Roll = gamepad_values_convert_for_kopter(Axis[GamepadLayout[5]]);
184
                externControl.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[6]]);
185
                externControl.Gas = gamepad_values_convert_for_kopter_gas(Axis[GamepadLayout[7]]);
186
        }
187
        externControl.Config = EC_VALID | EC_GAS_ADD | EC_IGNORE_RC;
188
        externControl.Frame = 'z';                                                                              //character for echo
189
        u8 *tmpData = (unsigned char *) &externControl;
190
        for (int i = 0; i < pdata_package_kopter_control->cmdLength; ++i)
191
        {
192
                pdata_package_kopter_control->data[i] = tmpData[i];
193
        }
194
        //Print out values on screen
195
        char tmpbuffer[255];
196
        sprintf(tmpbuffer, "Nick: %d    \n     Roll: %d     \n     Gier: %d     \n     Gas : %d   \n", pdata_package_kopter_control->data[0], pdata_package_kopter_control->data[1], pdata_package_kopter_control->data[2], pdata_package_kopter_control->data[3]);
197
        frame_values("VALUES", 5, 2);
198
        frame_values(tmpbuffer, 5, 4);
199
}
200
 
201
void read_gamepad_data(){
202
        read(padFd, &js, sizeof(struct js_event));
203
        switch (js.type & ~JS_EVENT_INIT)
204
        {
205
                case JS_EVENT_AXIS:
206
                        Axis   [ js.number ] = js.value;
207
                        break;
208
                case JS_EVENT_BUTTON:
209
                        Button [ js.number ] = js.value;
210
                        break;
211
        }
212
}
213
 
214
//>> Initialize Gamepad
215
//------------------------------------------------------------------------------------------------------
216
void gamepad_init(){
217
        if((padFd = open(GAME_PAD, O_RDONLY)) == -1)
218
        {
219
                printf( "Couldn't open gamepad\n" );
220
        }
221
        ioctl( padFd, JSIOCGAXES, &NumberOfAxesAvailiable );
222
        ioctl( padFd, JSIOCGBUTTONS, &NumberOfButtonsAvailiable );
223
        ioctl( padFd, JSIOCGNAME(80), &GamepadConnected );
224
        Axis = (int *) calloc( NumberOfAxesAvailiable, sizeof( int ) );
225
        Button = (char *) calloc( NumberOfButtonsAvailiable, sizeof( char ) );
226
        fcntl( padFd, F_SETFL, O_NONBLOCK );    /* use non-blocking mode */
227
}