KL25Z driving an ILI9320 LCD board with touch panel (HY28A-LCDB SPI)

Dependencies:   SPI_TFT_ILI9320 mbed

Committer:
frankvnk
Date:
Sun Mar 23 20:04:35 2014 +0000
Revision:
2:e21bf3a79d1f
Parent:
1:76f92f6b3737
variable positions in Touch.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 0:ff976ad09b2f 1 /******************************************************************
frankvnk 0:ff976ad09b2f 2 ***** *****
frankvnk 0:ff976ad09b2f 3 ***** Name: Touch.h *****
frankvnk 0:ff976ad09b2f 4 ***** Ver.: 1.0 *****
frankvnk 0:ff976ad09b2f 5 ***** Date: 04/01/2013 *****
frankvnk 0:ff976ad09b2f 6 ***** Auth: Frank Vannieuwkerke *****
frankvnk 0:ff976ad09b2f 7 ***** Erik Olieman *****
frankvnk 0:ff976ad09b2f 8 ***** Func: Touch driver for use with ADS7843 *****
frankvnk 0:ff976ad09b2f 9 ***** *****
frankvnk 0:ff976ad09b2f 10 ******************************************************************/
frankvnk 0:ff976ad09b2f 11
frankvnk 0:ff976ad09b2f 12 #ifndef MBED_Touch_H
frankvnk 0:ff976ad09b2f 13 #define MBED_Touch_H
frankvnk 0:ff976ad09b2f 14
frankvnk 0:ff976ad09b2f 15 #include "SPI_TFT_ILI9320.h"
frankvnk 0:ff976ad09b2f 16 #include "mbed.h"
frankvnk 0:ff976ad09b2f 17
frankvnk 0:ff976ad09b2f 18 typedef struct
frankvnk 0:ff976ad09b2f 19 {
frankvnk 0:ff976ad09b2f 20 int x;
frankvnk 0:ff976ad09b2f 21 int y;
frankvnk 0:ff976ad09b2f 22 } Coordinate;
frankvnk 0:ff976ad09b2f 23
frankvnk 0:ff976ad09b2f 24 typedef struct
frankvnk 0:ff976ad09b2f 25 {
frankvnk 0:ff976ad09b2f 26 int An,
frankvnk 0:ff976ad09b2f 27 Bn,
frankvnk 0:ff976ad09b2f 28 Cn,
frankvnk 0:ff976ad09b2f 29 Dn,
frankvnk 0:ff976ad09b2f 30 En,
frankvnk 0:ff976ad09b2f 31 Fn,
frankvnk 0:ff976ad09b2f 32 Divider ;
frankvnk 0:ff976ad09b2f 33 } Matrix;
frankvnk 0:ff976ad09b2f 34
frankvnk 0:ff976ad09b2f 35 class TouchScreenADS7843 {
frankvnk 0:ff976ad09b2f 36 public:
frankvnk 0:ff976ad09b2f 37 Coordinate display;
frankvnk 0:ff976ad09b2f 38 Coordinate screen;
frankvnk 0:ff976ad09b2f 39
frankvnk 0:ff976ad09b2f 40 /*
frankvnk 1:76f92f6b3737 41 * Create a Touchscreen object connected to SPI and two pins.
frankvnk 0:ff976ad09b2f 42 *
frankvnk 0:ff976ad09b2f 43 * @param mosi,miso,sclk SPI
frankvnk 0:ff976ad09b2f 44 * @param cs pin connected to CS of ADS7843
frankvnk 0:ff976ad09b2f 45 * @param irq pin connected to IRQ of ADS7843
frankvnk 0:ff976ad09b2f 46 * @param pointer to SPI_TFT constructor
frankvnk 0:ff976ad09b2f 47 *
frankvnk 0:ff976ad09b2f 48 */
frankvnk 0:ff976ad09b2f 49 TouchScreenADS7843(PinName tp_mosi, PinName tp_miso, PinName tp_sclk, PinName tp_cs, PinName tp_irq, SPI_TFT *_LCD);
frankvnk 0:ff976ad09b2f 50
frankvnk 0:ff976ad09b2f 51 /*
frankvnk 1:76f92f6b3737 52 * Draw a 2 by 2 dot on the LCD screen.
frankvnk 0:ff976ad09b2f 53 *
frankvnk 0:ff976ad09b2f 54 * @param x (horizontal position)
frankvnk 0:ff976ad09b2f 55 * @param y (vertical position)
frankvnk 0:ff976ad09b2f 56 * @param color (16 bit pixel color)
frankvnk 0:ff976ad09b2f 57 *
frankvnk 0:ff976ad09b2f 58 */
frankvnk 0:ff976ad09b2f 59 void TP_DrawPoint(unsigned int Xpos,unsigned int Ypos,unsigned int color);
frankvnk 0:ff976ad09b2f 60
frankvnk 0:ff976ad09b2f 61 /*
frankvnk 1:76f92f6b3737 62 * Obtain averaged data from ADS7846.
frankvnk 0:ff976ad09b2f 63 * does 9 consecutive reads and only stores averaged data
frankvnk 0:ff976ad09b2f 64 * when the 9 points are within the treshold limits.
frankvnk 0:ff976ad09b2f 65 *
frankvnk 0:ff976ad09b2f 66 * @param screenPTR (pointer to store data)
frankvnk 0:ff976ad09b2f 67 * @returns 1 on success
frankvnk 0:ff976ad09b2f 68 * @returns 0 on failure
frankvnk 0:ff976ad09b2f 69 *
frankvnk 0:ff976ad09b2f 70 * If called with screenPTR = NULL - 'screen' variable is used, otherwise (parameter) is used.
frankvnk 0:ff976ad09b2f 71 *
frankvnk 0:ff976ad09b2f 72 */
frankvnk 0:ff976ad09b2f 73 unsigned char Read_Ads7843(Coordinate * screenPtr = NULL);
frankvnk 0:ff976ad09b2f 74
frankvnk 0:ff976ad09b2f 75 /*
frankvnk 0:ff976ad09b2f 76 * Calibrate the touch panel.
frankvnk 0:ff976ad09b2f 77 * Three crosshairs are drawn and need to be touched in sequence.
frankvnk 0:ff976ad09b2f 78 * A calibration matrix is set accordingly.
frankvnk 0:ff976ad09b2f 79 *
frankvnk 0:ff976ad09b2f 80 */
frankvnk 0:ff976ad09b2f 81 void TouchPanel_Calibrate(void);
frankvnk 0:ff976ad09b2f 82
frankvnk 0:ff976ad09b2f 83 /*
frankvnk 1:76f92f6b3737 84 * Obtain real x,y coordinates.
frankvnk 0:ff976ad09b2f 85 * The x,y coordinates are calculated using the calibration matrix.
frankvnk 0:ff976ad09b2f 86 *
frankvnk 0:ff976ad09b2f 87 */
frankvnk 0:ff976ad09b2f 88 unsigned char getDisplayPoint(void);
frankvnk 0:ff976ad09b2f 89
frankvnk 1:76f92f6b3737 90 /*
frankvnk 1:76f92f6b3737 91 * Read touchpanel screensample and matrix values.
frankvnk 1:76f92f6b3737 92 *
frankvnk 1:76f92f6b3737 93 * In your code, create 2 structures using Matrix and screenPtr
frankvnk 1:76f92f6b3737 94 * and call this function with these structures.
frankvnk 1:76f92f6b3737 95 * the calibration values are returned into these structures.
frankvnk 1:76f92f6b3737 96 * Example:
frankvnk 1:76f92f6b3737 97 * Matrix matrix;
frankvnk 1:76f92f6b3737 98 * Coordinate ScreenSample[3];
frankvnk 1:76f92f6b3737 99 * GetCalibration(&matrix, &ScreenSample[0]);
frankvnk 1:76f92f6b3737 100 */
frankvnk 1:76f92f6b3737 101 void GetCalibration(Matrix * matrixPtr, Coordinate * screenPtr);
frankvnk 1:76f92f6b3737 102
frankvnk 1:76f92f6b3737 103 /*
frankvnk 1:76f92f6b3737 104 * Set touchpanel calibration using screensample and matrix values.
frankvnk 1:76f92f6b3737 105 *
frankvnk 1:76f92f6b3737 106 * In your code, create 2 structures based on Matrix and screenPtr,
frankvnk 1:76f92f6b3737 107 * copy saved calibration values into these structures
frankvnk 1:76f92f6b3737 108 * and call this function with these structures.
frankvnk 1:76f92f6b3737 109 * Example:
frankvnk 1:76f92f6b3737 110 * Matrix matrix;
frankvnk 1:76f92f6b3737 111 * Coordinate ScreenSample[3];
frankvnk 1:76f92f6b3737 112 * <pseudocode> load matrix with values from external storage (eg eeprom).
frankvnk 1:76f92f6b3737 113 * <pseudocode> load ScreenSample with values from external storage (eg eeprom).
frankvnk 1:76f92f6b3737 114 * SetCalibration(&matrix, &ScreenSample[0]);
frankvnk 1:76f92f6b3737 115 */
frankvnk 1:76f92f6b3737 116 void SetCalibration(Matrix * matrixPtr, Coordinate * screenPtr);
frankvnk 1:76f92f6b3737 117
frankvnk 2:e21bf3a79d1f 118 SPI_TFT *LCD;
frankvnk 2:e21bf3a79d1f 119 SPI _tp_spi;
frankvnk 2:e21bf3a79d1f 120 DigitalOut _tp_cs;
frankvnk 2:e21bf3a79d1f 121 DigitalIn _tp_irq;
frankvnk 2:e21bf3a79d1f 122
frankvnk 0:ff976ad09b2f 123 protected:
frankvnk 0:ff976ad09b2f 124
frankvnk 0:ff976ad09b2f 125 #define SPI_RD_DELAY 1
frankvnk 0:ff976ad09b2f 126 #define CHX 0xd0 // 12 bit mode
frankvnk 0:ff976ad09b2f 127 #define CHY 0x90
frankvnk 0:ff976ad09b2f 128
frankvnk 0:ff976ad09b2f 129 Coordinate DisplaySample[3];
frankvnk 0:ff976ad09b2f 130 Coordinate ScreenSample[3];
frankvnk 0:ff976ad09b2f 131 Matrix matrix;
frankvnk 0:ff976ad09b2f 132
frankvnk 0:ff976ad09b2f 133 /*
frankvnk 0:ff976ad09b2f 134 * Obtain raw x,y data from ADS7846
frankvnk 0:ff976ad09b2f 135 *
frankvnk 0:ff976ad09b2f 136 * @param pointer to raw x and y coordinates (pointer to store data)
frankvnk 0:ff976ad09b2f 137 * @returns x (horizontal position)
frankvnk 0:ff976ad09b2f 138 * @returns y (vertical position)
frankvnk 0:ff976ad09b2f 139 *
frankvnk 0:ff976ad09b2f 140 */
frankvnk 0:ff976ad09b2f 141 void TP_GetAdXY(int *x,int *y);
frankvnk 0:ff976ad09b2f 142
frankvnk 0:ff976ad09b2f 143 /*
frankvnk 0:ff976ad09b2f 144 * Obtain raw single channel data from ADS7846 (Called by TP_GetADXY)
frankvnk 0:ff976ad09b2f 145 *
frankvnk 0:ff976ad09b2f 146 * @param channel to be read (CHX or CHY)
frankvnk 0:ff976ad09b2f 147 * @returns raw scaled down value (return value range must be between 0 and 1024)
frankvnk 0:ff976ad09b2f 148 *
frankvnk 0:ff976ad09b2f 149 */
frankvnk 0:ff976ad09b2f 150 int Read_XY(unsigned char XY);
frankvnk 0:ff976ad09b2f 151
frankvnk 0:ff976ad09b2f 152 /*
frankvnk 0:ff976ad09b2f 153 * Draw a calibration crosshair on the LCD screen
frankvnk 0:ff976ad09b2f 154 *
frankvnk 0:ff976ad09b2f 155 * @param x (horizontal position)
frankvnk 0:ff976ad09b2f 156 * @param y (vertical position)
frankvnk 0:ff976ad09b2f 157 *
frankvnk 0:ff976ad09b2f 158 */
frankvnk 0:ff976ad09b2f 159 void DrawCross(unsigned int Xpos,unsigned int Ypos);
frankvnk 0:ff976ad09b2f 160
frankvnk 0:ff976ad09b2f 161 /*
frankvnk 0:ff976ad09b2f 162 * Set the calibration matrix
frankvnk 0:ff976ad09b2f 163 *
frankvnk 0:ff976ad09b2f 164 * @param displayPTR (pointer to display data)
frankvnk 0:ff976ad09b2f 165 * @param screenPTR (pointer to screen data)
frankvnk 0:ff976ad09b2f 166 * @param matrixPTR (pointer to calibration matrix)
frankvnk 0:ff976ad09b2f 167 *
frankvnk 0:ff976ad09b2f 168 * @returns 0 when matrix.divider != 0
frankvnk 0:ff976ad09b2f 169 * @returns 1 when matrix.divider = 0
frankvnk 0:ff976ad09b2f 170 *
frankvnk 0:ff976ad09b2f 171 */
frankvnk 0:ff976ad09b2f 172 unsigned char setCalibrationMatrix( Coordinate * displayPtr,Coordinate * screenPtr,Matrix * matrixPtr);
frankvnk 0:ff976ad09b2f 173
frankvnk 0:ff976ad09b2f 174 };
frankvnk 0:ff976ad09b2f 175 #endif