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

MSHID-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup mshid HID Sensor
00004  * HID Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: MSHID-driver.h 46 2011-01-16 16:56:49Z xander $
00010  */
00011 
00012 /** \file MSHID-driver.h
00013  * \brief Mindsensors HID Sensor driver
00014  *
00015  * MSHID-driver.h provides an API for the Mindsensors HID Sensor.
00016  *
00017  * Changelog:
00018  * - 0.1: Initial release
00019  * - 0.2: Allow I2C address to be specified as an optional argument\n
00020  *        Added prototypes
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 18 December 2010
00030  * \version 0.2
00031  * \example MSHID-test1.c
00032  */
00033 
00034 #pragma systemFile
00035 
00036 #ifndef __MSHID_H__
00037 #define __MSHID_H__
00038 
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042 
00043 #define MSHID_I2C_ADDR    0x04
00044 #define MSHID_CMD         0x41
00045 #define MSHID_KEYBMOD     0x42
00046 #define MSHID_KEYBDATA    0x43
00047 
00048 #define MSHID_XMIT        0x54
00049 #define MSHID_ASCII       0x41
00050 #define MSHID_DDATA       0x44
00051 
00052 // Keyboard modifyers
00053 #define MSHID_MOD_NONE    0x00
00054 #define MSHID_MOD_LCTRL   0x01
00055 #define MSHID_MOD_LSHIFT  0x02
00056 #define MSHID_MOD_LALT    0x04
00057 #define MSHID_MOD_LGUI    0x08
00058 #define MSHID_MOD_RCTRL   0x10
00059 #define MSHID_MOD_RSHIFT  0x20
00060 #define MSHID_MOD_RALT    0x40
00061 #define MSHID_MOD_RGUI    0x80
00062 
00063 bool MSHIDsendCommand(tSensors link, byte command, ubyte address = MSHID_I2C_ADDR);
00064 bool MSHIDsendKeyboardData(tSensors link, byte modifier, byte keybdata, ubyte address = MSHID_I2C_ADDR);
00065 bool MSHIDsendString(tSensors link, string data, ubyte address = MSHID_I2C_ADDR);
00066 
00067 tByteArray MSHID_I2CRequest;       /*!< Array to hold I2C command data */
00068 
00069 
00070 /**
00071  * Send a direct command to the HID sensor
00072  * @param link the HID port number
00073  * @param command the command to be sent
00074  * @param address the I2C address to use, optional, defaults to 0x04
00075  * @return true if no error occured, false if it did
00076  */
00077 bool MSHIDsendCommand(tSensors link, byte command, ubyte address) {
00078   memset(MSHID_I2CRequest, 0, sizeof(tByteArray));
00079   MSHID_I2CRequest[0] = 3;
00080   MSHID_I2CRequest[1] = address;
00081   MSHID_I2CRequest[2] = MSHID_CMD;
00082   MSHID_I2CRequest[3] = command;
00083 
00084   return writeI2C(link, MSHID_I2CRequest, 0);
00085 }
00086 
00087 
00088 /**
00089  * Send keyboard data to the HID sensor.  Must be followed by a MSHID_XMIT
00090  * command using MSHIDsendCommand()
00091  * @param link the HID port number
00092  * @param modifier the keyboard modifier, like shift, control. Can be OR'd together.
00093  * @param keybdata the keystroke to be sent to the computer
00094  * @param address the I2C address to use, optional, defaults to 0x04
00095  * @return true if no error occured, false if it did
00096  */
00097 bool MSHIDsendKeyboardData(tSensors link, byte modifier, byte keybdata, ubyte address) {
00098   memset(MSHID_I2CRequest, 0, sizeof(tByteArray));
00099   MSHID_I2CRequest[0] = 4;
00100   MSHID_I2CRequest[1] = address;
00101   MSHID_I2CRequest[2] = MSHID_KEYBMOD;
00102   MSHID_I2CRequest[3] = modifier;
00103   MSHID_I2CRequest[4] = keybdata;
00104 
00105   return writeI2C(link, MSHID_I2CRequest, 0);
00106 }
00107 
00108 
00109 /**
00110  * Send a string to the computer.  Can be up to 19 characters long.<br>
00111  * It recognises the following escaped keys:<br>
00112  * - \n: new line
00113  * - \r: carriage return
00114  * - \t: tab
00115  * - \\: a backslash
00116  * - \": double quote
00117  * @param link the HID port number
00118  * @param data the string to be transmitted
00119  * @param address the I2C address to use, optional, defaults to 0x04
00120  * @return true if no error occured, false if it did
00121  */
00122 bool MSHIDsendString(tSensors link, string data, ubyte address) {
00123   byte buffer[19];
00124   int len = strlen(data);
00125   if (len < 20) {
00126     memcpy(buffer, data, len);
00127   } else {
00128     return false;
00129   }
00130 
00131   for (int i = 0; i < len; i++) {
00132                 if (buffer[i] == 0x5C && i < (len - 1)) {
00133                   switch (buffer[i+1]) {
00134         case 'r':
00135                                         if (!MSHIDsendKeyboardData(link, MSHID_MOD_NONE, 0x0A))
00136                                           return false;
00137                                         break;
00138         case 'n':
00139                                         if (!MSHIDsendKeyboardData(link, MSHID_MOD_NONE, 0x0D))
00140                                           return false;
00141                                         break;
00142                                 case 't':
00143                                         if (!MSHIDsendKeyboardData(link, MSHID_MOD_NONE, 0x09))
00144                                           return false;
00145                                         break;
00146                                 case 0x5C:
00147                                         if (!MSHIDsendKeyboardData(link, MSHID_MOD_NONE, 0x5C))
00148                                           return false;
00149                                         break;
00150                                 case 0x22:
00151                                         if (!MSHIDsendKeyboardData(link, MSHID_MOD_NONE, 0x22))
00152                                           return false;
00153                                         break;
00154         default:
00155                                         break;
00156                         }
00157                         i++;
00158                 } else {
00159                         if (!MSHIDsendKeyboardData(link, MSHID_MOD_NONE, buffer[i]))
00160                           return false;
00161           }
00162                 if (!MSHIDsendCommand(link, MSHID_XMIT))
00163                   return false;
00164     wait1Msec(50);
00165   }
00166   return true;
00167 }
00168 
00169 #endif // __MSHID_H__
00170 
00171 /*
00172  * $Id: MSHID-driver.h 46 2011-01-16 16:56:49Z xander $
00173  */
00174 /* @} */
00175 /* @} */