[136] | 1 | /*
|
---|
| 2 | Esplora Kart
|
---|
| 3 |
|
---|
| 4 | This sketch turns the Esplora into a PC game pad.
|
---|
| 5 |
|
---|
| 6 | It uses the both the analog joystick and the four switches.
|
---|
| 7 | By moving the joystick in a direction or by pressing a switch,
|
---|
| 8 | the PC will "see" that a key is pressed. If the PC is running
|
---|
| 9 | a game that has keyboard input, the Esplora can control it.
|
---|
| 10 |
|
---|
| 11 | The default configuration is suitable for SuperTuxKart, an
|
---|
| 12 | open-source racing game. It can be downloaded from
|
---|
| 13 | http://supertuxkart.sourceforge.net/ .
|
---|
| 14 |
|
---|
| 15 | Created on 22 november 2012
|
---|
| 16 | By Enrico Gueli <enrico.gueli@gmail.com>
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | #include <Esplora.h>
|
---|
| 21 |
|
---|
| 22 | /*
|
---|
| 23 | You're going to handle eight different buttons. You'll use arrays,
|
---|
| 24 | which are ordered lists of variables with a fixed size. Each array
|
---|
| 25 | has an index (counting from 0) to keep track of the position
|
---|
| 26 | you're reading in the array, and each position can contain a number.
|
---|
| 27 |
|
---|
| 28 | This code uses three different arrays: one for the buttons you'll read;
|
---|
| 29 | a second to hold the current states of those buttons; and a third to hold
|
---|
| 30 | the keystrokes associated with each button.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /*
|
---|
| 34 | This array holds the last sensed state of each of the buttons
|
---|
| 35 | you're reading.
|
---|
| 36 | Later in the code, you'll read the button states, and compare them
|
---|
| 37 | to the previous states that are stored in this array. If the two
|
---|
| 38 | states are different, it means that the button was either
|
---|
| 39 | pressed or released.
|
---|
| 40 | */
|
---|
| 41 | boolean buttonStates[8];
|
---|
| 42 |
|
---|
| 43 | /*
|
---|
| 44 | This array holds the names of the buttons being read.
|
---|
| 45 | Later in the sketch, you'll use these names with
|
---|
| 46 | the method Esplora.readButton(x), where x
|
---|
| 47 | is one of these buttons.
|
---|
| 48 | */
|
---|
| 49 | const byte buttons[] = {
|
---|
| 50 | JOYSTICK_DOWN,
|
---|
| 51 | JOYSTICK_LEFT,
|
---|
| 52 | JOYSTICK_UP,
|
---|
| 53 | JOYSTICK_RIGHT,
|
---|
| 54 | SWITCH_RIGHT, // fire
|
---|
| 55 | SWITCH_LEFT, // bend
|
---|
| 56 | SWITCH_UP, // nitro
|
---|
| 57 | SWITCH_DOWN, // look back
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | /*
|
---|
| 61 | This array tells what keystroke to send to the PC when a
|
---|
| 62 | button is pressed.
|
---|
| 63 | If you look at this array and the above one, you can see that
|
---|
| 64 | the "cursor down" keystroke is sent when the joystick is moved
|
---|
| 65 | down, the "cursor up" keystroke when the joystick is moved up
|
---|
| 66 | and so on.
|
---|
| 67 | */
|
---|
| 68 | const char keystrokes[] = {
|
---|
| 69 | KEY_DOWN_ARROW,
|
---|
| 70 | KEY_LEFT_ARROW,
|
---|
| 71 | KEY_UP_ARROW,
|
---|
| 72 | KEY_RIGHT_ARROW,
|
---|
| 73 | ' ',
|
---|
| 74 | 'V',
|
---|
| 75 | 'N',
|
---|
| 76 | 'B'
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | /*
|
---|
| 80 | This is code is run only at startup, to initialize the
|
---|
| 81 | virtual USB keyboard.
|
---|
| 82 | */
|
---|
| 83 | void setup() {
|
---|
| 84 | Keyboard.begin();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /*
|
---|
| 88 | After setup() is finished, this code is run continuously.
|
---|
| 89 | Here we continuously check if something happened with the
|
---|
| 90 | buttons.
|
---|
| 91 | */
|
---|
| 92 | void loop() {
|
---|
| 93 |
|
---|
| 94 | // Iterate through all the buttons:
|
---|
| 95 | for (byte thisButton = 0; thisButton < 8; thisButton++) {
|
---|
| 96 | boolean lastState = buttonStates[thisButton];
|
---|
| 97 | boolean newState = Esplora.readButton(buttons[thisButton]);
|
---|
| 98 | if (lastState != newState) { // Something changed!
|
---|
| 99 | /*
|
---|
| 100 | The Keyboard library allows you to "press" and "release" the
|
---|
| 101 | keys as two distinct actions. These actions can be
|
---|
| 102 | linked to the buttons we're handling.
|
---|
| 103 | */
|
---|
| 104 | if (newState == PRESSED) {
|
---|
| 105 | Keyboard.press(keystrokes[thisButton]);
|
---|
| 106 | }
|
---|
| 107 | else if (newState == RELEASED) {
|
---|
| 108 | Keyboard.release(keystrokes[thisButton]);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | // Store the new button state, so you can sense a difference later:
|
---|
| 113 | buttonStates[thisButton] = newState;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /*
|
---|
| 117 | Wait a little bit (50ms) between a check and another.
|
---|
| 118 | When a mechanical switch is pressed or released, the
|
---|
| 119 | contacts may bounce very rapidly. If the check is done too
|
---|
| 120 | fast, these bounces may be confused as multiple presses and
|
---|
| 121 | may lead to unexpected behaviour.
|
---|
| 122 | */
|
---|
| 123 | delay(50);
|
---|
| 124 | }
|
---|
| 125 |
|
---|