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

hitechnic-irseeker-v1.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htirs IR Seeker V1
00004  * HiTechnic Color IR Seeker V1
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-irseeker-v1.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTIRS_H__
00013 #define __HTIRS_H__
00014 /** \file hitechnic-irseeker-v1.h
00015  * \brief HiTechnic IR Seeker driver
00016  *
00017  * hitechnic-irseeker-v1.h provides an API for the HiTechnic IR Seeker.
00018  *
00019  * NOTE: THIS DRIVER WILL NOT WORK WITH THE IR SEEKER V2!<br>
00020  * Use the HTDIR driver for the V2 sensor.
00021  *
00022  * Changelog:
00023  * - 0.1: Initial release
00024  * - 0.2: Changed comments on HTIRReadDir()
00025  * - 0.3: Reduced MAX_BUFSIZE to 5 to reduce overhead
00026  * - 0.4: Major rewrite of code, uses common.h for most functions
00027  * - 0.5: Fixed incorrect register numbers for signal strength registers
00028  * - 0.6: Added SMUX functions
00029  * - 0.7: HTIRSreadAllStrength() is now pass by reference to reduce memory<br>
00030  *        SMUX tByteArray removed, reuses HTIRS_I2CReply
00031  * - 0.8: Use new calls in common.h that don't require SPORT/MPORT macros
00032  * - 0.9: Removed HTIRSreadStrength(), use HTIRSreadAllStrength() instead
00033  *
00034  * Credits:
00035  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00036  * - JamesD for testing and finding bugs.
00037  *
00038  * License: You may use this code as you wish, provided you give credit where its due.
00039  *
00040  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00041 
00042  * \author Xander Soldaat (xander_at_botbench.com)
00043  * \date 25 November 2009
00044  * \version 0.9
00045  * \example hitechnic-irseeker-v1-test1.c
00046  * \example hitechnic-irseeker-v1-SMUX-test1.c
00047  */
00048 
00049 #pragma systemFile
00050 
00051 #ifndef __COMMON_H__
00052 #include "common.h"
00053 #endif
00054 
00055 #define HTIRS_I2C_ADDR 0x02      /*!< IR Seeker I2C device address */
00056 #define HTIRS_OFFSET   0x42      /*!< Offset for data registers */
00057 #define HTIRS_DIR      0x00      /*!< Address of Direction data */
00058 #define HTIRS_SSTR1    0x01      /*!< Address of Sensor 0 signal strength */
00059 #define HTIRS_SSTR2    0x02      /*!< Address of Sensor 1 signal strength */
00060 #define HTIRS_SSTR3    0x03      /*!< Address of Sensor 2 signal strength */
00061 #define HTIRS_SSTR4    0x04      /*!< Address of Sensor 3 signal strength */
00062 #define HTIRS_SSTR5    0x05      /*!< Address of Sensor 4 signal strength */
00063 
00064 int HTIRSreadDir(tSensors link);
00065 bool HTIRSreadAllStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00066 
00067 #ifdef __HTSMUX_SUPPORT__
00068 int HTIRSreadDir(tMUXSensor muxsensor);
00069 bool HTIRSreadAllStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00070 
00071 tConfigParams HTIRS_config = {HTSMUX_CHAN_I2C, 7, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00072 #endif // __HTSMUX_SUPPORT__
00073 
00074 tByteArray HTIRS_I2CRequest;    /*!< Array to hold I2C command data */
00075 tByteArray HTIRS_I2CReply;      /*!< Array to hold I2C reply data */
00076 
00077 /**
00078  * Read the value of the Direction data register and return it.
00079  * @param link the HTIRS port number
00080  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00081  */
00082 int HTIRSreadDir(tSensors link) {
00083   memset(HTIRS_I2CRequest, 0, sizeof(tByteArray));
00084 
00085   HTIRS_I2CRequest[0] = 2;                        // Message size
00086   HTIRS_I2CRequest[1] = HTIRS_I2C_ADDR;           // I2C Address
00087   HTIRS_I2CRequest[2] = HTIRS_OFFSET + HTIRS_DIR; // Start direction register
00088 
00089   if (!writeI2C(link, HTIRS_I2CRequest, HTIRS_I2CReply, 1))
00090     return -1;
00091 
00092   return (int)HTIRS_I2CReply[0];
00093 }
00094 
00095 
00096 /**
00097  * Read the value of the Direction data register and return it.
00098  * @param muxsensor the SMUX sensor port number
00099  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00100  */
00101 #ifdef __HTSMUX_SUPPORT__
00102 int HTIRSreadDir(tMUXSensor muxsensor) {
00103         memset(HTIRS_I2CReply, 0, sizeof(tByteArray));
00104 
00105   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00106     HTSMUXconfigChannel(muxsensor, HTIRS_config);
00107 
00108   if (!HTSMUXreadPort(muxsensor, HTIRS_I2CReply, 1, HTIRS_DIR))
00109     return -1;
00110 
00111   return (int)HTIRS_I2CReply[0];
00112 }
00113 #endif // __HTSMUX_SUPPORT__
00114 
00115 
00116 /**
00117  * Read the value of the all of the internal sensors.
00118  * @param link the HTIRS port number
00119  * @param dcS1 data from internal sensor nr 1
00120  * @param dcS2 data from internal sensor nr 2
00121  * @param dcS3 data from internal sensor nr 3
00122  * @param dcS4 data from internal sensor nr 4
00123  * @param dcS5 data from internal sensor nr 5
00124  * @return true if no error occured, false if it did
00125  */
00126 bool HTIRSreadAllStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00127   memset(HTIRS_I2CRequest, 0, sizeof(tByteArray));
00128 
00129   HTIRS_I2CRequest[0] = 2;                          // Message size
00130   HTIRS_I2CRequest[1] = HTIRS_I2C_ADDR;             // I2C Address
00131   HTIRS_I2CRequest[2] = HTIRS_OFFSET + HTIRS_SSTR1; // Address of Sensor 0 signal strength
00132 
00133   if (!writeI2C(link, HTIRS_I2CRequest, HTIRS_I2CReply, 5))
00134     return false;
00135 
00136   dcS1 = (int)HTIRS_I2CReply[0];
00137   dcS2 = (int)HTIRS_I2CReply[1];
00138   dcS3 = (int)HTIRS_I2CReply[2];
00139   dcS4 = (int)HTIRS_I2CReply[3];
00140   dcS5 = (int)HTIRS_I2CReply[4];
00141 
00142   return true;
00143 }
00144 
00145 
00146 /**
00147  * Read the value of the all of the internal sensors.
00148  * @param muxsensor the SMUX sensor port number
00149  * @param dcS1 data from internal sensor nr 1
00150  * @param dcS2 data from internal sensor nr 2
00151  * @param dcS3 data from internal sensor nr 3
00152  * @param dcS4 data from internal sensor nr 4
00153  * @param dcS5 data from internal sensor nr 5
00154  * @return true if no error occured, false if it did
00155  */
00156 #ifdef __HTSMUX_SUPPORT__
00157 bool HTIRSreadAllStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00158   memset(HTIRS_I2CReply, 0, sizeof(tByteArray));
00159 
00160   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00161     HTSMUXconfigChannel(muxsensor, HTIRS_config);
00162 
00163   if (!HTSMUXreadPort(muxsensor, HTIRS_I2CReply, 5, HTIRS_SSTR1))
00164     return false;
00165 
00166   dcS1 = (int)HTIRS_I2CReply[0];
00167   dcS2 = (int)HTIRS_I2CReply[1];
00168   dcS3 = (int)HTIRS_I2CReply[2];
00169   dcS4 = (int)HTIRS_I2CReply[3];
00170   dcS5 = (int)HTIRS_I2CReply[4];
00171   return true;
00172 }
00173 #endif // __HTSMUX_SUPPORT__
00174 
00175 #endif // __HTIRS_H__
00176 
00177 /*
00178  * $Id: hitechnic-irseeker-v1.h 133 2013-03-10 15:15:38Z xander $
00179  */
00180 /* @} */
00181 /* @} */