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

mindsensors-lightsensorarray.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup msll LineLeader Sensor
00004  * LineLeader Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-lightsensorarray.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSLSA_H__
00013 #define __MSLSA_H__
00014 
00015 /** \file mindsensors-lightsensorarray.h
00016  * \brief Mindsensors LigthSensorArray
00017  *
00018  * mindsensors-lightsensorarray.h provides an API for the Mindsensors LightSensorArray
00019  *
00020  * Changelog:
00021  *  - 0.1 Initial       release<br>
00022  *
00023  * License: You may use this code as you wish, provided you give credit where it's due.
00024  *
00025  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00026 
00027  * \author Xander Soldaat
00028  * \date 29 September 2012
00029  * \version 0.1
00030  * \example mindsensors-ligthsensorarray-test1.c
00031  * \example mindsensors-ligthsensorarray-test3.c
00032  */
00033 
00034 #pragma systemFile
00035 
00036 #ifndef __COMMON_H__
00037         #include "common.h"
00038 #endif
00039 
00040 #define MSLSA_I2C_ADDR                      0x14  /*!< I2C address used by the MSLSA */
00041 #define MSLSA_CMD_REG                       0x41  /*!< Register used for issuing commands */
00042 
00043 #define MSLSA_CALIBRATED        0x42  /*!< Calibrated Sensor reading */
00044 #define MSLSA_WHITE_LIMIT           0x4A  /*!< White Reading Limit */
00045 #define MSLSA_BLACK LIMIT         0x52  /*!< Black Reading Limit */
00046 #define MSLSA_WHITE_CALIB_DATA  0x5A  /*!< White Calibration data */
00047 #define MSLSA_BLACK_CALIB_DATA  0x62  /*!< Black Calibration data */
00048 #define MSLSA_UNCALIBRATED                0x6A  /*!< Uncalibrated sensor voltage  */
00049 
00050 #define MSLSA_CMD_FREQ_US       'A'   /*!< American frequency compensation  */
00051 #define MSLSA_CMD_CALIB_BLACK   'B'   /*!< Calibrate black values  */
00052 #define MSLSA_CMD_POWERDOWN     'D'   /*!< Power down the sensor array  */
00053 #define MSLSA_CMD_FREQ_EU       'E'   /*!< European frequency compensation  */
00054 #define MSLSA_CMD_POWERUP       'P'   /*!< Power up the sensor array  */
00055 #define MSLSA_CMD_FREQ_UNI      'U'   /*!< Universal frequency compensation (default) */
00056 #define MSLDA_CMD_CALIB_WHITE   'W'   /*!< Calibrate white values  */
00057 
00058 tByteArray MSLSA_I2CRequest;       /*!< Array to hold I2C command data */
00059 tByteArray MSLSA_I2CReply;         /*!< Array to hold I2C reply data */
00060 
00061 #define MSLSAwakeUp(X)    _MSLSAsendCommand(X, MSLSA_CMD_POWERUP)     /*!< Wake sensor from sleep mode */
00062 #define MSLSASleep(X)     _MSLSAsendCommand(X, MSLSA_CMD_POWERDOWN)   /*!< Put sensor into sleep mode */
00063 #define MSLSAcalWhite(X)  _MSLSAsendCommand(X, MSLDA_CMD_CALIB_WHITE) /*!< Calibrate the white value */
00064 #define MSLSAcalBlack(X)  _MSLSAsendCommand(X, MSLSA_CMD_CALIB_BLACK) /*!< Calibrate the black value */
00065 #define MSLSAsetEU(X)     _MSLSAsendCommand(X, MSLSA_CMD_FREQ_EU)     /*!< Calibrate the black value */
00066 #define MSLSAsetUS(X)     _MSLSAsendCommand(X, MSLSA_CMD_FREQ_US)     /*!< Calibrate the black value */
00067 #define MSLSAsetUni(X)    _MSLSAsendCommand(X, MSLSA_CMD_FREQ_UNI)    /*!< Calibrate the black value */
00068 
00069 
00070 bool MSLSAreadSensors(tSensors link, ubyte *values);
00071 bool MSLSAreadRawSensors(tSensors link, int *values);
00072 bool _MSLSAsendCommand(tSensors link, ubyte cmd);                      /*!< Send a command to the LightSensorArray */
00073 
00074 /**
00075  * This function sends a command to the lightsensorarray.
00076  *
00077  * Note: this is an internal function and should not be called directly.
00078  * @param link the sensor port number
00079  * @param cmd the command to be sent
00080  * @return true if no error occured, false if it did
00081 
00082 PRELIMINARY COMMANDS FROM NXC LIB CODE
00083  - A American frequency compensation
00084  - B for black calibration
00085  - D sensor power down
00086  - E European frequency compensation
00087  - P power on sensor
00088  - U Universal frequency compensation (default)
00089  - W White balance
00090  */
00091 bool _MSLSAsendCommand(tSensors link, ubyte cmd) {
00092   MSLSA_I2CRequest[0] = 3;             // Message size
00093   MSLSA_I2CRequest[1] = MSLSA_I2C_ADDR;   // I2C Address
00094   MSLSA_I2CRequest[2] = MSLSA_CMD_REG;    // Register used for issuing commands
00095   MSLSA_I2CRequest[3] = cmd;           // Command to be executed
00096 
00097   return writeI2C(link, MSLSA_I2CRequest);
00098 }
00099 
00100 
00101 /**
00102  * Return the calibrated values from the sensors
00103  * @param link the Mindsensors LightSensorArray port number
00104  * @param values pointer to unsigned byte array to hold the sensor values
00105  * @return true if no error occured, false if it did
00106  */
00107 bool MSLSAreadSensors(tSensors link, ubyte *values)
00108 {
00109   MSLSA_I2CRequest[0] = 2;
00110   MSLSA_I2CRequest[1] = MSLSA_I2C_ADDR;
00111   MSLSA_I2CRequest[2] = MSLSA_CALIBRATED;
00112 
00113   if (!writeI2C(link, MSLSA_I2CRequest, MSLSA_I2CReply, 8))
00114     return false;
00115 
00116   // clear out the old values
00117   memset(values, 0, sizeof(values));
00118 
00119   // copy the new values (first 8 bytes)
00120   memcpy(values, MSLSA_I2CReply, 8);
00121 
00122   return true;
00123 }
00124 
00125 
00126 /**
00127  * Return the uncalibrated values from the sensors
00128  * @param link the Mindsensors LightSensorArray port number
00129  * @param values pointer to integer array to hold the uncalibrated sensor values
00130  * @return true if no error occured, false if it did
00131  */
00132 bool MSLSAreadRawSensors(tSensors link, int *values)
00133 {
00134   MSLSA_I2CRequest[0] = 2;
00135   MSLSA_I2CRequest[1] = MSLSA_I2C_ADDR;
00136   MSLSA_I2CRequest[2] = MSLSA_UNCALIBRATED;
00137 
00138   if (!writeI2C(link, MSLSA_I2CRequest, MSLSA_I2CReply, 16))
00139     return false;
00140 
00141   // clear out the old values
00142   memset(values, 0, sizeof(values));
00143 
00144   for (int i = 0; i < 8; i++)
00145   {
00146     values[i] = MSLSA_I2CReply[i * 2] + (MSLSA_I2CReply[(i * 2) + 1] << 8);
00147   }
00148 
00149   // copy the new values (first 8 bytes)
00150   memcpy(values, MSLSA_I2CReply, 8);
00151 
00152   return true;
00153 }
00154 
00155 #endif // __MSLSA_H__
00156 
00157 /*
00158  * $Id: mindsensors-lightsensorarray.h 133 2013-03-10 15:15:38Z xander $
00159  */
00160 /* @} */
00161 /* @} */