Main Site Documentation

Problem finding IO72 and more


#1

Hello,

I’m using the FEZ Cobra board with the EMX module

I’m writing my own touchscreen driver, for this I need IO72 / IO73 and AD0 / AD1.

But when i use the code:

static OutputPort xMin = new OutputPort((Cpu.Pin)FEZ_Pin.Digital...;

i cannot find IO72 or IO73??

This is also for the AD0 and AD1, how can i solve this??

Thanks already


#2

just do this

static OutputPort xMin = new OutputPort((Cpu.Pin)72, ....);

#3

You can also include this library:

GHIElectronics.NETMF.Hardware.EMX.dll

ans use this:

GHIElectronics.NETMF.Hardware.EMX.Pin.IO72

AD0 == IO8
AD1 == IO5


#4

I once started to write a touchscreen driver but didn’t finish it, it might get you started:


using System;
using System.Threading;
using GHIElectronics.NETMF.Hardware;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Math = System.Math;

namespace CobraTouchTest
{
    public delegate void TouchEvent(int x, int y);

    public class TouchDriver : IDisposable
    {
        #region Consts

        private const int RES_PLATE_X = 602;
        private const int RES_PLATE_Y = 377;
        private const int ADC_BITS = 10;
        private const int MIN_Z_VALUE = 40;
        private const int MAX_PRESSURE_RES = 1000;
        private const int IDLE_DELAY_MS = 40;
        private const int TOUCH_DELAY_MS = 20;

        #endregion

        #region Declarations

        private int resPlateX;
        private int resPlateY;
        private int adcBits;

        private Thread thread;
        private ManualResetEvent terminate = new ManualResetEvent(false);
        private bool rawMode = false;

        #endregion

        #region Construction / destruction

        public TouchDriver()
            : this(RES_PLATE_X, RES_PLATE_Y, ADC_BITS)
        {
        }

        public TouchDriver(int resPlateX, int resPlateY, int maxAdc)
        {
            this.resPlateX = resPlateX;
            this.resPlateY = resPlateY;
            this.adcBits = maxAdc;

            thread = new Thread(new ThreadStart(TouchThread));
            thread.Priority = ThreadPriority.AboveNormal;
            thread.Start();
        }

        ~TouchDriver()
        {
            Dispose();
        }

        public void Dispose()
        {
            if (thread != null)
            {
                terminate.Set();
                thread.Join();
                thread = null;
            }
        }

        #endregion

        #region Thread proc

        private void TouchThread()
        {
            int x = 0, y = 0, z, pressure;
            int prevX = -1, prevY = -1;
            int maxAdc = (1 << adcBits) - 1;

            while (!terminate.WaitOne(IDLE_DELAY_MS, false))
            {
                while ((z = ReadZ()) > MIN_Z_VALUE)
                {
                    x = ReadX();
                    y = ReadY();

                    // calculate touch resistance
                    pressure = (x * resPlateX) / z - ((x * resPlateX) - ((maxAdc - y) * RES_PLATE_Y)) / maxAdc;

                    Debug.Print("X=" + x.ToString() + " Y=" + y.ToString() + " P=" + pressure.ToString());

                    if (pressure < MAX_PRESSURE_RES)
                    {
                        if (prevX < 0)
                            OnPenDown(x, y);
                        else
                        {
                            if ((Math.Abs(x - prevX) > 3) || (Math.Abs(y - prevY) > 3))
                                OnPenMove(x, y);
                        }

                        prevX = x;
                        prevY = y;
                    }

                    if (terminate.WaitOne(TOUCH_DELAY_MS, false))
                        break;
                }

                if (prevX >= 0)
                {
                    OnPenUp(prevX, prevY);
                    prevX = prevY = -1;
                }
            }
        }

        #endregion

        #region Private methods

        private int ReadX()
        {
            using (AnalogIn yu = new AnalogIn((AnalogIn.Pin)1))
            {
                using (OutputPort xl = new OutputPort((Cpu.Pin)8, true))
                using (OutputPort xr = new OutputPort((Cpu.Pin)72, false))
                {
                    return yu.Read();
                }
            }
        }

        private int ReadY()
        {
            using (AnalogIn xl = new AnalogIn((AnalogIn.Pin)0))
            {
                using (OutputPort yu = new OutputPort((Cpu.Pin)5, true))
                using (OutputPort yd = new OutputPort((Cpu.Pin)73, false))
                {
                    return xl.Read();
                }
            }
        }

        private int ReadZ()
        {
            using (AnalogIn xl = new AnalogIn((AnalogIn.Pin)0))
            {
                using (OutputPort xr = new OutputPort((Cpu.Pin)72, false))
                using (OutputPort yu = new OutputPort((Cpu.Pin)73, true))
                {
                    return xl.Read();
                }
            }
        }

        #endregion

        #region Event

        public event TouchEvent PenDown;
        private void OnPenDown(int x, int y)
        {
            Debug.Print("OnPenDown");
            if (PenDown != null)
                PenDown(x, y);
        }

        public event TouchEvent PenUp;
        private void OnPenUp(int x, int y)
        {
            Debug.Print("OnPenUp");
            if (PenUp != null)
                PenUp(x, y);
        }

        public event TouchEvent PenMove;
        private void OnPenMove(int x, int y)
        {
            Debug.Print("OnPenMove"); 
            if (PenMove != null)
                PenMove(x, y);
        }

        #endregion

        #region Properties

        public bool RawMode
        {
            get { return rawMode; }
            set { rawMode = value; }
        }
        
        #endregion
    }
}

Note that I removed the resistors from the 4.3inch panel that are in parrallel with the touchpanel signals, to get the z axis working.


#5

Thanks for the reply, I will try this!


#6

Hello,

I’m busy with you anwser, but when I use the analog input a get an error:

An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.NETMF.Hardware.dll

The code:

 //Read x value for touchscreen
        static private int TouchScreenReadX()
        {
            AnalogIn x = new AnalogIn((AnalogIn.Pin)1);
            x.SetLinearScale(0, 1023);
            
            int _x = x.Read();
            
            return _x;
        }

What is wrong?!


#7

I was forget to tell you that i use TristatePort to switch betwoon output/inpt


#8

The problem is you can only assign a pin once. So you’re stuck with that enormous overhead of creating and disposing objects, unless you use rlp.


#9

Hello,

Thanks to Wouter for piece of driver code. I have modified it adopted to my EMX custom board which uses different pins for touch panel. But I have not found any example or explantions on the net to hook this driver to touch system. Is it possible?


#10

Glide library has an easy way to take user input. An example is provided


#11

Thanks Gus, I will try…