Plain NETMF and CP7 display?

Does anyone have a non-Gadgeteer code sample for working with the CP7 display?

I see Steven’s LCD configuration in Codeshare (http://www.ghielectronics.com/community/codeshare/entry/519), but I’m a little fuzzy on where LCDController resides at.

Why multiple posts? :slight_smile: we will answer other topic

The two posts are related but aren’t exactly the same. The Codeshare post is specific to that snippet and asks a very specific question about the example to keep it on-topic. This one is more general, and I was hoping to solicit some examples beyond just LCD configuration in this thread.

Kinda like the difference between asking, “Do FEZ monkeys eat bananas?” vs “What kinds of foods do FEZ monkeys eat?”

One question is more open-ended. :slight_smile:

using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace Skewworks.Gadgeteer
{

    #region Event Delegates

    public delegate void OnScreenReleased();
    public delegate void OnGestureDetected(CP7NETMF.Gesture_ID e);
    public delegate void OnHomeButtonPressed();
    public delegate void OnBackButtonPressed();
    public delegate void OnMenuButtonPressed();
    public delegate void OnScreenPressed(CP7NETMF.TouchStatus e);

    #endregion

    [Serializable]
    public class CP7NETMF : MarshalByRefObject
    {

        #region Classes

        /// <summary>
        /// A class that is responisble for holding information of the current touches on the screen
        /// </summary>
        public class TouchStatus
        {
            /// <summary>
            /// An array of positions, one for each detected touch point
            /// </summary>
            public Finger[] touchPos;

            /// <summary>
            /// Number of current touches
            /// </summary>
            public int numTouches;

            /// <summary>
            /// Constructor
            /// </summary>
            public TouchStatus()
            {
                touchPos = new Finger[5];
            }
        }

        #endregion

        #region Enumerations

        /// <summary>
        /// An enum to describe the possible gestures
        /// </summary>
        public enum Gesture_ID
        {
            /// <summary>
            /// A move up gesture
            /// </summary>
            Move_Up = 0x10,

            /// <summary>
            /// A move left gesture
            /// </summary>
            Move_Left = 0x14,

            /// <summary>
            /// A move down gesture
            /// </summary>
            Move_Down = 0x18,

            /// <summary>
            /// A move right gesture
            /// </summary>
            Move_Right = 0x1C,

            /// <summary>
            /// A zoom in gesture
            /// </summary>
            Zoom_In = 0x48,

            /// <summary>
            /// A zoom out gesture
            /// </summary>
            Zoom_Out = 0x49,

            /// <summary>
            /// No gesture detected
            /// </summary>
            No_Gesture = 0x00
        };

        #endregion

        #region Structures

        /// Structure that represents a single position
        /// </summary>
        public struct Finger
        {
            /// <summary>
            /// X coordinate of the touch position
            /// </summary>
            public int xPos;

            /// <summary>
            /// Y coordinate of the touch position
            /// </summary>
            public int yPos;

            /// <summary>
            /// Determines if this touch is currently in use.
            /// True: this touch is currently registered with the screen
            /// False: this touch is not currently registered with the screen
            /// </summary>
            public bool bActive;

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="x">X coordinate</param>
            /// <param name="y">Y coordinate</param>
            /// <param name="active">If the position is active</param>
            public Finger(int x, int y, bool active)
            {
                this.xPos = x;
                this.yPos = y;
                this.bActive = active;
            }
        }

        #endregion

        #region Variables

        I2CDevice _cp7I2C;
        I2CDevice.Configuration _cp7cfg;
        InterruptPort _cp7IP;
        private bool _bBackLightOn = true;
        bool bSentReleased = false;
        
        #endregion

        #region Constructor

        public CP7NETMF()
        {
            _cp7cfg = new I2CDevice.Configuration(0x38, 400);
            _cp7I2C = new I2CDevice(_cp7cfg);
            _cp7IP = new InterruptPort((Cpu.Pin)25, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            _cp7IP.OnInterrupt += new NativeEventHandler(_cp7IP_OnInterrupt);
        }

        #endregion

        #region Events

        public event OnScreenReleased ScreenReleased;
        protected virtual void OnScreenReleased()
        {
            if (ScreenReleased != null)
                ScreenReleased();
        }

        public event OnGestureDetected GestureDetected;
        protected virtual void OnGestureDetected(CP7NETMF.Gesture_ID e)
        {
            if (GestureDetected != null)
                GestureDetected(e);
        }

        public event OnScreenPressed ScreenPressed;
        protected virtual void OnScreenPressed(CP7NETMF.TouchStatus e)
        {
            if (ScreenPressed != null)
                ScreenPressed(e);
        }

        public event OnHomeButtonPressed HomeButtonPressed;
        protected virtual void OnHomeButtonPressed()
        {
            if (HomeButtonPressed != null)
                HomeButtonPressed();
        }

        public event OnBackButtonPressed BackButtonPressed;
        protected virtual void OnBackButtonPressed()
        {
            if (BackButtonPressed != null)
                BackButtonPressed();
        }

        public event OnMenuButtonPressed MenuButtonPressed;
        protected virtual void OnMenuButtonPressed()
        {
            if (MenuButtonPressed != null)
                MenuButtonPressed();
        }


        #endregion

        #region Private Methods

        private void _cp7IP_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            int numberOfFingers = (ReadRegister(0x02) & 0xF);

            if (numberOfFingers == 0)
            {
                if (!bSentReleased)
                {
                    OnScreenReleased();
                    bSentReleased = true;
                }

                return;
            }

            bSentReleased = false;

            int gesture = (ReadRegister(0x01) & 0xFF);

            if (gesture != (int)Gesture_ID.No_Gesture)
            {
                OnGestureDetected((Gesture_ID)gesture);
                return;
            }

            TouchStatus ts = new TouchStatus();

            ts.numTouches = numberOfFingers;

            for (int i = 0; i < 5; i++)
            {
                if (i < numberOfFingers)
                {
                    int x = ((ReadRegister((byte)(3 + i * 6)) & 0xF) << 8) + ReadRegister((byte)(4 + i * 6));
                    int y = ((ReadRegister((byte)(5 + i * 6)) & 0xF) << 8) + ReadRegister((byte)(6 + i * 6));

                    ts.touchPos[i] = new Finger(x, y, true);

                    //////////////////////////////////////////////////////////////////////
                    // HEY LISTEN
                    // DO THE BUTTON THINGS RIGHT HERE
                    /////////////////////////////////////////////////////////////////////
                    // Check to see if a user has used one of the "Android" buttons
                    if (x > 800)
                    {
                        if (y >= 0 && y <= 50)
                            OnHomeButtonPressed();
                        if (y >= 100 && y <= 150)
                            OnMenuButtonPressed();
                        else if (y >= 200 && y <= 250)
                            OnBackButtonPressed();
                    }
                }
                else
                    ts.touchPos[i] = new Finger(-1, -1, false);

            }

            OnScreenPressed(ts);
        }

        private byte ReadRegister(byte Address)
        {
            I2CDevice.I2CTransaction[] Actions = new I2CDevice.I2CTransaction[2];

            // Create Write Buffer (1 byte)
            byte[] RegisterAddress = new byte[1] { Address };
            Actions[0] = I2CDevice.CreateWriteTransaction(RegisterAddress);

            // Create Read Buffer
            byte[] RegisterValue = new byte[1];
            Actions[1] = I2CDevice.CreateReadTransaction(RegisterValue);

            if (_cp7I2C.Execute(Actions, 500) == 0)
                Debug.Print("I2C Failed");

            return RegisterValue[0];
        }



        #endregion

    }
}

2 Likes

@ Skewworks - Thanks! Very informative.