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

hitechnic-irrecv.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htirr IR Receiver Sensor
00004  * HiTechnic IR Receiver Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-irrecv.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTIRR_H__
00013 #define __HTIRR_H__
00014 /** \file hitechnic-irrecv.h
00015  * \brief HiTechnic IR Receiver Sensor driver
00016  *
00017  * HTIRR2-driver.h provides an API for the IR Receiver Sensor driver.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Changed HTIRRreadChannel() proto to use signed bytes like function.
00022  *
00023  * Credits:
00024  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00025  *
00026  * License: You may use this code as you wish, provided you give credit where its due.
00027  *
00028  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00029 
00030  * \author Xander Soldaat (xander_at_botbench.com)
00031  * \date 03 November 2009
00032  * \version 0.2
00033  * \example hitechnic-irrecv-test1.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 #define HTIRR_I2C_ADDR        0x02      /*!< HTIRR I2C device address */
00043 #define HTIRR_OFFSET          0x42      /*!< Offset for data registers */
00044 
00045 // Values contained by registers in active mode
00046 #define HTIRR_MOTOR_1A        0x00      /*!< Color number */
00047 #define HTIRR_MOTOR_1B        0x01      /*!< Color number */
00048 #define HTIRR_MOTOR_2A        0x02      /*!< Color number */
00049 #define HTIRR_MOTOR_2B        0x03      /*!< Color number */
00050 #define HTIRR_MOTOR_3A        0x04      /*!< Color number */
00051 #define HTIRR_MOTOR_3B        0x05      /*!< Color number */
00052 #define HTIRR_MOTOR_4A        0x06      /*!< Color number */
00053 #define HTIRR_MOTOR_4B        0x07      /*!< Color number */
00054 
00055 /*! Some define. */
00056 #define MOTOR_BRAKE           -128      /*!< Motor brake */
00057 
00058 /*
00059 <function prototypes>
00060 */
00061 bool HTIRRreadChannel(tSensors link, byte channel, sbyte &motA, sbyte &motB);
00062 bool HTIRRreadAllChannels(tSensors link, tsByteArray &motorSpeeds);
00063 
00064 tByteArray HTIRR_I2CRequest;           /*!< Array to hold I2C command data */
00065 tByteArray HTIRR_I2CReply;             /*!< Array to hold I2C reply data */
00066 
00067 
00068 /**
00069  * Get the speeds of the motors for a given channel.
00070  * @param link the HTIRR port number
00071  * @param channel the channel number (1 to 4)
00072  * @param motA the speed for Motor A (-100 to +100, -128 = brake)
00073  * @param motB the speed for Motor B (-100 to +100, -128 = brake)
00074  * @return true if no error occured, false if it did
00075  */
00076 bool HTIRRreadChannel(tSensors link, byte channel, sbyte &motA, sbyte &motB) {
00077   memset(HTIRR_I2CRequest, 0, sizeof(tByteArray));
00078 
00079   HTIRR_I2CRequest[0] = 2;                                // Message size
00080   HTIRR_I2CRequest[1] = HTIRR_I2C_ADDR;                   // I2C Address
00081   HTIRR_I2CRequest[2] = HTIRR_OFFSET + ((channel - 1) * 2); // Start of speed registry
00082 
00083   if (!writeI2C(link, HTIRR_I2CRequest, HTIRR_I2CReply, 2))
00084     return false;
00085 
00086   motA = (HTIRR_I2CReply[0] >= 128) ? (int)HTIRR_I2CReply[0] - 256 : (int)HTIRR_I2CReply[0];
00087   motB = (HTIRR_I2CReply[1] >= 128) ? (int)HTIRR_I2CReply[1] - 256 : (int)HTIRR_I2CReply[1];
00088 
00089   return true;
00090 }
00091 
00092 
00093 /**
00094  * Get the speeds of the motors for all channels.
00095  * @param link the HTIRR port number
00096  * @param motorSpeeds the speeds for all the motors (-100 to +100, -128 = brake)
00097  * @return true if no error occured, false if it did
00098  */
00099 bool HTIRRreadAllChannels(tSensors link, tsByteArray &motorSpeeds){
00100   memset(motorSpeeds, 0, sizeof(tsByteArray));
00101   memset(HTIRR_I2CRequest, 0, sizeof(tByteArray));
00102 
00103   HTIRR_I2CRequest[0] = 2;                // Message size
00104   HTIRR_I2CRequest[1] = HTIRR_I2C_ADDR;   // I2C Address
00105   HTIRR_I2CRequest[2] = HTIRR_OFFSET;     // Start of speed registry
00106 
00107   if (!writeI2C(link, HTIRR_I2CRequest, HTIRR_I2CReply, 8))
00108     return false;
00109 
00110   memcpy(motorSpeeds, HTIRR_I2CReply, 8);
00111   return true;
00112 }
00113 
00114 #endif // __HTIRR_H__
00115 /*
00116  * $Id: hitechnic-irrecv.h 133 2013-03-10 15:15:38Z xander $
00117  */
00118 /* @} */
00119 /* @} */