|
00001 /*!@addtogroup mindsensors 00002 * @{ 00003 * @defgroup mstmux Touc Sensor MUX 00004 * Touc Sensor MUX 00005 * @{ 00006 */ 00007 00008 /* 00009 * $Id: MSTMUX-driver.h 47 2011-01-17 19:47:22Z xander $ 00010 */ 00011 00012 #ifndef __MSTMUX_HIGH___ 00013 #define __MSTMUX_HIGH___ 00014 /** \file MSTMUX-driver.h 00015 * \brief Mindsensors Touch Multiplexer Sensor driver 00016 * 00017 * MSTMUX-driver.h provides an API for the Mindsensors Touch Multiplexer Sensor. 00018 * 00019 * Changelog: 00020 * - 0.1: Initial release 00021 * - 0.2: Added support for HiTechnic Sensor MUX 00022 * 00023 * Credits: 00024 * - Big thanks to HiTechnic and 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 its due. 00027 * 00028 * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER. 00029 * \author Xander Soldaat (mightor_at_gmail.com) 00030 * \date 24 March 2009 00031 * \version 0.2 00032 * \example MSTMUX-test1.c 00033 */ 00034 00035 #pragma systemFile 00036 00037 #ifndef __COMMON_H__ 00038 #include "common.h" 00039 #endif 00040 00041 // Sensor values for each combo of buttons pressed 00042 #define MSTMUX_LOW_1 60 00043 #define MSTMUX_HIGH_1 160 00044 #define MSTMUX_LOW_2 210 00045 #define MSTMUX_HIGH_2 290 00046 #define MSTMUX_LOW_12 370 00047 #define MSTMUX_HIGH_12 460 00048 #define MSTMUX_LOW_3 500 00049 #define MSTMUX_HIGH_3 599 00050 #define MSTMUX_LOW_13 600 00051 #define MSTMUX_HIGH_13 658 00052 #define MSTMUX_LOW_23 659 00053 #define MSTMUX_HIGH_23 723 00054 #define MSTMUX_LOW_123 724 00055 #define MSTMUX_HIGH_123 800 00056 00057 #define MSTMUX_SMUX_LOW_1 163 00058 #define MSTMUX_SMUX_HIGH_1 203 00059 #define MSTMUX_SMUX_LOW_2 263 00060 #define MSTMUX_SMUX_HIGH_2 303 00061 #define MSTMUX_SMUX_LOW_12 373 00062 #define MSTMUX_SMUX_HIGH_12 413 00063 #define MSTMUX_SMUX_LOW_3 473 00064 #define MSTMUX_SMUX_HIGH_3 513 00065 #define MSTMUX_SMUX_LOW_13 523 00066 #define MSTMUX_SMUX_HIGH_13 563 00067 #define MSTMUX_SMUX_LOW_23 568 00068 #define MSTMUX_SMUX_HIGH_23 598 00069 #define MSTMUX_SMUX_LOW_123 603 00070 #define MSTMUX_SMUX_HIGH_123 643 00071 00072 00073 int MSTMUXgetActive(tSensors link); 00074 bool MSTMUXisActive(tSensors link, int touch); 00075 00076 #ifdef __HTSMUX_SUPPORT__ 00077 int MSTMUXgetActive(tMUXSensor muxsensor); 00078 bool MSTMUXisActive(tMUXSensor muxsensor, int touch); 00079 #endif 00080 00081 /** 00082 * Read the value of all of the currently connected touch sensors. The status is logically OR'd 00083 * together. Touch 1 = 1, Touch 2 = 2, Touch 3 = 4, Touch 4 = 8. If Touch 1 and 3 are active, 00084 * the return value will be 1 + 4 == 5. 00085 * @param link the MSTMUX port number 00086 * @return the value of the switches status 00087 */ 00088 int MSTMUXgetActive(tSensors link) { 00089 00090 // Make sure the sensor is configured as type sensorLightInactive 00091 if (SensorType[link] != sensorLightInactive) { 00092 SetSensorType(link, sensorLightInactive); 00093 wait1Msec(10); 00094 } 00095 00096 int s; 00097 s = SensorRaw[link]; 00098 00099 if ( MSTMUX_LOW_1 < s && s < MSTMUX_HIGH_1 ) { 00100 return 1; 00101 } else if ( MSTMUX_LOW_2 < s && s < MSTMUX_HIGH_2 ) { 00102 return 2; 00103 } else if ( MSTMUX_LOW_3 < s && s < MSTMUX_HIGH_3 ) { 00104 return 4; 00105 } else if ( MSTMUX_LOW_12 < s && s < MSTMUX_HIGH_12 ) { 00106 return 1 + 2; 00107 } else if ( MSTMUX_LOW_13 < s && s < MSTMUX_HIGH_13 ) { 00108 return 1 + 4; 00109 } else if ( MSTMUX_LOW_23 < s && s < MSTMUX_HIGH_23 ) { 00110 return 2 + 4; 00111 } else if ( MSTMUX_LOW_123 < s && s < MSTMUX_HIGH_123 ) { 00112 return 1 + 2 + 4; 00113 } else { 00114 return 0; 00115 } 00116 } 00117 00118 00119 /** 00120 * Read the value of all of the currently connected touch sensors. The status is logically OR'd 00121 * together. Touch 1 = 1, Touch 2 = 2, Touch 3 = 4, Touch 4 = 8. If Touch 1 and 3 are active, 00122 * the return value will be 1 + 4 == 5. 00123 * @param muxsensor the SMUX sensor port number 00124 * @return the value of the switches status 00125 */ 00126 #ifdef __HTSMUX_SUPPORT__ 00127 int MSTMUXgetActive(tMUXSensor muxsensor) { 00128 00129 int s; 00130 s = 1023 - HTSMUXreadAnalogue(muxsensor); 00131 00132 if ( MSTMUX_SMUX_LOW_1 < s && s < MSTMUX_SMUX_HIGH_1 ) { 00133 return 1; 00134 } else if ( MSTMUX_SMUX_LOW_2 < s && s < MSTMUX_SMUX_HIGH_2 ) { 00135 return 2; 00136 } else if ( MSTMUX_SMUX_LOW_3 < s && s < MSTMUX_SMUX_HIGH_3 ) { 00137 return 4; 00138 } else if ( MSTMUX_SMUX_LOW_12 < s && s < MSTMUX_SMUX_HIGH_12 ) { 00139 return 1 + 2; 00140 } else if ( MSTMUX_SMUX_LOW_13 < s && s < MSTMUX_SMUX_HIGH_13 ) { 00141 return 1 + 4; 00142 } else if ( MSTMUX_SMUX_LOW_23 < s && s < MSTMUX_SMUX_HIGH_23 ) { 00143 return 2 + 4; 00144 } else if ( MSTMUX_SMUX_LOW_123 < s && s < MSTMUX_SMUX_HIGH_123 ) { 00145 return 1 + 2 + 4; 00146 } else { 00147 return 0; 00148 } 00149 } 00150 #endif // __HTSMUX_SUPPORT__ 00151 00152 00153 /** 00154 * Read the value of specific touch sensor. 00155 * @param link the MSTMUX port number 00156 * @param touch the touch sensor to be checked, numbered 1 to 4. 00157 * @return the value of the switches status 00158 */ 00159 bool MSTMUXisActive(tSensors link, int touch) { 00160 if (MSTMUXgetActive(link) & (1 << (touch - 1))) 00161 return true; 00162 else 00163 return false; 00164 } 00165 00166 00167 /** 00168 * Read the value of specific touch sensor. 00169 * @param muxsensor the SMUX sensor port number 00170 * @param touch the touch sensor to be checked, numbered 1 to 4. 00171 * @return the value of the switches status 00172 */ 00173 #ifdef __HTSMUX_SUPPORT__ 00174 bool MSTMUXisActive(tMUXSensor muxsensor, int touch) { 00175 if (MSTMUXgetActive(muxsensor) & (1 << (touch - 1))) 00176 return true; 00177 else 00178 return false; 00179 } 00180 #endif // __HTSMUX_SUPPORT__ 00181 00182 00183 #endif // __MSTMUX_HIGH___ 00184 00185 /* 00186 * $Id: MSTMUX-driver.h 47 2011-01-17 19:47:22Z xander $ 00187 */ 00188 /* @} */ 00189 /* @} */