NETMF Joystick Shield Driver

Adding a joystick to your NETMF device is easy with the Sparkfun joystick shield and the following code.

/*
Copyright 2011 William Stacey
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
*/
using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace RobotJoystickRemote
{
    /// <summary>
    /// Joystick class to control Sparkfun's Joystick Shield kit.
    /// http://www.sparkfun.com/products/9760
    /// This class uses the GHI Button class found here:
    /// http://www.ghielectronics.com/downloads/FEZ/Component/FEZ_Components_Button.cs
    /// </summary>
    public class JoystickShield
    {
        public event FEZ_Components.Button.ButtonPressEventHandler UpClick;
        public event FEZ_Components.Button.ButtonPressEventHandler DownClick;
        public event FEZ_Components.Button.ButtonPressEventHandler LeftClick;
        public event FEZ_Components.Button.ButtonPressEventHandler RightClick;
        public event FEZ_Components.Button.ButtonPressEventHandler JoystickClick;

        AnalogIn xPin;
        AnalogIn yPin;
        FEZ_Components.Button joyClick;
        FEZ_Components.Button up;
        FEZ_Components.Button down;
        FEZ_Components.Button left;
        FEZ_Components.Button right;

        public JoystickShield()
        {
            // Hookup Joystick.
            xPin = new AnalogIn(AnalogIn.Pin.Ain0);
            yPin = new AnalogIn(AnalogIn.Pin.Ain1);
            xPin.SetLinearScale(-100, 101);
            yPin.SetLinearScale(-100, 101);
            joyClick = new FEZ_Components.Button(FEZ_Pin.Interrupt.Di2);
            joyClick.ButtonPressEvent += (FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state) =>
            {
                var ev = JoystickClick;
                if (ev != null)
                    ev(pin, state);
            };

            // Hookup push buttons.
            up = new FEZ_Components.Button(FEZ_Pin.Interrupt.Di4);
            down = new FEZ_Components.Button(FEZ_Pin.Interrupt.Di5);
            left = new FEZ_Components.Button(FEZ_Pin.Interrupt.Di6);
            right = new FEZ_Components.Button(FEZ_Pin.Interrupt.Di3);
            up.ButtonPressEvent += (FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state) =>
            {
                var ev = UpClick;
                if (ev != null)
                    ev(pin, state);
            };
            down.ButtonPressEvent += (FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state) =>
            {
                var ev = DownClick;
                if (ev != null)
                    ev(pin, state);
            };
            left.ButtonPressEvent += (FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state) =>
            {
                var ev = LeftClick;
                if (ev != null)
                    ev(pin, state);
            };
            right.ButtonPressEvent += (FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state) =>
            {
                var ev = RightClick;
                if (ev != null)
                    ev(pin, state);
            };
        }

        /// <summary>
        /// Gets joystick X position.
        /// </summary>
        public int X
        {
            get { return xPin.Read(); }
        }

        /// <summary>
        /// Gets joystick Y position.
        /// </summary>
        public int Y
        {
            get { return yPin.Read(); }
        }

        /// <summary>
        /// Sets the X and Y scale of the joystick.
        /// Default scale is -100 minimum to 101 maximum. Max scale is non-inclusive.
        /// </summary>
        public void SetScale(int minX=-100, int maxX=101, int minY=-100, int maxY=101)
        {
            xPin.SetLinearScale(minX, maxX);
            yPin.SetLinearScale(minY, maxY);
        }

        /// <summary>
        /// Gets current X and Y position of joystick.
        /// </summary>
        public JoyPosition Position
        {
            get
            {
                JoyPosition jp = new JoyPosition();
                jp.X = xPin.Read();
                jp.Y = yPin.Read();
                return jp;
            }
        }
    }

    public struct JoyPosition
    {
        public int X;
        public int Y;
        public override string ToString()
        {
            return "X: " + X + " Y: " + Y;
        }
    }
}

Have fun.

Advertisements
This entry was posted in .Net Micro Framework, C#, FEZ, Robotics and tagged , , , , , . Bookmark the permalink.