N64 Controller Serial Protocol Rs232

This code was written by me, with parts of the N64_Arduino file based on assembly code written by Andrew Brown.
ZIP Archives:
The two Zip files below contain the code needed to run the Arduino and then to interpret the data it sends to the computer. The N64_Arduino file needs to be compiled in the Arduino IDE, and the N64_Controller runs in Processing 1.0.
N64_Arduino
This PDE file should upload to your Arduino and run without a hitch if you have everything connected properly. It simply queries the N64 controller for data on the buttons and Analog stick and sends it back to the computer over the serial port. It is easy enough to modify, for example, you could use the methods from this file to query a controller and use the data to run an Arduino robot instead of transmitting it back to the computer.
N64_Controller
This is a Processing 1.0 project that takes the data transmitted by the Arduino and converts it into keyboard presses that you can map to an emulator like Project 64. You might need to change the line String portName = Serial.list()[1];to match the your Arduino, it should be either Serial.list()[0]; Serial.list()[1]; or Serial.list()[2];
EDIT: Add
'import java.awt.Robot;'
'import java.awt.AWTException;'
'import java.awt.event.InputEvent;'
to the code if you are using Processing 1.1
N64_Controller_mouse
This is the same as N64_Controller, except that the analog stick controls your mouse, not the arrow keys. A and B are right and left click, respectively. To activate the mouse, press the start button on your controller.

  1. N64 Controller Serial Protocol Rs232 Driver

Online payment facility Other Payment Options Home > Businesses, Agents and Trade Professionals > Cargo support, trade and goods > Paying invoices to the. PTZOptics IP Joystick Controller with ONVIF Protocol for All PTZ. Series RS232 8 Pin Mini DIN to DB9F Serial. NINTENDO Wii U- -Wireless Wii U Pro Controller to PC /PS3 Adapter. FEATURES: W0. 09 Firmware Updates on February 1. XINPUT Mode: To switch the controller buttons to A- A, B- B,X- X,Y- Y.

Linux on board

Peter turns his old laptop into a simple remote device controller

Content series:

N64 Controller Serial Protocol Rs232 Driver

This content is part # of # in the series: Linux on board

https://www.ibm.com/developerworks/library/?series_title_by=linux+on+board

This content is part of the series:Linux on board

Stay tuned for additional content in this series.

Understanding X10

The X10 protocol is a fairly primitive tool for transmitting data over power lines. Perhaps because it is fairly primitive, it's also fairly robust, although a good line filter or surge suppressor can probably defeat it. The X10 data transfer rate is far too slow for networking, but it's pretty good for turning things on and off remotely.

To send X10 signals, you need something that plugs into an electrical outlet and provides a computer interface. These exist in both USB and RS232 serial forms; I went with the serial version. They run about US$30, and can be obtained from several vendors; I went with Smarthome's 1132B model (see Related topics for a link). The actual hardware gizmo is just a transceiver: it picks up X10 signals and sends them over the serial port, and it picks up serial signals and sends them out over the wire. The signals are broadcast; every device receives them. To keep this from being too much like cheap slapstick humor, though, each signal contains codes indicating which device (or devices) it's being sent to.

In general, a signal contains a house code, a unit code, and a function code. House codes are, as the name suggests, used for broad groupings of devices; there are 16 of them, called A through P. A typical X10 controller unit might have four rocker switches, corresponding to units 1-4, and a dial indicating which house code to use. Unless you're using a lot of devices, it's most likely that you'll use a single house code for everything. Unit codes, ranging from 1-16, denote specific devices. Some older hardware doesn't support unit codes outside the 1-8 range; likewise, some older devices support house codes A-H only.

Many devices have some kind of dial or set of switches to determine house and unit codes. Some newer devices are programmable electronically; more on this later.

Hardware and hookups

Serial

