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

hitechnic-pir.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup HTPIR PIR Sensor
00004  * HiTechnic PIR Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-pir.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 /** \file hitechnic-pir.h
00013  * \brief HiTechnic PIR Sensor Driver
00014  *
00015  * hitechnic-pir.h provides an API for the HiTechnic PIR Sensor.
00016  *
00017  * Changelog:
00018  * - 0.1: Initial release
00019  *
00020  * License: You may use this code as you wish, provided you give credit where its due.
00021  *
00022  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00023 
00024  * \author Xander Soldaat (xander_at_botbench.com)
00025  * \date 15 August 2012
00026  * \version 0.1
00027  * \example hitechnic-pir-test1.c
00028  */
00029 
00030 #pragma systemFile
00031 
00032 #ifndef __COMMON_H__
00033 #include "common.h"
00034 #endif
00035 
00036 // I2C address + registers
00037 #define HTPIR_I2C_ADDR       0x02  /*!< HTPIR I2C device address */
00038 #define HTPIR_DEADBAND       0x41  /*!< HTPIR Mode control */
00039 #define HTPIR_READING        0x42  /*!< HTPIR Heading Upper bits */
00040 
00041 #define HTPIR_DEFAULT_DEADBAND 12
00042 
00043 bool HTPIRsetDeadband(tSensors link, int deadband);
00044 int HTPIRreadSensor(tSensors link);
00045 
00046 #ifdef __HTSMUX_SUPPORT__
00047 int HTPIRreadSensor(tMUXSensor muxsensor);
00048 
00049 tConfigParams HTPIR_config = {HTSMUX_CHAN_I2C, 1, 0x02, 0x42}; /*!< Array to hold SMUX config data for sensor */
00050 #endif // __HTSMUX_SUPPORT__
00051 
00052 tByteArray HTPIR_I2CRequest;       /*!< Array to hold I2C command data */
00053 tByteArray HTPIR_I2CReply;         /*!< Array to hold I2C reply data */
00054 
00055 
00056 /**
00057  * The sensor element with the PIR sensor generates continuous noise.
00058  * The size of the deadband is set to minimize the number of false
00059  * detections which will be reported by the sensor. If the deadband
00060  * value is too small, some of the noise fluctuations will exceed the
00061  * deadband threshold and will appear as actual non-zero readings. \n
00062  * The Deadband field may be set from 0 to 47 to define the half width
00063  * of the deadband. The default value is 12.
00064  * @param link the HTPIR port number
00065  * @param deadband the amount
00066  * @return true if no error occured, false if it did
00067  */
00068 bool HTPIRsetDeadband(tSensors link, int deadband) {
00069   memset(HTPIR_I2CRequest, 0, sizeof(tByteArray));
00070   // Ensure the values are valid
00071   deadband = clip(deadband, 0, 47);
00072 
00073   HTPIR_I2CRequest[0] = 3;                  // Number of bytes in I2C command
00074   HTPIR_I2CRequest[1] = HTPIR_I2C_ADDR;     // I2C address of PIR sensor
00075   HTPIR_I2CRequest[2] = HTPIR_DEADBAND;     // Set write address to sensor mode register
00076   HTPIR_I2CRequest[3] = deadband;           // The calibration mode command
00077 
00078   // Start the calibration
00079   return writeI2C(link, HTPIR_I2CRequest);
00080 }
00081 
00082 
00083 /**
00084  * Read the current levels detected by the sensor
00085  * @param link the HTPIR port number
00086  * @return level detected by sensor, will be between -128 and 127.
00087  */
00088 int HTPIRreadSensor(tSensors link) {
00089   memset(HTPIR_I2CRequest, 0, sizeof(tByteArray));
00090 
00091   HTPIR_I2CRequest[0] = 2;               // Number of bytes in I2C command
00092   HTPIR_I2CRequest[1] = HTPIR_I2C_ADDR;   // I2C address of PIR sensor
00093   HTPIR_I2CRequest[2] = HTPIR_READING;     // Set write address to sensor mode register
00094 
00095   if (!writeI2C(link, HTPIR_I2CRequest, HTPIR_I2CReply, 1))
00096     return -1;
00097 
00098   return (HTPIR_I2CReply[0] >= 128) ? (int)HTPIR_I2CReply[0] - 256 : (int)HTPIR_I2CReply[0];
00099 }
00100 
00101 /**
00102  * Read the current levels detected by the sensor
00103  * @param muxsensor the SMUX sensor port number
00104  * @return level detected by sensor, will be between -128 and 127.
00105  */
00106 #ifdef __HTSMUX_SUPPORT__
00107 int HTPIRreadSensor(tMUXSensor muxsensor) {
00108   memset(HTPIR_I2CReply, 0, sizeof(tByteArray));
00109 
00110   if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00111     HTSMUXconfigChannel(muxsensor, HTPIR_config);
00112 
00113   if (!HTSMUXreadPort(muxsensor, HTPIR_I2CReply, 1)) {
00114     return -1;
00115   }
00116 
00117   return (HTPIR_I2CReply[0] >= 128) ? (int)HTPIR_I2CReply[0] - 256 : (int)HTPIR_I2CReply[0];
00118 }
00119 #endif // __HTSMUX_SUPPORT__
00120 
00121 
00122 /*
00123  * $Id: hitechnic-pir.h 133 2013-03-10 15:15:38Z xander $
00124  */
00125 /* @} */
00126 /* @} */