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

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