ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / Soft / Lib / ardrone_tool / UI / ardrone_input.c
1 #include <VP_Os/vp_os_print.h>
2 #include <VP_Os/vp_os_assert.h>
3
4 #include <ardrone_tool/UI/ardrone_input.h>
5 #include <ardrone_tool/UI/ardrone_tool_ui.h>
6
7 static input_device_t* devices[MAX_NUM_DEVICES];
8
9 static input_state_t input_state = { 0 };
10
11 C_RESULT ardrone_tool_input_add( input_device_t* device )
12 {
13   C_RESULT res;
14   int32_t i;
15
16   VP_OS_ASSERT( device != NULL );
17
18   res = C_FAIL;
19   i   = 0;
20
21   while( i < MAX_NUM_DEVICES && devices[i] != NULL ) i++;
22
23   if( i < MAX_NUM_DEVICES )
24   {
25     if( VP_SUCCEEDED(device->init()) )
26     {
27       devices[i] = device;
28       PRINT("Input device %s added\n", device->name);
29       res = C_OK;
30     }
31     else
32     {
33       PRINT("Input device %s init failed\n", device->name);
34       res = C_FAIL;
35     }
36   }
37   else
38   {
39     PRINT("Not enough memory to add input device %s\n", device->name);
40   }
41
42   return res;
43 }
44
45 static C_RESULT ardrone_tool_input_remove_i( int32_t i )
46 {
47   C_RESULT res;
48
49   res = C_OK;
50
51   if( devices[i] != NULL )
52   {
53     if( VP_SUCCEEDED(devices[i]->shutdown()) )
54     {
55       PRINT("Input device %s removed\n", devices[i]->name);
56       res = C_OK;
57     }
58     else
59     {
60       PRINT("Input device %s removed but an error occured during its shutdown\n", devices[i]->name);
61       res = C_FAIL;
62     }
63
64     devices[i] = NULL;
65   }
66
67   return res;
68 }
69
70 C_RESULT ardrone_tool_input_remove( input_device_t* device )
71 {
72   C_RESULT res;
73   int32_t i;
74
75   VP_OS_ASSERT( device != NULL );
76
77   res = C_FAIL;
78   i   = 0;
79
80   while( i < MAX_NUM_DEVICES && devices[i] != device ) i++;
81
82   if( i < MAX_NUM_DEVICES )
83   {
84     res = ardrone_tool_input_remove_i(i);
85   }
86   else
87   {
88     DEBUG_PRINT_SDK("Input %s not found while removing\n", device->name);
89   }
90
91   return res;
92 }
93
94 C_RESULT ardrone_tool_set_ui_pad_ab(int32_t value)
95 {
96         input_state.ab = value;
97         return ui_pad_ab(value);
98 }
99
100 C_RESULT ardrone_tool_set_ui_pad_ag(int32_t value)
101 {
102         input_state.ag = value;
103         return ui_pad_ag(value);
104 }
105
106 C_RESULT ardrone_tool_set_ui_pad_ad(int32_t value)
107 {
108         input_state.ad = value;
109         return ui_pad_ad(value);
110 }
111
112 C_RESULT ardrone_tool_set_ui_pad_ah(int32_t value)
113 {
114         input_state.ah = value;
115         return ui_pad_ah(value);
116 }
117
118 C_RESULT ardrone_tool_set_ui_pad_l1(int32_t value)
119 {
120         input_state.l1 = value;
121         return ui_pad_l1(value);
122 }
123
124 C_RESULT ardrone_tool_set_ui_pad_r1(int32_t value)
125 {
126         input_state.r1 = value;
127         return ui_pad_r1(value);
128 }
129
130 C_RESULT ardrone_tool_set_ui_pad_l2(int32_t value)
131 {
132         input_state.l2 = value;
133         return ui_pad_l2(value);
134 }
135
136 C_RESULT ardrone_tool_set_ui_pad_r2(int32_t value)
137 {
138         input_state.r2 = value;
139         return ui_pad_r2(value);
140 }
141
142 C_RESULT ardrone_tool_set_ui_pad_select(int32_t value)
143 {
144         input_state.select = value;
145         return ui_pad_select(value);
146 }
147
148
149 C_RESULT ardrone_tool_set_ui_pad_start(int32_t value)
150 {
151         input_state.start = value;
152         return ui_pad_start_stop(value);
153 }
154
155 C_RESULT ardrone_tool_set_ui_pad_xy(int32_t x, int32_t y)
156 {
157   input_state.x   = x;
158   input_state.y   = y;
159
160   return ui_pad_xy_change(x, y);
161 }
162
163 C_RESULT ardrone_tool_input_init(void)
164 {
165   int32_t i;
166
167   i   = 0;
168
169   while( i < MAX_NUM_DEVICES )
170   {
171     devices[i] = NULL;
172     i++;
173   }
174
175   return ardrone_tool_input_reset();
176 }
177
178 C_RESULT ardrone_tool_input_reset(void)
179 {
180   return ui_pad_reset_user_input(&input_state);
181 }
182
183 C_RESULT ardrone_tool_start_reset(void)
184 {
185   input_state.start    = 0;
186
187   return ui_pad_reset_user_input_start(&input_state);
188 }
189
190 C_RESULT ardrone_tool_input_update(void)
191 {
192   C_RESULT res;
193   int32_t i;
194
195   res = C_OK;
196   i   = 0;
197
198   while( VP_SUCCEEDED(res) && i < MAX_NUM_DEVICES )
199   {
200     if( devices[i] != NULL && VP_FAILED(devices[i]->update()) )
201     {
202       PRINT("Input device %s update failed... it'll be removed\n", devices[i]->name);
203       ardrone_tool_input_remove_i(i);
204
205       res = C_FAIL;
206     }
207     i++;
208   }
209
210   ui_pad_update_user_input(&input_state);
211
212   return res;
213 }
214
215 C_RESULT ardrone_tool_input_shutdown(void)
216 {
217   int32_t i;
218
219   i   = 0;
220
221   while( i < MAX_NUM_DEVICES )
222   {
223     ardrone_tool_input_remove_i(i);
224     i++;
225   }
226
227   return C_OK;
228 }
229
230 input_state_t* ardrone_tool_get_input_state( void )
231 {
232    return &input_state;
233 }
234
235
236 C_RESULT custom_reset_user_input(input_state_t* input_state, uint32_t user_input ) { return C_OK; }
237 C_RESULT custom_update_user_input(input_state_t* input_state, uint32_t user_input ) { return C_OK; }