You are not logged in.

Read the FAQ and Knowledge Base before posting.
We won't make a 3DS/2DS emulator.



#1 2016-07-27 20:12:34

xcvista
Member
Registered: 2016-07-26
Posts: 5

Arduino Esplora as controller (non-intrusive way)

Here is the sketch:

/*
 * NDSController.ino
 * 
 * Use your Arduino Esplora as DeSmuME controller, with (almost)
 * identical controls as an actual NDS.
 * 
 * The analog joystick is implemented as the D-pad replacement.
 * Tilt it to a direction and you get a key press to that direction.
 * It is possible to get diagnal directions but not opposites.
 * 
 * Press down the joystick to issue the START button.
 * 
 * The four buttons on the right have the identical alyout as NDS
 * Use them the same way.
 * 
 * Depend on the default keyboard bindings. Emulates a keyboard so
 * no interfacing software or changes to DeSmuME required.
 * 
 * Copyright (c) 2016 Max Chan.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Max Chan nor the names of its contributors may
 *       be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#include <Esplora.h>
#include <Keyboard.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Keyboard.begin();
}

uint16_t statemap = 0;

#define LEFT  _BV(0)
#define RIGHT _BV(1)
#define UP    _BV(2)
#define DOWN  _BV(3)
#define BTN_A _BV(4)
#define BTN_B _BV(5)
#define BTN_X _BV(6)
#define BTN_Y _BV(7)
#define START _BV(8)

#define KEYB

void loop() {
  // put your main code here, to run repeatedly:
  uint16_t newstate = 0;

  int joy_x = Esplora.readJoystickX();
  if (joy_x < -128) newstate |= RIGHT;
  if (joy_x > 128) newstate |= LEFT;

  int joy_y = Esplora.readJoystickY();
  if (joy_y < -128) newstate |= UP;
  if (joy_y > 128) newstate |= DOWN;

  if (!Esplora.readJoystickButton()) newstate |= START;
  if (!Esplora.readButton(SWITCH_1)) newstate |= BTN_B;
  if (!Esplora.readButton(SWITCH_2)) newstate |= BTN_Y;
  if (!Esplora.readButton(SWITCH_3)) newstate |= BTN_X;
  if (!Esplora.readButton(SWITCH_4)) newstate |= BTN_A;

  uint16_t changemask = newstate ^ statemap;
#ifdef KEYB
#define CHECK(check, ch, key) if (changemask & check) { if (newstate & check) { Serial.println(ch); Keyboard.press(key); } else { Serial.println((char)(ch + ' ')); Keyboard.release(key); } }
#else
#define CHECK(check, ch, key) if (changemask & check) { if (newstate & check) { Serial.println(ch); } else { Serial.println((char)(ch + ' ')); } }
#endif
  CHECK(LEFT, 'L', KEY_LEFT_ARROW);
  CHECK(RIGHT, 'R', KEY_RIGHT_ARROW);
  CHECK(UP, 'U', KEY_UP_ARROW);
  CHECK(DOWN, 'D', KEY_DOWN_ARROW);
  CHECK(BTN_A, 'A', 'x');
  CHECK(BTN_B, 'B', 'z');
  CHECK(BTN_X, 'X', 's');
  CHECK(BTN_Y, 'Y', 'a');
  CHECK(START, 'S', KEY_RETURN);
  statemap = newstate;
}

Upload this into your Esplora and you get a NDS-like controller. It uses keyboard emulation and depend on default keyboard bindings. You can change the keys by changing the corresponding ones on the "CHECK" macros below.

Offline

Board footer

Powered by FluxBB