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

mindsensors-rcxsensorsmux.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: mindsensors-rcxsensorsmux.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 /** \file mindsensors-rcxsensorsmux.h
00013  * \brief Mindsensors MSRXMUX RCX Sensor MUX Sensor driver
00014  *
00015  * mindsensors-rcxsensorsmux.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 3.59 AND HIGHER. 
00026 
00027  * \author Xander Soldaat (xander_at_botbench.com)
00028  * \date 30 August 2009
00029  * \version 0.1
00030  * \example mindsensors-rcxsensorsmux-test1.c
00031  */
00032 
00033 #pragma systemFile
00034 
00035 #ifndef __MSRXMUX_H__
00036 #define __MSRXMUX_H__
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 #define MSRXMUX_I2C_ADDR  0x7E      /*!< I2C address for sensor */
00043 #define MSRXMUX_CHAN1     0xFE      /*!< Select MUX channel 1 */
00044 #define MSRXMUX_CHAN2     0xFD      /*!< Select MUX channel 2 */
00045 #define MSRXMUX_CHAN3     0xFB      /*!< Select MUX channel 3 */
00046 #define MSRXMUX_CHAN4     0xF7      /*!< Select MUX channel 4 */
00047 #define MSRXMUX_NONE      0xFF      /*!< Deselect all MUX channels */
00048 
00049 tByteArray MSRXMUX_I2CRequest;       /*!< Array to hold I2C command data */
00050 
00051 TSensorTypes RCXSensorTypes[4][4] = {{sensorNone, sensorNone, sensorNone, sensorNone},
00052                                      {sensorNone, sensorNone, sensorNone, sensorNone},
00053                                      {sensorNone, sensorNone, sensorNone, sensorNone},
00054                                      {sensorNone, sensorNone, sensorNone, sensorNone}};
00055 TSensorModes RCXSensorModes[4][4] = {{modeRaw, modeRaw, modeRaw, modeRaw},
00056                                      {modeRaw, modeRaw, modeRaw, modeRaw},
00057                                      {modeRaw, modeRaw, modeRaw, modeRaw},
00058                                      {modeRaw, modeRaw, modeRaw, modeRaw}};
00059 ubyte RCXSensorDelays[4][4] =        {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
00060 
00061 /**
00062  * Send a direct command to the MUX sensor
00063  *
00064  * Note: this is an internal command and should not be called directly.
00065  * @param link the PFMate port number
00066  * @param chan the channel to be configured
00067  * @param chantype the sensor type connected to the channel
00068  * @param chanmode the sensor mode of the sensor
00069  * @param delay the initialisation delay time
00070  * @return true if no error occured, false if it did
00071  */
00072 void MSRXMUXsetupChan(tSensors link, ubyte chan, TSensorTypes chantype, TSensorModes chanmode, ubyte delay) {
00073   if (chan < 1 || chan > 4)
00074     return;
00075 
00076   RCXSensorTypes[link][chan-1] = chantype;
00077   RCXSensorModes[link][chan-1] = chanmode;
00078   RCXSensorDelays[link][chan-1] = delay;
00079 }
00080 
00081 int MSRXMUXreadChan(tSensors link, byte chan) {
00082   if (SensorType[link] != sensorI2CCustom9V) {
00083     SensorType[link] = sensorI2CCustom9V;
00084     wait1Msec(3);
00085   }
00086 
00087   MSRXMUX_I2CRequest[0] = 2;
00088   MSRXMUX_I2CRequest[1] = MSRXMUX_I2C_ADDR;
00089   switch(chan) {
00090     case 0: MSRXMUX_I2CRequest[2] =  MSRXMUX_NONE; break;
00091     case 1: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN1; break;
00092     case 2: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN2; break;
00093     case 3: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN3; break;
00094     case 4: MSRXMUX_I2CRequest[2] =  MSRXMUX_CHAN4; break;
00095     default: MSRXMUX_I2CRequest[2] =  MSRXMUX_NONE;
00096   }
00097 
00098   if (!writeI2C(link, MSRXMUX_I2CRequest))
00099     return -1;
00100 
00101   wait10Msec(3+RCXSensorDelays[link][chan-1]);
00102   SensorType[link] = RCXSensorTypes[link][chan-1];
00103   SensorMode[link] = RCXSensorModes[link][chan-1];
00104   return(SensorValue[link]);
00105 }
00106 
00107 #endif // __MSRXMUX_H__
00108 
00109 /*
00110  * $Id: mindsensors-rcxsensorsmux.h 133 2013-03-10 15:15:38Z xander $
00111  */
00112 /* @} */
00113 /* @} */