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

hitechnic-touchmux.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup httmux Touch Sensor MUX
00004  * HiTechnic Touch Sensor MUX
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-touchmux.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTTMUX_H__
00013 #define __HTTMUX_H__
00014 /** \file hitechnic-touchmux.h
00015  * \brief HiTechnic Touch Sensor Multiplexer Sensor driver
00016  *
00017  * hitechnic-touchmux.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 3.59 AND HIGHER. 
00028 
00029  * \author Xander Soldaat (xander_at_botbench.com)
00030  * \date 15 March 2009
00031  * \version 0.1
00032  * \example hitechnic-touchmux-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 int HTTMUXgetActive(tSensors link);
00038 bool HTTMUXisActive(tSensors link, int touch);
00039 
00040 /**
00041  * Read the value of all of the currently connected touch sensors.  The status is logically OR'd
00042  * together. Touch 1 = 1, Touch 2 = 2, Touch 3 = 4, Touch 4 = 8.  If Touch 1 and 3 are active,
00043  * the return value will be 1 + 4 == 5.
00044  * @param link the HTTMUX port number
00045  * @return the value of the switches status
00046  */
00047 int HTTMUXgetActive(tSensors link) {
00048   long muxvalue = 0;
00049   long switches = 0;
00050 
00051   // Make sure the sensor is configured as type sensorRawValue
00052   if (SensorType[link] != sensorRawValue) {
00053     SetSensorType(link, sensorRawValue);
00054     wait1Msec(100);
00055   }
00056 
00057   // Voodoo magic starts here.  This is taken straight from the Touch MUX pamphlet.
00058   // No small furry animals were hurt during the calculation of this algorithm.
00059   muxvalue = 1023 - SensorRaw[link];
00060   switches = 339 * muxvalue;
00061   switches /= (1023 - muxvalue);
00062   switches += 5;
00063   switches /= 10;
00064 
00065   return (int)switches;
00066 }
00067 
00068 /**
00069  * Read the value of specific touch sensor.
00070  * @param link the HTTMUX port number
00071  * @param touch the touch sensor to be checked, numbered 1 to 4.
00072  * @return the value of the switches status
00073  */
00074 bool HTTMUXisActive(tSensors link, int touch) {
00075   if (HTTMUXgetActive(link) & (1 << (touch - 1)))
00076     return true;
00077   else
00078     return false;
00079 }
00080 
00081 #endif // __HTTMUX_H__
00082 
00083 /*
00084  * $Id: hitechnic-touchmux.h 133 2013-03-10 15:15:38Z xander $
00085  */
00086 /* @} */
00087 /* @} */