chipKIT® Development Platform

Inspired by Arduino™

Simple 4-Wire Touch Library

Created Mon, 04 Jun 2012 22:00:46 +0000 by BoH_Havoc


BoH_Havoc

Mon, 04 Jun 2012 22:00:46 +0000

Thought i should share this. It's not the greatest library in the world, but at least it works and will get you on the right track when dealing with touchpanels. ;)

TouchpanelPic32.h

/*

Touchpanel Library by Wolfgang "BoH_Havoc" Reichardt

for chipKIT uno32/max32


Touchpanel with touchsensitiv side facing the viewer

          [T]OP
      ------------
      |           |
[L]EFT|           |[R]IGHT
      |           |
      |           |
      ------------
        ||||[B]OTTOM
        ||||
        ||||
        TRBL    
  

Create a Touchpanel instance by calling
TouchpanelPic32(int inPinLeft, int inPinRight, int inPinBottom, int inPinTop, int inMinValue, int inMaxValue)
	inPinLeft, inPinRight, inPinBottom, inPinTop = analog input pins for 4-wire touchpad
	inMinValue, inMaxValue = minimum/maximum xy-value at which to return "true" for a touch (150 & 800 is a good starting point)
	
Call touchpanel.GetTouch() 3 times (with a delay of min. 5 in between). If touchpanel.GetTouch() returns true, there was a valid touch.
You can then access the touchposition by touchpanel.x and touchpanel.y

Example:

#include <TouchpanelPic32.h>
TouchpanelPic32 touchpanel(A0,A2,A1,A3, 150, 800);

void setup()
{
  Serial.begin(9600);
}

void loop(){
  
  if( touchpanel.GetTouch() )
  {
    Serial.print(touchpanel.x,DEC);   
    Serial.print(",");     
    Serial.println(touchpanel.y,DEC); 
  }
  
  delay (5);
}

*/

#ifndef TouchpanelPic32_h
#define TouchpanelPic32_h

#include "WProgram.h"

class TouchpanelPic32
{
  public:
	TouchpanelPic32(int inPinLeft, int inPinRight, int inPinBottom, int inPinTop, int inMinValue, int inMaxValue);
	boolean GetTouch();
	int x;
	int y;
  private:
	int touchPhase;
	int minValue, maxValue;
	int pinLeft, pinRight, pinBottom, pinTop;
};

#endif

TouchpanelPic32.cpp

#include "TouchpanelPic32.h"

TouchpanelPic32::TouchpanelPic32(int inPinLeft, int inPinRight, int inPinBottom, int inPinTop, int inMinValue, int inMaxValue)
{
   x = 0;
   y = 0;
   touchPhase = 1;
   minValue = inMinValue;
   maxValue = inMaxValue;
   
   pinLeft = inPinLeft;
   pinRight = inPinRight;
   pinBottom = inPinBottom;
   pinTop = inPinTop;
}

boolean TouchpanelPic32::GetTouch()
{
   
   if(touchPhase == 1)
   {
      //read X
      pinMode(pinRight, INPUT);
      pinMode(pinLeft, INPUT);
      x = analogRead(pinLeft);
      //prepare Y for next read
      pinMode(pinRight, OUTPUT);
      pinMode(pinLeft, OUTPUT);
      analogWrite(pinLeft, 0);
      analogWrite(pinRight, 255);
      
      touchPhase++;
      return false;
   }
   if(touchPhase == 2) 
   {
      //read Y
      pinMode(pinBottom, INPUT);
      pinMode(pinTop, INPUT);
      y = analogRead(pinTop);
      //prepare X for next read
      pinMode(pinBottom, OUTPUT);
      pinMode(pinTop, OUTPUT);
      analogWrite(pinBottom, 0);
      analogWrite(pinTop, 255);

      touchPhase++;
      return false;
   }
   
   
   touchPhase = 1;
   if(
      (x > minValue && y > minValue)
      && (x < maxValue && y < maxValue) 
      )
   {
      //touch detected
      return true;
   }   
   return false;
}