New 3D renderer in Cogwheel

Monday, 10th August 2009

I have written a new 3D-compatible renderer for Cogwheel. It holds two textures, one for each eye, and uses one of a number of different effect file techniques to mix the two views.

Row-interleaved 3D

Based on the interlacing work from the previous entry, the first technique is one that uses interleaved rows. I'm not really sure if there's a good way to convert texture coordinates into device coordinates, so am passing in the viewport height as a parameter and hoping that floating point errors don't trip me up (they haven't, yet).

float4 RowInterleavedPixelShader(VertexPositionTexture input) : COLOR0 {
	float row = input.Texture.y * ViewportHeight * 0.5f;
	if (abs(round(row) - row) < 0.1f) {
		return tex2D(LeftEyeSampler, input.Texture);
	} else {
		return tex2D(RightEyeSampler, input.Texture);
	}
}

Alternate pixel centres may also pose a problem in the future. If anyone had any recommendations, suggestions or warnings on the way I'm detecting the evenness or oddness of a particular "scanline" then I'd appreciate hearing them!

Colunn-interleaved 3D Chequerboard-interleaved 3D

I have also added two other interleaving modes; one in columns and another in a chequerboard pattern. I included these two as I've seen that some 3D LCD panels use a column interleaving pattern (I suppose that with a lenticular lens in front of such a panel you may not even need 3D glasses) and apparently Sharp have displays that use the chequerboard pattern.

I have also taken advantage of pixel shaders to create colour and monochrome anaglyphs (previously calculated in software), though neither look as good as the above full-colour modes for shutter glasses or similar hardware.

There are a few issues I need to sort out first before I can release this; for example, there's no way to set whether the first row/column/pixel is for the left or right eye. More problematic is the removal of support for non power-of-two textures; the Master System's 256×192 display is fine, but the Game Gear's 160×144 display gets rounded up to 192 pixels wide (and yes, I know that's not a power of two) on my video card. I also mean to give Promit's SlimTune profiler a look to see if I can optimise some of the less efficient pieces of my code. The C# version of emu2413 is probably a good candidate, being a "dumb" translation from the original macro-heavy C.

Taking advantage of interlacing for 3D

Friday, 7th August 2009

To achieve smooth, glitch-free 3D in an ideal world, one would like to be able to alternate between left and right eye views every time the monitor refreshes. You could then use the monitor's vertical synchronisation pulse to alternate which eye shutter is currently open.

Relying on software is not so bad if you can guarantee that you will be able to keep up with the video hardware and output alternating frames without dropping any. This is pretty much impossible with today's complex operating systems running any number of background tasks that could interfere with your render loop at any moment.

Fortunately, video cards already have a mode that is guaranteed to output a different image every frame - interlaced scan. Rather than send each scanline (row) of the image every frame ("progressive scan"), it alternates between sending every even-numbered scanline and every odd-numbered scanline. This halves the vertical resolution, but allows you to double the refresh rate using the same amount of bandwidth.

Combining the left and right views into a single image.

We can take advantage of this scan mode to encode the left and right views into a single image. The view for the left eye is stored in the even-numbered scanlines and the view for the right eye is stored in the odd-numbered scanlines, as in the image above. When displayed on a monitor using interlaced scan, it appears as the following:

Simulated view of encoded 3D image on an interlaced scan monitor.

As the video card takes care of alternating which set of scanlines (or field) is displayed, the result is that the left and right views alternate perfectly in time with the monitor's refresh rate.

To test this, I've attached a counter IC to the vertical synchronisation pulse of a VGA monitor. The low bit of the counter toggles every time the screen refreshes, and this is used to select which eye shutter is open on the 3D glasses. The glasses are driven using the serial port adaptor described in the previous post.

Circuit to alternate eye shutter on vsync.

