Whitworth-EN173-Resources / DmTouch_UniGraphic

Fork of DmTouch_UniGraphic by Whitworth-EN173-2016

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DmTouch.h Source File

DmTouch.h

00001 /**********************************************************************************************
00002  Copyright (c) 2014 DisplayModule. All rights reserved.
00003  
00004  Redistribution and use of this source code, part of this source code or any compiled binary
00005  based on this source code is permitted as long as the above copyright notice and following
00006  disclaimer is retained.
00007  
00008  DISCLAIMER:
00009  THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
00010  NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
00011  ********************************************************************************************/
00012  
00013 #ifndef DM_TOUCH_h
00014 #define DM_TOUCH_h
00015  
00016 #include "mbed.h"
00017 //#include "dm_platform.h"  // What is needed from dm_platform.h?
00018 
00019 #define sbi(reg, _bitmask) (*(reg) = 1)
00020 #define cbi(reg, _bitmask) (*(reg) = 0)
00021 #define delay(ms) wait_ms(ms)
00022 #define pulse_high(reg, _bitmask) do { *(reg) = 1; *(reg) = 0; } while(0)
00023 #define pulse_low(reg, _bitmask) do { *(reg) = 0; *(reg) = 1; } while(0)
00024 #define slow_pulse_high(reg, _bitmask) do {\
00025    *(reg) = 1;    \
00026    slow_pulse_delay(); \
00027    *(reg) = 0;    \
00028    slow_pulse_delay(); \
00029 } while(0)
00030 #define slow_pulse_low(reg, _bitmask) do {\
00031    *(reg) = 0;    \
00032    slow_pulse_delay(); \
00033    *(reg) = 1;    \
00034    slow_pulse_delay(); \
00035 } while(0)
00036 #define slow_pulse_delay()
00037  
00038 typedef struct calibrationMatrix {
00039   int  a, b, c, d, e, f;
00040 } CalibrationMatrix;
00041  
00042 class DmTouch
00043 {
00044 public:
00045   enum Display {
00046     DM_TFT28_103 = 103,
00047     DM_TFT24_104 = 104,
00048     DM_TFT28_105 = 105,
00049     DM_TFT35_107 = 107,
00050     DM_TFT43_108 = 108,
00051     DM_TFT50_111 = 111
00052   };
00053  
00054   enum SpiMode {
00055     Auto,
00056     Software,
00057     Hardware
00058   };
00059   
00060   enum TouchId{
00061     IC_8875 = 0x8875,
00062     IC_2046 = 0x2046        
00063   };
00064 
00065   DmTouch(Display disp, PinName mosi, PinName miso, PinName clk, PinName cs, PinName irq);      // Add cs and irq to input parameters
00066   void init();
00067   void readTouchData(uint16_t& posX, uint16_t& posY, bool& touching);
00068   bool isTouched();
00069   bool getMiddleXY(uint16_t &x, uint16_t &y); // Raw Touch Data, used for calibration
00070   void setCalibrationMatrix(CalibrationMatrix calibrationMatrix);
00071   void setPrecison(uint8_t samplesPerMeasurement);
00072   void waitForTouch();
00073   void waitForTouchRelease();
00074   uint32_t rescaleFactor() { return 1000000; };
00075   Display getDisplay() { return _disp; };
00076   static CalibrationMatrix getDefaultCalibrationData(Display disp);                             // (JML) Added to this class because avoid DmTouchCalibration initially
00077   void setOrientation(char orient);                                                             // Set screen orientation mode
00078  
00079 private:
00080   void spiWrite(uint8_t data);
00081   uint8_t spiRead();
00082   uint16_t readData12(uint8_t command);
00083   void enableIrq();
00084   void readRawData(uint16_t &x, uint16_t &y);
00085   void getAverageXY(uint16_t &x, uint16_t &y);
00086   uint16_t getDisplayCoordinateX(uint16_t x_touch, uint16_t y_touch);
00087   uint16_t getDisplayCoordinateY(uint16_t x_touch, uint16_t y_touch);
00088   uint16_t calculateMiddleValue(uint16_t values[], uint8_t count);
00089   bool isSampleValid();
00090  
00091   Display _disp;
00092   uint16_t _width, _height;
00093   bool _hardwareSpi;
00094   uint8_t _samplesPerMeasurement;
00095   CalibrationMatrix _calibrationMatrix;
00096   uint16_t _touch_id;
00097   char _orient;                                                         // Adjust to changes in screen orientation
00098  
00099   PinName _cs, _clk, _mosi, _miso, _irq;
00100   DigitalOut *_pinDC, *_pinCS, *_pinCLK, *_pinMOSI, *_led;
00101   DigitalIn *_pinMISO, *_pinIrq;
00102   SPI *_spi;
00103 };
00104 #endif