Vekatech / Touch
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Touch.hpp Source File

Touch.hpp

00001 /**************************************************************************//**
00002  * @file         Touch.h
00003  * @brief        Touch driver class for VK-LCD panels
00004 ******************************************************************************/
00005 
00006 #ifndef MBED_TOUCH_H
00007 #define MBED_TOUCH_H
00008 
00009 #include "mbed.h"
00010 #include "stmpe811iic.h"
00011 #include <string>
00012 
00013 
00014 
00015 namespace Vekatech {
00016 
00017     /*! @enum Touch_type_t
00018         @brief Type of the touch controller
00019     */
00020     typedef enum {
00021         RESISTIVE = 0, /*!< Resistive */
00022         CAPACITIVE     /*!< Capacitive */
00023     } Touch_type_t;
00024 
00025     /*! @enum Touch_interface_type_t
00026         @brief Communication interface
00027     */
00028     typedef enum {
00029         I_2_C = 0,       /*!< I2C */
00030         S_P_I            /*!< SPI */
00031     } Touch_communication_type_t;
00032 
00033     /*! @enum IRQ_trigger_t
00034         @brief Type of the interrupt
00035     */
00036     typedef enum {
00037         INT_ON_EDGE  = 0,   /*!< generate INT on front change */
00038         INT_ON_LEVEL        /*!< generate INT on active level */
00039     } IRQ_trigger_t;
00040 
00041     /*! @enum IRQ_polarity_t
00042         @brief Edge of a signal
00043     */
00044     typedef enum {
00045         RISING_OR_ACTIVE_HI  = 0, /*!< Rising edge/HI level     */
00046         FALLING_OR_ACTIVE_LO      /*!< Falling edge/LOW level   */
00047     } IRQ_polarity_t;
00048 
00049     /*! @struct touch_config_t
00050         @brief Touch Config structure
00051     */
00052     typedef struct {
00053         string          name ;               /*!< Name of the Touch driver */
00054         int             screen ;             /*!< reserved                 */
00055         Touch_type_t    type ;               /*!< Resistive or Capacitive  */
00056         struct {                            /*!< I2C or SPI & the pins of selected periphery */
00057             Touch_communication_type_t type ;
00058             PinName sda;
00059             PinName scl;
00060             PinName mosi;
00061             PinName miso;
00062             PinName sclk;
00063             PinName ssel;
00064             int     freq;
00065         }interface;
00066         struct {                            /*!< IRQ : front and pin of selected periphery */
00067             IRQ_trigger_t   trigger ;
00068             IRQ_polarity_t  polarity;
00069             PinName         pin;
00070         }activity_irq;
00071     }touch_config_t;
00072 
00073 extern const touch_config_t STMPE811_cfg;
00074 
00075 
00076 
00077 /*! @class Touch
00078  *  @brief Touch driver class for VK-LCD panels
00079  *
00080  *  Example:
00081  *
00082  *  @code
00083  *  #include "mbed.h"
00084  *  #include "Touch.hpp"
00085  *
00086  *  using namespace Vekatech;
00087  *
00088  *  Touch   STMPE811;
00089  *
00090  *  // main() runs in its own thread in the OS
00091  *  int main()
00092  *  {
00093  *     osEvent evt;
00094  *
00095  *     STMPE811.Init();
00096  *
00097  *     while (true)
00098  *     {
00099  *          evt = STMPE811.Wait_MSG();
00100  *          if (evt.status == osEventMail)
00101  *          {
00102  *              Touch::MSGQ_t* msg = (Touch::MSGQ_t*)evt.value.p;
00103  *              printf("[EV (%d)], start_idx: %d end_idx: %d\r\n", msg->event, msg->start_idx, msg->stop_idx);
00104  *
00105  *              switch(msg->event)
00106  *              {
00107  *                  case Touch::EV_STYLUS_UP:
00108  *                      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 );
00109  *                  break;
00110  *
00111  *                  case Touch::EV_STYLUS_DOWN:
00112  *                      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 );
00113  *                  break;
00114  *
00115  *                  case Touch::EV_STYLUS_HOLD:
00116  *                      if(msg->stop_idx >= (STMPE811.Get_Dot_thd()-1) )
00117  *                          for(int i = (msg->stop_idx+1 - STMPE811.Get_Dot_thd()); i<=msg->stop_idx; i++)
00118  *                              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 );
00119  *                  break;
00120  *
00121  *                  default: break;
00122  *              }
00123  *
00124  *              STMPE811.Del_MSG((Touch::MSGQ_t*)evt.value.p);
00125  *          }
00126  *      }
00127  *   }
00128  *  @endcode
00129  */
00130 class Touch : public I2C, public InterruptIn
00131 {
00132 public:
00133     /*! @enum Init_err_t
00134         @brief Error codes
00135      */
00136     typedef enum {
00137         TOUCH_OK  = 0,                   /*!< Initialization successful      */
00138         TOUCH_INIT_ERR  = -1,            /*!< Communication interface err while configuring driver */
00139         TOUCH_UNSUPP_ERR  = -2,          /*!< Unsupported driver */
00140         TOUCH_ERR  = -3,                 /*!< unknown error */
00141     } init_err_t ;
00142 
00143     /*! @enum EVENTLIST_t
00144         @brief Type of the Event:
00145      */
00146     typedef enum {
00147         EV_STYLUS_UP =0,
00148         EV_STYLUS_DOWN,
00149         EV_STYLUS_HOLD,
00150         //EV_STYLUS_MOVE,
00151         NEVENTS
00152     }EVENTLIST_t;
00153 
00154     /*! @struct MSGQ_t
00155         @brief Touch Message structure
00156      */
00157     typedef struct {
00158         EVENTLIST_t         event;
00159         unsigned short      start_idx;
00160         unsigned short      stop_idx;
00161     }MSGQ_t;
00162 
00163     /** Constructor method of Touch object
00164     */
00165     Touch( const touch_config_t * tp_cfg = &STMPE811_cfg );
00166 
00167     /** Destructor method of Touch object
00168     */
00169     virtual ~Touch( void );
00170 
00171     /** Touch controller initialization
00172      *  @retval       Error code
00173      */
00174     init_err_t  Init();
00175 
00176     /** Set Calibration data (raw data interpretation)
00177      *  @retval       Error code
00178      */
00179     virtual init_err_t  Clb_Setup();
00180 
00181     /** Set Touch Controller settings
00182     *  @retval       Error code
00183     */
00184     virtual init_err_t  Drv_Setup();
00185 
00186     /** Get Status of the pen
00187      * @retval      true    : StylusDown
00188      *              false   : StyluUp
00189      */
00190     virtual bool Get_Pen_Status();
00191 
00192     /** Get one sample of data
00193      *  @param[in]   raw: pointer to ring buffer to store the samples
00194      */
00195     virtual void Get_Data( unsigned long long * raw );
00196 
00197     /** Get all available samples of data
00198      *  @param[in]   raw: pointer to ring buffer to store the samples
00199      *  @retval           samples count
00200      */
00201     virtual int Get_Fifo( unsigned long long * raw );
00202 
00203     /** Transfer function (from raw coordinates to screen coordinates)
00204      *  @param[in]   points : number of samples to transfer
00205      */
00206     virtual void Get_XYZ( int points );
00207 
00208     /** New Data available
00209      */
00210     static void Irq_Alert();
00211 
00212     /** Pull the new samples if new data is available
00213      */
00214     virtual void Handle_touch();
00215 
00216     /** Get index of the last sample in the ring buffer
00217      */
00218     virtual int Get_Last_Idx();
00219 
00220     /** Get the minimum amounts of dots when pen is hold down
00221      */
00222     virtual int Get_Dot_thd();
00223 
00224     /** Wait message from the touch thread
00225      */
00226     virtual osEvent Wait_MSG( uint32_t millisec=osWaitForever ) { return mail.get( millisec ); };
00227 
00228     /** Delete message from the mail queue
00229     */
00230     virtual osStatus Del_MSG( MSGQ_t *ptr ) { return mail.free( ptr ); };
00231 
00232     /** raw coordinates of the last received dot
00233      */
00234     unsigned short adc_x;
00235     unsigned short adc_y;
00236     unsigned short adc_z;
00237 
00238     /** Coordinates of the last received dot
00239      */
00240     short x;
00241     short y;
00242     float z;
00243 
00244     /** Raw History of the received dots
00245     */
00246     const touch_raw_data_t *adc_data;
00247 
00248     /** History of the received dots
00249     */
00250     const touch_screen_data_t *xyz_data;
00251 
00252 protected:
00253     /** Send message to the main application
00254      */
00255     osStatus MSG(EVENTLIST_t touch_msg, unsigned short touch_on, unsigned short touch_off)
00256     {
00257         MSGQ_t * msg = mail.alloc();
00258         msg->event = touch_msg;
00259         msg->start_idx = touch_on;
00260         msg->stop_idx = touch_off;
00261         return mail.put(msg);
00262     };
00263 
00264     int                  last_xyz_idx;
00265     int                  dot_thd;
00266     unsigned char        fraction;
00267     touch_raw_data_t     raw_data[FIFO_DEPTH];
00268     touch_screen_data_t  screen_data[FIFO_DEPTH];
00269     const touch_config_t *touch_cfg;
00270     touch_calib_data_t   calib;
00271     Thread               thd;
00272     Mail<MSGQ_t, 32>     mail;
00273 };
00274 
00275 } // namespace Vekatech
00276 
00277 #endif /* MBED_TOUCH_H */