diff --git a/XPT2046.cpp b/XPT2046.cpp index 0a3367b..e2da59c 100644 --- a/XPT2046.cpp +++ b/XPT2046.cpp @@ -1,241 +1,245 @@ -/** - * @file XPT2046.cpp - * @date 19.02.2016 - * @author Markus Sattler - * - * Copyright (c) 2015 Markus Sattler. All rights reserved. - * This file is originally part of the XPT2046 driver for Arduino. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#include "XPT2046.h" - -#define XPT2046_CFG_START 1<<7 - -#define XPT2046_CFG_MUX(v) ((v&0b111) << (4)) - -#define XPT2046_CFG_8BIT 1<<3 -#define XPT2046_CFG_12BIT (0) - -#define XPT2046_CFG_SER 1<<2 -#define XPT2046_CFG_DFR (0) - -#define XPT2046_CFG_PWR(v) ((v&0b11)) - -#define XPT2046_MUX_Y 0b101 -#define XPT2046_MUX_X 0b001 - -#define XPT2046_MUX_Z1 0b011 -#define XPT2046_MUX_Z2 0b100 - -XPT2046::XPT2046() { - _maxValue = 0x0fff; - _width = 480; - _height = 320; - _rotation = 0; - - _minX = 0; - _minY = 0; - - _maxX = _maxValue; - _maxY = _maxValue; - - _lastX = -1; - _lastY = -1; - - _minChange = 10; - - spi_cs = - 1 << 0 | //Chip select 1 - 0 << 3 | //low idle clock polarity - 0 << 6 | //chip select active low - 0 << 22 | //chip select low polarity - 0 << 8 | //DMA disabled - 0 << 11; //Manual chip select - - - // FIFO file path - // Creating the named file(FIFO) - // mkfifo(, ) - mkfifo(tcfifo, 0666); -} - - -XPT2046::~XPT2046() { - -} - -int XPT2046::SpiWriteAndRead(unsigned char *data, int length) -{ - uint32_t old_spi_cs = spi->cs; - - for (int i = 0; i < length; i++) - { - spi->cs = spi_cs | 1 << 7;//Set TA to high - - while (!(spi->cs & (1 << 18))) ; //Poll TX Fifo until it has space - - spi->fifo = data[i]; - - while(! (spi->cs & (1 << 17))) ; //Wait until RX FIFO contains data - - //while(!(spi->cs & (1 << 16))); //Wait until DONE - - uint32_t spi_fifo = spi->fifo; - - spi->cs = (spi_cs & (~(1 << 7))) | 1 << 5 | 1 << 4; //Set TA to LOW //Clear Fifox - - data[i] = spi_fifo; - } - - return 0; -} - -void XPT2046::read_touchscreen() { - - uint32_t old_spi_cs = spi->cs; - uint32_t old_spi_clk = spi->clk; - //printBits(sizeof(old_spi_cs), (void*)&(old_spi_cs)); - spi->clk = 256; - SET_GPIO_MODE(GPIO_SPI0_CE0, 0x00); - SET_GPIO_MODE(GPIO_SPI0_CE1, 0x04); - // touch on low - - uint16_t x, y, z; - read(&x, &y, &z); - if (abs(x - _lastX) > _minChange || abs(y - _lastY) > _minChange) { - _lastX = x; - _lastY = y; - } - - if (z > 100) - { - char output[30] = ""; - sprintf(output, "x:%d, y:%d, z:%d\n", x, y, z); - int fd = open(tcfifo, O_WRONLY); - write(fd, output, strlen(output) + 1); - close(fd); - } - - - //spi->cs = (old_spi_cs | 1 << 4 | 1 << 5) & (~(1 << 7)); //Clear Fifos and TA - spi->cs = old_spi_cs; - spi->clk = old_spi_clk; - - SET_GPIO_MODE(GPIO_SPI0_CE0, 0x04); - SET_GPIO_MODE(GPIO_SPI0_CE1, 0x00); -} - -void XPT2046::setRotation(uint8_t m) { - _rotation = m % 4; -} - -void XPT2046::setCalibration(uint16_t minX, uint16_t minY, uint16_t maxX, uint16_t maxY) { - _minX = minX; - _minY = minY; - _maxX = maxX; - _maxY = maxY; -} - -void XPT2046::read(uint16_t * oX, uint16_t * oY, uint16_t * oZ) { - uint16_t x, y; - readRaw(&x, &y, oZ); - - uint32_t cX = x; - uint32_t cY = y; - - if(cX < _minX) { - cX = 0; - } else if(cX > _maxX) { - cX = _width; - } else { - cX -= _minX; - cX = ((cX << 8) / (((_maxX - _minX) << 8) / (_width << 8)) )>> 8; - } - - if(cY < _minY) { - cY = 0; - } else if(cY > _maxY) { - cY = _height; - } else { - cY -= _minY; - cY = ((cY << 8) / (((_maxY - _minY) << 8) / (_height << 8))) >> 8; - } - - *oX = cX; - *oY = cY; - *oZ = std::max(0,4096 - (int)(*oZ)); -} - -void XPT2046::readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ) { - uint32_t x = 0; - uint32_t y = 0; - uint32_t z1 = 0; - uint32_t z2 = 0; - uint8_t i = 0; - - for(; i < 15; i++) { - // SPI requires 32bit alignment - uint8_t buf[12] = { - (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Y) | XPT2046_CFG_PWR(3)), 0x00, 0x00, - (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_X) | XPT2046_CFG_PWR(3)), 0x00, 0x00, - (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z1)| XPT2046_CFG_PWR(3)), 0x00, 0x00, - (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z2)| XPT2046_CFG_PWR(3)), 0x00, 0x00 - }; - - SpiWriteAndRead(buf, 12); - - y += (buf[1] << 8 | buf[2])>>3; - x += (buf[4] << 8 | buf[5])>>3; - z1 += (buf[7] << 8 | buf[8])>>3; - z2 += (buf[10] << 8 | buf[11])>>3; - } - - if(i == 0) { - *oX = 0; - *oY = 0; - *oZ = 0; - return; - } - - x /= i; - y /= i; - z1 /= i; - z2 /= i; - - switch(_rotation) { - case 0: - default: - break; - case 1: - x = (_maxValue - x); - y = (_maxValue - y); - break; - case 2: - y = (_maxValue - y); - break; - case 3: - x = (_maxValue - x); - break; - } - - int z = z1 + _maxValue - z2; - - *oX = x; - *oY = y; - *oZ = z2; -} +/** + * @file XPT2046.cpp + * @date 19.02.2016 + * @author Markus Sattler + * + * Copyright (c) 2015 Markus Sattler. All rights reserved. + * This file is originally part of the XPT2046 driver for Arduino. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "XPT2046.h" + +#define XPT2046_CFG_START 1<<7 + +#define XPT2046_CFG_MUX(v) ((v&0b111) << (4)) + +#define XPT2046_CFG_8BIT 1<<3 +#define XPT2046_CFG_12BIT (0) + +#define XPT2046_CFG_SER 1<<2 +#define XPT2046_CFG_DFR (0) + +#define XPT2046_CFG_PWR(v) ((v&0b11)) + +#define XPT2046_MUX_Y 0b101 +#define XPT2046_MUX_X 0b001 + +#define XPT2046_MUX_Z1 0b011 +#define XPT2046_MUX_Z2 0b100 + +XPT2046::XPT2046() { + _maxValue = 0x0fff; + _width = 480; + _height = 320; + _rotation = 0; + + spi_cs = 0; + z_average = 0; + tcfifo = "/tmp/TCfifo"; + + _minX = 0; + _minY = 0; + + _maxX = _maxValue; + _maxY = _maxValue; + + _lastX = -1; + _lastY = -1; + + _minChange = 10; + + spi_cs = + 1 << 0 | //Chip select 1 + 0 << 3 | //low idle clock polarity + 0 << 6 | //chip select active low + 0 << 22 | //chip select low polarity + 0 << 8 | //DMA disabled + 0 << 11; //Manual chip select + + + // FIFO file path + // Creating the named file(FIFO) + // mkfifo(, ) + mkfifo(tcfifo, 0666); +} + + +XPT2046::~XPT2046() { + +} + +int XPT2046::SpiWriteAndRead(unsigned char *data, int length) +{ + uint32_t old_spi_cs = spi->cs; + + for (int i = 0; i < length; i++) + { + spi->cs = spi_cs | 1 << 7;//Set TA to high + + while (!(spi->cs & (1 << 18))) ; //Poll TX Fifo until it has space + + spi->fifo = data[i]; + + while(! (spi->cs & (1 << 17))) ; //Wait until RX FIFO contains data + + //while(!(spi->cs & (1 << 16))); //Wait until DONE + + uint32_t spi_fifo = spi->fifo; + + spi->cs = (spi_cs & (~(1 << 7))) | 1 << 5 | 1 << 4; //Set TA to LOW //Clear Fifox + + data[i] = spi_fifo; + } + + return 0; +} + +void XPT2046::read_touchscreen() { + + uint32_t old_spi_cs = spi->cs; + uint32_t old_spi_clk = spi->clk; + //printBits(sizeof(old_spi_cs), (void*)&(old_spi_cs)); + spi->clk = 256; + SET_GPIO_MODE(GPIO_SPI0_CE0, 0x00); + SET_GPIO_MODE(GPIO_SPI0_CE1, 0x04); + // touch on low + + uint16_t x, y, z; + read(&x, &y, &z); + if (abs(x - _lastX) > _minChange || abs(y - _lastY) > _minChange) { + _lastX = x; + _lastY = y; + } + + if (z > 100) + { + char output[30] = ""; + sprintf(output, "x:%d, y:%d, z:%d\n", x, y, z); + int fd = open(tcfifo, O_WRONLY); + write(fd, output, strlen(output) + 1); + close(fd); + } + + + //spi->cs = (old_spi_cs | 1 << 4 | 1 << 5) & (~(1 << 7)); //Clear Fifos and TA + spi->cs = old_spi_cs; + spi->clk = old_spi_clk; + + SET_GPIO_MODE(GPIO_SPI0_CE0, 0x04); + SET_GPIO_MODE(GPIO_SPI0_CE1, 0x00); +} + +void XPT2046::setRotation(uint8_t m) { + _rotation = m % 4; +} + +void XPT2046::setCalibration(uint16_t minX, uint16_t minY, uint16_t maxX, uint16_t maxY) { + _minX = minX; + _minY = minY; + _maxX = maxX; + _maxY = maxY; +} + +void XPT2046::read(uint16_t * oX, uint16_t * oY, uint16_t * oZ) { + uint16_t x, y; + readRaw(&x, &y, oZ); + + uint32_t cX = x; + uint32_t cY = y; + + if(cX < _minX) { + cX = 0; + } else if(cX > _maxX) { + cX = _width; + } else { + cX -= _minX; + cX = ((cX << 8) / (((_maxX - _minX) << 8) / (_width << 8)) )>> 8; + } + + if(cY < _minY) { + cY = 0; + } else if(cY > _maxY) { + cY = _height; + } else { + cY -= _minY; + cY = ((cY << 8) / (((_maxY - _minY) << 8) / (_height << 8))) >> 8; + } + + *oX = cX; + *oY = cY; + *oZ = std::max(0,4096 - (int)(*oZ)); +} + +void XPT2046::readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ) { + uint32_t x = 0; + uint32_t y = 0; + uint32_t z1 = 0; + uint32_t z2 = 0; + uint8_t i = 0; + + for(; i < 15; i++) { + // SPI requires 32bit alignment + uint8_t buf[12] = { + (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Y) | XPT2046_CFG_PWR(3)), 0x00, 0x00, + (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_X) | XPT2046_CFG_PWR(3)), 0x00, 0x00, + (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z1)| XPT2046_CFG_PWR(3)), 0x00, 0x00, + (XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z2)| XPT2046_CFG_PWR(3)), 0x00, 0x00 + }; + + SpiWriteAndRead(buf, 12); + + y += (buf[1] << 8 | buf[2])>>3; + x += (buf[4] << 8 | buf[5])>>3; + z1 += (buf[7] << 8 | buf[8])>>3; + z2 += (buf[10] << 8 | buf[11])>>3; + } + + if(i == 0) { + *oX = 0; + *oY = 0; + *oZ = 0; + return; + } + + x /= i; + y /= i; + z1 /= i; + z2 /= i; + + switch(_rotation) { + case 0: + default: + break; + case 1: + x = (_maxValue - x); + y = (_maxValue - y); + break; + case 2: + y = (_maxValue - y); + break; + case 3: + x = (_maxValue - x); + break; + } + + int z = z1 + _maxValue - z2; + + *oX = x; + *oY = y; + *oZ = z2; +} diff --git a/XPT2046.h b/XPT2046.h index f50c1d7..f6c56c6 100644 --- a/XPT2046.h +++ b/XPT2046.h @@ -1,106 +1,106 @@ -/** - * @file XPT2046.h - * @date 19.02.2016 - * @author Markus Sattler - * - * Copyright (c) 2015 Markus Sattler. All rights reserved. - * This file is part of the XPT2046 driver for Arduino. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifndef XPT2046_H_ -#define XPT2046_H_ - -#include -#include //Needed for SPI port -#include //Needed for SPI port -#include //Needed for SPI port -#include -#include -#include -#include -#include -#include -#include - -#include "spi.h" - -class XPT2046 { - public: - - XPT2046(); - - ~XPT2046(); - - int SpiWriteAndRead(unsigned char *data, int length); - - void read_touchscreen(); - - void setRotation(uint8_t m); - void setCalibration(uint16_t minX, uint16_t minY, uint16_t maxX, uint16_t maxY); - - void read(uint16_t * oX, uint16_t * oY, uint16_t * oZ); - void readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ); - - static void printBits(size_t const size, void const * const ptr) - { - unsigned char *b = (unsigned char*) ptr; - unsigned char byte; - int i, j; - - for (i = size - 1; i >= 0; i--) - { - for (j = 7; j >= 0; j--) - { - byte = (b[i] >> j) & 1; - printf("%u", byte); - } - printf(" "); - } - puts(""); - } - - protected: - - uint16_t _width; - uint16_t _height; - - uint16_t _rotation; - - uint16_t _minX; - uint16_t _minY; - - uint16_t _maxX; - uint16_t _maxY; - - uint16_t _maxValue; - - int _lastX; - int _lastY; - - uint16_t _minChange; - - uint32_t spi_cs = 0; - - uint32_t z_average = 0; - - char * tcfifo = "/tmp/TCfifo"; -}; - - - -#endif /* XPT2046_H_ */ +/** + * @file XPT2046.h + * @date 19.02.2016 + * @author Markus Sattler + * + * Copyright (c) 2015 Markus Sattler. All rights reserved. + * This file is part of the XPT2046 driver for Arduino. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef XPT2046_H_ +#define XPT2046_H_ + +#include +#include //Needed for SPI port +#include //Needed for SPI port +#include //Needed for SPI port +#include +#include +#include +#include +#include +#include +#include + +#include "spi.h" + +class XPT2046 { + public: + + XPT2046(); + + ~XPT2046(); + + int SpiWriteAndRead(unsigned char *data, int length); + + void read_touchscreen(); + + void setRotation(uint8_t m); + void setCalibration(uint16_t minX, uint16_t minY, uint16_t maxX, uint16_t maxY); + + void read(uint16_t * oX, uint16_t * oY, uint16_t * oZ); + void readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ); + + static void printBits(size_t const size, void const * const ptr) + { + unsigned char *b = (unsigned char*) ptr; + unsigned char byte; + int i, j; + + for (i = size - 1; i >= 0; i--) + { + for (j = 7; j >= 0; j--) + { + byte = (b[i] >> j) & 1; + printf("%u", byte); + } + printf(" "); + } + puts(""); + } + + protected: + + uint16_t _width; + uint16_t _height; + + uint16_t _rotation; + + uint16_t _minX; + uint16_t _minY; + + uint16_t _maxX; + uint16_t _maxY; + + uint16_t _maxValue; + + int _lastX; + int _lastY; + + uint16_t _minChange; + + uint32_t spi_cs; + + uint32_t z_average; + + const char * tcfifo; +}; + + + +#endif /* XPT2046_H_ */ diff --git a/mpi3501.cpp b/mpi3501.cpp index 51a6fff..058c942 100644 --- a/mpi3501.cpp +++ b/mpi3501.cpp @@ -127,26 +127,45 @@ void InitKeDeiV63() spi->clk = SPI_BUS_CLOCK_DIVISOR; } -void TurnBacklightOff() -{ -} -void TurnBacklightOn() +void TurnBacklightOff() { +#if defined(GPIO_TFT_BACKLIGHT) && defined(BACKLIGHT_CONTROL) + SET_GPIO_MODE(GPIO_TFT_BACKLIGHT, 0x01); // Set backlight pin to digital 0/1 output mode (0x01) in case it had been PWM controlled + CLEAR_GPIO(GPIO_TFT_BACKLIGHT); // And turn the backlight off. +#endif } void TurnDisplayOff() { + TurnBacklightOff(); +#if 0 + QUEUE_SPI_TRANSFER(0x28/*Display OFF*/); + QUEUE_SPI_TRANSFER(0x10/*Enter Sleep Mode*/); + usleep(120*1000); // Sleep off can be sent 120msecs after entering sleep mode the earliest, so synchronously sleep here for that duration to be safe. +#endif +// printf("Turned display OFF\n"); } void TurnDisplayOn() { +#if 0 + QUEUE_SPI_TRANSFER(0x11/*Sleep Out*/); + usleep(120 * 1000); + QUEUE_SPI_TRANSFER(0x29/*Display ON*/); +#endif +#if defined(GPIO_TFT_BACKLIGHT) && defined(BACKLIGHT_CONTROL) + SET_GPIO_MODE(GPIO_TFT_BACKLIGHT, 0x01); // Set backlight pin to digital 0/1 output mode (0x01) in case it had been PWM controlled + SET_GPIO(GPIO_TFT_BACKLIGHT); // And turn the backlight on. +#endif +// printf("Turned display ON\n"); } void DeinitSPIDisplay() { ClearScreen(); - TurnDisplayOff(); + SPI_TRANSFER(/*Display OFF*/0x28); + TurnBacklightOff(); } #endif