Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

mindsensors-touchpanel.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup mptp TouchPanel
00004  * TouchPanel
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-touchpanel.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSTP_H__
00013 #define __MSTP_H__
00014 
00015 /** \file mindsensors-touchpanel.h
00016  * \brief Mindsensors TouchPanel
00017  *
00018  * mindsensors-touchpanel.h provides an API for the Mindsensors TouchPanel.
00019  *
00020  * Changelog:
00021  *  - 0.1 Initial       release.
00022  *
00023  * Credits:
00024  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00025  *
00026  * License: You may use this code as you wish, provided you give credit where it's due.
00027  *
00028  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00029 
00030  * \author Xander Soldaat
00031  * \date 30 November 2011
00032  * \version 0.1
00033  * \example mindsensors-touchpanel-test1.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 #ifndef __COMMON_H__
00039         #include "common.h"
00040 #endif
00041 
00042 #define MSTP_I2C_ADDR     0x04
00043 #define MSTP_REG_TOUCH_X  0x42
00044 #define MSTP_REG_TOUCH_Y  0x43
00045 #define MSTP_REG_BUTTONS  0x44
00046 
00047 #define MSTP_REG_CAL_XD1  0x45
00048 #define MSTP_REG_CAL_YD1  0x46
00049 #define MSTP_REG_CAL_XT1  0x47
00050 #define MSTP_REG_CAL_YT1  0x48
00051 #define MSTP_REG_CAL_XD2  0x49
00052 #define MSTP_REG_CAL_YD2  0x4A
00053 #define MSTP_REG_CAL_XT2  0x4B
00054 #define MSTP_REG_CAL_YT2  0x4C
00055 #define MSTP_REG_G_AVAIL  0x4D
00056 #define MSTP_REG_G_NEXTX  0x4E
00057 #define MSTP_REG_G_NEXTY  0x4F
00058 
00059 #define BUTTON_L1         0
00060 #define BUTTON_L2         1
00061 #define BUTTON_L3         2
00062 #define BUTTON_L4         3
00063 #define BUTTON_R1         4
00064 #define BUTTON_R2         5
00065 #define BUTTON_R3         6
00066 #define BUTTON_R4         7
00067 
00068 // raw ubyte count
00069 #define MSTP_REG_RAW_BC 0x4D
00070 #define MSTP_REG_RAW_X  0x4E
00071 #define MSTP_REG_RAW_Y  0x4F
00072 
00073 // typedef ubyte tHugeArray[220];
00074 
00075 tByteArray MSTP_I2CRequest;    /*!< Array to hold I2C command data */
00076 tByteArray MSTP_I2CReply;      /*!< Array to hold I2C reply data */
00077 // tHugeArray MSTP_GestureData;
00078 
00079 #define isButtonTouched(X, Y) (X >> Y) & 0x01         /*!< Check if the specified button is pressed */
00080 
00081 #define isButtonL1Touched(X) (X >> BUTTON_L1) & 0x01  /*!< Check if the L1 button is pressed */
00082 #define isButtonL2Touched(X) (X >> BUTTON_L2) & 0x01  /*!< Check if the L2 button is pressed */
00083 #define isButtonL3Touched(X) (X >> BUTTON_L3) & 0x01  /*!< Check if the L3 button is pressed */
00084 #define isButtonL4Touched(X) (X >> BUTTON_L4) & 0x01  /*!< Check if the L4 button is pressed */
00085 #define isButtonR1Touched(X) (X >> BUTTON_R1) & 0x01  /*!< Check if the R1 button is pressed */
00086 #define isButtonR2Touched(X) (X >> BUTTON_R2) & 0x01  /*!< Check if the R2 button is pressed */
00087 #define isButtonR3Touched(X) (X >> BUTTON_R3) & 0x01  /*!< Check if the R3 button is pressed */
00088 #define isButtonR4Touched(X) (X >> BUTTON_R4) & 0x01  /*!< Check if the R4 button is pressed */
00089 
00090 
00091 bool MSTPgetTouch(tSensors link, int &x, int &y, ubyte &buttons, ubyte addr = MSTP_I2C_ADDR);
00092 bool MSTPsendCmd(tSensors link, ubyte cmd, ubyte addr = MSTP_I2C_ADDR);
00093 
00094 #define MSTPresetCalibration(x) MSTPsendCmd(tSensors link, ubyte cmd, ubyte addr = MSTP_I2C_ADDR);
00095 
00096 
00097 /**
00098  * Fetch all the information from the TouchPanel, including
00099  * coordinates of current touch and button state.
00100  * by configuring the sensor as sensorLightInactive
00101  * @param link the TouchPanel port number
00102  * @param x the X coordinate of the current pen position, 0 if nothing is detected
00103  * @param y the Y coordinate of the current pen position, 0 if nothing is detected
00104  * @param buttons the currently touched buttons, use isButtonXXTouched() to check the individual status.
00105  * @param addr the I2C address of the TouchPanel, is optional, defaults to 0x04
00106  * @return true if no error has occured, false if it did
00107  */
00108 bool MSTPgetTouch(tSensors link, int &x, int &y, ubyte &buttons, ubyte addr) {
00109   memset(MSTP_I2CRequest, 0, sizeof(tByteArray));
00110 
00111   MSTP_I2CRequest[0] = 2;                      // Message size
00112   MSTP_I2CRequest[1] = addr;         // I2C Address
00113   MSTP_I2CRequest[2] = MSTP_REG_TOUCH_X;
00114 
00115   if (!writeI2C(link, MSTP_I2CRequest, MSTP_I2CReply, 3))
00116     return false;
00117 
00118   x = MSTP_I2CReply[0];
00119   y = MSTP_I2CReply[1];
00120   buttons = MSTP_I2CReply[2];
00121 
00122   return true;
00123 }
00124 
00125 
00126 /**
00127  * Send a command to the TouchPanel
00128  * @param link the TouchPanel port number
00129  * @param cmd the command to be sent
00130  * @param addr the I2C address of the TouchPanel, is optional, defaults to 0x04
00131  * @return true if no error has occured, false if it did
00132  */
00133 bool MSTPsendCmd(tSensors link, ubyte cmd, ubyte addr)
00134 {
00135   memset(MSTP_I2CRequest, 0, sizeof(tByteArray));
00136 
00137   MSTP_I2CRequest[0] = 3;                      // Message size
00138   MSTP_I2CRequest[1] = MSTP_I2C_ADDR;         // I2C Address
00139   MSTP_I2CRequest[2] = 0x41;         //
00140   MSTP_I2CRequest[3] = cmd;
00141 
00142   return writeI2C(link, MSTP_I2CRequest);
00143 }
00144 
00145 
00146 /*
00147 int MSTPgetGesture(tSensors link)
00148 {
00149   memset(MSTP_I2CRequest, 0, sizeof(tByteArray));
00150 
00151   MSTP_I2CRequest[0] = 2;                      // Message size
00152   MSTP_I2CRequest[1] = addr;         // I2C Address
00153   MSTP_I2CRequest[2] = MSTP_REG_G_AVAIL;
00154 
00155   if (!writeI2C(link, MSTP_I2CRequest, 1))
00156     return false;
00157 
00158   if (!readI2C(link, MSTP_I2CReply, 1))
00159     return false;
00160 
00161   if (MSTP_I2CReply[0] == 0)
00162     return 0;
00163 
00164 
00165 }
00166 */
00167 #endif // __MSTP_H__
00168 
00169 /*
00170  * $Id: mindsensors-touchpanel.h 133 2013-03-10 15:15:38Z xander $
00171  */
00172 /* @} */
00173 /* @} */