Tuesday, May 03, 2016

Node.js Hosting – Windows vs. Linux – Case Sensitivity

The Problem

I recently worked on a Node.js project were we were doing our development on Windows 7 machines, but the actual hosting was being done with Cloud Foundry (running Linux). This website contained a number of graphical images, which appeared correctly on our Windows 7 development machines, but when we deployed our application to Cloud Foundry, a number of the images would not appear. The cause for this discrepancy between environments was Linux is a case sensitive operating system, but Windows is not case sensitive.

The Quick Solution

To resolve this issue we went through our code and verify the case of the filenames matched any references to those files in our source code.

Preventing the Issue

We wanted to prevent an issue like this from happening again, so we decided to institute a naming convention for all of our image files. In our case we decided to make them all lower case. To enforce this convention we used the gulp-check-file-naming-convention (see https://github.com/HAKASHUN/gulp-check-file-naming-convention for more details) gulp plugin by HAKASHUN.

The following is a simple gulp example showing how this plugin can be used:

1
2
3
4
5
6
7
const gulp = require('gulp');
const fileNamingConventionChecker = require("gulp-check-file-naming-convention");

gulp.task('default', function() {
    gulp.src("./public/**/*")
        .pipe(fileNamingConventionChecker({ caseName: 'lowerCase' }));
});

The error message that is generated by this gulp plugin will look something like the following:

[08:27:59] Using gulpfile ~\Source\git\app\gulpfile.js
[08:27:59] Starting 'default'...
[08:27:59] Finished 'default' after 11 ms

events.js:160
      throw er; // Unhandled 'error' event
      ^
 Error: Invalid file name at ←[31mC:\Users\NAME\Source\git\app\public
\img\SquareProfile.jpg←[39m :
 > ←[32msquareprofile.jpg←[39m is valid.

Thursday, March 03, 2016

Remote Desktop & Remote Control Raspberry Pi

Introduction

This article discusses how to log into a Raspberry Pi Remote Desktop from a Windows machine and how to Remote Control a Raspberry Pi from a Windows machine.

Remote Desktop Raspberry Pi from Windows

Remote desktop allows you to log into a desktop (i.e. X-Windows session) on your Raspberry Pi. If you have the Raspberry Pi connected to a physical monitor, you will not be logging into that desktop, but a new desktop that will be dedicated to your remote desktop session. For instructions on how to remote control the Raspberry Pi from Windows, see the second half of the article.

Setup Raspberry Pi for Remote Desktop

  1. Startup the Raspberry Pi.
  2. Open a terminal prompt.
  3. Enter the following command to install xrdp (http://www.xrdp.org/):
    sudo apt-get install xrdp

    You may be prompted for your password (the default password for the Raspberry Pi is “raspberry”).
  4. Reboot the Raspberry Pi. The Remote Desktop Protocol server (xrdp sesman) should automatically start on reboot.
  5. Get the IP address of the Raspberry Pi. This can be done by executing the following command from a terminal prompt:
    ifconfig

Connect From Windows Machine

  1. Start the Remote Desktop Connection application.
    • On Windows 7 click Start, All Programs, Accessories, Remote Desktop Connection.
    • On Windows 10 click Start and enter Remote Desktop Connection.
  2. Enter the IP address of the Raspberry Pi in the “Remote Desktop Connection” dialog and click Connect.
  3. When connecting you may see one or more of the following dialogs:


  4. Once connected, a login dialog will be displayed.
  5. Enter a username and password. The default Raspberry Pi username is pi and the default password is raspberry.
  6. Once you are logged in, you should see the Raspberry Pi desktop.

Remote Control Raspberry Pi from Windows

Remote controlling allows you to control the currently active desktop (i.e. X-Windows session) on your Raspberry Pi. With this technique, you can control (e.g. manipulate the mouse & keyboard) the desktop that is shown on the physical monitor that is connected to the Raspberry Pi.

Setup Raspberry Pi for Remote Desktop

  1. Startup the Raspberry Pi.
  2. Open a terminal prompt.
  3. Enter the following command to install x11vnc (http://www.karlrunge.com/x11vnc/):
    sudo apt-get install x11vnc
  4. Enter the following command to set the vnc password. This is the password users will need to enter to remote control the desktop.
    x11vnc –storepasswd
  5. You can test the x11vnc server by entering the following at a terminal prompt.
    x11vnc –usepw
  6. You should see a message like the following on the terminal.

Connect From Windows Machine

  1. Install a VNC client. In this example I use TightVNC Viewer (http://www.tightvnc.com/download.php).
  2. In the VNC client, enter the IP address of the Raspberry Pi followed by a “:” and the display number, which can be found terminal window running x11vnc (this will normally be a 0, but in our example above this is a 2).
  3. Enter the password that was used when setting up x11vnc.
  4. You should now be able to see and interact with the Raspberry Pi desktop.

Automatically Startup x11vnc on the Raspberry Pi

  1. Create the directory ~/.config/autostart, if it does not already exist. This can be done using the following terminal command.
    mkdir –p ~/.config/autostart
  2. Create a text file in this directory, called xterm-autostart.desktop. You can create and edit this file with the following terminal command.
    leafpad ~/.config/autostart/xterm-autostart.desktop
  3. Put the following text into this file.
    [Desktop Entry]
    Encoding=UTF-8
    Name=x11vnc autostart
    Comment=Starts the x11vnc server to allow for remote VNC connections.
    Exec=x11vnc –forever -usepw
  4. Reboot the Raspberry Pi.
  5. Once the Raspberry Pi desktop appears, you should be able to remote control the Raspberry Pi using a VNC Client.

Monday, December 28, 2015

Ultimate Classic Game Console Joystick to USB Adapter

Background

Original Classic Joystick to USB AdaptorBack in September of 2014 I posted an article (Arduino: Classic Joystick to USB Adaptor) explaining how to use an Arduino Leonardo or Arduino Micro to create an adapter that would allow a classic game console controller/joystick (e.g. Atari 2600, ColecoVision, and possibly others) to work on a modern computer (e.g. Windows PC, Mac, or Linux). This adapter mapped the various joystick actions (Up, Down, Left, Right, Fire Button, etc.) to keyboard keystrokes. I used the keystrokes used by the ADAMEm (http://www.komkon.org/~dekogel/adamem.html) ColecoVision / Coleco ADAM emulator, but any keystroke mapping could be used.

Throughout 2015 I have been posting the following articles that explain how to make an Arduino Leonardo or Arduino Micro appear as a USB Game Controller:

All of these articles have been building up to this article which will explain how to create the “ultimate” classic game console joystick to USB adapter.

Overview

Final VersionThis article describes how to use an Arduino Leonardo or Arduino Micro to make up to three classic console joysticks (e.g. Atari 2600, ColecoVision, and possibly others) available to a modern computer (e.g. Windows PC, Mac, or Linux). This adapter can be placed into one of the following modes:

  • Joystick Mode
    Each of the three classic console joysticks appear as a Game Controller.
  • ADAMEm Mode
    Configured for use with the ADAMEm emulator. Joystick 1’s direction and fire buttons are mapped to a Game Controller and the keypad is mapped to the keyboard’s numeric keypad keys. Joystick 2 is mapped to keyboard keys (e.g. up arrow key, down arrow key, etc.). This allows ADAMEm to support two player games. Joystick 3 is not used in this mode.
  • MAME Mode
    Configured for use with the MAME emulator. All three joysticks’ direction and fire buttons are mapped to the three Game Controllers. The numeric keys on all three joysticks are mapped to keyboard keys.

Classic Console Joysticks

Atari 2600 Joysticks

Atari-2600-Joystick The Atari 2600 joysticks are the simplest of the controllers supported by this adaptor. They have a D-Subminiature Female 9 Position connector. The following table describes the function of each pin:
DSUB9-Pinout
Pin Number Description
1 Up
2 Down
3 Left
4 Right
6 Fire
8 Ground

ColecoVision Controllers

ColecoVision-Joystick ADAM-Joystick

ColecoVision Controller

ADAM Controller

ColecoVision-Super-Action-Controller  

ColecoVision Super Action Controller

 

The ColecoVision and ADAM joysticks are much more complicated. In addition to the four direction control stick, they have two fire buttons (or 4 in the case of the Super Action Controller) and a 12 button keypad. Like the Atari 2600 joystick, they have a D-Subminiature Female 9 Position connector, but the pin outs have two different modes. When pin 8 is grounded, pins 1-4 are the four directions and pin 6 is the left fire button. When pin 5 is grounded, pins 1-4 are a keypad scan code and pin 6 is the right fire button. The table below lists the pin outs for the ColecoVision and ADAM controllers.

Pin Number Pin 8 Grounded Pin 5 Grounded
1 Up Bit 0
2 Down Bit 2
3 Left Bit 3
4 Right Bit 1
5 Keypad Mode Keypad Mode
6 Left Fire Right Fire
7 Spinner Spinner
8 Control Stick Mode Control Stick Mode
9 Spinner Spinner

The keypad scan codes are listed below:

Keypad Value
Bit 3 / Pin 3
Bit 2 / Pin 2
Bit 1 / Pin 4
Bit 0 / Pin 1
Decimal Value
Hex Value
1
0
0
1
0
2
2
2
1
0
0
0
8
8
3
0
0
1
1
3
3
4
1
1
0
1
13
D
5
1
1
0
0
12
C
6
0
0
0
1
1
1
7
1
0
1
0
10
A
8
1
1
1
0
14
E
9
0
1
0
0
4
4
0
0
1
0
1
5
5
*
0
1
1
0
6
6
#
1
0
0
1
9
9
Purple Fire Button
0
1
1
1
7
7
Blue Fire Button
1
0
1
1
11
B

The solution I have developed will support Atari 2600 joysticks, ColecoVision and ADAM controllers, and most of the ColecoVision Super Action Controllers’ functions. This solution does not currently support the Spinner located below the keypad on the Super Action Controllers.

Parts List

Required Parts

Quantity Item
1 Arduino Leonardo - http://arduino.cc/en/Main/ArduinoBoardLeonardo
or
Arduino Micro - http://arduino.cc/en/Main/ArduinoBoardMicro
3 D-Subminiature Male 9 Position Connector
For example: http://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_12504_-1, http://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_1975029_-1, etc.
3 LEDs (any color(s) you want)
3 330 Ohm Resistors (or whatever is appropriate for the LEDs chosen)
1 Momentary Push Button
1 - 3 Classic Console Joysticks
1 Solderless Plugin Breadboard or Solderable PC Breadboard

Optional Parts

Quantity Item
1Project Box

Hardware

D-Sub Male 9 Position Connector Preparation

DSUB-Prep-1

Depending on the type of D-Sub Male 9 Position connector you use, you may need to do some work to get the connector ready to use. In my case I used a connector and a SparkFun Serial DB9 Breakout (https://www.sparkfun.com/products/retired/8552), so I needed to do a little soldering to get it ready to use.

DSUB-Prep-2

Momentary Push Button Preparation

Depending on what type of momentary push button you use, you many need to solder some wires to the push button so it can be connected to the Arduino.

Momentary-Push-Button-Prep

LED Preparation

Depending on the type of LED you use and how you plan to package the final device, you many need to solder some wires to the LEDs so they can be connected to the Arduino.

Solderable PC Breadboard Preparation

For my project I used an Arduino Micro (http://arduino.cc/en/Main/ArduinoBoardMicro) which I soldered to a solderable PC breadboard. I wanted to make sure I had good connections between my Arduino Micro and my joystick lines, so I threaded very small wires through the breadboard to act as the conductive traces.

Arduino-Prep

Arduino-Prep-Back

Wiring Diagram

The following diagram shows how to connect the Arduino Leonardo or Arduino Micro to the three, D-Subminiature male 9-pin connectors, resistors, LEDs, and momentary switch.

Classic Controller to USB Adapter

The following table describes how the pins of the Arduino are connected to the Joystick’s D-Subminiature pins.

Arduino Description Joystick 1 Joystick 2 Joystick 3
0 RX – Reserved for serial communication      
1 TX – Reserved for serial communication      
2 UP / Data 0 1 1 1
3 DOWN / Data2 2 2 2
4 LEFT / Data 3 3 3 3
5 RIGHT / Data 1 4 4 4
6 FIRE 6 6 6
7 Spinner A 7 7 7
8 Spinner B 9 9 9
9 Emulation Mode Toggle Pin – Connect one end of momentary switch to this pin and connect the other end to ground      
10 ADAMEm Mode Indicator – connect to 330 Ohm resistor, 330 Ohm resistor is then connected to LED, which is connected to ground      
11 MAME Mode Indicator – connect to 330 Ohm resistor, 330 Ohm resistor is then connected to LED, which is connected to ground      
12 Joystick Mode Indicator – connect to 330 Ohm resistor, 330 Ohm resistor is then connected to LED, which is connected to ground      
13 Fire Monitor Pin – on-board led      
A0 Joystick 1 Keypad Mode 5    
A1 Joystick 1 Control Stick Mode 8    
A2 Joystick 2 Keypad Mode   5  
A3 Joystick 2 Control Stick Mode   8  
A4 Joystick 3 Keypad Mode     5
A5 Joystick 3 Control Stick Mode     8

Wiring-top   Wiring-back

Enclosure

So the final product would look nice (and I would not worry about my kids breaking any of the wires) I put the adapter into a project box. I had to drill holes for the momentary switch, the LEDs, and the d-subminiature connectors.

Enclosure-Front-Switch   Enclosure-back-switch   Enclosure-half   Enclosure-top-off-side   Enclosure-top-off-top   Enclosure-top-on-side

To finish it up I put some labels on the connectors and on the LED mode indicators.

Enclosure-labels

Software

Arduino made a major change to how they implemented the USB HID functionality in Arduino IDE Version 1.6.6, so I have two different sets of instructions depending on which version of the Arduino IDE you are using. I recommend using Version 1.6.6 or above if you can, because it is much simpler.

Arduino Version 1.6.5 and Below

  1. Follow the instructions provided in the following article to add 3 USB Game Controllers to the Arduino Leonardo or Arduino Micro: Add Up To 3 USB Game Controllers to Arduino Leonardo or Micro.
  2. Download and install the ClassicController library from the Arduino-1.6.5 branch of the MHeironimus/ClassicJoystickAdaptor GitHub repository. This library can be installed by copying it to the Arduino libraries folder (typically %userprofile%\Documents\Arduino\libraries).
  3. Download the ClassicJoystickAdaptor.ino sketch file from the Arduino-1.6.5 branch of the folder of the MHeironimus/ClassicJoystickAdaptor GitHub repository and open it in the Arduino IDE.
  4. Compile and upload the sketch file to the Arduino Leonardo or Arduino Micro.
  5. The adapter can be testing using the “Game Controllers” dialog in Microsoft Windows. See “Add Up To 3 USB Game Controllers to Arduino Leonardo or Micro” (http://mheironimus.blogspot.com/2015/04/add-up-to-3-usb-game-controllers-to.html) for more details on testing the adapter.

Arduino version 1.6.6 and Above

  1. Download and install the Arduino Joystick Library from the MHeironimus/ArduinoJoystickLibrary GitHub repository. This library can be installed by copying it to the Arduino libraries folder (typically %userprofile%\Documents\Arduino\libraries). For more information on how to install this library, see the following article: Arduino Joystick Library.
  2. Download and install the ClassicController library from the master branch of the MHeironimus/ClassicJoystickAdaptor GitHub repository. This library can be installed by copying it to the Arduino libraries folder (typically %userprofile%\Documents\Arduino\libraries).
  3. Download the ClassicJoystickAdaptor.ino sketch file from the master branch of the folder of the MHeironimus/ClassicJoystickAdaptor GitHub repository and open it in the Arduino IDE.
  4. Compile and upload the sketch file to the Arduino Leonardo or Arduino Micro.
  5. The adapter can be testing using the “Game Controllers” dialog in Microsoft Windows. See “Arduino Joystick Library” (http://mheironimus.blogspot.com/2015/11/arduino-joystick-library.html) for more details on testing the adapter.

Final Product

Final VersionI have tested this “ultimate” classic game console joystick to USB adapter on Windows 7, Windows 10, and Raspbian on the Raspberry Pi Model B. To work properly on the Raspberry Pi (or any other linux devices), see Linux Support for Arduino Leonardo / Micro USB Game Controllers.