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

hitechnic-colour-v1.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: hitechnic-colour-v1.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTCS_H__
00013 #define __HTCS_H__
00014 /** \file hitechnic-colour-v1.h
00015  * \brief HiTechnic Color Sensor driver
00016  *
00017  * hitechnic-colour-v1.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 3.59 AND HIGHER. 
00034 
00035  * \author Xander Soldaat (xander_at_botbench.com)
00036  * \date 20 February 2011
00037  * \version 0.5
00038  * \example hitechnic-colour-v1-test1.c
00039  * \example hitechnic-colour-v1-test2.c
00040  * \example hitechnic-colour-v1-SMUX-test1.c
00041  */
00042 
00043 #pragma systemFile
00044 
00045 #ifndef __COMMON_H__
00046 #include "common.h"
00047 #endif
00048 
00049 #ifndef __LIGHT_COMMON_H__
00050 #include "common-light.h"
00051 #endif
00052 
00053 #define HTCS_I2C_ADDR       0x02      /*!< HTCS I2C device address */
00054 #define HTCS_CMD_REG        0x41      /*!< Command register */
00055 #define HTCS_OFFSET         0x42      /*!< Offset for data registers */
00056 #define HTCS_COLNUM_REG     0x00      /*!< Color number */
00057 #define HTCS_RED_REG        0x01      /*!< Red reading */
00058 #define HTCS_GREEN_REG      0x02      /*!< Green reading */
00059 #define HTCS_BLUE_REG       0x03      /*!< Blue reading */
00060 #define HTCS_RED_RAW_REG    0x04      /*!< Raw red reading (2 bytes) */
00061 #define HTCS_GREEN_RAW_REG  0x05      /*!< Raw green reading (2 bytes) */
00062 #define HTCS_BLUE_RAW_REG   0x06      /*!< Raw blue reading (2 bytes) */
00063 #define HTSC_COL_INDEX_REG  0x07      /*!< Color index number */
00064 #define HTSC_RED_NORM_REG   0x08      /*!< Normalised red reading */
00065 #define HTSC_GREEN_NORM_REG 0x09      /*!< Normalised green reading */
00066 #define HTSC_BLUE_NORM_REG  0x0A      /*!< Normalised blue reading */
00067 
00068 #define HTCS_CAL_WHITE      0x43      /*!< Command to calibrate white */
00069 
00070 int HTCSreadColor(tSensors link);
00071 bool HTCSreadRGB(tSensors link, int &red, int &green, int &blue);
00072 bool HTCSreadNormRGB(tSensors link, int &red, int &green, int &blue);
00073 bool HTCSreadRawRGB(tSensors link, int &red, int &green, int &blue);
00074 bool HTCScalWhite(tSensors link);
00075 
00076 #ifdef __HTSMUX_SUPPORT__
00077 int HTCSreadColor(tMUXSensor muxsensor);
00078 bool HTCSreadRGB(tMUXSensor muxsensor, int &red, int &green, int &blue);
00079 
00080 tConfigParams HTCS_config = {HTSMUX_CHAN_I2C, 4, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00081 #endif
00082 
00083 tByteArray HTCS_I2CRequest;           /*!< Array to hold I2C command data */
00084 tByteArray HTCS_I2CReply;             /*!< Array to hold I2C reply data */
00085 
00086 /**
00087  * Return the color number currently detected.
00088  * @param link the HTCS port number
00089  * @return color index number or -1 if an error occurred.
00090  */
00091 int HTCSreadColor(tSensors link) {
00092   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00093 
00094   HTCS_I2CRequest[0] = 2;                             // Message size
00095   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                 // I2C Address
00096   HTCS_I2CRequest[2] = HTCS_OFFSET + HTCS_COLNUM_REG; // Start colour number register
00097 
00098   if (!writeI2C(link, HTCS_I2CRequest, HTCS_I2CReply, 1))
00099     return -1;
00100 
00101   return HTCS_I2CReply[0];
00102 }
00103 
00104 
00105 /**
00106  * Return the color number currently detected.
00107  * @param muxsensor the SMUX sensor port number
00108  * @return color index number or -1 if an error occurred.
00109  */
00110 #ifdef __HTSMUX_SUPPORT__
00111 int HTCSreadColor(tMUXSensor muxsensor) {
00112         memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00113 
00114   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00115     HTSMUXconfigChannel(muxsensor, HTCS_config);
00116 
00117   if (!HTSMUXreadPort(muxsensor, HTCS_I2CReply, 1, HTCS_COLNUM_REG)) {
00118     return -1;
00119   }
00120 
00121   return HTCS_I2CReply[0];
00122 }
00123 #endif // __HTSMUX_SUPPORT__
00124 
00125 
00126 /**
00127  * Get the detection levels for the three color components.
00128  * @param link the HTCS port number
00129  * @param red the red value
00130  * @param green the green value
00131  * @param blue the blue value
00132  * @return true if no error occured, false if it did
00133  */
00134 bool HTCSreadRGB(tSensors link, int &red, int &green, int &blue) {
00135   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00136 
00137   HTCS_I2CRequest[0] = 2;                           // Message size
00138   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;               // I2C Address
00139   HTCS_I2CRequest[2] = HTCS_OFFSET + HTCS_RED_REG;  // Start red sensor value
00140 
00141   if (!writeI2C(link, HTCS_I2CRequest, HTCS_I2CReply, 3))
00142     return false;
00143 
00144   red = HTCS_I2CReply[0];
00145   green = HTCS_I2CReply[1];
00146   blue = HTCS_I2CReply[2];
00147 
00148   return true;
00149 }
00150 
00151 
00152 /**
00153  * Get the detection levels for the three color components.
00154  * @param muxsensor the SMUX sensor port number
00155  * @param red the red value
00156  * @param green the green value
00157  * @param blue the blue value
00158  * @return true if no error occured, false if it did
00159  */
00160 #ifdef __HTSMUX_SUPPORT__
00161 bool HTCSreadRGB(tMUXSensor muxsensor, int &red, int &green, int &blue) {
00162   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00163 
00164   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00165     HTSMUXconfigChannel(muxsensor, HTCS_config);
00166 
00167   if (!HTSMUXreadPort(muxsensor, HTCS_I2CReply, 3, HTCS_RED_REG)) {
00168     return false;
00169   }
00170 
00171   red = HTCS_I2CReply[0];
00172   green = HTCS_I2CReply[1];
00173   blue = HTCS_I2CReply[2];
00174 
00175   return true;
00176 }
00177 #endif // __HTSMUX_SUPPORT__
00178 
00179 
00180 /**
00181  * Get the detection levels for the hue, saturation, value components.
00182  * @param link the HTCS port number
00183  * @param hue the hue output value (from 0 to 365, or -1 if n/a)
00184  * @param saturation the saruration output value (from 0 to 100, or -1 if n/a)
00185  * @param value the value output value (from 0 to 100)
00186  * @return true if no error occured, false if it did
00187  */
00188 bool HTCSreadHSV(tSensors link, float &hue, float &saturation, float &value) {
00189 
00190   int red,green,blue;
00191   bool ret = HTCSreadRGB(link, red, green, blue);
00192   RGBtoHSV(red,green,blue, hue, saturation, value);
00193 
00194   return ret;
00195 }
00196 
00197 
00198 /**
00199  * Get the detection levels for the hue, saturation, value components.
00200  * @param muxsensor the SMUX sensor port number
00201  * @param hue the hue output value (from 0 to 365, or -1 if n/a)
00202  * @param saturation the saruration output value (from 0 to 100, or -1 if n/a)
00203  * @param value the value output value (from 0 to 100)
00204  * @return true if no error occured, false if it did
00205  */
00206 #ifdef __HTSMUX_SUPPORT__
00207 bool HTCSreadHSV(tMUXSensor muxsensor, float &hue, float &saturation, float &value) {
00208 
00209   int red,green,blue;
00210 
00211   bool ret = HTCSreadRGB(muxsensor, red, green, blue);
00212   RGBtoHSV(red,green,blue, hue, saturation, value);
00213 
00214   return ret;
00215 }
00216 #endif // __HTSMUX_SUPPORT__
00217 
00218 
00219 /**
00220  * Get the normalised RGB readings. The normalization sets the highest
00221  * value of the three Red, Green and Blue reading to 255 and adjusts the
00222  * other two proportionately.
00223  * @param link the HTCS port number
00224  * @param red the red value
00225  * @param green the green value
00226  * @param blue the blue value
00227  * @return true if no error occured, false if it did
00228  */
00229 bool HTCSreadNormRGB(tSensors link, int &red, int &green, int &blue) {
00230   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00231 
00232   HTCS_I2CRequest[0] = 2;                               // Message size
00233   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                   // I2C Address
00234   HTCS_I2CRequest[2] = HTCS_OFFSET + HTSC_RED_NORM_REG; // Start red normalised sensor values
00235 
00236   if (!writeI2C(link, HTCS_I2CRequest, HTCS_I2CReply, 3))
00237     return false;
00238 
00239   red = HTCS_I2CReply[0];
00240   green = HTCS_I2CReply[1];
00241   blue = HTCS_I2CReply[2];
00242 
00243   return true;
00244 }
00245 
00246 /**
00247  * Get the raw RGB readings, these are 10bit values.
00248  * @param link the HTCS port number
00249  * @param red the red value
00250  * @param green the green value
00251  * @param blue the blue value
00252  * @return true if no error occured, false if it did
00253  */
00254 
00255 bool HTCSreadRawRGB(tSensors link, int &red, int &green, int &blue) {
00256   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00257 
00258   HTCS_I2CRequest[0] = 2;                               // Message size
00259   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                   // I2C Address
00260   HTCS_I2CRequest[2] = HTCS_OFFSET + HTCS_RED_RAW_REG;  // Start red raw sensor value
00261 
00262   if (!writeI2C(link, HTCS_I2CRequest, HTCS_I2CReply, 6))
00263     return false;
00264 
00265   red = HTCS_I2CReply[0];
00266   green = HTCS_I2CReply[1];
00267   blue = HTCS_I2CReply[2];
00268 
00269   return true;
00270 }
00271 
00272 /**
00273  * Return the color index number currently detected. This is a single
00274  * 6 bit number color index. Bits 5 and 4 encode the red signal level,
00275  * bits 3 and 2 encode the green signal level and bits 1 and 0 encode
00276  * the blue signal levels.
00277  * @param link the HTCS port number
00278  * @return color index number or -1 if an error occurred.
00279  */
00280 int HTCSreadColorIndex(tSensors link) {
00281   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00282 
00283   HTCS_I2CRequest[0] = 2;                                // Message size
00284   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;                    // I2C Address
00285   HTCS_I2CRequest[2] = HTCS_OFFSET + HTSC_COL_INDEX_REG; // Start colour index register
00286 
00287   if (!writeI2C(link, HTCS_I2CRequest, HTCS_I2CReply, 1))
00288     return -1;
00289 
00290   return HTCS_I2CReply[0];
00291 }
00292 
00293 /**
00294  * Calibrate the sensor for white.
00295  * @param link the HTCS port number
00296  * @return true if no error occured, false if it did
00297  */
00298 bool HTCScalWhite(tSensors link) {
00299   memset(HTCS_I2CRequest, 0, sizeof(tByteArray));
00300 
00301   HTCS_I2CRequest[0] = 3;               // Message size
00302   HTCS_I2CRequest[1] = HTCS_I2C_ADDR;   // I2C Address
00303   HTCS_I2CRequest[2] = HTCS_CMD_REG;    // Command register
00304   HTCS_I2CRequest[3] = HTCS_CAL_WHITE;  // Command to calibrate white
00305 
00306   return writeI2C(link, HTCS_I2CRequest);
00307 }
00308 
00309 #endif // __HTCS_H__
00310 
00311 /*
00312  * $Id: hitechnic-colour-v1.h 133 2013-03-10 15:15:38Z xander $
00313  */
00314 /* @} */
00315 /* @} */