Coder Social home page Coder Social logo

joystick's Introduction

joystick++

A minimal C++ object-oriented API onto joystick devices under Linux.

Build

mkdir build
cmake ..
make

or

g++ joystick_demo.cc joystick.cc -std=c++0x -Wall -o test

Usage

Create an instance of Joystick:

Joystick joystick;

Ensure that it was found and that we can use it:

if (!joystick.isFound())
{
  printf("open failed.\n");
  // hmm
}

Sample events from the Joystick:

JoystickEvent event;
if (joystick.sample(&event))
{
  // use 'event'
}

Example

You might run this in a loop:

while (true)
{
  // Restrict rate
  usleep(1000);

  // Attempt to sample an event from the joystick
  JoystickEvent event;
  if (joystick.sample(&event))
  {
    if (event.isButton())
    {
      printf("Button %u is %s\n", event.number, event.value == 0 ? "up" : "down");
    }
    else if (event.isAxis())
    {
      printf("Axis %u is at position %d\n", event.number, event.value);
    }
  }
}

This produces something similar to:

Button 1 is up
Button 2 is down
Axis 0 is at position 122
Axis 1 is at position -11
Axis 2 is at position 9796
Axis 3 is at position -13850

Options

You can specify the particular joystick by id:

Joystick js0(0);
Joystick js1(1);

Or provide a specific device name:

Joystick js0("/dev/input/js0");

License

Released under Apache License 2.0.

Copyright Drew Noakes 2013-2017.

joystick's People

Contributors

drewnoakes avatar mpkuse avatar jacobperron avatar romainreignier avatar sgvandijk avatar kvanvel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.