-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoystick_input.cpp
More file actions
126 lines (104 loc) · 4.97 KB
/
joystick_input.cpp
File metadata and controls
126 lines (104 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Control the Maplin A37JN Robotic Arm via a joystick.
*
* The device has five bidirectional DC motors, and an LED.
*
* Copyright (C) 2013 Steve Cotton (steve@s.cotton.clara.co.uk)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; version 2 of the License, or
* (at your option) any later version.
*
*/
#include <iostream>
#include <SDL.h>
#include "robotic_device.h"
#include "input_config.h"
namespace {
/** How far the joystick has to be pushed to be registered as being moved */
const Sint16 INPUT_VALUE_THRESHOLD=16192;
}
int main(int argc, char** argv) {
ArmDevice device;
std::cout << "Setting up SDL" << std::endl;
// SDL_INIT_VIDEO is needed for SDL_WaitEvent
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_ENABLE);
int numJoysticks = SDL_NumJoysticks();
if (1 != numJoysticks) {
std::cout << "Found " << numJoysticks << " possible joysticks." << std::endl;
std::cout << "Handling none or more than one is currently unsupported." << std::endl;
// TODO if this ever reaches release quality
} else {
// Eventually this might support stick selection, but for now only one stick will work
const int joystickIndex = 0;
SDL_Joystick* inputStick = SDL_JoystickOpen(joystickIndex);
const int numAxis = SDL_JoystickNumAxes(inputStick);
const int numButtons = SDL_JoystickNumButtons(inputStick);
InputMapping::sanityCheckConfig(numAxis, numButtons);
bool keepRunning = true;
while (keepRunning) {
std::cout << "eventloop" << std::endl;
SDL_Event event;
int waitResult = SDL_WaitEvent(&event);
if (1 != waitResult) {
std::cout << "Event loop failed: " << waitResult << std::endl;
event.type = SDL_QUIT;
}
switch (event.type) {
case SDL_QUIT: {
keepRunning = false;
break;
}
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
case SDL_JOYAXISMOTION: {
// For any change on the joystick, simply reread the whole state
std::cout << "Event " << (int)event.type << std::endl;
std::array<ArmDevice::Motion, ArmDevice::NUMBER_OF_AXIS> movement;
movement.fill(ArmDevice::Motion::STOP);
auto& axisMapping = InputMapping::getAxisMappings();
for (auto mapping = axisMapping.begin(); mapping != axisMapping.end(); mapping++) {
const int inAxis = mapping->inAxis;
const int outAxis = mapping->outAxis;
if (0 > inAxis) {
std::cout << "The joystick configuration is wrong (negative numbers for axis)." << std::endl;
} else if (inAxis >= numAxis) {
std::cout << "Your joystick ." << std::endl;
}
// SDL joystick coordinates are positive down-right. The arm seems to associate down-left with negative, so reverse one axis.
// The SDL value is a signed 16-bit, promoted in this code to handle 16-bit MIN_VALUE.
int32_t value = SDL_JoystickGetAxis(inputStick, inAxis);
if (mapping->reversed) {
value = -value;
}
std::cout << "axis " << inAxis << "->" << outAxis << " value " << value << std::endl;
if (value < -INPUT_VALUE_THRESHOLD) {
movement[outAxis] = ArmDevice::Motion::UP_RIGHT_CLOSE;
} else if (value > INPUT_VALUE_THRESHOLD) {
movement[outAxis] = ArmDevice::Motion::DOWN_LEFT_OPEN;
}
}
auto& buttonMapping = InputMapping::getButtonMappings();
for (auto mapping = buttonMapping.begin(); mapping != buttonMapping.end(); mapping++) {
const bool pressed = SDL_JoystickGetButton(inputStick, mapping->inButton);
if (pressed) {
movement[mapping->outAxis] = mapping->direction;
}
}
auto& lightButtons = InputMapping::getLightButtons();
bool led = false;
for (auto mapping = lightButtons.begin(); mapping != lightButtons.end(); mapping++) {
if (SDL_JoystickGetButton(inputStick, *mapping)) {
led = true;
}
}
device.motion(movement, led);
}
}
}
}
SDL_Quit();
return 0;
}