|
00001 /*!@addtogroup HiTechnic 00002 * @{ 00003 * @defgroup httmux Touch Sensor MUX 00004 * HiTechnic Touch Sensor MUX 00005 * @{ 00006 */ 00007 00008 /* 00009 * $Id: HTTMUX-driver.h 29 2010-06-25 12:55:41Z xander $ 00010 */ 00011 00012 #ifndef __HTTMUX_H__ 00013 #define __HTTMUX_H__ 00014 /** \file HTTMUX-driver.h 00015 * \brief HiTechnic Touch Sensor Multiplexer Sensor driver 00016 * 00017 * HTTMUX-driver.h provides an API for the HiTechnic Touch Sensor Multiplexer Sensor. 00018 * 00019 * Changelog: 00020 * - 0.1: Initial release 00021 * 00022 * Credits: 00023 * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this. 00024 * 00025 * License: You may use this code as you wish, provided you give credit where its due. 00026 * 00027 * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER. 00028 * \author Xander Soldaat (mightor_at_gmail.com) 00029 * \date 15 March 2009 00030 * \version 0.1 00031 * \example HTTMUX-test1.c 00032 */ 00033 00034 #pragma systemFile 00035 00036 int HTTMUXgetActive(tSensors link); 00037 bool HTTMUXisActive(tSensors link, int touch); 00038 00039 /** 00040 * Read the value of all of the currently connected touch sensors. The status is logically OR'd 00041 * together. Touch 1 = 1, Touch 2 = 2, Touch 3 = 4, Touch 4 = 8. If Touch 1 and 3 are active, 00042 * the return value will be 1 + 4 == 5. 00043 * @param link the HTTMUX port number 00044 * @return the value of the switches status 00045 */ 00046 int HTTMUXgetActive(tSensors link) { 00047 long muxvalue = 0; 00048 long switches = 0; 00049 00050 // Make sure the sensor is configured as type sensorRawValue 00051 if (SensorType[link] != sensorRawValue) { 00052 SetSensorType(link, sensorRawValue); 00053 wait1Msec(100); 00054 } 00055 00056 // Voodoo magic starts here. This is taken straight from the Touch MUX pamphlet. 00057 // No small furry animals were hurt during the calculation of this algorithm. 00058 muxvalue = 1023 - SensorRaw[link]; 00059 switches = 339 * muxvalue; 00060 switches /= (1023 - muxvalue); 00061 switches += 5; 00062 switches /= 10; 00063 00064 return (int)switches; 00065 } 00066 00067 /** 00068 * Read the value of specific touch sensor. 00069 * @param link the HTTMUX port number 00070 * @param touch the touch sensor to be checked, numbered 1 to 4. 00071 * @return the value of the switches status 00072 */ 00073 bool HTTMUXisActive(tSensors link, int touch) { 00074 if (HTTMUXgetActive(link) & (1 << (touch - 1))) 00075 return true; 00076 else 00077 return false; 00078 } 00079 00080 #endif // __HTTMUX_H__ 00081 00082 /* 00083 * $Id: HTTMUX-driver.h 29 2010-06-25 12:55:41Z xander $ 00084 */ 00085 /* @} */ 00086 /* @} */