Add joystick input

template
PgSocks 2 years ago
parent 19993ced8e
commit 150c450210

@ -21,8 +21,6 @@ float x = 0, y = 0, z = 0;
static Uint32 drive_timer_handler(Uint32 interval, void* param) {
robot_drive((Robot)param, x, y, z);
robot_aim((Robot)param, pitch, yaw);
yaw *= 0.80;
pitch *= 0.80;
return 75;
}
static Uint32 heartbeat_timer_handler(Uint32 interval, void* param) {
@ -31,10 +29,15 @@ static Uint32 heartbeat_timer_handler(Uint32 interval, void* param) {
}
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0) {
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
fprintf(stderr, "%s", SDL_GetError());
return 1;
}
printf("Detected %d joysticks\n", SDL_NumJoysticks());
SDL_Joystick* joystick = NULL;
if(SDL_NumJoysticks() > 0) {
joystick = SDL_JoystickOpen(0);
}
SDL_Window* win = SDL_CreateWindow(
"Robomaster",
SDL_WINDOWPOS_UNDEFINED,
@ -94,6 +97,28 @@ int main(int argc, char* argv[]) {
yaw = (float)event.motion.xrel;
pitch = (float)event.motion.yrel;
break;
case SDL_JOYAXISMOTION:
switch(event.jaxis.axis) {
case 0:
x = (float)event.jaxis.value / 32767;
break;
case 1:
y = (float)event.jaxis.value / 32767;
break;
case 4:
z = (float)event.jaxis.value / 32767 / 2;
break;
case 2:
yaw = (float)event.jaxis.value / 32767;
break;
case 3:
pitch = (float)event.jaxis.value / 32767;
break;
default:
printf("axis: %d\n", event.jaxis.axis);
break;
}
break;
case SDL_WINDOWEVENT:
if(event.window.event != SDL_WINDOWEVENT_CLOSE) break;
case SDL_QUIT:
@ -103,6 +128,7 @@ int main(int argc, char* argv[]) {
}
}
SDL_JoystickClose(joystick);
SDL_Quit();
return 0;
}

Loading…
Cancel
Save