The result is perfectly stable 3D images. The circuit does not know which field, and hence which eye's view, is visible at any time - it just alternates left and right, which means that there is a 50% chance that the left and right views are flipped. This can be fixed by switching the circuit off then on again to try and resynchronise, but a better solution would be to add a switch to toggle the uncovered eye manually and to fix the synchronisation. As this would only need to be done once per session this isn't much of an issue! An alternative fix would be to add a switch to switch the left and right views in software.

The logic probe in the above photo is an integral part of the design - at least, I assume it is, as if I remove it the circuit doesn't work correctly! I assume there's a noise issue that's interfering with the circuit (none of the unused inputs on the counter chip are tied low) and the logic probe contains some noise suppression circuitry of its own.

The only real fly in the ointment here is video driver support for interlaced scan modes. My video card only supports 1920×1080 at 30i, 29i and 25i, and only if the primary monitor is an LCD. I can work around the problem by cloning the primary LCD to the CRT and setting both to 1920×1080 at 30i, but my LCD displays a warning message and makes a distressing noise and 30i, whilst faithful to the Master System's 30fps 3D, is unpleasantly flickery. It would be wonderful if I could drop the resolution down a bit and switch to 60i, but I can't see a way to do that with ATi's drivers.

3D LCD Shutter Glasses Experimentation

Wednesday, 5th August 2009

The Sega Master System supported 3D LCD shutter glasses to provide a more immersive (if somewhat flickery) playing experience. Having caught wind of an eBay member selling compatible glasses for $9 and being rather interested in stereoscopy I decided to experiment a little for myself.

The glasses are pretty simple; they consist of two LCD panels that can be "switched on" to block light from passing through to each eye. A 3.5mm stereo jack plug provides the electrical connection.

To display the 3D image you alternate between showing the image for the left eye and the right eye on the monitor, uncovering the corresponding eye immediately before the image appears on the monitor. This effectively halves the refresh rate (and results in fairly noticeable flicker when run at the standard NTSC 60Hz) and prevents the 3D glasses from working with displays that respond too slowly (eg LCD panels). I've had to dig out my old CRT monitor for this project. Even if my LCD did refresh quickly enough, its polarisation is perpendicular to the polarisation of the shutter glasses, meaning that no light can pass from the LCD through the glasses even when both eye LCD panels are switched off.

The adaptor I'm using is based on this circuit (I'm using the second variation with the variable resistor to fine-tune the driving frequency). The LCD panels require an AC voltage, and using a EOR gate as an oscillator allows the whole device to be constructed out of a single IC with a handful of external components. More importantly, being based on an existing and public design allows me to ensure that any work I do should be compatible with adaptors other people have built.

The DTR pin on the serial port supplies power to the circuit and the RTS pin is used to choose which LCD panel is switched on to cover an eye.

Test pattern seen through glasses
Test pattern seen through glasses

The above image displays a test pattern viewed through the glasses. The software alternates between clearing the screen to red with the left eye shutter open and clearing the screen to cyan with the right eye shutter open. The colours were deliberately chosen to match the colours of the common anaglyph glasses. As the colours are alternated very quickly (in the interests of avoiding a headache I used a refresh rate of 120Hz) the screen appears a light grey colour to the naked eye.

Most LCD shutter glasses appear to use some form of feedback from the video signal to synchronise the alternating of eyes. On a PC they could alternate every time the vsync signal appeared on the VGA port, on a TV they could open the correct eye shutter depending on the current field of the interlaced image that was being displayed. The Sega Master System's video chip can generate an interrupt when entering the vblank period which you can use to prepare the next frame and update the glasses. An adaptor connected to the PC's serial port has no such luck - I have not yet found a way to reliably synchronise the glasses to the refresh rate.

Poor synchronisation

Poor synchronisation (and even worse photography) results in images like the above, as seen through the left shutter. As the LCD shutters have been updated too late, some of the display intended for the right eye is seen through the left eye at the top of the display - the cyan band in this case.

So far, the best result I've had is to use Direct3DEx's DeviceEx, which provides a handy WaitForVBlank method. Less handy is the Vista requirement, as is the slight delay between returning from this function and updating the serial port, resulting in a flickering band near the top of the display as per the previous photograph. For the best results I need to set the presentation interval to "immediate", which compounds the issue with occasional tearing caused by the delay between WaitForVBlank returning and calling Present.

