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

mindsensors-touchmux.h

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