Add new scene controller with touch display example.

Upgraded RF24 to latest version which supports SoftSPI. 
Added DigitalIO and UTFT libraries to support the new example.
Fix problem reported in RelayWithButtonActuator example.
This commit is contained in:
Henrik Ekblad
2014-10-17 00:01:55 +02:00
parent 27885fad9b
commit 2fd948fc66
276 changed files with 28573 additions and 4061 deletions
+262
View File
@@ -0,0 +1,262 @@
/*
UTouch.cpp - Arduino/chipKit library support for Color TFT LCD Touch screens
Copyright (C)2010-2014 Henning Karlsen. All right reserved
Basic functionality of this library are based on the demo-code provided by
ITead studio. You can find the latest version of the library at
http://www.henningkarlsen.com/electronics
If you make any modifications or improvements to the code, I would appreciate
that you share the code with me so that I might include it in the next release.
I can be contacted through http://www.henningkarlsen.com/electronics/contact.php
This library is free software; you can redistribute it and/or
modify it under the terms of the CC BY-NC-SA 3.0 license.
Please see the included documents for further information.
Commercial use of this library requires you to buy a license that
will allow commercial use. This includes using the library,
modified or not, as a tool to sell products.
The license applies to all part of the library including the
examples and tools supplied with the library.
*/
#include "UTouch.h"
#include "UTouchCD.h"
#if defined(__AVR__)
#include "hardware/avr/HW_AVR.inc"
#elif defined(__PIC32MX__)
#include "hardware/pic32/HW_PIC32.inc"
#elif defined(__arm__)
#include "hardware/arm/HW_ARM.inc"
#endif
UTouch::UTouch(byte tclk, byte tcs, byte din, byte dout, byte irq)
{
T_CLK = tclk;
T_CS = tcs;
T_DIN = din;
T_DOUT = dout;
T_IRQ = irq;
}
void UTouch::InitTouch(byte orientation)
{
orient = orientation;
_default_orientation = CAL_S>>31;
touch_x_left = (CAL_X>>14) & 0x3FFF;
touch_x_right = CAL_X & 0x3FFF;
touch_y_top = (CAL_Y>>14) & 0x3FFF;
touch_y_bottom = CAL_Y & 0x3FFF;
disp_x_size = (CAL_S>>12) & 0x0FFF;
disp_y_size = CAL_S & 0x0FFF;
prec = 10;
P_CLK = portOutputRegister(digitalPinToPort(T_CLK));
B_CLK = digitalPinToBitMask(T_CLK);
P_CS = portOutputRegister(digitalPinToPort(T_CS));
B_CS = digitalPinToBitMask(T_CS);
P_DIN = portOutputRegister(digitalPinToPort(T_DIN));
B_DIN = digitalPinToBitMask(T_DIN);
P_DOUT = portInputRegister(digitalPinToPort(T_DOUT));
B_DOUT = digitalPinToBitMask(T_DOUT);
P_IRQ = portInputRegister(digitalPinToPort(T_IRQ));
B_IRQ = digitalPinToBitMask(T_IRQ);
pinMode(T_CLK, OUTPUT);
pinMode(T_CS, OUTPUT);
pinMode(T_DIN, OUTPUT);
pinMode(T_DOUT, INPUT);
pinMode(T_IRQ, OUTPUT);
sbi(P_CS, B_CS);
sbi(P_CLK, B_CLK);
sbi(P_DIN, B_DIN);
sbi(P_IRQ, B_IRQ);
}
void UTouch::read()
{
unsigned long tx=0, temp_x=0;
unsigned long ty=0, temp_y=0;
unsigned long minx=99999, maxx=0;
unsigned long miny=99999, maxy=0;
int datacount=0;
cbi(P_CS, B_CS);
pinMode(T_IRQ, INPUT);
for (int i=0; i<prec; i++)
{
if (!rbi(P_IRQ, B_IRQ))
{
touch_WriteData(0x90);
pulse_high(P_CLK, B_CLK);
temp_x=touch_ReadData();
if (!rbi(P_IRQ, B_IRQ))
{
touch_WriteData(0xD0);
pulse_high(P_CLK, B_CLK);
temp_y=touch_ReadData();
if ((temp_x>0) and (temp_x<4096) and (temp_y>0) and (temp_y<4096))
{
tx+=temp_x;
ty+=temp_y;
if (prec>5)
{
if (temp_x<minx)
minx=temp_x;
if (temp_x>maxx)
maxx=temp_x;
if (temp_y<miny)
miny=temp_y;
if (temp_y>maxy)
maxy=temp_y;
}
datacount++;
}
}
}
}
pinMode(T_IRQ, OUTPUT);
if (prec>5)
{
tx = tx-(minx+maxx);
ty = ty-(miny+maxy);
datacount -= 2;
}
sbi(P_CS, B_CS);
if ((datacount==(prec-2)) or (datacount==PREC_LOW))
{
if (orient == _default_orientation)
{
TP_X=ty/datacount;
TP_Y=tx/datacount;
}
else
{
TP_X=tx/datacount;
TP_Y=ty/datacount;
}
}
else
{
TP_X=-1;
TP_Y=-1;
}
}
bool UTouch::dataAvailable()
{
bool avail;
pinMode(T_IRQ, INPUT);
avail = !(rbi(P_IRQ, B_IRQ));
pinMode(T_IRQ, OUTPUT);
return avail;
}
int16_t UTouch::getX()
{
long c;
if ((TP_X==-1) or (TP_Y==-1))
return -1;
if (orient == _default_orientation)
{
c = long(long(TP_X - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
if (c<0)
c = 0;
if (c>disp_x_size)
c = disp_x_size;
}
else
{
if (_default_orientation == PORTRAIT)
c = long(long(TP_X - touch_y_top) * (-disp_y_size)) / long(touch_y_bottom - touch_y_top) + long(disp_y_size);
else
c = long(long(TP_X - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
if (c<0)
c = 0;
if (c>disp_y_size)
c = disp_y_size;
}
return c;
}
int16_t UTouch::getY()
{
int c;
if ((TP_X==-1) or (TP_Y==-1))
return -1;
if (orient == _default_orientation)
{
c = long(long(TP_Y - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
if (c<0)
c = 0;
if (c>disp_y_size)
c = disp_y_size;
}
else
{
if (_default_orientation == PORTRAIT)
c = long(long(TP_Y - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
else
c = long(long(TP_Y - touch_x_left) * (-disp_x_size)) / long(touch_x_right - touch_x_left) + long(disp_x_size);
if (c<0)
c = 0;
if (c>disp_x_size)
c = disp_x_size;
}
return c;
}
void UTouch::setPrecision(byte precision)
{
switch (precision)
{
case PREC_LOW:
prec=1; // DO NOT CHANGE!
break;
case PREC_MEDIUM:
prec=12; // Iterations + 2
break;
case PREC_HI:
prec=27; // Iterations + 2
break;
case PREC_EXTREME:
prec=102; // Iterations + 2
break;
default:
prec=12; // Iterations + 2
break;
}
}
void UTouch::calibrateRead()
{
unsigned long tx=0;
unsigned long ty=0;
cbi(P_CS, B_CS);
touch_WriteData(0x90);
pulse_high(P_CLK, B_CLK);
tx=touch_ReadData();
touch_WriteData(0xD0);
pulse_high(P_CLK, B_CLK);
ty=touch_ReadData();
sbi(P_CS, B_CS);
TP_X=ty;
TP_Y=tx;
}
+80
View File
@@ -0,0 +1,80 @@
/*
UTouch.h - Arduino/chipKit library support for Color TFT LCD Touch screens
Copyright (C)2010-2014 Henning Karlsen. All right reserved
Basic functionality of this library are based on the demo-code provided by
ITead studio. You can find the latest version of the library at
http://www.henningkarlsen.com/electronics
If you make any modifications or improvements to the code, I would appreciate
that you share the code with me so that I might include it in the next release.
I can be contacted through http://www.henningkarlsen.com/electronics/contact.php
This library is free software; you can redistribute it and/or
modify it under the terms of the CC BY-NC-SA 3.0 license.
Please see the included documents for further information.
Commercial use of this library requires you to buy a license that
will allow commercial use. This includes using the library,
modified or not, as a tool to sell products.
The license applies to all part of the library including the
examples and tools supplied with the library.
*/
#ifndef UTouch_h
#define UTouch_h
#define UTOUCH_VERSION 124
#if defined(__AVR__)
#include "Arduino.h"
#include "hardware/avr/HW_AVR_defines.h"
#elif defined(__PIC32MX__)
#include "WProgram.h"
#include "hardware/pic32/HW_PIC32_defines.h"
#elif defined(__arm__)
#include "Arduino.h"
#include "hardware/arm/HW_ARM_defines.h"
#endif
#define PORTRAIT 0
#define LANDSCAPE 1
#define PREC_LOW 1
#define PREC_MEDIUM 2
#define PREC_HI 3
#define PREC_EXTREME 4
class UTouch
{
public:
int16_t TP_X ,TP_Y;
UTouch(byte tclk, byte tcs, byte tdin, byte dout, byte irq);
void InitTouch(byte orientation = LANDSCAPE);
void read();
bool dataAvailable();
int16_t getX();
int16_t getY();
void setPrecision(byte precision);
void calibrateRead();
private:
regtype *P_CLK, *P_CS, *P_DIN, *P_DOUT, *P_IRQ;
regsize B_CLK, B_CS, B_DIN, B_DOUT, B_IRQ;
byte T_CLK, T_CS, T_DIN, T_DOUT, T_IRQ;
long _default_orientation;
byte orient;
byte prec;
byte display_model;
long disp_x_size, disp_y_size, default_orientation;
long touch_x_left, touch_x_right, touch_y_top, touch_y_bottom;
void touch_WriteData(byte data);
word touch_ReadData();
};
#endif
Binary file not shown.
+21
View File
@@ -0,0 +1,21 @@
// UTouchCD.h
// ----------
//
// Since there are slight deviations in all touch screens you should run a
// calibration on your display module. Run the UTouch_Calibration sketch
// that came with this library and follow the on-screen instructions to
// update this file.
//
// Remember that is you have multiple display modules they will probably
// require different calibration data so you should run the calibration
// every time you switch to another module.
// You can, of course, store calibration data for all your modules here
// and comment out the ones you dont need at the moment.
//
// These calibration settings works with my ITDB02-3.2S.
// They MIGHT work on your display module, but you should run the
// calibration sketch anyway.
#define CAL_X 0x00378F66UL
#define CAL_Y 0x03C34155UL
#define CAL_S 0x000EF13FUL
@@ -0,0 +1,253 @@
// UTouch_ButtonTest (C)2010-2014 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT myGLCD(ITDB32S,38,39,40,41);
// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch myTouch( 6, 5, 4, 3, 2);
// Declare which fonts we will be using
extern uint8_t BigFont[];
int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
/*************************
** Custom functions **
*************************/
void drawButtons()
{
// Draw the upper row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.printNumI(x+1, 27+(x*60), 27);
}
// Draw the center row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
if (x<4)
myGLCD.printNumI(x+6, 27+(x*60), 87);
}
myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10, 130, 150, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 130, 150, 180);
myGLCD.print("Clear", 40, 147);
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (160, 130, 300, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (160, 130, 300, 180);
myGLCD.print("Enter", 190, 147);
myGLCD.setBackColor (0, 0, 0);
}
void updateStr(int val)
{
if (stCurrentLen<20)
{
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, LEFT, 224);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
/*************************
** Required functions **
*************************/
void setup()
{
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(0, 0, 255);
drawButtons();
}
void loop()
{
while (true)
{
if (myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((y>=10) && (y<=60)) // Upper row
{
if ((x>=10) && (x<=60)) // Button: 1
{
waitForIt(10, 10, 60, 60);
updateStr('1');
}
if ((x>=70) && (x<=120)) // Button: 2
{
waitForIt(70, 10, 120, 60);
updateStr('2');
}
if ((x>=130) && (x<=180)) // Button: 3
{
waitForIt(130, 10, 180, 60);
updateStr('3');
}
if ((x>=190) && (x<=240)) // Button: 4
{
waitForIt(190, 10, 240, 60);
updateStr('4');
}
if ((x>=250) && (x<=300)) // Button: 5
{
waitForIt(250, 10, 300, 60);
updateStr('5');
}
}
if ((y>=70) && (y<=120)) // Center row
{
if ((x>=10) && (x<=60)) // Button: 6
{
waitForIt(10, 70, 60, 120);
updateStr('6');
}
if ((x>=70) && (x<=120)) // Button: 7
{
waitForIt(70, 70, 120, 120);
updateStr('7');
}
if ((x>=130) && (x<=180)) // Button: 8
{
waitForIt(130, 70, 180, 120);
updateStr('8');
}
if ((x>=190) && (x<=240)) // Button: 9
{
waitForIt(190, 70, 240, 120);
updateStr('9');
}
if ((x>=250) && (x<=300)) // Button: 0
{
waitForIt(250, 70, 300, 120);
updateStr('0');
}
}
if ((y>=130) && (y<=180)) // Upper row
{
if ((x>=10) && (x<=150)) // Button: Clear
{
waitForIt(10, 130, 150, 180);
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 224, 319, 239);
}
if ((x>=160) && (x<=300)) // Button: Enter
{
waitForIt(160, 130, 300, 180);
if (stCurrentLen>0)
{
for (x=0; x<stCurrentLen+1; x++)
{
stLast[x]=stCurrent[x];
}
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 208, 319, 239);
myGLCD.setColor(0, 255, 0);
myGLCD.print(stLast, LEFT, 208);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER EMPTY", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER EMPTY", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
}
}
}
}
@@ -0,0 +1,382 @@
// UTouch_Calibration (C)2013-2014 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program can be used to calibrate the touchscreen
// of the display modules.
// This program requires the UTFT library and a touch
// screen module that is compatible with UTFT.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
// Instructions will be given on the display.
//
#include <UTFT.h>
#include <UTouch.h>
// Define the orientation of the touch screen. Further
// information can be found in the instructions.
#define TOUCH_ORIENTATION PORTRAIT
// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT myGLCD(ITDB32S,38,39,40,41);
// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch myTouch( 6, 5, 4, 3, 2);
// ************************************
// DO NOT EDIT ANYTHING BELOW THIS LINE
// ************************************
// Declare which fonts we will be using
extern uint8_t SmallFont[];
uint32_t cx, cy;
uint32_t rx[8], ry[8];
uint32_t clx, crx, cty, cby;
float px, py;
int dispx, dispy, text_y_center;
uint32_t calx, caly, cals;
char buf[13];
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myTouch.InitTouch(TOUCH_ORIENTATION);
dispx=myGLCD.getDisplayXSize();
dispy=myGLCD.getDisplayYSize();
text_y_center=(dispy/2)-6;
}
void drawCrossHair(int x, int y)
{
myGLCD.drawRect(x-10, y-10, x+10, y+10);
myGLCD.drawLine(x-5, y, x+5, y);
myGLCD.drawLine(x, y-5, x, y+5);
}
void readCoordinates()
{
int iter = 5000;
int failcount = 0;
int cnt = 0;
uint32_t tx=0;
uint32_t ty=0;
boolean OK = false;
while (OK == false)
{
myGLCD.setColor(255, 255, 255);
myGLCD.print("* PRESS *", CENTER, text_y_center);
while (myTouch.dataAvailable() == false) {}
myGLCD.print("* HOLD! *", CENTER, text_y_center);
while ((myTouch.dataAvailable() == true) && (cnt<iter) && (failcount<10000))
{
myTouch.calibrateRead();
if (!((myTouch.TP_X==65535) || (myTouch.TP_Y==65535)))
{
tx += myTouch.TP_X;
ty += myTouch.TP_Y;
cnt++;
}
else
failcount++;
}
if (cnt>=iter)
{
OK = true;
}
else
{
tx = 0;
ty = 0;
cnt = 0;
}
if (failcount>=10000)
fail();
}
cx = tx / iter;
cy = ty / iter;
}
void calibrate(int x, int y, int i)
{
myGLCD.setColor(255, 255, 255);
drawCrossHair(x,y);
myGLCD.setBackColor(255, 0, 0);
readCoordinates();
myGLCD.setColor(255, 255, 255);
myGLCD.print("* RELEASE *", CENTER, text_y_center);
myGLCD.setColor(80, 80, 80);
drawCrossHair(x,y);
rx[i]=cx;
ry[i]=cy;
while (myTouch.dataAvailable() == true) {}
}
void waitForTouch()
{
while (myTouch.dataAvailable() == true) {}
while (myTouch.dataAvailable() == false) {}
while (myTouch.dataAvailable() == true) {}
}
void toHex(uint32_t num)
{
buf[0] = '0';
buf[1] = 'x';
buf[10] = 'U';
buf[11] = 'L';
buf[12] = 0;
for (int zz=9; zz>1; zz--)
{
if ((num & 0xF) > 9)
buf[zz] = (num & 0xF) + 55;
else
buf[zz] = (num & 0xF) + 48;
num=num>>4;
}
}
void startup()
{
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, dispx-1, 13);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.drawLine(0, 14, dispx-1, 14);
myGLCD.print("UTouch Calibration", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
if (dispx==220)
{
myGLCD.print("Use a stylus or something", LEFT, 30);
myGLCD.print("similar to touch as close", LEFT, 42);
myGLCD.print("to the center of the", LEFT, 54);
myGLCD.print("highlighted crosshair as", LEFT, 66);
myGLCD.print("possible. Keep as still as", LEFT, 78);
myGLCD.print("possible and keep holding", LEFT, 90);
myGLCD.print("until the highlight is", LEFT, 102);
myGLCD.print("removed. Repeat for all", LEFT, 114);
myGLCD.print("crosshairs in sequence.", LEFT, 126);
myGLCD.print("Touch screen to continue", CENTER, 162);
}
else
{
myGLCD.print("INSTRUCTIONS", CENTER, 30);
myGLCD.print("Use a stylus or something similar to", LEFT, 50);
myGLCD.print("touch as close to the center of the", LEFT, 62);
myGLCD.print("highlighted crosshair as possible. Keep", LEFT, 74);
myGLCD.print("as still as possible and keep holding", LEFT, 86);
myGLCD.print("until the highlight is removed. Repeat", LEFT, 98);
myGLCD.print("for all crosshairs in sequence.", LEFT, 110);
myGLCD.print("Further instructions will be displayed", LEFT, 134);
myGLCD.print("when the calibration is complete.", LEFT, 146);
myGLCD.print("Do NOT use your finger as a calibration", LEFT, 170);
myGLCD.print("stylus or the result WILL BE imprecise.", LEFT, 182);
myGLCD.print("Touch screen to continue", CENTER, 226);
}
waitForTouch();
myGLCD.clrScr();
}
void done()
{
myGLCD.clrScr();
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, dispx-1, 13);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.drawLine(0, 14, dispx-1, 14);
myGLCD.print("UTouch Calibration", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
if (dispx==220)
{
myGLCD.print("To use the new calibration", LEFT, 30);
myGLCD.print("settings you must edit the", LEFT, 42);
myGLCD.setColor(160, 160, 255);
myGLCD.print("UTouchCD.h", LEFT, 54);
myGLCD.setColor(255, 255, 255);
myGLCD.print("file and change", 88, 54);
myGLCD.print("the following values. The", LEFT, 66);
myGLCD.print("values are located right", LEFT, 78);
myGLCD.print("below the opening comment.", LEFT, 90);
myGLCD.print("CAL_X", LEFT, 110);
myGLCD.print("CAL_Y", LEFT, 122);
myGLCD.print("CAL_S", LEFT, 134);
toHex(calx);
myGLCD.print(buf, 75, 110);
toHex(caly);
myGLCD.print(buf, 75, 122);
toHex(cals);
myGLCD.print(buf, 75, 134);
}
else
{
myGLCD.print("CALIBRATION COMPLETE", CENTER, 30);
myGLCD.print("To use the new calibration", LEFT, 50);
myGLCD.print("settings you must edit the", LEFT, 62);
myGLCD.setColor(160, 160, 255);
myGLCD.print("UTouchCD.h", LEFT, 74);
myGLCD.setColor(255, 255, 255);
myGLCD.print("file and change", 88, 74);
myGLCD.print("the following values.", LEFT, 86);
myGLCD.print("The values are located right", LEFT, 98);
myGLCD.print("below the opening comment in", LEFT, 110);
myGLCD.print("the file.", LEFT, 122);
myGLCD.print("CAL_X", LEFT, 150);
myGLCD.print("CAL_Y", LEFT, 162);
myGLCD.print("CAL_S", LEFT, 174);
toHex(calx);
myGLCD.print(buf, 75, 150);
toHex(caly);
myGLCD.print(buf, 75, 162);
toHex(cals);
myGLCD.print(buf, 75, 174);
}
}
void fail()
{
myGLCD.clrScr();
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, dispx-1, 13);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.drawLine(0, 14, dispx-1, 14);
myGLCD.print("UTouch Calibration FAILED", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
myGLCD.print("Unable to read the position", LEFT, 30);
myGLCD.print("of the press. This is a", LEFT, 42);
myGLCD.print("hardware issue and can", 88, 54);
myGLCD.print("not be corrected in", LEFT, 66);
myGLCD.print("software.", LEFT, 78);
while(true) {};
}
void loop()
{
startup();
myGLCD.setColor(80, 80, 80);
drawCrossHair(dispx-11, 10);
drawCrossHair(dispx/2, 10);
drawCrossHair(10, 10);
drawCrossHair(dispx-11, dispy/2);
drawCrossHair(10, dispy/2);
drawCrossHair(dispx-11, dispy-11);
drawCrossHair(dispx/2, dispy-11);
drawCrossHair(10, dispy-11);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print("***********", CENTER, text_y_center-12);
myGLCD.print("***********", CENTER, text_y_center+12);
calibrate(10, 10, 0);
calibrate(10, dispy/2, 1);
calibrate(10, dispy-11, 2);
calibrate(dispx/2, 10, 3);
calibrate(dispx/2, dispy-11, 4);
calibrate(dispx-11, 10, 5);
calibrate(dispx-11, dispy/2, 6);
calibrate(dispx-11, dispy-11, 7);
if (TOUCH_ORIENTATION == LANDSCAPE)
cals=(long(dispx-1)<<12)+(dispy-1);
else
cals=(long(dispy-1)<<12)+(dispx-1);
if (TOUCH_ORIENTATION == PORTRAIT)
px = abs(((float(rx[2]+rx[4]+rx[7])/3)-(float(rx[0]+rx[3]+rx[5])/3))/(dispy-20)); // PORTRAIT
else
px = abs(((float(rx[5]+rx[6]+rx[7])/3)-(float(rx[0]+rx[1]+rx[2])/3))/(dispy-20)); // LANDSCAPE
if (TOUCH_ORIENTATION == PORTRAIT)
{
clx = (((rx[0]+rx[3]+rx[5])/3)); // PORTRAIT
crx = (((rx[2]+rx[4]+rx[7])/3)); // PORTRAIT
}
else
{
clx = (((rx[0]+rx[1]+rx[2])/3)); // LANDSCAPE
crx = (((rx[5]+rx[6]+rx[7])/3)); // LANDSCAPE
}
if (clx<crx)
{
clx = clx - (px*10);
crx = crx + (px*10);
}
else
{
clx = clx + (px*10);
crx = crx - (px*10);
}
if (TOUCH_ORIENTATION == PORTRAIT)
py = abs(((float(ry[5]+ry[6]+ry[7])/3)-(float(ry[0]+ry[1]+ry[2])/3))/(dispx-20)); // PORTRAIT
else
py = abs(((float(ry[0]+ry[3]+ry[5])/3)-(float(ry[2]+ry[4]+ry[7])/3))/(dispx-20)); // LANDSCAPE
if (TOUCH_ORIENTATION == PORTRAIT)
{
cty = (((ry[5]+ry[6]+ry[7])/3)); // PORTRAIT
cby = (((ry[0]+ry[1]+ry[2])/3)); // PORTRAIT
}
else
{
cty = (((ry[0]+ry[3]+ry[5])/3)); // LANDSCAPE
cby = (((ry[2]+ry[4]+ry[7])/3)); // LANDSCAPE
}
if (cty<cby)
{
cty = cty - (py*10);
cby = cby + (py*10);
}
else
{
cty = cty + (py*10);
cby = cby - (py*10);
}
calx = (long(clx)<<14) + long(crx);
caly = (long(cty)<<14) + long(cby);
if (TOUCH_ORIENTATION == LANDSCAPE)
cals = cals + (1L<<31);
done();
while(true) {}
}
@@ -0,0 +1,65 @@
// UTouch_QuickDraw (C)2010-2014 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how to use the library.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT myGLCD(ITDB32S,38,39,40,41);
// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch myTouch( 6, 5, 4, 3, 2);
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
}
void loop()
{
long x, y;
while (myTouch.dataAvailable() == true)
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x!=-1) and (y!=-1))
{
myGLCD.drawPixel (x, y);
}
}
}
@@ -0,0 +1,172 @@
// UTouch_QuickPaint (C)2013-2014 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how to use the library.
//
// This program requires the UTFT library and a display
// module with at least 320x240 pixels resolution.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT myGLCD(ITDB32S,38,39,40,41);
// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch myTouch( 6, 5, 4, 3, 2);
// Declare which fonts we will be using
extern uint8_t BigFont[];
int color = 0;
word colorlist[] = {VGA_WHITE, VGA_BLACK, VGA_RED, VGA_BLUE, VGA_GREEN, VGA_FUCHSIA, VGA_YELLOW, VGA_AQUA};
int bsize = 4;
void drawColorMarkerAndBrushSize(int col)
{
myGLCD.setColor(VGA_BLACK);
myGLCD.fillRect(25, 0, 31, 239);
myGLCD.fillRect(myGLCD.getDisplayXSize()-31, 161, myGLCD.getDisplayXSize()-1, 191);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawPixel(25, (col*30)+15);
for (int i=1; i<7; i++)
myGLCD.drawLine(25+i, ((col*30)+15)-i, 25+i, ((col*30)+15)+i);
if (color==1)
myGLCD.setColor(VGA_WHITE);
else
myGLCD.setColor(colorlist[col]);
if (bsize==1)
myGLCD.drawPixel(myGLCD.getDisplayXSize()-15, 177);
else
myGLCD.fillCircle(myGLCD.getDisplayXSize()-15, 177, bsize);
myGLCD.setColor(colorlist[col]);
}
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_HI);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawLine(32,0,32,myGLCD.getDisplayYSize()-1);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,0,myGLCD.getDisplayXSize()-32,myGLCD.getDisplayYSize()-1);
myGLCD.print("C", myGLCD.getDisplayXSize()-24, 8);
myGLCD.print("L", myGLCD.getDisplayXSize()-24, 24);
myGLCD.print("E", myGLCD.getDisplayXSize()-24, 40);
myGLCD.print("A", myGLCD.getDisplayXSize()-24, 56);
myGLCD.print("R", myGLCD.getDisplayXSize()-24, 72);
myGLCD.print("+", myGLCD.getDisplayXSize()-24, 136);
myGLCD.print("-", myGLCD.getDisplayXSize()-24, 200);
myGLCD.fillRect(myGLCD.getDisplayXSize()-32,96,myGLCD.getDisplayXSize()-1,128);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,160,myGLCD.getDisplayXSize()-1,160);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,192,myGLCD.getDisplayXSize()-1,192);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,224,myGLCD.getDisplayXSize()-1,224);
for (int i=0; i<8; i++)
{
myGLCD.setColor(colorlist[i]);
myGLCD.fillRect(0, (i*30), 24, (((i+1)*30)-1));
}
drawColorMarkerAndBrushSize(color);
}
void loop()
{
long x, y;
while (myTouch.dataAvailable() == true)
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x!=-1) and (y!=-1))
{
if (x>(31+bsize) and (x<myGLCD.getDisplayXSize()-(31+bsize)))
{
if (bsize==1)
myGLCD.drawPixel(x, y);
else
myGLCD.fillCircle(x, y, bsize);
}
else
{
if (x<(30+bsize))
{
if (y<240)
{
color = y / 30;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
}
else
{
if (y<96)
{
myGLCD.setColor(VGA_BLACK);
myGLCD.fillRect(33, 0, myGLCD.getDisplayXSize()-33, myGLCD.getDisplayYSize()-1);
myGLCD.setColor(colorlist[color]);
}
if ((y>128) and (y<160))
{
if (bsize<7)
{
bsize++;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
}
if ((y>160) and (y<192))
{
bsize=4;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
if ((y>192) and (y<224))
{
if (bsize>1)
{
bsize--;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
}
}
}
}
}
}
@@ -0,0 +1,236 @@
// UTouch_ButtonTest (C)2010-2012 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Declare which fonts we will be using
extern uint8_t BigFont[];
// Uncomment the next line for chipKit Uno32
//UTFT myGLCD(ITDB24D,34,35,36,37); // Remember to change the model parameter to suit your display module!
//UTouch myTouch(20,21,22,23,24);
// Uncomment the next line for chipKit Max32
UTFT myGLCD(ITDB32S,82,83,84,85); // Remember to change the model parameter to suit your display module!
UTouch myTouch(62,63,64,65,66);
int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
/*************************
** Custom functions **
*************************/
void drawButtons()
{
// Draw the upper row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.printNumI(x+1, 27+(x*60), 27);
}
// Draw the center row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
if (x<4)
myGLCD.printNumI(x+6, 27+(x*60), 87);
}
myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10, 130, 150, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 130, 150, 180);
myGLCD.print("Clear", 40, 147);
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (160, 130, 300, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (160, 130, 300, 180);
myGLCD.print("Enter", 190, 147);
myGLCD.setBackColor (0, 0, 0);
}
void updateStr(int val)
{
if (stCurrentLen<20)
{
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, LEFT, 224);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
/*************************
** Required functions **
*************************/
void setup()
{
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(0, 0, 255);
drawButtons();
}
void loop()
{
while (true)
{
if (myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((y>=10) && (y<=60)) // Upper row
{
if ((x>=10) && (x<=60)) // Button: 1
{
waitForIt(10, 10, 60, 60);
updateStr('1');
}
if ((x>=70) && (x<=120)) // Button: 2
{
waitForIt(70, 10, 120, 60);
updateStr('2');
}
if ((x>=130) && (x<=180)) // Button: 3
{
waitForIt(130, 10, 180, 60);
updateStr('3');
}
if ((x>=190) && (x<=240)) // Button: 4
{
waitForIt(190, 10, 240, 60);
updateStr('4');
}
if ((x>=250) && (x<=300)) // Button: 5
{
waitForIt(250, 10, 300, 60);
updateStr('5');
}
}
if ((y>=70) && (y<=120)) // Center row
{
if ((x>=10) && (x<=60)) // Button: 6
{
waitForIt(10, 70, 60, 120);
updateStr('6');
}
if ((x>=70) && (x<=120)) // Button: 7
{
waitForIt(70, 70, 120, 120);
updateStr('7');
}
if ((x>=130) && (x<=180)) // Button: 8
{
waitForIt(130, 70, 180, 120);
updateStr('8');
}
if ((x>=190) && (x<=240)) // Button: 9
{
waitForIt(190, 70, 240, 120);
updateStr('9');
}
if ((x>=250) && (x<=300)) // Button: 0
{
waitForIt(250, 70, 300, 120);
updateStr('0');
}
}
if ((y>=130) && (y<=180)) // Upper row
{
if ((x>=10) && (x<=150)) // Button: Clear
{
waitForIt(10, 130, 150, 180);
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 224, 319, 239);
}
if ((x>=160) && (x<=300)) // Button: Enter
{
waitForIt(160, 130, 300, 180);
if (stCurrentLen>0)
{
for (x=0; x<stCurrentLen+1; x++)
{
stLast[x]=stCurrent[x];
}
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 208, 319, 239);
myGLCD.setColor(0, 255, 0);
myGLCD.print(stLast, LEFT, 208);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER EMPTY", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER EMPTY", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
}
}
}
}
@@ -0,0 +1,364 @@
// UTouch_Calibration (C)2013 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program can be used to calibrate the touchscreen
// of the display modules.
// This program requires the UTFT library and a touch
// screen module that is compatible with UTFT.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
// Instructions will be given on the display.
//
#include <UTFT.h>
#include <UTouch.h>
// Define the orientation of the touch screen. Further
// information can be found in the instructions.
#define TOUCH_ORIENTATION PORTRAIT
// Declare which fonts we will be using
extern uint8_t SmallFont[];
// Uncomment the next line for chipKit Uno32
//UTFT myGLCD(ITDB24D,34,35,36,37); // Remember to change the model parameter to suit your display module!
//UTouch myTouch(20,21,22,23,24);
// Uncomment the next line for chipKit Max32
UTFT myGLCD(ITDB32S,82,83,84,85); // Remember to change the model parameter to suit your display module!
UTouch myTouch(62,63,64,65,66);
// ************************************
// DO NOT EDIT ANYTHING BELOW THIS LINE
// ************************************
uint32_t cx, cy;
uint32_t rx[8], ry[8];
uint32_t clx, crx, cty, cby;
float px, py;
int dispx, dispy, text_y_center;
uint32_t calx, caly, cals;
char buf[13];
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myTouch.InitTouch(TOUCH_ORIENTATION);
dispx=myGLCD.getDisplayXSize();
dispy=myGLCD.getDisplayYSize();
text_y_center=(dispy/2)-6;
}
void drawCrossHair(int x, int y)
{
myGLCD.drawRect(x-10, y-10, x+10, y+10);
myGLCD.drawLine(x-5, y, x+5, y);
myGLCD.drawLine(x, y-5, x, y+5);
}
void readCoordinates()
{
int iter = 5000;
int failcount = 0;
int cnt = 0;
uint32_t tx=0;
uint32_t ty=0;
boolean OK = false;
while (OK == false)
{
myGLCD.setColor(255, 255, 255);
myGLCD.print("* PRESS *", CENTER, text_y_center);
while (myTouch.dataAvailable() == false) {}
myGLCD.print("* HOLD! *", CENTER, text_y_center);
while ((myTouch.dataAvailable() == true) && (cnt<iter) && (failcount<10000))
{
myTouch.calibrateRead();
if (!((myTouch.TP_X==65535) || (myTouch.TP_Y==65535)))
{
tx += myTouch.TP_X;
ty += myTouch.TP_Y;
cnt++;
}
else
failcount++;
}
if (cnt>=iter)
{
OK = true;
}
else
{
tx = 0;
ty = 0;
cnt = 0;
}
if (failcount>=10000)
fail();
}
cx = tx / iter;
cy = ty / iter;
}
void calibrate(int x, int y, int i)
{
myGLCD.setColor(255, 255, 255);
drawCrossHair(x,y);
myGLCD.setBackColor(255, 0, 0);
readCoordinates();
myGLCD.setColor(255, 255, 255);
myGLCD.print("* RELEASE *", CENTER, text_y_center);
myGLCD.setColor(80, 80, 80);
drawCrossHair(x,y);
rx[i]=cx;
ry[i]=cy;
while (myTouch.dataAvailable() == true) {}
}
void waitForTouch()
{
while (myTouch.dataAvailable() == true) {}
while (myTouch.dataAvailable() == false) {}
while (myTouch.dataAvailable() == true) {}
}
void toHex(uint32_t num)
{
buf[0] = '0';
buf[1] = 'x';
buf[10] = 'U';
buf[11] = 'L';
buf[12] = 0;
for (int zz=9; zz>1; zz--)
{
if ((num & 0xF) > 9)
buf[zz] = (num & 0xF) + 55;
else
buf[zz] = (num & 0xF) + 48;
num=num>>4;
}
}
void startup()
{
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, dispx-1, 13);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.drawLine(0, 14, dispx-1, 14);
myGLCD.print("UTouch Calibration", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
if (dispx==220)
{
myGLCD.print("Use a stylus or something", LEFT, 30);
myGLCD.print("similar to touch as close", LEFT, 42);
myGLCD.print("to the center of the", LEFT, 54);
myGLCD.print("highlighted crosshair as", LEFT, 66);
myGLCD.print("possible. Keep as still as", LEFT, 78);
myGLCD.print("possible and keep holding", LEFT, 90);
myGLCD.print("until the highlight is", LEFT, 102);
myGLCD.print("removed. Repeat for all", LEFT, 114);
myGLCD.print("crosshairs in sequence.", LEFT, 126);
myGLCD.print("Touch screen to continue", CENTER, 162);
}
else
{
myGLCD.print("INSTRUCTIONS", CENTER, 30);
myGLCD.print("Use a stylus or something similar to", LEFT, 50);
myGLCD.print("touch as close to the center of the", LEFT, 62);
myGLCD.print("highlighted crosshair as possible. Keep", LEFT, 74);
myGLCD.print("as still as possible and keep holding", LEFT, 86);
myGLCD.print("until the highlight is removed. Repeat", LEFT, 98);
myGLCD.print("for all crosshairs in sequence.", LEFT, 110);
myGLCD.print("Further instructions will be displayed", LEFT, 134);
myGLCD.print("when the calibration is complete.", LEFT, 146);
myGLCD.print("Do NOT use your finger as a calibration", LEFT, 170);
myGLCD.print("stylus or the result WILL BE imprecise.", LEFT, 182);
myGLCD.print("Touch screen to continue", CENTER, 226);
}
waitForTouch();
myGLCD.clrScr();
}
void done()
{
myGLCD.clrScr();
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, dispx-1, 13);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.drawLine(0, 14, dispx-1, 14);
myGLCD.print("UTouch Calibration", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
if (dispx==220)
{
myGLCD.print("To use the new calibration", LEFT, 30);
myGLCD.print("settings you must edit the", LEFT, 42);
myGLCD.setColor(160, 160, 255);
myGLCD.print("UTouchCD.h", LEFT, 54);
myGLCD.setColor(255, 255, 255);
myGLCD.print("file and change", 88, 54);
myGLCD.print("the following values. The", LEFT, 66);
myGLCD.print("values are located right", LEFT, 78);
myGLCD.print("below the opening comment.", LEFT, 90);
myGLCD.print("CAL_X", LEFT, 110);
myGLCD.print("CAL_Y", LEFT, 122);
myGLCD.print("CAL_S", LEFT, 134);
toHex(calx);
myGLCD.print(buf, 75, 110);
toHex(caly);
myGLCD.print(buf, 75, 122);
toHex(cals);
myGLCD.print(buf, 75, 134);
}
else
{
myGLCD.print("CALIBRATION COMPLETE", CENTER, 30);
myGLCD.print("To use the new calibration", LEFT, 50);
myGLCD.print("settings you must edit the", LEFT, 62);
myGLCD.setColor(160, 160, 255);
myGLCD.print("UTouchCD.h", LEFT, 74);
myGLCD.setColor(255, 255, 255);
myGLCD.print("file and change", 88, 74);
myGLCD.print("the following values.", LEFT, 86);
myGLCD.print("The values are located right", LEFT, 98);
myGLCD.print("below the opening comment in", LEFT, 110);
myGLCD.print("the file.", LEFT, 122);
myGLCD.print("CAL_X", LEFT, 150);
myGLCD.print("CAL_Y", LEFT, 162);
myGLCD.print("CAL_S", LEFT, 174);
toHex(calx);
myGLCD.print(buf, 75, 150);
toHex(caly);
myGLCD.print(buf, 75, 162);
toHex(cals);
myGLCD.print(buf, 75, 174);
}
}
void fail()
{
myGLCD.clrScr();
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, dispx-1, 13);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.drawLine(0, 14, dispx-1, 14);
myGLCD.print("UTouch Calibration FAILED", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
myGLCD.print("Unable to read the position", LEFT, 30);
myGLCD.print("of the press. This is a", LEFT, 42);
myGLCD.print("hardware issue and can", 88, 54);
myGLCD.print("not be corrected in", LEFT, 66);
myGLCD.print("software.", LEFT, 78);
while(true) {};
}
void loop()
{
startup();
myGLCD.setColor(80, 80, 80);
drawCrossHair(dispx-11, 10);
drawCrossHair(dispx/2, 10);
drawCrossHair(10, 10);
drawCrossHair(dispx-11, dispy/2);
drawCrossHair(10, dispy/2);
drawCrossHair(dispx-11, dispy-11);
drawCrossHair(dispx/2, dispy-11);
drawCrossHair(10, dispy-11);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print("***********", CENTER, text_y_center-12);
myGLCD.print("***********", CENTER, text_y_center+12);
calibrate(10, 10, 0);
calibrate(10, dispy/2, 1);
calibrate(10, dispy-11, 2);
calibrate(dispx/2, 10, 3);
calibrate(dispx/2, dispy-11, 4);
calibrate(dispx-11, 10, 5);
calibrate(dispx-11, dispy/2, 6);
calibrate(dispx-11, dispy-11, 7);
if (TOUCH_ORIENTATION == LANDSCAPE)
cals=(long(dispx-1)<<12)+(dispy-1);
else
cals=(long(dispy-1)<<12)+(dispx-1);
if (TOUCH_ORIENTATION == PORTRAIT)
px = abs(((float(rx[2]+rx[4]+rx[7])/3)-(float(rx[0]+rx[3]+rx[5])/3))/(dispy-20)); // PORTRAIT
else
px = abs(((float(rx[5]+rx[6]+rx[7])/3)-(float(rx[0]+rx[1]+rx[2])/3))/(dispy-20)); // LANDSCAPE
if (TOUCH_ORIENTATION == PORTRAIT)
{
clx = (((rx[0]+rx[3]+rx[5])/3)); // PORTRAIT
crx = (((rx[2]+rx[4]+rx[7])/3)); // PORTRAIT
}
else
{
clx = (((rx[0]+rx[1]+rx[2])/3)); // LANDSCAPE
crx = (((rx[5]+rx[6]+rx[7])/3)); // LANDSCAPE
}
if (clx<crx)
{
clx = clx - (px*10);
crx = crx + (px*10);
}
else
{
clx = clx + (px*10);
crx = crx - (px*10);
}
if (TOUCH_ORIENTATION == PORTRAIT)
py = abs(((float(ry[5]+ry[6]+ry[7])/3)-(float(ry[0]+ry[1]+ry[2])/3))/(dispx-20)); // PORTRAIT
else
py = abs(((float(ry[0]+ry[3]+ry[5])/3)-(float(ry[2]+ry[4]+ry[7])/3))/(dispx-20)); // LANDSCAPE
if (TOUCH_ORIENTATION == PORTRAIT)
{
cty = (((ry[5]+ry[6]+ry[7])/3)); // PORTRAIT
cby = (((ry[0]+ry[1]+ry[2])/3)); // PORTRAIT
}
else
{
cty = (((ry[0]+ry[3]+ry[5])/3)); // LANDSCAPE
cby = (((ry[2]+ry[4]+ry[7])/3)); // LANDSCAPE
}
if (cty<cby)
{
cty = cty - (py*10);
cby = cby + (py*10);
}
else
{
cty = cty + (py*10);
cby = cby - (py*10);
}
calx = (long(clx)<<14) + long(crx);
caly = (long(cty)<<14) + long(cby);
if (TOUCH_ORIENTATION == LANDSCAPE)
cals = cals + (1L<<31);
done();
while(true) {}
}
@@ -0,0 +1,46 @@
// UTouch_QuickDraw (C)2010-2012 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how to use the library.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Uncomment the next line for chipKit Uno32
//UTFT myGLCD(ITDB24D,34,35,36,37); // Remember to change the model parameter to suit your display module!
//UTouch myTouch(20,21,22,23,24);
// Uncomment the next line for chipKit Max32
UTFT myGLCD(ITDB32S,82,83,84,85); // Remember to change the model parameter to suit your display module!
UTouch myTouch(62,63,64,65,66);
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
}
void loop()
{
long x, y;
while (myTouch.dataAvailable() == true)
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x!=-1) and (y!=-1))
myGLCD.drawPixel (x, y);
}
}
@@ -0,0 +1,155 @@
// UTouch_QuickPaint (C)2013 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how to use the library.
//
// This program requires the UTFT library and a display
// module with at least 320x240 pixels resolution.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Declare which fonts we will be using
extern uint8_t BigFont[];
// Uncomment the next line for chipKit Uno32
//UTFT myGLCD(ITDB24D,34,35,36,37); // Remember to change the model parameter to suit your display module!
//UTouch myTouch(20,21,22,23,24);
// Uncomment the next line for chipKit Max32
UTFT myGLCD(ITDB32S,82,83,84,85); // Remember to change the model parameter to suit your display module!
UTouch myTouch(62,63,64,65,66);
int color = 0;
word colorlist[] = {VGA_WHITE, VGA_BLACK, VGA_RED, VGA_BLUE, VGA_GREEN, VGA_FUCHSIA, VGA_YELLOW, VGA_AQUA};
int bsize = 4;
void drawColorMarkerAndBrushSize(int col)
{
myGLCD.setColor(VGA_BLACK);
myGLCD.fillRect(25, 0, 31, 239);
myGLCD.fillRect(myGLCD.getDisplayXSize()-31, 161, myGLCD.getDisplayXSize()-1, 191);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawPixel(25, (col*30)+15);
for (int i=1; i<7; i++)
myGLCD.drawLine(25+i, ((col*30)+15)-i, 25+i, ((col*30)+15)+i);
if (color==1)
myGLCD.setColor(VGA_WHITE);
else
myGLCD.setColor(colorlist[col]);
if (bsize==1)
myGLCD.drawPixel(myGLCD.getDisplayXSize()-15, 177);
else
myGLCD.fillCircle(myGLCD.getDisplayXSize()-15, 177, bsize);
myGLCD.setColor(colorlist[col]);
}
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_HI);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawLine(32,0,32,myGLCD.getDisplayYSize()-1);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,0,myGLCD.getDisplayXSize()-32,myGLCD.getDisplayYSize()-1);
myGLCD.print("C", myGLCD.getDisplayXSize()-24, 8);
myGLCD.print("L", myGLCD.getDisplayXSize()-24, 24);
myGLCD.print("E", myGLCD.getDisplayXSize()-24, 40);
myGLCD.print("A", myGLCD.getDisplayXSize()-24, 56);
myGLCD.print("R", myGLCD.getDisplayXSize()-24, 72);
myGLCD.print("+", myGLCD.getDisplayXSize()-24, 136);
myGLCD.print("-", myGLCD.getDisplayXSize()-24, 200);
myGLCD.fillRect(myGLCD.getDisplayXSize()-32,96,myGLCD.getDisplayXSize()-1,128);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,160,myGLCD.getDisplayXSize()-1,160);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,192,myGLCD.getDisplayXSize()-1,192);
myGLCD.drawLine(myGLCD.getDisplayXSize()-32,224,myGLCD.getDisplayXSize()-1,224);
for (int i=0; i<8; i++)
{
myGLCD.setColor(colorlist[i]);
myGLCD.fillRect(0, (i*30), 24, (((i+1)*30)-1));
}
drawColorMarkerAndBrushSize(color);
}
void loop()
{
long x, y;
while (myTouch.dataAvailable() == true)
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x!=-1) and (y!=-1))
{
if (x>(31+bsize) and (x<myGLCD.getDisplayXSize()-(31+bsize)))
{
if (bsize==1)
myGLCD.drawPixel(x, y);
else
myGLCD.fillCircle(x, y, bsize);
}
else
{
if (x<(30+bsize))
{
if (y<240)
{
color = y / 30;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
}
else
{
if (y<96)
{
myGLCD.setColor(VGA_BLACK);
myGLCD.fillRect(33, 0, myGLCD.getDisplayXSize()-33, myGLCD.getDisplayYSize()-1);
myGLCD.setColor(colorlist[color]);
}
if ((y>128) and (y<160))
{
if (bsize<7)
{
bsize++;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
}
if ((y>160) and (y<192))
{
bsize=4;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
if ((y>192) and (y<224))
{
if (bsize>1)
{
bsize--;
drawColorMarkerAndBrushSize(color);
while (myTouch.dataAvailable()) {};
delay(50);
}
}
}
}
}
}
}
+36
View File
@@ -0,0 +1,36 @@
// Results becomes inaccurate if direct port manipulation is used hence the use of digitalWrite() and digitalRead()
void UTouch::touch_WriteData(byte data)
{
byte temp;
temp=data;
cbi(P_CLK, B_CLK);
for(byte count=0; count<8; count++)
{
if(temp & 0x80)
digitalWrite(T_DIN, HIGH);
else
digitalWrite(T_DIN, LOW);
temp = temp << 1;
digitalWrite(T_CLK, LOW);
digitalWrite(T_CLK, HIGH);
}
}
word UTouch::touch_ReadData()
{
word data = 0;
for(byte count=0; count<12; count++)
{
data <<= 1;
digitalWrite(T_CLK, HIGH);
digitalWrite(T_CLK, LOW);
if (digitalRead(T_DOUT))
data++;
}
return(data);
}
@@ -0,0 +1,17 @@
// *** Hardwarespecific defines ***
#define cbi(reg, bitmask) *reg &= ~bitmask
#define sbi(reg, bitmask) *reg |= bitmask
#define rbi(reg, bitmask) ((*reg) & bitmask)
#define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
#define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
#define swap(type, i, j) {type t = i; i = j; j = t;}
#if defined(TEENSYDUINO) && TEENSYDUINO >= 117
#define regtype volatile uint8_t
#define regsize uint8_t
#else
#define regtype volatile uint32_t
#define regsize uint32_t
#endif
+34
View File
@@ -0,0 +1,34 @@
void UTouch::touch_WriteData(byte data)
{
byte temp;
temp=data;
cbi(P_CLK, B_CLK);
for(byte count=0; count<8; count++)
{
if(temp & 0x80)
sbi(P_DIN, B_DIN);
else
cbi(P_DIN, B_DIN);
temp = temp << 1;
cbi(P_CLK, B_CLK);
sbi(P_CLK, B_CLK);
}
}
word UTouch::touch_ReadData()
{
word data = 0;
for(byte count=0; count<12; count++)
{
data <<= 1;
sbi(P_CLK, B_CLK);
cbi(P_CLK, B_CLK);
if (rbi(P_DOUT, B_DOUT))
data++;
}
return(data);
}
@@ -0,0 +1,12 @@
// *** Hardwarespecific defines ***
#define cbi(reg, bitmask) *reg &= ~bitmask
#define sbi(reg, bitmask) *reg |= bitmask
#define rbi(reg, bitmask) ((*reg) & bitmask)
#define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
#define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
#define swap(type, i, j) {type t = i; i = j; j = t;}
#define regtype volatile uint8_t
#define regsize uint8_t
@@ -0,0 +1,36 @@
// Direct port manipulation does not work at the moment, hence the use of digitalWrite() and digitalRead()
void UTouch::touch_WriteData(byte data)
{
byte temp;
temp=data;
cbi(P_CLK, B_CLK);
for(byte count=0; count<8; count++)
{
if(temp & 0x80)
digitalWrite(T_DIN, HIGH);
else
digitalWrite(T_DIN, LOW);
temp = temp << 1;
digitalWrite(T_CLK, LOW);
digitalWrite(T_CLK, HIGH);
}
}
word UTouch::touch_ReadData()
{
word data = 0;
for(byte count=0; count<12; count++)
{
data <<= 1;
digitalWrite(T_CLK, HIGH);
digitalWrite(T_CLK, LOW);
if (digitalRead(T_DOUT))
data++;
}
return(data);
}
@@ -0,0 +1,12 @@
// *** Hardwarespecific defines ***
#define cbi(reg, bitmask) (*(reg + 1)) = bitmask
#define sbi(reg, bitmask) (*(reg + 2)) = bitmask
#define rbi(reg, bitmask) (*(reg) & bitmask)
#define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
#define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
#define swap(type, i, j) {type t = i; i = j; j = t;}
#define regtype volatile uint32_t
#define regsize uint16_t
+17
View File
@@ -0,0 +1,17 @@
UTouch KEYWORD1
InitTouch KEYWORD2
read KEYWORD2
dataAvailable KEYWORD2
getX KEYWORD2
getY KEYWORD2
setPrecision KEYWORD2
PREC_LOW LITERAL1
PREC_MEDIUM LITERAL1
PREC_HI LITERAL1
PREC_EXTREME LITERAL1
PORTRAIT LITERAL1
LANDSCAPE LITERAL1
TP_X LITERAL1
TP_Y LITERAL1
+18
View File
@@ -0,0 +1,18 @@
Version:
1.0 1 Dec 2012 - initial release
1.1 23 May 2013 - added support for more display modules
modified calibration to try to compensate for slight flaws in some (larger) touchscreens
changed license to CC BY-NC-SA 3.0
1.2 22 Sep 2013 - general optimization
fixed some issues with calibration
made modifications to reduce erroneous readings
added support for more display modules
added UTouch_QuickPaint example
1.21 06 Dec 2013 - added support for ElecFreaks TFT01-4.3
added support for Coldtears 3.5" and 4.0" modules
1.22 18 Feb 2014 - added support for Teensy 3.x Boards
added support for three display modules from ElecHouse
made some modifications to improve accuracy when precision is set to medium or higher
1.23 16 Mar 2014 - fixed a bug that were introduced in v1.22
1.24 05 May 2014 - added support for DisplayModule DM-TFT24-104 and DM-TFT28-103
added support for Coldtears 5" and 7" CPLD modules