Switching the presentation interval back to "1" (tying the refresh rate of the render loop to that of the monitor) results in complete frames (no tearing or bands of the wrong colour/image), but the additional delay before presenting the next frame puts updating the LCD glasses out of sync by one frame. As the uncovered eye should alternate between subsequent frames one can simply uncover the "opposite" eye to uncover the correct image, but any dropped frames throw this out of sync and you get the occasional "inside out" view when the wrong eye is uncovered. Any background tasks on the PC kicking in could potentially cause a dropped frame. This is one reason that a VGA pass-through adaptor that automatically alternates the uncovered eye every frame wouldn't work, as it would get thrown out of sync by these dropped frames.

A demo compatible with the Sega 3D glasses
A demo compatible with the Sega 3D glasses, showing the images for each eye as stereo pair

The advantage of using an existing adaptor design makes me reluctant to pursue solutions that involve additional hardware to fix the problem. One possible solution I can think of would be an additional pass-through box that contains a simple latch that is clocked by the VGA vsync signal between the serial port and the glasses adaptor. You could then set the state of the glasses immediately before calling Present, safe in the knowledge that your signal will only get through to the adaptor box perfectly synchronised with the CRT's vertical refresh, assuming the CRT doesn't enter vblank between updating the serial port and calling Present. Not having to manually synchronise to vsync in software would remove the need for the Vista-only Direct3DEx too.

RC-5, NEC, JVC and Panasonic infrared codes

Wednesday, 3rd June 2009

I've rewritten the remote control signal decoding software to handle multiple protocols. As well as SIRCS, it now supports RC-5, NEC, JVC and two Panasonic codes (one "old" 11-bit code and one "new" 48-bit code). There's not much in the way of screenshots at the moment, other than a debug window that gets filled when keys are pressed:

NecCommand Address=27526, Command=5, Extended=True, Repeat=1
NecCommand Address=24, Command=87, Extended=False, Repeat=1
RC5Command Address=20, Command=53, Repeat=True, Repeat=1
NewPanasonicCommand OEM Device 1=2, OEM Device 2=32, Device=144, Sub-Device=0, Command=10, Repeat=1
RC5Command Address=8, Command=35, Repeat=True, Repeat=1
OldPanasonicCommand Address=0, Command=20, Repeat=1
SircsCommand Address=2362, Command=121, Length=20, Repeat=1
SircsCommand Address=7002, Command=84, Length=20, Repeat=1
NecCommand Address=64, Command=146, Extended=False, Repeat=1
NecCommand Address=81, Command=8, Extended=False, Repeat=1
JvcCommand Address=3, Command=23, Repeat=1

Gripping stuff, I'm sure you'll agree.

The C# source code for this can be downloaded here.

A keyring remote control (courtesy of Poundland) has highlighted one possible issue in handling repeating buttons. Rather than target any particular device, it will try and brute-force a response. For example, here's the result of pressing the power button once in one particular mode:

SircsCommand Address=1, Command=21, Length=12, Repeat=1
SircsCommand Address=1, Command=21, Length=12, Repeat=2
OldPanasonicCommand Address=0, Command=32, Repeat=1
NecCommand Address=32, Command=11, Extended=False, Repeat=1
NewPanasonicCommand OEM Device 1=2, OEM Device 2=32, Device=128, Sub-Device=0, Command=61, Repeat=1

That's four different protocols from one button. I suppose some sort of mapping from protocol-specific code to a string (so those five commands would be translated into five "power" strings) and comparing the time between signals to turn the input into something meaningful may help, but that would require an enormous database of known codes.

Remote controlling Windows the Sony way

Wednesday, 27th May 2009

It's been a while since I last posted, and unfortunately this post is to do with Sony remote controls again. rolleyes.gif

