diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/README.md b/STM32F1/libraries/Serasidis_XPT2046_touch/README.md new file mode 100644 index 0000000..014df87 --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/README.md @@ -0,0 +1,78 @@ +Description +--- + +A simple **XPT2046** Touch Screen library for STM32 micro-controllers. + +* The first example sketch (**TouchTest.ino**) checks if the touch screen has been pressed and prints the X,Y coordinates on the Serial port 1. +* The second example sketch (**TouchButtons.ino**) creates some virtual buttons defined by the user. + +Copyright (c) 03 December 2015 by **Vassilis Serasidis** + +Home: http://www.serasidis.gr , https://github.com/Serasidis + +The library is written for STM32duino (http://stm32duino.com) + +``` + //Create 4*2=8 virtual buttons + #define LINES 2 //Divide the touch screen into 2 lines + #define COLUMNS 4 //Divide the touch screen into 4 columns + +``` + + +Screenshot of the Serial port 1 (TouchButtons.ino) +``` +------------------------------------------------- +XPT2046 touch screen buttons +Copyright (c) 02 Dec 2015 by Vassilis Serasidis +Home: http://www.serasidis.gr +------------------------------------------------- +Button: 1 +X: 1097 +Y: 800 + +Button: 2 +X: 3455 +Y: 617 + +Button: 3 +X: 684 +Y: 1483 + +Button: 4 +X: 3412 +Y: 1198 + +Button: 5 +X: 860 +Y: 2395 + +Button: 6 +X: 3353 +Y: 2355 + +``` + +PIN Connections between XPT2046 and STM32F103 +---- +| XPT2046 | STM32F103 | +|:------:|:-----:| +|T_DO|PA6| +|T_DIN|PA7| +|T_CS|PA3| +|T_CLK|PA5| + +Selecting the SPI port number and Chip Select pin +---- +Can be used other SPI port than the default SPI1 port. Just select the SPI port you want by replacing the following line into the sketch: + +``` +SPIClass mySPI(1); //Create an SPI instance on SPI1 port. +//SPIClass mySPI(2); //Create an SPI instance on SPI2 port. +``` + +The Chip select pin can be defined by the user also. + +``` +#define CS_PIN PA3 // The pin PA3 has been chosen as Chip Select pin. +``` diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/examples/TouchButtons/TouchButtons.ino b/STM32F1/libraries/Serasidis_XPT2046_touch/examples/TouchButtons/TouchButtons.ino new file mode 100644 index 0000000..5ca9606 --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/examples/TouchButtons/TouchButtons.ino @@ -0,0 +1,60 @@ +/** + * XPT2046 touch screen buttons. + * + * It divides the touch screen into COLUMNS * LINES areas (4*2=8 buttons) and creates virtual buttons. + * if the touch screen area is pressed and prints on Serial Port 1 the X,Y coordinates. + * + * Copyright (c) 02 Dec 2015 by Vassilis Serasidis + * Home: http://www.serasidis.gr + * email: avrsite@yahoo.gr + * + * The sketch example has been created for using it with STM32Duino (http://www.stm32duino.com) + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "XPT2046_touch.h" +#include ; + +#define CS_PIN PA3 // Chip Select pin +#define LINES 2 +#define COLUMNS 4 + +SPIClass mySPI(1); //Create an SPI instance on SPI1 port. +XPT2046_touch ts(CS_PIN, mySPI); // Chip Select pin, SPI port + +uint16_t xy[2]; + +void setup() { + Serial1.begin(9600); + Serial1.println("-------------------------------------------------"); + Serial1.println("XPT2046 touch screen buttons"); + Serial1.println("Copyright (c) 02 Dec 2015 by Vassilis Serasidis"); + Serial1.println("Home: http://www.serasidis.gr"); + Serial1.println("-------------------------------------------------"); + ts.begin(); //Begin TouchScreen. + ts.setButtonsNumber(COLUMNS, LINES); //Divide the Touch screen area into 4 columns and 2 lines and make them act as buttons. +} + +void loop() { + if(ts.read_XY(xy)){ //If the touch screen is preesed, read the X,Y coordinates and print them on Serial port. + uint8_t buttonNumber = ts.getButtonNumber(); + if(buttonNumber > 0){ + Serial1.print("Button: "); + Serial1.println(buttonNumber); + + Serial1.print("X: "); + Serial1.println(xy[0]); //Print X value + Serial1.print("Y: "); + Serial1.println(xy[1]); //Print Y value + Serial1.println(); + } + delay(500); + } +} \ No newline at end of file diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/examples/TouchTest/TouchTest.ino b/STM32F1/libraries/Serasidis_XPT2046_touch/examples/TouchTest/TouchTest.ino new file mode 100644 index 0000000..7e693a3 --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/examples/TouchTest/TouchTest.ino @@ -0,0 +1,51 @@ +/** + * XPT2046 Touch Screen Controller example. + * + * It checks if the touch screen area is pressed and prints on Serial Port 1 the X,Y coordinates. + * + * Copyright (c) 02 Dec 2015 by Vassilis Serasidis + * Home: http://www.serasidis.gr + * email: avrsite@yahoo.gr + * + * The sketch example has been created for using it with STM32Duino (http://www.stm32duino.com) + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "XPT2046_touch.h" +#include ; + +#define CS_PIN PA3 // Chip Select pin + +SPIClass mySPI(1); //Create an SPI instance on SPI1 port. +XPT2046_touch ts(CS_PIN, mySPI); // Chip Select pin, SPI port + +uint16_t xy[2]; + +void setup() { + Serial1.begin(9600); + Serial1.println("-------------------------------------------------"); + Serial1.println("XPT2046 example sketch"); + Serial1.println("Copyright (c) 02 Dec 2015 by Vassilis Serasidis"); + Serial1.println("Home: http://www.serasidis.gr"); + Serial1.println("-------------------------------------------------"); + ts.begin(); //Begin TouchScreen. + +} + +void loop() { + if(ts.read_XY(xy)){ //If the touch screen is preesed, read the X,Y coordinates and print them on Serial port. + Serial1.print("X: "); + Serial1.println(xy[0]); //Print X value + Serial1.print("Y: "); + Serial1.println(xy[1]); //Print Y value + Serial1.println(); + delay(500); + } +} \ No newline at end of file diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/keywords.txt b/STM32F1/libraries/Serasidis_XPT2046_touch/keywords.txt new file mode 100644 index 0000000..de5653f --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/keywords.txt @@ -0,0 +1,27 @@ +####################################### +# Syntax Coloring Map For XPT2046_touch +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +XPT2046_touch KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +setButtonsNumber KEYWORD2 +getButtonNumber KEYWORD2 +read_XY KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/library.properties b/STM32F1/libraries/Serasidis_XPT2046_touch/library.properties new file mode 100644 index 0000000..7afd17e --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/library.properties @@ -0,0 +1,8 @@ +name=Serasidis_XPT2046_touch +version=1.0 +author=Vasillis Serasidis. +email=avrsite@yahoo.gr +sentence=A simple XPT2046 Touch screen driver +paragraph=A simple XPT2046 Touch screen driver +url=http://wwww.serasidis.gr +architectures=STM32F1 diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/src/XPT2046_touch.cpp b/STM32F1/libraries/Serasidis_XPT2046_touch/src/XPT2046_touch.cpp new file mode 100644 index 0000000..d1b0bdf --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/src/XPT2046_touch.cpp @@ -0,0 +1,88 @@ +/** + * + * + */ + + + +#include "Arduino.h" +#include "XPT2046_touch.h" + + +/****************************************************************************/ + XPT2046_touch::XPT2046_touch(uint8_t _cs_pin, SPIClass _spiChan) : cs_pin(_cs_pin), my_SPI(_spiChan){ + } + + +/****************************************************************************/ + + void XPT2046_touch::begin(){ + pinMode(cs_pin, OUTPUT); + digitalWrite(cs_pin, HIGH); + my_SPI.begin(); + } + + /****************************************************************************/ + + boolean XPT2046_touch::read_XY(uint16_t *xy){ + int z1, z2, tmpH, tmpL; + digitalWrite(cs_pin, LOW); + + //Check if touch screen is pressed. + SPI.transfer(B10110011); // Z1 + delay(10); + tmpH = (my_SPI.transfer(0) << 5); + tmpL = (my_SPI.transfer(0) >> 3); + z1 = tmpH | tmpL; + + SPI.transfer(B11000011); // Z2 + delay(10); + tmpH = (my_SPI.transfer(0) << 5); + tmpL = (my_SPI.transfer(0) >> 3); + z2 = tmpH | tmpL; + + if((z2 - z1) < Z_THRESHOLD){ //If the touch screen is pressed, read the X,Y coordinates from XPT2046. + my_SPI.transfer(B11010011); // X + delay(10); + tmpH = (my_SPI.transfer(0) << 5); + tmpL = (my_SPI.transfer(0) >> 3); + xy[0] = tmpH | tmpL; + + my_SPI.transfer(B10010011); // Y + delay(10); + tmpH = (my_SPI.transfer(0) << 5); + tmpL = (my_SPI.transfer(0) >> 3); + xy[1] = tmpH | tmpL; + digitalWrite(cs_pin, HIGH); + return true; + } + digitalWrite(cs_pin, HIGH); + return false; + } + + /****************************************************************************/ + void XPT2046_touch::setButtonsNumber(byte columnButtons, byte rowButtons ){ + _rowButtons = rowButtons; + _columnButtons = columnButtons; + } + + /****************************************************************************/ + uint8_t XPT2046_touch::getButtonNumber(){ + uint16_t xy[2]; + uint8_t tmp, buttonNum; + int div; + + if(read_XY(xy)){ + + div = (X_MAX + X_MIN) / (_columnButtons + 1); + buttonNum = ((xy[1] / div)); + + div = (Y_MAX + Y_MIN) / _rowButtons; + tmp = ((xy[0] / div)); + + return ((buttonNum * _rowButtons) + tmp + 1); //Return the button number. + } + + return 0; //Touch screen is not pressed. + } +/****************************************************************************/ diff --git a/STM32F1/libraries/Serasidis_XPT2046_touch/src/XPT2046_touch.h b/STM32F1/libraries/Serasidis_XPT2046_touch/src/XPT2046_touch.h new file mode 100644 index 0000000..0830162 --- /dev/null +++ b/STM32F1/libraries/Serasidis_XPT2046_touch/src/XPT2046_touch.h @@ -0,0 +1,42 @@ +/** + * + * + * + * + */ + + +#ifndef XPT2046_touch_h +#define XPT2046_touch_h +#include +#include + +#define Z_THRESHOLD 3000 + +// Pre-defined touch screen calibration for using the 2.4" ILI9341 LCD +#define X_MIN 830 +#define X_MAX 3800 +#define Y_MIN 550 +#define Y_MAX 3550 + +/** + * + */ + +class XPT2046_touch{ + private: + uint8_t cs_pin; + SPIClass my_SPI; + uint8_t _rowButtons = 1; + uint8_t _columnButtons = 1; + + public: + XPT2046_touch(uint8_t _cs_pin, SPIClass _spiChan); //Contructor. + void begin(); + void setButtonsNumber(byte rowButtons, byte columnButtons); + uint8_t getButtonNumber(); + boolean read_XY(uint16_t *xy); +}; + +#endif +