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

MSRXMUX-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup msrmux RCX Sensor MUX
00004  * RCX Sensor MUX
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: MSRXMUX-driver.h 56 2011-05-30 19:47:12Z xander $
00010  */
00011 
00012 /** \file MSRXMUX-driver.h
00013  * \brief Mindsensors MSRXMUX RCX Sensor MUX Sensor driver
00014  *
00015  * MSRXMUX-driver.h provides an API for the Mindsensors MSRXMUX RCX Sensor MUX Sensor
00016  *
00017  * Changelog:
00018  * - 0.1: Initial release
00019  *
00020  * Credits:
00021  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00022  *
00023  * License: You may use this code as you wish, provided you give credit where its due.
00024  *
00025  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00026  * \author Xander Soldaat (mightor_at_gmail.com)
00027  * \date 30 August 2009
00028  * \version 0.1
00029  * \example MSRXMUX-test1.c
00030  */
00031 
00032 #pragma systemFile
00033 
00034 #ifndef __MSRXMUX_H__
00035 #define __MSRXMUX_H__
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define MSRXMUX_I2C_ADDR  0x7E      /*!< I2C address for sensor */
00042 #define MSRXMUX_CHAN1     0xFE      /*!< Select MUX channel 1 */
00043 #define MSRXMUX_CHAN2     0xFD      /*!< Select MUX channel 2 */
00044 #define MSRXMUX_CHAN3     0xFB      /*!< Select MUX channel 3 */
00045 #define MSRXMUX_CHAN4     0xF7      /*!< Select MUX channel 4 */
00046 #define MSRXMUX_NONE      0xFF      /*!< Deselect all MUX channels */
00047 
00048 tByteArray MSRXMUX_I2CRequest;       /*!< Array to hold I2C command data */
00049 
00050 TSensorTypes RCXSensorTypes[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
00051 TSensorModes RCXSensorModes[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
00052 ubyte RCXSensorDelays[4][4] =        {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
00053 
00054 /**
00055  * Send a direct command to the PFMate sensor
00056  *
00057  * Note: this is an internal command and should not be called directly.
00058  * @param link the PFMate port number
00059  * @param chan the channel to be configured
00060  * @param chantype the sensor type connected to the channel
00061  * @param chanmode the sensor mode of the sensor
00062  * @param delay the initialisation delay time
00063  * @return true if no error occured, false if it did
00064  */
00065 void MSRXMUXsetupChan(tSensors link, ubyte chan, TSensorTypes chantype, TSensorModes chanmode, ubyte delay) {
00066   if (chan < 1 || chan > 4)
00067     return;
00068 
00069   RCXSensorTypes[link][chan-1] = chantype;
00070   RCXSensorModes[link][chan-1] = chanmode;
00071   RCXSensorDelays[link][chan-1] = delay;
00072 }
00073 
00074 int MSRXMUXreadChan(tSensors link, byte chan) {
00075   if (SensorType[link] != sensorI2CCustom9V) {
00076     SensorType[link] = sensorI2CCustom9V;
00077     wait1Msec(3);
00078   }
00079 
00080   MSRXMUX_I2CRequest[0] = 2;
00081   MSRXMUX_I2CRequest[1] = MSRXMUX_I2C_ADDR;
00082   switch(chan) {
00083     case 0: MSRXMUX_I2CRequest[2] =  MSRXMUX_NONE; break;
00084     case 1: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN1; break;
00085     case 2: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN2; break;
00086     case 3: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN3; break;
00087     case 4: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN4; break;
00088     default: MSRXMUX_I2CRequest[2] =  MSRXMUX_NONE;
00089   }
00090 
00091   if (!writeI2C(link, MSRXMUX_I2CRequest, 0))
00092     return -1;
00093 
00094   wait10Msec(3+RCXSensorDelays[link][chan-1]);
00095   SensorType[link] = RCXSensorTypes[link][chan-1];
00096   SensorMode[link] = RCXSensorModes[link][chan-1];
00097   return(SensorValue(link));
00098 }
00099 
00100 #endif // __MSRXMUX_H__
00101 
00102 /*
00103  * $Id: MSRXMUX-driver.h 56 2011-05-30 19:47:12Z xander $
00104  */
00105 /* @} */
00106 /* @} */