This time I'm attempting to use Sony (or compatible) remote controls to control software running on a Windows PC. I've recently been watching more films in PowerDVD, and some of the keyboard shortcuts (eg Ctrl+P for the menu) are a little difficult to hit in the dark and from a distance. I have a ready supply of universal remote controls as well as the PlayStation 2 DVD remote control, all of which work with the SIRCS protocol.

Serial port infra-red receiver built into an old TI GraphLink cable
Serial port infra-red receiver built into an old TI GraphLink cable

First up is the required hardware. This involves an infrared demodulator connected to a free serial port. I chose the serial port as .NET provides a way to handle pin change events and you do not need administrator rights to access it (as per the parallel port). I also had a broken Texas Instruments GraphLink cable that could be ripped apart to act as a case.

Infra-red receiver module schematic
Infra-red receiver module schematic

The circuit is pretty simple. Pin 4 (DTR) and 5 (GND) from the serial port form the power supply. DTR can be set to either +12V or -12V, so a rectifier diode is used to keep the input voltage above 0V. Following that is a reverse-biased zener diode and resistor to regulate the voltage below 5.1V. Finally, the output pin of the infra-red demodulator is connected to the input pin 8 (CTS) of the serial port.

Infra-red receiver module assembled on stripboard
Infra-red receiver module assembled on stripboard

The software handles the SerialPort.PinChanged event to time the length of input pulses. Once it detects a start bit (2.4mS) it starts decoding the rest of the command. When it's finished receiving a command it fires an event of its own, which the main software can react to.

using System;
using System.Diagnostics;
using System.IO.Ports;

namespace BeeDevelopment.Sircs {

    /// <summary>
    /// Represents a command sent by a SIRCS remote control.
    /// </summary>
    public struct SircsCommand : IEquatable<SircsCommand> {

        #region Properties

        private byte command;
        /// <summary>
        /// Gets or sets the command value.
        /// </summary>
        public byte Command {
            get { return this.command; }
            set { this.command = value; }
        }

        private short device;
        /// <summary>
        /// Gets or sets the device identifier.
        /// </summary>
        public short Device {
            get { return this.device; }
            set { this.device = value; }
        }

        private int length;
        /// <summary>
        /// Gets or sets the length of the command in bits.
        /// </summary>
        public int Length {
            get { return this.length; }
            set { this.length = value; }
        }

        #endregion

        #region Construction

        /// <summary>
        /// Creates an instance of a <see cref="SircsCommand"/> structure.
        /// </summary>
        /// <param name="command">The command value.</param>
        /// <param name="device">The device identifier.</param>
        /// <param name="length">The length of the command in bits.</param>
        public SircsCommand(byte command, short device, int length) {
            this.command = command;
            this.device = device;
            this.length = length;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Converts the <see cref="SircsCommand"/> into a string.
        /// </summary>
        /// <returns>A string representation of the <see cref="SircsCommand"/>.</returns>
        public override string ToString() {
            return string.Format("Command={0:X2}, Device={1:X4}, Length={2}", this.command, this.device, this.length);
        }

        /// <summary>
        /// Returns the hash code for this instance.
        /// </summary>
        /// <returns>The hash code for this instance.</returns>
        public override int GetHashCode() {
            return this.command ^ this.device ^ this.length;
        }

        /// <summary>
        /// Returns a value indicating whether this instance is equal to another <see cref="SircsCommand"/> instance.
        /// </summary>
        /// <param name="other">The instance to compare to this one for equality.</param>
        /// <returns>True if the instances are equal, false otherwise.</returns>
        public bool Equals(SircsCommand other) {
            return this.command == other.command && this.device == other.device && this.length == other.length;
        }

        /// <summary>
        /// Returns a value indicating whether this instance is equal to another <see cref="SircsCommand"/> instance.
        /// </summary>
        /// <param name="other">The instance to compare to this one for equality.</param>
        /// <returns>True if the instances are equal, false otherwise.</returns>
        public override bool Equals(object other) {
            return other != null && other is SircsCommand && ((SircsCommand)other).Equals(this);
        }

        #endregion

        #region Operators

        public static bool operator ==(SircsCommand a, SircsCommand b) { return a.Equals(b); }
        public static bool operator !=(SircsCommand a, SircsCommand b) { return !a.Equals(b); }

        #endregion

    }

