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 136 2013-03-13 18:52:29Z 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   // Switch to the virtual channel (0)
00062   MSSMUXsetChan(link, 0);
00063   wait1Msec(50);
00064 
00065   SensorType[link] = sensorI2CCustomFastSkipStates;
00066   memset(MSMX_I2CRequest, 0, sizeof(tByteArray));
00067 
00068   MSMX_I2CRequest[0] = 2;                      // Message size
00069   MSMX_I2CRequest[1] = MSMX_I2C_ADDR;         // I2C Address
00070   MSMX_I2CRequest[2] = MSMX_REG_VOLTAGE;
00071 
00072   if (!writeI2C(link, MSMX_I2CRequest, MSMX_I2CReply, 1))
00073   {
00074     writeDebugStreamLine("error with i2c");
00075     return false;
00076   }
00077 
00078   return (0x00FF & MSMX_I2CReply[0]) * 37;
00079 }
00080 
00081 /**
00082  * Switch to the specified channel
00083  * @param link the port number
00084  * @param channel the sensor mux channel number
00085  */
00086 void MSSMUXsetChan(tSensors link, int channel)
00087 {
00088   static int currChannel[4] = {-1, -1, -1, -1};
00089 
00090   // Message to send to SMUX
00091   ubyte MUXmsg[] = {0x55, 0xAA, 0x30 + (channel & 0xFF)};
00092 
00093   // Do nothing if the channel is already set correctly
00094   if (currChannel[link] == channel)
00095     return;
00096 
00097   currChannel[link] = channel;
00098 
00099   TSensorTypes previous = SensorType[link];
00100   writeDebugStreamLine("channel type: %d", previous);
00101 
00102   // Set the sensor type to sensorCustom so we can
00103   // start sending messages
00104   SensorType[link] = sensorCustom;
00105 
00106   // If the sensor was previously configured as a colour sensor
00107   // give it a little time
00108   switch (previous)
00109   {
00110     case sensorCOLORFULL:
00111     case sensorCOLORRED:
00112     case sensorCOLORGREEN:
00113     case sensorCOLORBLUE:
00114     case sensorCOLORNONE: wait1Msec(12); break;
00115   }
00116 
00117   // Set both pins as output
00118   DigitalPinDirection[link] = 0x03;
00119   wait1Msec(1);
00120 
00121   for (int i = 0; i < 3; i++)
00122   {
00123     DigitalPinValue[link] = 0x00;
00124     wait1Msec(1);
00125 
00126     for (int j = 0; j < 8; j++) {
00127       if ((MUXmsg[i] >> j) & 0x01)
00128         DigitalPinValue[link] = 0x00;
00129       else
00130         DigitalPinValue[link] = 0x01;
00131       wait1Msec(1);
00132     }
00133     DigitalPinValue[link] = 0x01;
00134     wait1Msec(1);
00135   }
00136 }
00137 
00138 #endif // __MSSMUX_H__
00139 
00140 /*
00141  * $Id: mindsensors-sensormux.h 136 2013-03-13 18:52:29Z xander $
00142  */
00143 /* @} */
00144 /* @} */