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

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