    #region Events

    /// <summary>
    /// Represents the method that will handle the <c>SircsCommandReceived</c> event.
    /// </summary>
    /// <param name="sender">The object that fired the event.</param>
    /// <param name="e">Information about the event.</param>
    public delegate void SircsCommandReceivedEventHandler(object sender, SircsCommandReceivedEventArgs e);

    /// <summary>
    /// Provides data for the <c>SircsReceived.SircsCommandReceived</c> event.
    /// </summary>
    public class SircsCommandReceivedEventArgs : EventArgs {

        #region Properties

        /// <summary>
        /// Gets the <see cref="SircsCommand"/> that was received.
        /// </summary>
        public SircsCommand Command { get; private set; }

        /// <summary>
        /// Gets the number of times that the incoming command has been repeated when held.
        /// </summary>
        public int Repeat { get; private set; }

        #endregion

        #region Construction

        /// <summary>
        /// Creates a <see cref="SircsCommandReceivedEventArgs"/> instance.
        /// </summary>
        /// <param name="command">The <see cref="SircsCommand"/> that was recieved.</param>
        /// <param name="repeat">The number of times that the incoming command has been repeated when held.</param>
        public SircsCommandReceivedEventArgs(SircsCommand command, int repeat) {
            this.Command = command;
            this.Repeat = repeat;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Converts the <see cref="SircsCommandReceivedEventArgs"/> into a string.
        /// </summary>
        /// <returns>A string representation of the <see cref="SircsCommandReceivedEventArgs"/>.</returns>
        public override string ToString() {
            return string.Format("{0}, Repeat={1}", this.Command, this.Repeat);
        }

        #endregion
    }

    #endregion

    /// <summary>
    /// Provides a way to receive SIRCS commands from a simple receiver attached to a serial port.
    /// </summary>
    public class SircsReceiver : IDisposable {

        #region Constants

        /// <summary>
        /// The minimum time length for a start bit (nominally 2.4ms).
        /// </summary>
        private const double StartBitMinLength = 2.0E-3;

        /// <summary>
        /// Threshold time length between a "low" (0.6ms) and a "high" (1.2ms) bit.
        /// </summary>
        private const double DataBitLengthThreshold = 0.9E-3;

        /// <summary>
        /// The maximum time length between data bits. If this is exceeded, any data command transfer is cancelled.
        /// </summary>
        private const double IntraBitMaxLength = 0.8E-3;

        /// <summary>
        /// The maximum time length between repeating commands. Commands are supposed to repeat every 45ms.
        /// </summary>
        private const double RepeatCommandMaxLength = 120.0E-3;

        #endregion

        #region Private Fields

        /// <summary>
        /// The <see cref="SerialPort"/> that the receiver is connected to.
        /// </summary>
        private SerialPort Port = null;

        /// <summary>
        /// The last time that the pin state changed in ticks.
        /// </summary>
        private long LastPinChangedTime = 0;

        /// <summary>
        /// A <see cref="Stopwatch"/> instance used to time incoming bits.
        /// </summary>
        private Stopwatch BitTimer = null;

        /// <summary>
        /// Set to <c>true</c> when receiving a command, <c>false</c> otherwise.
        /// </summary>
        private bool ReceivingCommand = false;

        /// <summary>
        /// Counts the number of bits currently received.
        /// </summary>
        private int BitsReceived = 0;

        /// <summary>
        /// Stores the command as it gets built up.
        /// </summary>
        private uint Command = 0;

        /// <summary>
        /// Stores the last received command.
        /// </summary>
        private SircsCommand LastCommand = default(SircsCommand);

        /// <summary>
        /// Stores the number of times the received command has been repeated.
        /// </summary>
        private int LastCommandRepeatCount = 0;

