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

HTIRS-driver.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: HTIRS-driver.h 48 2011-02-13 20:35:38Z xander $
00010  */
00011 
00012 #ifndef __HTIRS_H__
00013 #define __HTIRS_H__
00014 /** \file HTIRS-driver.h
00015  * \brief HiTechnic IR Seeker driver
00016  *
00017  * HTIRS-driver.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 2.00 AND HIGHER.
00041  * \author Xander Soldaat (mightor_at_gmail.com)
00042  * \date 25 November 2009
00043  * \version 0.9
00044  * \example HTIRS-test1.c
00045  * \example HTIRS-SMUX-test1.c
00046  */
00047 
00048 #pragma systemFile
00049 
00050 #ifndef __COMMON_H__
00051 #include "common.h"
00052 #endif
00053 
00054 #define HTIRS_I2C_ADDR 0x02      /*!< IR Seeker I2C device address */
00055 #define HTIRS_OFFSET   0x42      /*!< Offset for data registers */
00056 #define HTIRS_DIR      0x00      /*!< Address of Direction data */
00057 #define HTIRS_SSTR1    0x01      /*!< Address of Sensor 0 signal strength */
00058 #define HTIRS_SSTR2    0x02      /*!< Address of Sensor 1 signal strength */
00059 #define HTIRS_SSTR3    0x03      /*!< Address of Sensor 2 signal strength */
00060 #define HTIRS_SSTR4    0x04      /*!< Address of Sensor 3 signal strength */
00061 #define HTIRS_SSTR5    0x05      /*!< Address of Sensor 4 signal strength */
00062 
00063 int HTIRSreadDir(tSensors link);
00064 bool HTIRSreadAllStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00065 
00066 #ifdef __HTSMUX_SUPPORT__
00067 int HTIRSreadDir(tMUXSensor muxsensor);
00068 bool HTIRSreadAllStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00069 
00070 tConfigParams HTIRS_config = {HTSMUX_CHAN_I2C, 7, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00071 #endif // __HTSMUX_SUPPORT__
00072 
00073 tByteArray HTIRS_I2CRequest;    /*!< Array to hold I2C command data */
00074 tByteArray HTIRS_I2CReply;      /*!< Array to hold I2C reply data */
00075 
00076 /**
00077  * Read the value of the Direction data register and return it.
00078  * @param link the HTIRS port number
00079  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00080  */
00081 int HTIRSreadDir(tSensors link) {
00082   memset(HTIRS_I2CRequest, 0, sizeof(tByteArray));
00083 
00084   HTIRS_I2CRequest[0] = 2;                        // Message size
00085   HTIRS_I2CRequest[1] = HTIRS_I2C_ADDR;           // I2C Address
00086   HTIRS_I2CRequest[2] = HTIRS_OFFSET + HTIRS_DIR; // Start direction register
00087 
00088   if (!writeI2C(link, HTIRS_I2CRequest, 1))
00089     return -1;
00090 
00091   if (!readI2C(link, HTIRS_I2CReply, 1))
00092     return -1;
00093 
00094   return (int)HTIRS_I2CReply[0];
00095 }
00096 
00097 
00098 /**
00099  * Read the value of the Direction data register and return it.
00100  * @param muxsensor the SMUX sensor port number
00101  * @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
00102  */
00103 #ifdef __HTSMUX_SUPPORT__
00104 int HTIRSreadDir(tMUXSensor muxsensor) {
00105         memset(HTIRS_I2CReply, 0, sizeof(tByteArray));
00106 
00107   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00108     HTSMUXconfigChannel(muxsensor, HTIRS_config);
00109 
00110   if (!HTSMUXreadPort(muxsensor, HTIRS_I2CReply, 1, HTIRS_DIR))
00111     return -1;
00112 
00113   return (int)HTIRS_I2CReply[0];
00114 }
00115 #endif // __HTSMUX_SUPPORT__
00116 
00117 
00118 /**
00119  * Read the value of the all of the internal sensors.
00120  * @param link the HTIRS port number
00121  * @param dcS1 data from internal sensor nr 1
00122  * @param dcS2 data from internal sensor nr 2
00123  * @param dcS3 data from internal sensor nr 3
00124  * @param dcS4 data from internal sensor nr 4
00125  * @param dcS5 data from internal sensor nr 5
00126  * @return true if no error occured, false if it did
00127  */
00128 bool HTIRSreadAllStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00129   memset(HTIRS_I2CRequest, 0, sizeof(tByteArray));
00130 
00131   HTIRS_I2CRequest[0] = 2;                          // Message size
00132   HTIRS_I2CRequest[1] = HTIRS_I2C_ADDR;             // I2C Address
00133   HTIRS_I2CRequest[2] = HTIRS_OFFSET + HTIRS_SSTR1; // Address of Sensor 0 signal strength
00134 
00135   if (!writeI2C(link, HTIRS_I2CRequest, 5))
00136     return false;
00137 
00138   if (!readI2C(link, HTIRS_I2CReply, 5))
00139     return false;
00140 
00141   dcS1 = (int)HTIRS_I2CReply[0];
00142   dcS2 = (int)HTIRS_I2CReply[1];
00143   dcS3 = (int)HTIRS_I2CReply[2];
00144   dcS4 = (int)HTIRS_I2CReply[3];
00145   dcS5 = (int)HTIRS_I2CReply[4];
00146 
00147   return true;
00148 }
00149 
00150 
00151 /**
00152  * Read the value of the all of the internal sensors.
00153  * @param muxsensor the SMUX sensor port number
00154  * @param dcS1 data from internal sensor nr 1
00155  * @param dcS2 data from internal sensor nr 2
00156  * @param dcS3 data from internal sensor nr 3
00157  * @param dcS4 data from internal sensor nr 4
00158  * @param dcS5 data from internal sensor nr 5
00159  * @return true if no error occured, false if it did
00160  */
00161 #ifdef __HTSMUX_SUPPORT__
00162 bool HTIRSreadAllStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00163   memset(HTIRS_I2CReply, 0, sizeof(tByteArray));
00164 
00165   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00166     HTSMUXconfigChannel(muxsensor, HTIRS_config);
00167 
00168   if (!HTSMUXreadPort(muxsensor, HTIRS_I2CReply, 5, HTIRS_SSTR1))
00169     return false;
00170 
00171   dcS1 = (int)HTIRS_I2CReply[0];
00172   dcS2 = (int)HTIRS_I2CReply[1];
00173   dcS3 = (int)HTIRS_I2CReply[2];
00174   dcS4 = (int)HTIRS_I2CReply[3];
00175   dcS5 = (int)HTIRS_I2CReply[4];
00176   return true;
00177 }
00178 #endif // __HTSMUX_SUPPORT__
00179 
00180 #endif // __HTIRS_H__
00181 
00182 /*
00183  * $Id: HTIRS-driver.h 48 2011-02-13 20:35:38Z xander $
00184  */
00185 /* @} */
00186 /* @} */