You are not logged in.

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

#1 Technical » Arduino Esplora as controller (non-intrusive way) » 2016-07-27 20:12:34

xcvista
Replies: 0

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.

#2 Re: Technical » Arduino Esplora? » 2016-07-27 16:46:32

zeromus wrote:

insufficient for microphone and accelerometers. If all you want is buttons and gamepads, that's more doable. You could do it right now by using luasockets in desmume's LUA, which is able to feed pad and touch input into the core. LUA was put there so that exotic things like this could be done without having to touch the codebase of desmume any more.

To be more sophisticated, I suggest you investigate adding this functionality to retroarch (which can run the desmume core) so that it can be done ONCE for every platform, instead of wedged ungracefully into desmume. I don't think anyone but you will be interested in adding such rarely-used functionality to desmume all by its lonesome.

Then can the built-in Lua environment handle device files (for direct tty access) or call C libraries (for libusb calls if direct HID is used) or use sockets (so a third party program can feed control into it) then?

#3 Re: Technical » Arduino Esplora? » 2016-07-26 20:34:08

zeromus wrote:

To use it as an input device, I recommend you begin by interfacing with a program running on your PC. Only if you have success with that should you bother breaking into desmume.

I have long established communication between that device and the PC (remembered me mentioning that I have already leveraged its USB HID keyboard simulation to perform basic interfacig between it and DeSmuME?)

I have long been able to use Esplora to control my own apps.

Here is my suggested method of allowing other programs to control DeSmuME, using pipes. I am disregarding the perks of Windows here.

Command-line flag: --gamepad=<file|stdin|[bind-address]:port|console> --touchscreen=<file|stdin|[bind-address]:port|console>

The first flag controls the source where DeSmuME takes keypad input from, and the second determines the touchscreen input:

  • console means taking the input from the user's keyboard or mouse (default behavior as of now);

  • file means taking control inputs from a normal file (for macro replaying,) character device (if the device can emit the corresponding instruction string right after being opened without any ioctl;) UNIX domain socket or pipe (usually from another program;)

  • stdin uses the standard input as the input file, with the same rule as when a file name is given (so desmume --gamepad /dev/ttyUSB0 and desmume --gamepad stdin < /dev/ttyUSB0 are synonymous, and it is possible to say myusbdriver /dev/ttyUSB0 | desmume --gamepad stdin);

  • [bind-address]:port means open a listening socket on this bind address (default to localhost) and port and listen to commands from there

By documenting the protocol used by this interface anyone can plug their own controller into DeSmuME without having to touch the codebase of DeSmuME any more. They just need to implement interface between their own method of input and this protocol, or in some cases, implement this protocol in hardware directly (e.g. a Wi-Fi connected controller can implement this protocol and speak to DeSmuME over network directly, if DeSmuME is launched with the option of taking gamepad input from network)

#4 Re: Technical » Arduino Esplora? » 2016-07-26 16:05:02

zeromus wrote:

In principle, sure. Good luck.
I doubt you could move enough data through that thing to control an LCD. Consider: 256x192x2x2x60 = 11.7MB/sec.
So just to use it as an input device, I recommend you begin by interfacing with a program running on your PC. Only if you have success with that should you bother breaking into desmume.

As an input device I have already able to implement some basic functionalities using the keyboard emulation feature of the microcontroller, translating operations on the Esplora into default keyboard bindings. However to make things like microphone or accelerometer work some kind of direct interface between DeSmuME and the firmware is needed.

#5 Technical » Arduino Esplora? » 2016-07-26 09:19:40

xcvista
Replies: 7

I have an Arduino Esplora. And that thing packs almost all I/O features of the NDS:

  • an analog joystick (can be used to emulate the D-pad)...

  • ... with a button under it (Start)

  • 4 big physical buttons (ABXY)

  • a microphone

  • a squeaky can speaker

  • a light sensor

  • an RGB LED

  • an analog slider

  • a 3-axis accelerometer

  • an optional LCD screen

  • running an Atmel ATmega32U4 microcontroller at 16MHz

  • connected to the computer over USB

So I wonder if there is any way to create a plugin or an adapter software, and the corresponding firmware for the microcontroller so DeSmuME can use it as an input device?

Board footer

Powered by FluxBB