        /// <summary>
        /// A <see cref="Stopwatch"/> instance used to time repeating commands.
        /// </summary>
        private Stopwatch RepeatTimer = null;

        #endregion

        #region Construction/Destruction

        /// <summary>
        /// Creates an instance of a <see cref="SircsReceiver"/> from a serial port name.
        /// </summary>
        /// <param name="portName">The name of the serial port the receiver is connected to.</param>
        public SircsReceiver(string portName) {

            // Set up the serial port.
            this.Port = new SerialPort(portName);
            this.Port.PinChanged += new SerialPinChangedEventHandler(PinChanged);

            // Open the port for access.
            this.Port.Open();
            this.Port.DtrEnable = true;
            this.Port.RtsEnable = true;

            // Get the timers running.
            this.BitTimer = new Stopwatch();
            this.BitTimer.Start();
            this.RepeatTimer = new Stopwatch();
            this.RepeatTimer.Start();
        }

        /// <summary>
        /// Releases the resources used by this <see cref="SircsReceiver"/> instance.
        /// </summary>
        public void Dispose() {
            if (this.Port != null) {
                this.Port.PinChanged -= new SerialPinChangedEventHandler(PinChanged);
                this.Port.Dispose();
                this.Port = null;
            }
        }

        ~SircsReceiver() {
            this.Dispose();
        }

        #endregion

        #region Events

        /// <summary>
        /// An event that is fired when a <see cref="SircsCommand"/> is received.
        /// </summary>
        public event SircsCommandReceivedEventHandler SircsCommandReceived;

        /// <summary>
        /// A method that is invoked when a <see cref="SircsCommand"/> is received.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnSircsCommandReceived(SircsCommandReceivedEventArgs e) {
            if (this.SircsCommandReceived != null) this.SircsCommandReceived(this, e);
        }

        #endregion

        #region SIRCS protocol handling

        void PinChanged(object sender, SerialPinChangedEventArgs e) {

            // Respond to changes on the CTS pin.
            if (e.EventType == SerialPinChange.CtsChanged) {

                // Quickly grab the current time and current CTS level.
                long CurrentPinChangedTime = this.BitTimer.ElapsedTicks;
                bool CurrentLevel = this.Port.CtsHolding;

                // Calculate the time elapsed.
                long DeltaTime = CurrentPinChangedTime - this.LastPinChangedTime;
                double SecondsElapsed = (double)DeltaTime / (double)Stopwatch.Frequency;
                this.LastPinChangedTime = CurrentPinChangedTime;

                if (CurrentLevel) { // If the current signal level is high, we may assume that we've just timed a low pulse.

                    // Have we received a start bit?
                    if (SecondsElapsed > SircsReceiver.StartBitMinLength) {
                        this.ReceivingCommand = true;
                        this.BitsReceived = 0;
                        this.Command = 0;
                    } else if (this.ReceivingCommand) {
                        // Process incoming bit.
                        this.Command >>= 1;
                        if (SecondsElapsed > SircsReceiver.DataBitLengthThreshold) {
                            this.Command |= unchecked((uint)(1 << 31));
                        }

                        // Have we received enough bits?
                        switch (++this.BitsReceived) {
                            case 12:
                            case 15:
                            case 20:
                                // We've received enough bits to handle the input as a received command.
                                // Check to see if there's any more data forthcoming.
                                long EndTime = CurrentPinChangedTime + (long)(Stopwatch.Frequency * SircsReceiver.IntraBitMaxLength);
                                while (BitTimer.ElapsedTicks < EndTime) {
                                    if (!(CurrentLevel = this.Port.CtsHolding)) break;
                                }
                                // The input is still high - there's no more data coming in; we've received a command.
                                if (CurrentLevel) {
                                    // Construct a struct to hold information about the recieved data.
                                    SircsCommand ReceivedCommand = new SircsCommand(
                                        (byte)((this.Command >> (32 - this.BitsReceived)) & 0x7F),
                                        (short)(this.Command >> ((32 + 7) - this.BitsReceived)),
                                        this.BitsReceived
                                    );
                                    
                                    // Reset the timer.
                                    this.ReceivingCommand = false;
                                    this.BitTimer.Reset();
                                    this.BitTimer.Start();
                                    this.LastPinChangedTime = 0;
                                    
                                    // Calculate the repeat count.

                                    // Quickly grab the current time and current CTS level.
                                    long RepeatTimeTicks = this.RepeatTimer.ElapsedTicks;
                                    this.RepeatTimer.Reset();
                                    this.RepeatTimer.Start();

                                    // Calculate the repeat time elapsed.
                                    double RepeatTimeSeconds = (double)RepeatTimeTicks / (double)Stopwatch.Frequency;

                                    // Is the command repeating?
                                    if (ReceivedCommand == this.LastCommand && RepeatTimeSeconds < SircsReceiver.RepeatCommandMaxLength) {
                                        ++this.LastCommandRepeatCount;
                                    } else {
                                        this.LastCommandRepeatCount = 1;
                                        this.LastCommand = ReceivedCommand;
                                    }
                                    
                                    // Fire the event.
                                    this.OnSircsCommandReceived(new SircsCommandReceivedEventArgs(ReceivedCommand, this.LastCommandRepeatCount));
                                }
                                break;
                        }
                    }
                } else { // If the current signal level is low, we may assume that we've just timed a high pulse.
                    // If a high pulse is too long, cancel any incoming commands.
                    if (SecondsElapsed > SircsReceiver.IntraBitMaxLength) {
                        this.ReceivingCommand = false;
                        this.BitTimer.Reset();
                        this.BitTimer.Start();
                        this.LastPinChangedTime = 0;
                    }
                }
            }
        }

