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

mindsensors-numericpad.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup MSNP Numeric Keypad Sensor
00004  * Numeric Keypad Numeric Keypad Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-numericpad.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSNP_H__
00013 #define __MSNP_H__
00014 /** \file mindsensors-numericpad.h
00015  * \brief Mindsensors Numeric Keypad Sensor driver
00016  *
00017  * mindsensors-numericpad.h provides an API for the Mindsensors Numeric Keypad Sensor.\n
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Mindsensors 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  * \author Xander Soldaat (xander_at_botbench.com)
00030  * \date 30 October 2010
00031  * \version 0.1
00032  * \example mindsensors-numericpad-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define MSNP_I2C_ADDR  0xB4     /*!< Numeric Pad I2C device address */
00042 #define MSNP_DATA_REG  0x00     /*!< Data registers start at 0x00 */
00043 
00044 tByteArray MSNP_I2CRequest;     /*!< Array to hold I2C command data */
00045 tByteArray MSNP_I2CReply;       /*!< Array to hold I2C reply data */
00046 
00047 
00048 #define KEY_STATUS_REG 0x00
00049 
00050 const unsigned byte MSNP_KeyMap[] = { '#', '9', '6', '3', '0', '8', '2', '5', '*', '7', '1', '4' };
00051 const signed byte MSNP_NumMap[] = { -1, 9, 6, 3, 0, 8, 2, 5, -2, 7, 1, 4};
00052 
00053 const unsigned byte MSNP_ConfigGroup1[] = {10, MSNP_I2C_ADDR, 0x2B, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0xFF, 0x02};
00054 const unsigned byte MSNP_ConfigGroup2[] = {16, MSNP_I2C_ADDR, 0x41, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A};
00055 const unsigned byte MSNP_ConfigGroup3[] = {14, MSNP_I2C_ADDR, 0x4F, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A};
00056 const unsigned byte MSNP_ConfigGroup4[] = { 5, MSNP_I2C_ADDR, 0x5C, 0x0B, 0x20, 0x0C};
00057 const unsigned byte MSNP_ConfigGroup5[] = { 3, MSNP_I2C_ADDR, 0x7B, 0x0B};
00058 const unsigned byte MSNP_ConfigGroup6[] = { 5, MSNP_I2C_ADDR, 0x7D, 0x9C, 0x65, 0x8C};
00059 
00060 bool _MSNPinitialised[] = {false, false, false, false};
00061 
00062 
00063 /**
00064  * Initialise the numeric pad sensor chip.  This must be done at the beginning
00065  * of every power cycle\n
00066  * Note: this is an internal function and should not be called directly.
00067  * @param link the MSNP port number
00068  * @return true if no error occured, false if it did
00069  */
00070 bool _MSNPinit(tSensors link) {
00071 // Must be called at the beginning of every power cycle
00072 
00073   memcpy(MSNP_I2CRequest, MSNP_ConfigGroup1, sizeof(MSNP_ConfigGroup1));
00074   if (!writeI2C(link, MSNP_I2CRequest))
00075     return false;
00076 
00077   memcpy(MSNP_I2CRequest, MSNP_ConfigGroup2, sizeof(MSNP_ConfigGroup2));
00078   if (!writeI2C(link, MSNP_I2CRequest))
00079     return false;
00080 
00081   memcpy(MSNP_I2CRequest, MSNP_ConfigGroup3, sizeof(MSNP_ConfigGroup3));
00082   if (!writeI2C(link, MSNP_I2CRequest))
00083     return false;
00084 
00085   memcpy(MSNP_I2CRequest, MSNP_ConfigGroup4, sizeof(MSNP_ConfigGroup4));
00086   if (!writeI2C(link, MSNP_I2CRequest))
00087     return false;
00088 
00089   memcpy(MSNP_I2CRequest, MSNP_ConfigGroup5, sizeof(MSNP_ConfigGroup5));
00090   if (!writeI2C(link, MSNP_I2CRequest))
00091     return false;
00092 
00093   memcpy(MSNP_I2CRequest, MSNP_ConfigGroup6, sizeof(MSNP_ConfigGroup6));
00094   if (!writeI2C(link, MSNP_I2CRequest))
00095     return false;
00096 
00097   return true;
00098 }
00099 
00100 
00101 /**
00102  * Scan the keypad for pressed keys.  This returns immediately.
00103  * @param link the MSNP port number
00104  * @param pressedKeys a binary representation of all keys pressed
00105  * @param key character representation of the first key pressed, X if no key was pressed
00106  * @param number the numeric equivalent, 0-9 for digits, -1 for # and -2 for *, -255 when nothing is pressed
00107  * @return true if no error occured, false if it did
00108  */
00109 bool MSNPscanKeys(tSensors link, int &pressedKeys, unsigned byte &key, int &number) {
00110   //int keyPress = 0;
00111 
00112   if (!_MSNPinitialised[link]) {
00113     _MSNPinit(link);
00114     _MSNPinitialised[link] = true;
00115     wait1Msec(10);
00116   }
00117 
00118   key = 'X';
00119   number = -255;
00120 
00121   MSNP_I2CRequest[0] = 2;
00122   MSNP_I2CRequest[1] = MSNP_I2C_ADDR;
00123   MSNP_I2CRequest[2] = MSNP_DATA_REG;
00124 
00125         if (!writeI2C(link, MSNP_I2CRequest, MSNP_I2CReply, 2))
00126           return false;
00127 
00128   pressedKeys = MSNP_I2CReply[0] + (MSNP_I2CReply[1] * 256);
00129 
00130         for (int i=0; i < 12; i++) {
00131           if (pressedKeys & (1<<i)) {
00132             key = MSNP_KeyMap[i];
00133             number = MSNP_NumMap[i];
00134             return true;
00135           }
00136         }
00137 
00138         return true;
00139 }
00140 
00141 
00142 /**
00143  * Scan the keypad for pressed keys.  This returns immediately.
00144  * @param link the MSNP port number
00145  * @return number the numeric equivalent, 0-9 for digits, -1 for # and -2 for *, -255 when nothing is pressed
00146  */
00147 int MSNPscanKeys(tSensors link) {
00148   int keyPress = 0;
00149 
00150   if (!_MSNPinitialised[link]) {
00151     _MSNPinit(link);
00152     _MSNPinitialised[link] = true;
00153     wait1Msec(10);
00154   }
00155 
00156 
00157   MSNP_I2CRequest[0] = 2;
00158   MSNP_I2CRequest[1] = MSNP_I2C_ADDR;
00159   MSNP_I2CRequest[2] = MSNP_DATA_REG;
00160 
00161         if (!writeI2C(link, MSNP_I2CRequest, MSNP_I2CReply, 2))
00162           return -255;
00163 
00164   keyPress = MSNP_I2CReply[0] + (MSNP_I2CReply[1] * 256);
00165 
00166         for (int i=0; i < 12; i++) {
00167           if (keyPress & (1<<i)) {
00168             return MSNP_NumMap[i];
00169           }
00170         }
00171 
00172         return -255;
00173 }
00174 
00175 
00176 #endif // __MSNP_H__
00177 
00178 /*
00179  * $Id: mindsensors-numericpad.h 133 2013-03-10 15:15:38Z xander $
00180  */
00181 /* @} */
00182 /* @} */