void JoystickManager::update() { for (int i = 0; i < Joystick::Count; ++i) { Item& item = m_joysticks[i]; if (item.state.connected) { // Get the current state of the joystick item.state = item.joystick.update(); // Check if it's still connected if (!item.state.connected) { item.joystick.close(); item.capabilities = JoystickCaps(); item.state = JoystickState(); item.identification = Joystick::Identification(); } } else { // Check if the joystick was connected since last update if (JoystickImpl::isConnected(i)) { if (item.joystick.open(i)) { item.capabilities = item.joystick.getCapabilities(); item.state = item.joystick.update(); item.identification = item.joystick.getIdentification(); } } } } }
bool JoystickImpl::open(unsigned int index) { if (plugged[index]) { char name[32]; std::snprintf(name, sizeof(name), "/dev/input/js%u", index); // Open the joystick's file descriptor (read-only and non-blocking) m_file = ::open(name, O_RDONLY | O_NONBLOCK); if (m_file >= 0) { // Retrieve the axes mapping ioctl(m_file, JSIOCGAXMAP, m_mapping); // Reset the joystick state m_state = JoystickState(); return true; } else { return false; } } else { return false; } }
JoystickState Joystick::getState(){ bool sw = digitalRead(this->sw_pin_) == 1; int x = analogRead(this->x_pin_); int y = analogRead(this->y_pin_); JoystickState state = JoystickState(); state.X = this->getAliasPosition(x, this->xAliasCenterValue); state.Y = this->getAliasPosition(y, this->yAliasCenterValue); state.SW = sw; return state; }
void Pi::InitJoysticks() { int joy_count = SDL_NumJoysticks(); for (int n = 0; n < joy_count; n++) { JoystickState *state; joysticks.push_back(JoystickState()); state = &joysticks.back(); state->joystick = SDL_JoystickOpen(n); if (state->joystick == NULL) { fprintf(stderr, "SDL_JoystickOpen(%i): %s\n", n, SDL_GetError()); continue; } state->axes.resize(SDL_JoystickNumAxes(state->joystick)); state->buttons.resize(SDL_JoystickNumButtons(state->joystick)); state->hats.resize(SDL_JoystickNumHats(state->joystick)); } }
std::vector<JoystickState> GLFW_App::get_joystick_states() const { std::vector<JoystickState> ret; int count; for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) { if(!glfwJoystickPresent(i)) continue; const float *glfw_axis = glfwGetJoystickAxes(i, &count); std::vector<float> axis(glfw_axis, glfw_axis + count); const uint8_t *glfw_buttons = glfwGetJoystickButtons(i, &count); std::vector<uint8_t> buttons(glfw_buttons, glfw_buttons + count); std::string name(glfwGetJoystickName(i)); ret.push_back(JoystickState(name, buttons, axis)); } return ret; }
bool JoystickImpl::Open(unsigned int index) { std::ostringstream oss; oss << "/dev/input/js" << index; myFile = open(oss.str().c_str(), O_RDONLY); if (myFile > 0) { // Use non-blocking mode fcntl(myFile, F_SETFL, O_NONBLOCK); // Retrieve the axes mapping ioctl(myFile, JSIOCGAXMAP, myMapping); // Reset the joystick state myState = JoystickState(); return true; } else { return false; } }
bool JoystickImpl::open(unsigned int index) { char name[32]; std::sprintf(name, "/dev/input/js%u", index); m_file = ::open(name, O_RDONLY); if (m_file > 0) { // Use non-blocking mode fcntl(m_file, F_SETFL, O_NONBLOCK); // Retrieve the axes mapping ioctl(m_file, JSIOCGAXMAP, m_mapping); // Reset the joystick state m_state = JoystickState(); return true; } else { return false; } }
//////////////////////////////////////////////////////////// /// Update the current joystick and return its new state //////////////////////////////////////////////////////////// JoystickState Joystick::UpdateState() { return JoystickState(); }