        #endregion

    }
}

Currently, the software reacts to input events by running through a list of scripts, passing the command ID, device ID and command length (in bits) to each until one of them returns zero (ie, success) to indicate that it has processed the button.

Scripts list
Scripts list

The advantage to this method is that the end-user could customise the behaviour of the software to their own liking very easily. For example, here's the PowerDVD.js file from above, which allows me to control PowerDVD from a PlayStation 2 DVD remote control:

// Table of commands.
var Commands = [
    { Command : 0x00, Device : 0x093A, Length : 20, Shortcut : '1' },         // 1
    { Command : 0x01, Device : 0x093A, Length : 20, Shortcut : '2' },         // 2
    { Command : 0x02, Device : 0x093A, Length : 20, Shortcut : '3' },         // 3
    { Command : 0x03, Device : 0x093A, Length : 20, Shortcut : '4' },         // 4
    { Command : 0x04, Device : 0x093A, Length : 20, Shortcut : '5' },         // 5
    { Command : 0x05, Device : 0x093A, Length : 20, Shortcut : '6' },         // 6
    { Command : 0x06, Device : 0x093A, Length : 20, Shortcut : '7' },         // 7
    { Command : 0x07, Device : 0x093A, Length : 20, Shortcut : '8' },         // 8
    { Command : 0x08, Device : 0x093A, Length : 20, Shortcut : '9' },         // 9
    { Command : 0x09, Device : 0x093A, Length : 20, Shortcut : '0' },         // 0
    { Command : 0x0B, Device : 0x093A, Length : 20, Shortcut : '{ENTER}' },   // Enter
    { Command : 0x0E, Device : 0x093A, Length : 20, Shortcut : '{ESC}' },     // Return
    { Command : 0x1A, Device : 0x093A, Length : 20, Shortcut : 'lt' },        // Title
    { Command : 0x2A, Device : 0x093A, Length : 20, Shortcut : 'x' },         // A<->B
    { Command : 0x28, Device : 0x093A, Length : 20, Shortcut : 'd' },         // Time
    { Command : 0x2C, Device : 0x093A, Length : 20, Shortcut : '^r' },        // Repeat
    { Command : 0x30, Device : 0x093A, Length : 20, Shortcut : 'p' },         // Previous
    { Command : 0x31, Device : 0x093A, Length : 20, Shortcut : 'n' },         // Next
    { Command : 0x32, Device : 0x093A, Length : 20, Shortcut : '{ENTER}' },   // Play
    { Command : 0x33, Device : 0x093A, Length : 20, Shortcut : 'b' },         // Scan <<
    { Command : 0x34, Device : 0x093A, Length : 20, Shortcut : 'f' },         // Scan >>
    { Command : 0x38, Device : 0x093A, Length : 20, Shortcut : 's' },         // Stop
    { Command : 0x39, Device : 0x093A, Length : 20, Shortcut : ' ' },         // Pause
    { Command : 0x54, Device : 0x093A, Length : 20, Shortcut : 'z' },         // Display
    { Command : 0x60, Device : 0x093A, Length : 20, Shortcut : '^b' },        // Slow <<
    { Command : 0x61, Device : 0x093A, Length : 20, Shortcut : 't' },         // Slow >>
    { Command : 0x63, Device : 0x093A, Length : 20, Shortcut : 'u' },         // Subtitle
    { Command : 0x64, Device : 0x093A, Length : 20, Shortcut : 'h' },         // Audio
    { Command : 0x65, Device : 0x093A, Length : 20, Shortcut : 'a' },         // Angle
    { Command : 0x79, Device : 0x093A, Length : 20, Shortcut : '{UP}' },      // Up
    { Command : 0x7A, Device : 0x093A, Length : 20, Shortcut : '{DOWN}' },    // Down
    { Command : 0x7B, Device : 0x093A, Length : 20, Shortcut : '{LEFT}' },    // Left
    { Command : 0x7C, Device : 0x093A, Length : 20, Shortcut : '{RIGHT}' },   // Right
];

// Search for the matching command.
var Command = null;
for (var enumerator = new Enumerator(Commands); !enumerator.atEnd(); enumerator.moveNext()) {
    var TestCommand = enumerator.item();
    if (TestCommand.Command == WScript.Arguments(1) && TestCommand.Device == WScript.Arguments(2) && TestCommand.Length == WScript.Arguments(3)) {
        Command = TestCommand;
        break;
    }
}

// No command.
if (!Command) WScript.Quit(1);

// Find the PowerDVD process ID.
var PowerDvdId = null;
var WmiService = GetObject('winmgmts://./root/cimv2');
var Processes = WmiService.ExecQuery('Select ProcessId From Win32_Process Where Name="PowerDVD.exe"');
for (var enumerator = new Enumerator(Processes); !enumerator.atEnd(); enumerator.moveNext()) {
    PowerDvdId = enumerator.item().ProcessId;
    break;
}

// If we haven't found the process ID, quit with an error.
if (!PowerDvdId) WScript.Quit(1);

// Activate the PowerDVD instance.
var WshShell = new ActiveXObject('WScript.Shell');
WshShell.AppActivate(PowerDvdId);

// Send the shortcut keys.
WshShell.SendKeys(Command.Shortcut);

WScript.Quit(0);

Unfortunately, this method has quite a lot of overhead. This becomes a problem when you consider that commands are repeated every 45ms. Currently I avoid the issue by not allowing any keys to repeat, but some keys - such as the volume keys - would need to repeat when held.

I'm unsure as the best path to take. One idea that has crossed my mind would be to set up each remote control you were going to use beforehand (though I suppose I could build up a database of remote controls and bundle them with the software). You could then set whether each key should repeat or not, and attach a meaningful string to each button. This would also allow for more protocols to be supported other than SIRCS, and you could set it up so that the Play button on a Sony remote control generated the string "play" and passed that to the script(s) as well as the Play button on a Panasonic or Toshiba remote control rather than juggling control codes.

Page 17 of 53 113 14 15 16 17 18 19 20 2153

Older postsNewer postsLatest posts RSSSearchBrowse by dateIndexTags