Touch driver for companion boards (VKLCD50RTA & VKLCD70RT)
Touch.hpp@0:0383b9f88d72, 2017-11-03 (annotated)
- Committer:
- tvendov
- Date:
- Fri Nov 03 08:52:07 2017 +0000
- Revision:
- 0:0383b9f88d72
Touch panel driver initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tvendov | 0:0383b9f88d72 | 1 | /**************************************************************************//** |
tvendov | 0:0383b9f88d72 | 2 | * @file Touch.h |
tvendov | 0:0383b9f88d72 | 3 | * @brief Touch driver class for VK-LCD panels |
tvendov | 0:0383b9f88d72 | 4 | ******************************************************************************/ |
tvendov | 0:0383b9f88d72 | 5 | |
tvendov | 0:0383b9f88d72 | 6 | #ifndef MBED_TOUCH_H |
tvendov | 0:0383b9f88d72 | 7 | #define MBED_TOUCH_H |
tvendov | 0:0383b9f88d72 | 8 | |
tvendov | 0:0383b9f88d72 | 9 | #include "mbed.h" |
tvendov | 0:0383b9f88d72 | 10 | #include "stmpe811iic.h" |
tvendov | 0:0383b9f88d72 | 11 | #include <string> |
tvendov | 0:0383b9f88d72 | 12 | |
tvendov | 0:0383b9f88d72 | 13 | |
tvendov | 0:0383b9f88d72 | 14 | |
tvendov | 0:0383b9f88d72 | 15 | namespace Vekatech { |
tvendov | 0:0383b9f88d72 | 16 | |
tvendov | 0:0383b9f88d72 | 17 | /*! @enum Touch_type_t |
tvendov | 0:0383b9f88d72 | 18 | @brief Type of the touch controller |
tvendov | 0:0383b9f88d72 | 19 | */ |
tvendov | 0:0383b9f88d72 | 20 | typedef enum { |
tvendov | 0:0383b9f88d72 | 21 | RESISTIVE = 0, /*!< Resistive */ |
tvendov | 0:0383b9f88d72 | 22 | CAPACITIVE /*!< Capacitive */ |
tvendov | 0:0383b9f88d72 | 23 | } Touch_type_t; |
tvendov | 0:0383b9f88d72 | 24 | |
tvendov | 0:0383b9f88d72 | 25 | /*! @enum Touch_interface_type_t |
tvendov | 0:0383b9f88d72 | 26 | @brief Communication interface |
tvendov | 0:0383b9f88d72 | 27 | */ |
tvendov | 0:0383b9f88d72 | 28 | typedef enum { |
tvendov | 0:0383b9f88d72 | 29 | I_2_C = 0, /*!< I2C */ |
tvendov | 0:0383b9f88d72 | 30 | S_P_I /*!< SPI */ |
tvendov | 0:0383b9f88d72 | 31 | } Touch_communication_type_t; |
tvendov | 0:0383b9f88d72 | 32 | |
tvendov | 0:0383b9f88d72 | 33 | /*! @enum IRQ_trigger_t |
tvendov | 0:0383b9f88d72 | 34 | @brief Type of the interrupt |
tvendov | 0:0383b9f88d72 | 35 | */ |
tvendov | 0:0383b9f88d72 | 36 | typedef enum { |
tvendov | 0:0383b9f88d72 | 37 | INT_ON_EDGE = 0, /*!< generate INT on front change */ |
tvendov | 0:0383b9f88d72 | 38 | INT_ON_LEVEL /*!< generate INT on active level */ |
tvendov | 0:0383b9f88d72 | 39 | } IRQ_trigger_t; |
tvendov | 0:0383b9f88d72 | 40 | |
tvendov | 0:0383b9f88d72 | 41 | /*! @enum IRQ_polarity_t |
tvendov | 0:0383b9f88d72 | 42 | @brief Edge of a signal |
tvendov | 0:0383b9f88d72 | 43 | */ |
tvendov | 0:0383b9f88d72 | 44 | typedef enum { |
tvendov | 0:0383b9f88d72 | 45 | RISING_OR_ACTIVE_HI = 0, /*!< Rising edge/HI level */ |
tvendov | 0:0383b9f88d72 | 46 | FALLING_OR_ACTIVE_LO /*!< Falling edge/LOW level */ |
tvendov | 0:0383b9f88d72 | 47 | } IRQ_polarity_t; |
tvendov | 0:0383b9f88d72 | 48 | |
tvendov | 0:0383b9f88d72 | 49 | /*! @struct touch_config_t |
tvendov | 0:0383b9f88d72 | 50 | @brief Touch Config structure |
tvendov | 0:0383b9f88d72 | 51 | */ |
tvendov | 0:0383b9f88d72 | 52 | typedef struct { |
tvendov | 0:0383b9f88d72 | 53 | string name; /*!< Name of the Touch driver */ |
tvendov | 0:0383b9f88d72 | 54 | int screen; /*!< reserved */ |
tvendov | 0:0383b9f88d72 | 55 | Touch_type_t type; /*!< Resistive or Capacitive */ |
tvendov | 0:0383b9f88d72 | 56 | struct { /*!< I2C or SPI & the pins of selected periphery */ |
tvendov | 0:0383b9f88d72 | 57 | Touch_communication_type_t type; |
tvendov | 0:0383b9f88d72 | 58 | PinName sda; |
tvendov | 0:0383b9f88d72 | 59 | PinName scl; |
tvendov | 0:0383b9f88d72 | 60 | PinName mosi; |
tvendov | 0:0383b9f88d72 | 61 | PinName miso; |
tvendov | 0:0383b9f88d72 | 62 | PinName sclk; |
tvendov | 0:0383b9f88d72 | 63 | PinName ssel; |
tvendov | 0:0383b9f88d72 | 64 | int freq; |
tvendov | 0:0383b9f88d72 | 65 | }interface; |
tvendov | 0:0383b9f88d72 | 66 | struct { /*!< IRQ : front and pin of selected periphery */ |
tvendov | 0:0383b9f88d72 | 67 | IRQ_trigger_t trigger; |
tvendov | 0:0383b9f88d72 | 68 | IRQ_polarity_t polarity; |
tvendov | 0:0383b9f88d72 | 69 | PinName pin; |
tvendov | 0:0383b9f88d72 | 70 | }activity_irq; |
tvendov | 0:0383b9f88d72 | 71 | }touch_config_t; |
tvendov | 0:0383b9f88d72 | 72 | |
tvendov | 0:0383b9f88d72 | 73 | extern const touch_config_t STMPE811_cfg; |
tvendov | 0:0383b9f88d72 | 74 | |
tvendov | 0:0383b9f88d72 | 75 | |
tvendov | 0:0383b9f88d72 | 76 | |
tvendov | 0:0383b9f88d72 | 77 | /*! @class Touch |
tvendov | 0:0383b9f88d72 | 78 | * @brief Touch driver class for VK-LCD panels |
tvendov | 0:0383b9f88d72 | 79 | * |
tvendov | 0:0383b9f88d72 | 80 | * Example: |
tvendov | 0:0383b9f88d72 | 81 | * |
tvendov | 0:0383b9f88d72 | 82 | * @code |
tvendov | 0:0383b9f88d72 | 83 | * #include "mbed.h" |
tvendov | 0:0383b9f88d72 | 84 | * #include "Touch.hpp" |
tvendov | 0:0383b9f88d72 | 85 | * |
tvendov | 0:0383b9f88d72 | 86 | * using namespace Vekatech; |
tvendov | 0:0383b9f88d72 | 87 | * |
tvendov | 0:0383b9f88d72 | 88 | * Touch STMPE811; |
tvendov | 0:0383b9f88d72 | 89 | * |
tvendov | 0:0383b9f88d72 | 90 | * // main() runs in its own thread in the OS |
tvendov | 0:0383b9f88d72 | 91 | * int main() |
tvendov | 0:0383b9f88d72 | 92 | * { |
tvendov | 0:0383b9f88d72 | 93 | * osEvent evt; |
tvendov | 0:0383b9f88d72 | 94 | * |
tvendov | 0:0383b9f88d72 | 95 | * STMPE811.Init(); |
tvendov | 0:0383b9f88d72 | 96 | * |
tvendov | 0:0383b9f88d72 | 97 | * while (true) |
tvendov | 0:0383b9f88d72 | 98 | * { |
tvendov | 0:0383b9f88d72 | 99 | * evt = STMPE811.Wait_MSG(); |
tvendov | 0:0383b9f88d72 | 100 | * if (evt.status == osEventMail) |
tvendov | 0:0383b9f88d72 | 101 | * { |
tvendov | 0:0383b9f88d72 | 102 | * Touch::MSGQ_t* msg = (Touch::MSGQ_t*)evt.value.p; |
tvendov | 0:0383b9f88d72 | 103 | * printf("[EV (%d)], start_idx: %d end_idx: %d\r\n", msg->event, msg->start_idx, msg->stop_idx); |
tvendov | 0:0383b9f88d72 | 104 | * |
tvendov | 0:0383b9f88d72 | 105 | * switch(msg->event) |
tvendov | 0:0383b9f88d72 | 106 | * { |
tvendov | 0:0383b9f88d72 | 107 | * case Touch::EV_STYLUS_UP: |
tvendov | 0:0383b9f88d72 | 108 | * printf("[UP (%d)] X: %d Y: %d Z: %f\r\n", msg->stop_idx, STMPE811.xyz_data[msg->stop_idx].axis.x, STMPE811.xyz_data[msg->stop_idx].axis.y, STMPE811.xyz_data[msg->stop_idx].axis.z ); |
tvendov | 0:0383b9f88d72 | 109 | * break; |
tvendov | 0:0383b9f88d72 | 110 | * |
tvendov | 0:0383b9f88d72 | 111 | * case Touch::EV_STYLUS_DOWN: |
tvendov | 0:0383b9f88d72 | 112 | * printf("[DOWN (%d)] X: %d Y: %d Z: %f\r\n", msg->start_idx, STMPE811.xyz_data[msg->start_idx].axis.x, STMPE811.xyz_data[msg->start_idx].axis.y, STMPE811.xyz_data[msg->start_idx].axis.z ); |
tvendov | 0:0383b9f88d72 | 113 | * break; |
tvendov | 0:0383b9f88d72 | 114 | * |
tvendov | 0:0383b9f88d72 | 115 | * case Touch::EV_STYLUS_HOLD: |
tvendov | 0:0383b9f88d72 | 116 | * if(msg->stop_idx >= (STMPE811.Get_Dot_thd()-1) ) |
tvendov | 0:0383b9f88d72 | 117 | * for(int i = (msg->stop_idx+1 - STMPE811.Get_Dot_thd()); i<=msg->stop_idx; i++) |
tvendov | 0:0383b9f88d72 | 118 | * printf("[HOLD (%d)] X: %d Y: %d Z: %f\r\n", i, STMPE811.xyz_data[i].axis.x, STMPE811.xyz_data[i].axis.y, STMPE811.xyz_data[i].axis.z ); |
tvendov | 0:0383b9f88d72 | 119 | * break; |
tvendov | 0:0383b9f88d72 | 120 | * |
tvendov | 0:0383b9f88d72 | 121 | * default: break; |
tvendov | 0:0383b9f88d72 | 122 | * } |
tvendov | 0:0383b9f88d72 | 123 | * |
tvendov | 0:0383b9f88d72 | 124 | * STMPE811.Del_MSG((Touch::MSGQ_t*)evt.value.p); |
tvendov | 0:0383b9f88d72 | 125 | * } |
tvendov | 0:0383b9f88d72 | 126 | * } |
tvendov | 0:0383b9f88d72 | 127 | * } |
tvendov | 0:0383b9f88d72 | 128 | * @endcode |
tvendov | 0:0383b9f88d72 | 129 | */ |
tvendov | 0:0383b9f88d72 | 130 | class Touch : public I2C, public InterruptIn |
tvendov | 0:0383b9f88d72 | 131 | { |
tvendov | 0:0383b9f88d72 | 132 | public: |
tvendov | 0:0383b9f88d72 | 133 | /*! @enum Init_err_t |
tvendov | 0:0383b9f88d72 | 134 | @brief Error codes |
tvendov | 0:0383b9f88d72 | 135 | */ |
tvendov | 0:0383b9f88d72 | 136 | typedef enum { |
tvendov | 0:0383b9f88d72 | 137 | TOUCH_OK = 0, /*!< Initialization successful */ |
tvendov | 0:0383b9f88d72 | 138 | TOUCH_INIT_ERR = -1, /*!< Communication interface err while configuring driver */ |
tvendov | 0:0383b9f88d72 | 139 | TOUCH_UNSUPP_ERR = -2, /*!< Unsupported driver */ |
tvendov | 0:0383b9f88d72 | 140 | TOUCH_ERR = -3, /*!< unknown error */ |
tvendov | 0:0383b9f88d72 | 141 | } init_err_t; |
tvendov | 0:0383b9f88d72 | 142 | |
tvendov | 0:0383b9f88d72 | 143 | /*! @enum EVENTLIST_t |
tvendov | 0:0383b9f88d72 | 144 | @brief Type of the Event: |
tvendov | 0:0383b9f88d72 | 145 | */ |
tvendov | 0:0383b9f88d72 | 146 | typedef enum { |
tvendov | 0:0383b9f88d72 | 147 | EV_STYLUS_UP =0, |
tvendov | 0:0383b9f88d72 | 148 | EV_STYLUS_DOWN, |
tvendov | 0:0383b9f88d72 | 149 | EV_STYLUS_HOLD, |
tvendov | 0:0383b9f88d72 | 150 | //EV_STYLUS_MOVE, |
tvendov | 0:0383b9f88d72 | 151 | NEVENTS |
tvendov | 0:0383b9f88d72 | 152 | }EVENTLIST_t; |
tvendov | 0:0383b9f88d72 | 153 | |
tvendov | 0:0383b9f88d72 | 154 | /*! @struct MSGQ_t |
tvendov | 0:0383b9f88d72 | 155 | @brief Touch Message structure |
tvendov | 0:0383b9f88d72 | 156 | */ |
tvendov | 0:0383b9f88d72 | 157 | typedef struct { |
tvendov | 0:0383b9f88d72 | 158 | EVENTLIST_t event; |
tvendov | 0:0383b9f88d72 | 159 | unsigned short start_idx; |
tvendov | 0:0383b9f88d72 | 160 | unsigned short stop_idx; |
tvendov | 0:0383b9f88d72 | 161 | }MSGQ_t; |
tvendov | 0:0383b9f88d72 | 162 | |
tvendov | 0:0383b9f88d72 | 163 | /** Constructor method of Touch object |
tvendov | 0:0383b9f88d72 | 164 | */ |
tvendov | 0:0383b9f88d72 | 165 | Touch( const touch_config_t * tp_cfg = &STMPE811_cfg ); |
tvendov | 0:0383b9f88d72 | 166 | |
tvendov | 0:0383b9f88d72 | 167 | /** Destructor method of Touch object |
tvendov | 0:0383b9f88d72 | 168 | */ |
tvendov | 0:0383b9f88d72 | 169 | virtual ~Touch( void ); |
tvendov | 0:0383b9f88d72 | 170 | |
tvendov | 0:0383b9f88d72 | 171 | /** Touch controller initialization |
tvendov | 0:0383b9f88d72 | 172 | * @retval Error code |
tvendov | 0:0383b9f88d72 | 173 | */ |
tvendov | 0:0383b9f88d72 | 174 | init_err_t Init(); |
tvendov | 0:0383b9f88d72 | 175 | |
tvendov | 0:0383b9f88d72 | 176 | /** Set Calibration data (raw data interpretation) |
tvendov | 0:0383b9f88d72 | 177 | * @retval Error code |
tvendov | 0:0383b9f88d72 | 178 | */ |
tvendov | 0:0383b9f88d72 | 179 | virtual init_err_t Clb_Setup(); |
tvendov | 0:0383b9f88d72 | 180 | |
tvendov | 0:0383b9f88d72 | 181 | /** Set Touch Controller settings |
tvendov | 0:0383b9f88d72 | 182 | * @retval Error code |
tvendov | 0:0383b9f88d72 | 183 | */ |
tvendov | 0:0383b9f88d72 | 184 | virtual init_err_t Drv_Setup(); |
tvendov | 0:0383b9f88d72 | 185 | |
tvendov | 0:0383b9f88d72 | 186 | /** Get Status of the pen |
tvendov | 0:0383b9f88d72 | 187 | * @retval true : StylusDown |
tvendov | 0:0383b9f88d72 | 188 | * false : StyluUp |
tvendov | 0:0383b9f88d72 | 189 | */ |
tvendov | 0:0383b9f88d72 | 190 | virtual bool Get_Pen_Status(); |
tvendov | 0:0383b9f88d72 | 191 | |
tvendov | 0:0383b9f88d72 | 192 | /** Get one sample of data |
tvendov | 0:0383b9f88d72 | 193 | * @param[in] raw: pointer to ring buffer to store the samples |
tvendov | 0:0383b9f88d72 | 194 | */ |
tvendov | 0:0383b9f88d72 | 195 | virtual void Get_Data( unsigned long long * raw ); |
tvendov | 0:0383b9f88d72 | 196 | |
tvendov | 0:0383b9f88d72 | 197 | /** Get all available samples of data |
tvendov | 0:0383b9f88d72 | 198 | * @param[in] raw: pointer to ring buffer to store the samples |
tvendov | 0:0383b9f88d72 | 199 | * @retval samples count |
tvendov | 0:0383b9f88d72 | 200 | */ |
tvendov | 0:0383b9f88d72 | 201 | virtual int Get_Fifo( unsigned long long * raw ); |
tvendov | 0:0383b9f88d72 | 202 | |
tvendov | 0:0383b9f88d72 | 203 | /** Transfer function (from raw coordinates to screen coordinates) |
tvendov | 0:0383b9f88d72 | 204 | * @param[in] points : number of samples to transfer |
tvendov | 0:0383b9f88d72 | 205 | */ |
tvendov | 0:0383b9f88d72 | 206 | virtual void Get_XYZ( int points ); |
tvendov | 0:0383b9f88d72 | 207 | |
tvendov | 0:0383b9f88d72 | 208 | /** New Data available |
tvendov | 0:0383b9f88d72 | 209 | */ |
tvendov | 0:0383b9f88d72 | 210 | static void Irq_Alert(); |
tvendov | 0:0383b9f88d72 | 211 | |
tvendov | 0:0383b9f88d72 | 212 | /** Pull the new samples if new data is available |
tvendov | 0:0383b9f88d72 | 213 | */ |
tvendov | 0:0383b9f88d72 | 214 | virtual void Handle_touch(); |
tvendov | 0:0383b9f88d72 | 215 | |
tvendov | 0:0383b9f88d72 | 216 | /** Get index of the last sample in the ring buffer |
tvendov | 0:0383b9f88d72 | 217 | */ |
tvendov | 0:0383b9f88d72 | 218 | virtual int Get_Last_Idx(); |
tvendov | 0:0383b9f88d72 | 219 | |
tvendov | 0:0383b9f88d72 | 220 | /** Get the minimum amounts of dots when pen is hold down |
tvendov | 0:0383b9f88d72 | 221 | */ |
tvendov | 0:0383b9f88d72 | 222 | virtual int Get_Dot_thd(); |
tvendov | 0:0383b9f88d72 | 223 | |
tvendov | 0:0383b9f88d72 | 224 | /** Wait message from the touch thread |
tvendov | 0:0383b9f88d72 | 225 | */ |
tvendov | 0:0383b9f88d72 | 226 | virtual osEvent Wait_MSG( uint32_t millisec=osWaitForever ) { return mail.get( millisec ); }; |
tvendov | 0:0383b9f88d72 | 227 | |
tvendov | 0:0383b9f88d72 | 228 | /** Delete message from the mail queue |
tvendov | 0:0383b9f88d72 | 229 | */ |
tvendov | 0:0383b9f88d72 | 230 | virtual osStatus Del_MSG( MSGQ_t *ptr ) { return mail.free( ptr ); }; |
tvendov | 0:0383b9f88d72 | 231 | |
tvendov | 0:0383b9f88d72 | 232 | /** raw coordinates of the last received dot |
tvendov | 0:0383b9f88d72 | 233 | */ |
tvendov | 0:0383b9f88d72 | 234 | unsigned short adc_x; |
tvendov | 0:0383b9f88d72 | 235 | unsigned short adc_y; |
tvendov | 0:0383b9f88d72 | 236 | unsigned short adc_z; |
tvendov | 0:0383b9f88d72 | 237 | |
tvendov | 0:0383b9f88d72 | 238 | /** Coordinates of the last received dot |
tvendov | 0:0383b9f88d72 | 239 | */ |
tvendov | 0:0383b9f88d72 | 240 | short x; |
tvendov | 0:0383b9f88d72 | 241 | short y; |
tvendov | 0:0383b9f88d72 | 242 | float z; |
tvendov | 0:0383b9f88d72 | 243 | |
tvendov | 0:0383b9f88d72 | 244 | /** Raw History of the received dots |
tvendov | 0:0383b9f88d72 | 245 | */ |
tvendov | 0:0383b9f88d72 | 246 | const touch_raw_data_t *adc_data; |
tvendov | 0:0383b9f88d72 | 247 | |
tvendov | 0:0383b9f88d72 | 248 | /** History of the received dots |
tvendov | 0:0383b9f88d72 | 249 | */ |
tvendov | 0:0383b9f88d72 | 250 | const touch_screen_data_t *xyz_data; |
tvendov | 0:0383b9f88d72 | 251 | |
tvendov | 0:0383b9f88d72 | 252 | protected: |
tvendov | 0:0383b9f88d72 | 253 | /** Send message to the main application |
tvendov | 0:0383b9f88d72 | 254 | */ |
tvendov | 0:0383b9f88d72 | 255 | osStatus MSG(EVENTLIST_t touch_msg, unsigned short touch_on, unsigned short touch_off) |
tvendov | 0:0383b9f88d72 | 256 | { |
tvendov | 0:0383b9f88d72 | 257 | MSGQ_t * msg = mail.alloc(); |
tvendov | 0:0383b9f88d72 | 258 | msg->event = touch_msg; |
tvendov | 0:0383b9f88d72 | 259 | msg->start_idx = touch_on; |
tvendov | 0:0383b9f88d72 | 260 | msg->stop_idx = touch_off; |
tvendov | 0:0383b9f88d72 | 261 | return mail.put(msg); |
tvendov | 0:0383b9f88d72 | 262 | }; |
tvendov | 0:0383b9f88d72 | 263 | |
tvendov | 0:0383b9f88d72 | 264 | int last_xyz_idx; |
tvendov | 0:0383b9f88d72 | 265 | int dot_thd; |
tvendov | 0:0383b9f88d72 | 266 | unsigned char fraction; |
tvendov | 0:0383b9f88d72 | 267 | touch_raw_data_t raw_data[FIFO_DEPTH]; |
tvendov | 0:0383b9f88d72 | 268 | touch_screen_data_t screen_data[FIFO_DEPTH]; |
tvendov | 0:0383b9f88d72 | 269 | const touch_config_t *touch_cfg; |
tvendov | 0:0383b9f88d72 | 270 | touch_calib_data_t calib; |
tvendov | 0:0383b9f88d72 | 271 | Thread thd; |
tvendov | 0:0383b9f88d72 | 272 | Mail<MSGQ_t, 32> mail; |
tvendov | 0:0383b9f88d72 | 273 | }; |
tvendov | 0:0383b9f88d72 | 274 | |
tvendov | 0:0383b9f88d72 | 275 | } // namespace Vekatech |
tvendov | 0:0383b9f88d72 | 276 | |
tvendov | 0:0383b9f88d72 | 277 | #endif /* MBED_TOUCH_H */ |