The hardware is stunningly easy to hook up. The device I got is a wall-socket attachment that looks a bit like a transformer for some portable electronics, with an RJ45 port on the bottom carrying various signals. It comes with a cable to provide a standard 9-pin serial port connection. No flow control, 9600, 8N1. Very standard.

Access to serial ports on Linux® is fairly straightforward. The default arrangement is to restrict access to members of group uucp; I added my account to this group, logged out, and logged back in.

Initializing the port is fairly trivial. The code fragment in Listing 1, lovingly extracted from the sample program, shows the initializations needed:

Listing 1. Opening a serial port and setting parameters

The dev variable holds the path to the device; by default, it's /dev/ttyS0. (The built-in serial port on the old Gateway laptop I've been using for these projects is COM1, or ttyS0. I wrote about the laptop in the first installment in this series.) The main routine allows dev to be overridden; another likely value would be /dev/ttyUSB0, which is the serial port on the USB port replicator.

The tcgetattr/tcsetattr operations, from termios.h, allow the device's settings to be altered. It is almost always best to get the old settings and modify them rather than trying to fully populate a termios structure yourself. The essential rule is to set any flags you want and clear any flags you don't want. Leave everything else alone.

Sometimes there are exceptions to this rule. Listing 2 shows my first code fragment:

Listing 2. A failed attempt to configure character size

This seemingly reasonable code set the character size to 5 bits. The reason? CS6 is 0x10, CS7 is 0x20, and CS8 is 0x30. Setting CS8, then clearing CS6 and CS7, leaves you with 0x00 -- the value for CS5. Clearing the CSIZE bits, then setting the value you want explicitly, works.

Protocol

The X10 protocol is frankly surreal. The RS232 gizmo has a protocol for describing X10 messages, which is even weirder. A message is 5 bytes; 0x63 ('send X10 command'), house code, unit code, function code, and repeat count. All of these are looked up in tables. For instance, repeat codes (indicating repeating a signal; used with dimmers, for instance) are sent as the letters A through O, with A representing 1, B representing 2, and so on. With very few exceptions, such as the 'send command' byte, every value sent is in the range from 0x40 to 0x5F.

Trying to find patterns in the protocol seems particularly interesting. The X10 hex codes are mostly in the range 0x00-1F; it seems as though the RS232 protocol probably just adds 0x40 to them to avoid control characters. However, X10 codes don't follow any kind of pattern that would be obvious to the untrained eye. The first few unit codes, representing 1-4, are 0x0C, 0x1C, 0x04, and 0x14. (Even unit codes always have 0x10 set.) All unit codes are even numbers; all command codes are odd numbers.

The protocol turns out to make some use of this. Some commands, such as 'all units off,' don't need or use a unit code. For these commands, the unit code is omitted. Not, as you might think, set to zero; just omitted. The order becomes 0x63, house code, function code, 0x00, repeat count. But since all unit codes are even and all function codes are odd, this isn't ambiguous.

Designing a software interface

While the code to drive X10 is not particularly complicated, duplicating it all over would be obnoxious. The UNIX® solution is a utility program that can send X10 commands from the command line. Everything else can run the utility program as needed. Once the utility program is done and debugged, it might make sense to change the program's group to uucp and give it the setgid bit so that access to the X10 hardware can be restricted.

The order in which X10 commands are sent can be disambiguated when working with codes, but it would be annoying to parse. Instead of trying to figure out which part each argument is, I wrote the utility to take the command code, house code, and then unit code. If the specified command doesn't require a unit, no unit is read. Commands, house codes, and units are looked up in tables; the user specifies them symbolically. For instance, the command 'x10 on a 1' would turn on unit 1 in house A. The -h option, or putting 'help' in a field, lists available codes along with the raw codes transmitted to the X10 adapter.

My initial design ignores repeat codes, because I haven't seen a particular need for them. The X10 protocol is slow enough that the overhead of multiple invocations of the program are not a significant loss of efficiency.

