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

dexterind-dlight.h

Go to the documentation of this file.
00001 /*!@addtogroup Dexter_Industries
00002  * @{
00003  * @defgroup DLIGHT dLight Sensor
00004  * Dexter Industries dLight Sensor Driver
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: dexterind-dlight.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __DLIGHT_H__
00013 #define __DLIGHT_H__
00014 /** \file dexterind-dlight.h
00015  * \brief Dexter Industries dLight Sensor Driver
00016  *
00017  * dexterind-dlight.h provides an API for the Dexter Industries dLight Sensor Driver
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Dexter Industries for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00028 
00029  
00030  * \author Xander Soldaat (xander_at_botbench.com)
00031  * \date 09 March 2013
00032  * \version 0.1
00033  * \example dexterind-dlight-test1.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042 
00043 #ifndef __COMMON_LIGHT_H__
00044 #include "drivers/common-light.h"
00045 #endif
00046 
00047 #define DLIGHT_I2C_ADDR_ALL 0xE0        /*!< dLight I2C device address for all connected devices */
00048 #define DLIGHT_I2C_ADDR_1   0x04        /*!< dLight I2C device address for device 1 (the NXT adapter) */
00049 #define DLIGHT_I2C_ADDR_2   0x14        /*!< dLight I2C device address for device 2 */
00050 #define DLIGHT_I2C_ADDR_3   0x24        /*!< dLight I2C device address for device 3 */
00051 #define DLIGHT_I2C_ADDR_4   0x34        /*!< dLight I2C device address for device 4 */
00052 
00053 #define DLIGHT_REG_MODE1    0x80        /*!< dLight register to configure autoincrement, must be set to 0x01 */
00054 #define DLIGHT_REG_MODE2    0x81        /*!< dLight register to configure blinking, must be set to 0x25 */
00055 #define DLIGHT_REG_RED      0x82        /*!< dLight register to configure the amount of red, 0-0xFF */
00056 #define DLIGHT_REG_GREEN    0x83        /*!< dLight register to configure the amount of green, 0-0xFF */
00057 #define DLIGHT_REG_BLUE     0x84        /*!< dLight register to configure the amount of blue, 0-0xFF */
00058 #define DLIGHT_REG_EXTERNAL 0x85        /*!< dLight register to configure the external LED, 0-0xFF */
00059 #define DLIGHT_REG_BPCT     0x86        /*!< dLight register to configure the blinking duty cycle */
00060 #define DLIGHT_REG_BFREQ    0x87        /*!< dLight register to configure the blinking frequency */
00061 #define DLIGHT_REG_LEDOUT   0x88        /*!< dLight register to configure enable/disable the LEDs and their blinking  */
00062 
00063 #define DLIGHT_CMD_DISABLE_LEDS   0x00  /*!< dLight cmmand to disable the LEDs completely */
00064 #define DLIGHT_CMD_DISABLE_BLINK  0xAA  /*!< dLight cmmand to disable blinking */
00065 #define DLIGHT_CMD_ENABLE_BLINK   0xFF  /*!< dLight cmmand to enable blinking  */
00066 
00067 
00068 tByteArray DLIGHT_I2CRequest;             /*!< Array to hold I2C command data */
00069 
00070 /**
00071  * Initialise the dLight sensor.  Turns off blinking.
00072  * @param link the dLight port number
00073  * @param addr the dLight I2C address
00074  * @return true if no error occured, false if it did
00075  */
00076 bool DLIGHTinit(tSensors link, ubyte addr){
00077   DLIGHT_I2CRequest[0] = 4;
00078   DLIGHT_I2CRequest[1] = addr;
00079   DLIGHT_I2CRequest[2] = DLIGHT_REG_MODE1;
00080   DLIGHT_I2CRequest[3] = 0x01;
00081   DLIGHT_I2CRequest[4] = 0x25;
00082 
00083   if (!writeI2C(link, DLIGHT_I2CRequest))
00084     return false;
00085 
00086   wait1Msec(50);
00087 
00088   DLIGHT_I2CRequest[0] = 3;
00089   DLIGHT_I2CRequest[1] = addr;
00090   DLIGHT_I2CRequest[2] = DLIGHT_REG_LEDOUT;
00091   DLIGHT_I2CRequest[3] = DLIGHT_CMD_DISABLE_BLINK;
00092 
00093   return writeI2C(link, DLIGHT_I2CRequest);
00094 }
00095 
00096 
00097 /**
00098  * Set the dLight to the specified RGB colour
00099  * @param link the dLight port number
00100  * @param addr the dLight I2C address
00101  * @param r the red LED brightness value (0-255)
00102  * @param g the green LED brightness value (0-255)
00103  * @param b the blue LED brightness value (0-255)
00104  * @return true if no error occured, false if it did
00105  */
00106 bool DLIGHTsetColor(tSensors link, ubyte addr, ubyte r, ubyte g, ubyte b){
00107   DLIGHT_I2CRequest[0] = 5;
00108   DLIGHT_I2CRequest[1] = addr;
00109   DLIGHT_I2CRequest[2] = DLIGHT_REG_RED;
00110   DLIGHT_I2CRequest[3] = r;
00111   DLIGHT_I2CRequest[4] = g;
00112   DLIGHT_I2CRequest[5] = b;
00113 
00114   return writeI2C(link, DLIGHT_I2CRequest);
00115 }
00116 
00117 
00118 /**
00119  * Set the dLight to the specified RGB colour
00120  * @param link the dLight port number
00121  * @param addr the dLight I2C address
00122  * @param external the external LED brightness value (0-255)
00123  * @return true if no error occured, false if it did
00124  */
00125 bool DLIGHTsetExternal(tSensors link, ubyte addr, ubyte external){
00126   DLIGHT_I2CRequest[0] = 3;
00127   DLIGHT_I2CRequest[1] = addr;
00128   DLIGHT_I2CRequest[2] = DLIGHT_REG_EXTERNAL;
00129   DLIGHT_I2CRequest[3] = external;
00130 
00131   return writeI2C(link, DLIGHT_I2CRequest);
00132 }
00133 
00134 
00135 /**
00136  * Set the dLight to the specified RGB colour
00137  * @param link the dLight port number
00138  * @param addr the dLight I2C address
00139  * @param BlinkRate the frequency at which to blink the light (Hz)
00140  * @param DutyCycle duty cycle of the light in percentage
00141  * @return true if no error occured, false if it did
00142  */
00143 bool DLIGHTsetBlinking(tSensors link, ubyte addr, float BlinkRate, long DutyCycle){
00144   BlinkRate*= 24;
00145   BlinkRate--;
00146 
00147   DLIGHT_I2CRequest[0] = 4;
00148   DLIGHT_I2CRequest[1] = addr;
00149   DLIGHT_I2CRequest[2] = DLIGHT_REG_BPCT;
00150   DLIGHT_I2CRequest[3] = (255 * DutyCycle) / 100;
00151   DLIGHT_I2CRequest[4] = round(BlinkRate);
00152   writeDebugStreamLine("rate: %d, duty: %d", DLIGHT_I2CRequest[4], DLIGHT_I2CRequest[3]);
00153   return writeI2C(link, DLIGHT_I2CRequest);
00154 }
00155 
00156 
00157 /**
00158  * Start blinking the LED
00159  * @param link the dLight port number
00160  * @param addr the dLight I2C address
00161  * @return true if no error occured, false if it did
00162  */
00163 bool DLIGHTstartBlinking(tSensors link, ubyte addr){
00164   DLIGHT_I2CRequest[0] = 3;
00165   DLIGHT_I2CRequest[1] = addr;
00166   DLIGHT_I2CRequest[2] = DLIGHT_REG_LEDOUT;
00167   DLIGHT_I2CRequest[3] = DLIGHT_CMD_ENABLE_BLINK;
00168   return writeI2C(link, DLIGHT_I2CRequest);
00169 }
00170 
00171 
00172 /**
00173  * Stop blinking the LED
00174  * @param link the dLight port number
00175  * @param addr the dLight I2C address
00176  * @return true if no error occured, false if it did
00177  */
00178 bool DLIGHTstopBlinking(tSensors link, ubyte addr){
00179   DLIGHT_I2CRequest[0] = 3;
00180   DLIGHT_I2CRequest[1] = addr;
00181   DLIGHT_I2CRequest[2] = DLIGHT_REG_LEDOUT;
00182   DLIGHT_I2CRequest[3] = DLIGHT_CMD_DISABLE_BLINK;
00183   return writeI2C(link, DLIGHT_I2CRequest);
00184 }
00185 
00186 
00187 /**
00188  * Turn off the LED
00189  * @param link the dLight port number
00190  * @param addr the dLight I2C address
00191  * @return true if no error occured, false if it did
00192  */
00193 bool DLIGHTdisable(tSensors link, ubyte addr)
00194 {
00195   DLIGHT_I2CRequest[0] = 3;
00196   DLIGHT_I2CRequest[1] = addr;
00197   DLIGHT_I2CRequest[2] = DLIGHT_REG_LEDOUT;
00198   DLIGHT_I2CRequest[3] = DLIGHT_CMD_DISABLE_LEDS;
00199   return writeI2C(link, DLIGHT_I2CRequest);
00200 }
00201 
00202 #endif // __DLIGHT_H__
00203 
00204 /*
00205  * $Id: dexterind-dlight.h 133 2013-03-10 15:15:38Z xander $
00206  */
00207 /* @} */
00208 /* @} */