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

HTCS-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htcs Color Sensor V1
00004  * HiTechnic Color Sensor V1
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: HTCS-driver.h 51 2011-04-27 16:29:06Z xander $
00010  */
00011 
00012 #ifndef __HTCS_H__
00013 #define __HTCS_H__
00014 /** \file HTCS-driver.h
00015  * \brief HiTechnic Color Sensor driver
00016  *
00017  * HTCS-driver.h provides an API for the HiTechnic Color Sensor driver.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Added SMUX functions
00022  * - 0.3: All functions that uses tIntArray are now pass by reference to reduce memory usage.<br>
00023  *        Removed SMUX data array
00024  * - 0.4: Use new calls in common.h that don't require SPORT/MPORT macros <br>
00025  *        Removed calls to ubyteToInt()
00026  * - 0.5: Replaced array structs with typedefs
00027  *
00028  * Credits:
00029  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00030  *
00031  * License: You may use this code as you wish, provided you give credit where its due.
00032  *
00033  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00034  * \author Xander Soldaat (mightor_at_gmail.com)
00035  * \date 20 February 2011
00036  * \version 0.5
00037  * \example HTCS-test1.c
00038  * \example HTCS-test2.c
00039  * \example HTCS-SMUX-test1.c
00040  */
00041 
00042 #pragma systemFile
00043 
00044 #ifndef __COMMON_H__
00045 #include "common.h"
00046 #endif
00047 
00048 #ifndef __LIGHT_COMMON_H__
00049 #include "light-common.h"
00050 #endif
00051 
00052 #define HTCS_I2C_ADDR       0x02      /*!< HTCS I2C device address */
00053 #define HTCS_CMD_REG        0x41      /*!< Command register */
00054 #define HTCS_OFFSET         0x42      /*!< Offset for data registers */
00055 #define HTCS_COLNUM_REG     0x00      /*!< Color number */
00056 #define HTCS_RED_REG        0x01      /*!< Red reading */
00057 #define HTCS_GREEN_REG      0x02      /*!< Green reading */
00058 #define HTCS_BLUE_REG       0x03      /*!< Blue reading */
00059 #define HTCS_RED_RAW_REG    0x04      /*!< Raw red reading (2 bytes) */
00060 #define HTCS_GREEN_RAW_REG  0x05      /*!< Raw green reading (2 bytes) */
00061 #define HTCS_BLUE_RAW_REG   0x06      /*!< Raw blue reading (2 bytes) */
00062 #define HTSC_COL_INDEX_REG  0x07      /*!< Color index number */
00063 #define HTSC_RED_NORM_REG   0x08      /*!< Normalised red reading */
00064 #define HTSC_GREEN_NORM_REG 0x09      /*!< Normalised green reading */
00065 #define HTSC_BLUE_NORM_REG  0x0A      /*!< Normalised blue reading */
00066 
00067 #define HTCS_CAL_WHITE      0x43      /*!< Command to calibrate white */
00068 
00069 int HTCSreadColor(tSensors link);
00070 bool HTCSreadRGB(tSensors link, int &red, int &green, int &blue);
00071 bool HTCSreadNormRGB(tSensors link, int &red, int &green, int &blue);
00072 bool HTCSreadRawRGB(tSensors link, int &red, int &green, int &blue);
00073 bool HTCScalWhite(tSensors link);
00074 
00075 #ifdef __HTSMUX_SUPPORT__
00076 int HTCSreadColor(tMUXSensor muxsensor);
00077 bool HTCSreadRGB(tMUXSensor muxsensor, int &red, int &green, int &blue);
00078 
00079 tConfigParams HTCS_config = {HTSMUX_CHAN_I2C, 4, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00080 #endif
00081 
00082 tByteArray HTCS_I2CRequest;           /*!< Array to hold I2C command data */
00083 tByteArray HTCS_I2CReply;             /*!< Array to hold I2C reply data */
00084 
00085 /**
00086  * Return the color number currently detected.
00087  * @param link the HTCS port number
00088  * @return color index number or -1 if an error occurred.
00089  */
00090 int HTCSreadColor(tSensors link) {
00091   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00092 
00093   HTCS_I2CRequest[0] = 2;                             // Message size
00094   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                 // I2C Address
00095   HTCS_I2CRequest[2] = HTCS_OFFSET + HTCS_COLNUM_REG; // Start colour number register
00096 
00097   if (!writeI2C(link, HTCS_I2CRequest, 1))
00098     return -1;
00099 
00100   if (!readI2C(link, HTCS_I2CReply, 1))
00101     return -1;
00102 
00103   return HTCS_I2CReply[0];
00104 }
00105 
00106 
00107 /**
00108  * Return the color number currently detected.
00109  * @param muxsensor the SMUX sensor port number
00110  * @return color index number or -1 if an error occurred.
00111  */
00112 #ifdef __HTSMUX_SUPPORT__
00113 int HTCSreadColor(tMUXSensor muxsensor) {
00114         memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00115 
00116   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00117     HTSMUXconfigChannel(muxsensor, HTCS_config);
00118 
00119   if (!HTSMUXreadPort(muxsensor, HTCS_I2CReply, 1, HTCS_COLNUM_REG)) {
00120     return -1;
00121   }
00122 
00123   return HTCS_I2CReply[0];
00124 }
00125 #endif // __HTSMUX_SUPPORT__
00126 
00127 
00128 /**
00129  * Get the detection levels for the three color components.
00130  * @param link the HTCS port number
00131  * @param red the red value
00132  * @param green the green value
00133  * @param blue the blue value
00134  * @return true if no error occured, false if it did
00135  */
00136 bool HTCSreadRGB(tSensors link, int &red, int &green, int &blue) {
00137   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00138 
00139   HTCS_I2CRequest[0] = 2;                           // Message size
00140   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;               // I2C Address
00141   HTCS_I2CRequest[2] = HTCS_OFFSET + HTCS_RED_REG;  // Start red sensor value
00142 
00143   if (!writeI2C(link, HTCS_I2CRequest, 3))
00144     return false;
00145 
00146   if (!readI2C(link, HTCS_I2CReply, 3))
00147     return false;
00148 
00149   red = HTCS_I2CReply[0];
00150   green = HTCS_I2CReply[1];
00151   blue = HTCS_I2CReply[2];
00152 
00153   return true;
00154 }
00155 
00156 
00157 /**
00158  * Get the detection levels for the three color components.
00159  * @param muxsensor the SMUX sensor port number
00160  * @param red the red value
00161  * @param green the green value
00162  * @param blue the blue value
00163  * @return true if no error occured, false if it did
00164  */
00165 #ifdef __HTSMUX_SUPPORT__
00166 bool HTCSreadRGB(tMUXSensor muxsensor, int &red, int &green, int &blue) {
00167   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00168 
00169   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00170     HTSMUXconfigChannel(muxsensor, HTCS_config);
00171 
00172   if (!HTSMUXreadPort(muxsensor, HTCS_I2CReply, 3, HTCS_RED_REG)) {
00173     return false;
00174   }
00175 
00176   red = HTCS_I2CReply[0];
00177   green = HTCS_I2CReply[1];
00178   blue = HTCS_I2CReply[2];
00179 
00180   return true;
00181 }
00182 #endif // __HTSMUX_SUPPORT__
00183 
00184 
00185 /**
00186  * Get the detection levels for the hue, saturation, value components.
00187  * @param link the HTCS port number
00188  * @param hue the hue output value (from 0 to 365, or -1 if n/a)
00189  * @param saturation the saruration output value (from 0 to 100, or -1 if n/a)
00190  * @param value the value output value (from 0 to 100)
00191  * @return true if no error occured, false if it did
00192  */
00193 bool HTCSreadHSV(tSensors link, float &hue, float &saturation, float &value) {
00194 
00195   int red,green,blue;
00196   bool ret = HTCSreadRGB(link, red, green, blue);
00197   RGBtoHSV(red,green,blue, hue, saturation, value);
00198 
00199   return ret;
00200 }
00201 
00202 
00203 /**
00204  * Get the detection levels for the hue, saturation, value components.
00205  * @param muxsensor the SMUX sensor port number
00206  * @param hue the hue output value (from 0 to 365, or -1 if n/a)
00207  * @param saturation the saruration output value (from 0 to 100, or -1 if n/a)
00208  * @param value the value output value (from 0 to 100)
00209  * @return true if no error occured, false if it did
00210  */
00211 #ifdef __HTSMUX_SUPPORT__
00212 bool HTCSreadHSV(tMUXSensor muxsensor, float &hue, float &saturation, float &value) {
00213 
00214   int red,green,blue;
00215 
00216   bool ret = HTCSreadRGB(muxsensor, red, green, blue);
00217   RGBtoHSV(red,green,blue, hue, saturation, value);
00218 
00219   return ret;
00220 }
00221 #endif // __HTSMUX_SUPPORT__
00222 
00223 
00224 /**
00225  * Get the normalised RGB readings. The normalization sets the highest
00226  * value of the three Red, Green and Blue reading to 255 and adjusts the
00227  * other two proportionately.
00228  * @param link the HTCS port number
00229  * @param red the red value
00230  * @param green the green value
00231  * @param blue the blue value
00232  * @return true if no error occured, false if it did
00233  */
00234 bool HTCSreadNormRGB(tSensors link, int &red, int &green, int &blue) {
00235   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00236 
00237   HTCS_I2CRequest[0] = 2;                               // Message size
00238   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                   // I2C Address
00239   HTCS_I2CRequest[2] = HTCS_OFFSET + HTSC_RED_NORM_REG; // Start red normalised sensor values
00240 
00241   if (!writeI2C(link, HTCS_I2CRequest, 3))
00242     return false;
00243 
00244   if (!readI2C(link, HTCS_I2CReply, 3))
00245     return false;
00246 
00247   red = HTCS_I2CReply[0];
00248   green = HTCS_I2CReply[1];
00249   blue = HTCS_I2CReply[2];
00250 
00251   return true;
00252 }
00253 
00254 /**
00255  * Get the raw RGB readings, these are 10bit values.
00256  * @param link the HTCS port number
00257  * @param red the red value
00258  * @param green the green value
00259  * @param blue the blue value
00260  * @return true if no error occured, false if it did
00261  */
00262 
00263 bool HTCSreadRawRGB(tSensors link, int &red, int &green, int &blue) {
00264   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00265 
00266   HTCS_I2CRequest[0] = 2;                               // Message size
00267   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                   // I2C Address
00268   HTCS_I2CRequest[2] = HTCS_OFFSET + HTCS_RED_RAW_REG;  // Start red raw sensor value
00269 
00270   if (!writeI2C(link, HTCS_I2CRequest, 6))
00271     return false;
00272 
00273   if (!readI2C(link, HTCS_I2CReply, 6))
00274     return false;
00275 
00276   red = HTCS_I2CReply[0];
00277   green = HTCS_I2CReply[1];
00278   blue = HTCS_I2CReply[2];
00279 
00280   return true;
00281 }
00282 
00283 /**
00284  * Return the color index number currently detected. This is a single
00285  * 6 bit number color index. Bits 5 and 4 encode the red signal level,
00286  * bits 3 and 2 encode the green signal level and bits 1 and 0 encode
00287  * the blue signal levels.
00288  * @param link the HTCS port number
00289  * @return color index number or -1 if an error occurred.
00290  */
00291 int HTCSreadColorIndex(tSensors link) {
00292   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00293 
00294   HTCS_I2CRequest[0] = 2;                                // Message size
00295   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                    // I2C Address
00296   HTCS_I2CRequest[2] = HTCS_OFFSET + HTSC_COL_INDEX_REG; // Start colour index register
00297 
00298   if (!writeI2C(link, HTCS_I2CRequest, 1))
00299     return -1;
00300 
00301   if (!readI2C(link, HTCS_I2CReply, 1))
00302     return -1;
00303 
00304   return HTCS_I2CReply[0];
00305 }
00306 
00307 /**
00308  * Calibrate the sensor for white.
00309  * @param link the HTCS port number
00310  * @return true if no error occured, false if it did
00311  */
00312 bool HTCScalWhite(tSensors link) {
00313   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00314 
00315   HTCS_I2CRequest[0] = 3;               // Message size
00316   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;   // I2C Address
00317   HTCS_I2CRequest[2] = HTCS_CMD_REG;    // Command register
00318   HTCS_I2CRequest[3] = HTCS_CAL_WHITE;  // Command to calibrate white
00319 
00320   if (!writeI2C(link, HTCS_I2CRequest, 0))
00321     return false;
00322 
00323   return true;
00324 }
00325 
00326 #endif // __HTCS_H__
00327 
00328 /*
00329  * $Id: HTCS-driver.h 51 2011-04-27 16:29:06Z xander $
00330  */
00331 /* @} */
00332 /* @} */