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

mindsensors-magicwand.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup Magic_Wand Magic Wand
00004  * Magic Wand
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-magicwand.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSMW_H__
00013 #define __MSMW_H__
00014 /** \file mindsensors-magicwand.h
00015  * \brief Mindsensors Magic Wand
00016  *
00017  * mindsensors-magicwand.h provides additional functionality on top of the PCF8574 driver for
00018  * the Mindsensors Magic Wand
00019  *
00020  * Changelog:
00021  * - 0.1: Initial release
00022  *
00023  * Credits:
00024  * - Originally written by Mike Partain a.k.a. Spiked3 (http://www.spiked3.com/)
00025  * - Modified by Xander Soldaat (xander_at_botbench.com)
00026  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00027  *
00028  * License: You may use this code as you wish, provided you give credit where its due.
00029  *
00030  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00031 
00032  * \author Mike Partain a.k.a. Spiked3 (http://www.spiked3.com/)
00033  * \author Xander Soldaat (xander_at_botbench.com)
00034  * \date 29 July 2012
00035  * \version 0.1
00036  * \example mindsensors-magicwand-test1.c
00037  */
00038 
00039 #ifndef __PCF8574_H__
00040 #include "philips-pcf8574.h"
00041 #endif
00042 
00043 #define MSMWclearALL(X)   PCF8574sendBytes(X, 0xFF)
00044 #define MSMWsetALL(X)     PCF8574sendBytes(X, 0x00)
00045 
00046 /**
00047  * Turn a specific LED on or off.
00048  * @param link the Magic Wand port
00049  * @param ledToChange the index of the LED to change (0-7)
00050  * @param on stateof the LED, true = on, false = off
00051  * @return true if no error occured, false if it did
00052  */
00053 bool MSMWsetLED(tSensors link, ubyte ledToChange, bool on)
00054 {
00055   ubyte ledState;
00056 
00057   if (!PCF8574readBytes(link, ledState))
00058   {
00059     return false;
00060   }
00061 
00062   return PCF8574sendBytes(link, (on ? ledState & ~(1 << ledToChange) : ledState | (1 << ledToChange)) );
00063 }
00064 
00065 
00066 /**
00067  * Toggle a LED
00068  * @param link the Magic Wand port
00069  * @param ledToChange the index of the LED to change (0-7)
00070  * @return true if no error occured, false if it did
00071  */
00072 bool MSMWtoggleLED(tSensors link, ubyte ledToChange)
00073 {
00074   ubyte ledState;
00075   if (!PCF8574readBytes(link, ledState))
00076   {
00077     return false;
00078   }
00079   return PCF8574sendBytes(link, ledState ^ (1 << ledToChange));
00080 }
00081 
00082 
00083 /**
00084  * Flash the LEDs and turn them off again.  Nice effect.
00085  * @param link the Magic Wand port
00086  * @param count number of times to flash the LEDs
00087  * @return true if no error occured, false if it did
00088  */
00089 bool MSMWflashAndClear(tSensors link, int count)
00090 {
00091   PCF8574sendBytes(link, 0xFF);    // all off
00092   for (int i = 0; i < count; i++)
00093   {
00094     for (int j = 0; j < 8; j++) {
00095       if (!MSMWsetLED(link, j, true))
00096       {
00097         return false;
00098       }
00099       wait1Msec(30);
00100     }
00101     for (int j = 0; j < 8; j++) {
00102       if (!MSMWsetLED(link, j, false))
00103       {
00104         return false;
00105       }
00106       wait1Msec(30);
00107     }
00108     if (!PCF8574sendBytes(link, 0xFF))
00109     {
00110       return false;
00111     }
00112   }
00113   PCF8574sendBytes(link, 0xFF);
00114   return true;
00115 }
00116 
00117 #endif //  __MSMW_H__
00118 
00119 /*
00120  * $Id: mindsensors-magicwand.h 133 2013-03-10 15:15:38Z xander $
00121  */
00122 /* @} */
00123 /* @} */