The structure I used for code lookups associates a string with a number. I could have added a 'flags' field, but I didn't think of it until I'd already written the interface, so I just tacked flags on as bits outside the unsigned character range. There turns out to be only one flag, SUPPRESS_UNIT, used with commands that don't require a unit. The special command none is used to send messages with no command; some programmable X10 hardware can use these messages to configure house and unit settings.

Extending the software

Having a program that can turn various devices on and off is neat, but it's not all that flexible; a few things would improve this situation.

First, some way to give symbolic names to devices would be really handy. During testing, I was using a grand total of three devices (two of them I had lying around my house already), and a couple of times I forgot which one was which. The reasonable thing to do would be to create a configuration file. I selected a traditional and fairly typical configuration file format; each line has three whitespace-delimited fields, containing the device's symbolic name, house code, and unit code. Lines starting with # are treated as comments. For instance, the file in Listing 3 describes a few devices:

N64 Controller Serial Protocol Rs232
Listing 3. Mapping device names to house and unit codes

The house and unit codes are looked up the same way that they would be from the command line.

Support for dimmers would be another likely feature; I haven't implemented it, but the protocol is there. One caveat: Don't send a whole lot of bright/dim messages at once; the interface is too slow.

Providing a Web interface

Remote access to your lights is an obvious convenience feature, but it's not really that useful most of the time, and a Web page makes a poor choice for it. Web page usability has come along way, but it has not in general caught up to the simplicity of the conventional light switch. This doesn't mean a Web interface can't be useful. Appliances are a better bet; the ability to turn on the coffeemaker from another room is sort of useful. If you get in the habit of putting grounds and water in the coffeemaker, then you can click a button and start coffee.

Another possible feature is configuring new X10 devices. The lamp controller I got for this column turns out to come preset as device A1, with no switches or knobs. To program it, you plug it in, then send three X10 messages within 30 seconds with a specific house and unit code; it programs itself to that house and unit code. This is an excellent thing to do from a trivial Web interface, even though it's not a task you perform very often. The following script ought to work fine:

Listing 4. Set a device's house and unit code

Call with the house and unit code, or device name from x10.conf, and it will program a device.

Using cron for scheduled tasks

Using cron to schedule certain things, such as turning on lights, is straightforward. If you're trying to make your house look 'lived-in' while out on vacation by turning on lights at night, it's not so good to have them come on at exactly the same time every night. What you need is a bit of randomization. The following trivial script performs an action within a little less than an hour:

Listing 5. Delay by under an hour

Save this script as /usr/local/bin/soonish, and you can use cron entrieslike the following:

Listing 6. Turning a lamp on during the evening

Summary and accounting

This month, I went over budget on shipping (see 'Linux on board: Breathe new life into an old machine' for my motivation and constraints for writing this series). The total cost of the X10 transceiver, a lamp module, and documentation was US$49.97. Unfortunately, shipping was about US$8 more. It turns out that the documentation was worthless anyway; not only can you download better documentation, but for reasons I can't explain, the documentation I was sent was five identical books full of technical information about the X10 protocol, with no reference at all to what signals to send to the transceiver. So, I could have saved the five dollars I was charged for the 'documentation.'

In the long run, having more than one module to control would be very useful, but they cost money. And it turns out that I could have come in under budget if I'd been paying attention; I have two X10 modules I picked up in 1997, which would have worked as well as the new one.

Downloadable resources

Related topics

  • 'Linux on board: Breathe new life into an old machine' (developerWorks, June 2005), Peter's first installment in this series, reveals his motivation and constraints in writing this series.
  • Read all installments in this Linux on board series on developerWorks.
  • Smarthome sells a variety of X10 hardware; they also provide downloadable manuals for their hardware. The page for the 1132b, the module Peter got, has links to various documentation and specs.
  • Dan Lanciani has a lot of support code providing more sophisticated interfaces to X10 hardware.
  • With IBM trial software, available for download directly from developerWorks, build your next development project on Linux.

Comments

Sign in or register to add and subscribe to comments.