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

mindsensors-sensormux.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup MSSMUX SensorMUX Sensor
00004  * Mindsensors SensorMUX Sensor (MSSMUX) driver
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-sensormux.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSSMUX_H__
00013 #define __MSSMUX_H__
00014 /** \file mindsensors-sensormux.h
00015  * \brief Mindsensors SensorMUX Sensor driver
00016  *
00017  * mindsensors-sensormux.h provides an API for the Mindsensors SensorMUX Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Mindsensors 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 02 March 2013
00031  * \version 0.1
00032  * \example mindsensors-sensormux-test1.c
00033  * \example mindsensors-sensormux-test2.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 #define MSMX_I2C_ADDR     0x24
00043 #define MSMX_REG_CHANSEL  0x42
00044 #define MSMX_REG_VOLTAGE  0x43
00045 
00046 
00047 tByteArray MSMX_I2CRequest;    /*!< Array to hold I2C command data */
00048 tByteArray MSMX_I2CReply;      /*!< Array to hold I2C reply data */
00049 
00050 void MSSMUXsetChan(tSensors link, int channel);
00051 int MSSMUXreadBattery(tSensors link);
00052 
00053 
00054 /**
00055  * Read the voltage level of the external battery.
00056  * @param link the port number
00057  * @return the battery voltage in mV
00058  */
00059 int MSSMUXreadBattery(tSensors link)
00060 {
00061   TSensorTypes previous = SensorType[link];
00062   SensorType[link] = sensorI2CCustomFastSkipStates;
00063 
00064   // Switch to the virtual channel (0)
00065   MSSMUXsetChan(link, 0);
00066   wait1Msec(50);
00067 
00068   memset(MSMX_I2CRequest, 0, sizeof(tByteArray));
00069 
00070   MSMX_I2CRequest[0] = 2;                      // Message size
00071   MSMX_I2CRequest[1] = MSMX_I2C_ADDR;         // I2C Address
00072   MSMX_I2CRequest[2] = MSMX_REG_VOLTAGE;
00073 
00074   if (!writeI2C(link, MSMX_I2CRequest, MSMX_I2CReply, 1))
00075   {
00076     writeDebugStreamLine("error with i2c");
00077     return false;
00078   }
00079 
00080   SensorType[link] = previous;
00081 
00082   return (0x00FF & MSMX_I2CReply[0]) * 37;
00083 }
00084 
00085 /**
00086  * Switch to the specified channel
00087  * @param link the port number
00088  * @param channel the sensor mux channel number
00089  */
00090 void MSSMUXsetChan(tSensors link, int channel)
00091 {
00092   static int currChannel[4] = {-1, -1, -1, -1};
00093 
00094   // Message to send to SMUX
00095   ubyte MUXmsg[] = {0x55, 0xAA, 0x30 + (channel & 0xFF)};
00096 
00097   // Do nothing if the channel is already set correctly
00098   if (currChannel[link] == channel)
00099     return;
00100 
00101   currChannel[link] = channel;
00102 
00103   TSensorTypes previous = SensorType[link];
00104   writeDebugStreamLine("channel type: %d", previous);
00105 
00106   // Set the sensor type to sensorCustom so we can
00107   // start sending messages
00108   SensorType[link] = sensorCustom;
00109 
00110   // If the sensor was previously configured as a colour sensor
00111   // give it a little time
00112   switch (previous)
00113   {
00114     case sensorCOLORFULL:
00115     case sensorCOLORRED:
00116     case sensorCOLORGREEN:
00117     case sensorCOLORBLUE:
00118     case sensorCOLORNONE: wait1Msec(12); break;
00119   }
00120 
00121   // Set both pins as output
00122   DigitalPinDirection[link] = 0x03;
00123   wait1Msec(1);
00124 
00125   for (int i = 0; i < 3; i++)
00126   {
00127     DigitalPinValue[link] = 0x00;
00128     wait1Msec(1);
00129 
00130     for (int j = 0; j < 8; j++) {
00131       if ((MUXmsg[i] >> j) & 0x01)
00132         DigitalPinValue[link] = 0x00;
00133       else
00134         DigitalPinValue[link] = 0x01;
00135       wait1Msec(1);
00136     }
00137     DigitalPinValue[link] = 0x01;
00138     wait1Msec(1);
00139   }
00140 }
00141 
00142 #endif // __MSSMUX_H__
00143 
00144 /*
00145  * $Id: mindsensors-sensormux.h 133 2013-03-10 15:15:38Z xander $
00146  */
00147 /* @} */
00148 /* @} */