00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __HTSMUX_H__
00013 #define __HTSMUX_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #pragma systemFile
00033
00034 #ifndef __COMMON_H__
00035 #include "common.h"
00036 #endif
00037
00038 #define __HTSMUX_SUPPORT__
00039
00040 #ifndef SPORT
00041 #define SPORT(X) (X / 4)
00042 #endif
00043
00044 #ifndef MPORT
00045 #define MPORT(X) (X % 4)
00046 #endif
00047
00048 #ifndef MAX_ARR_SIZE
00049
00050
00051
00052
00053
00054 #define MAX_ARR_SIZE 17
00055 #endif
00056
00057 #define HTSMUX_I2C_ADDR 0x10
00058 #define HTSMUX_COMMAND 0x20
00059 #define HTSMUX_STATUS 0x21
00060
00061
00062 #define HTSMUX_MODE 0x00
00063 #define HTSMUX_TYPE 0x01
00064 #define HTSMUX_I2C_COUNT 0x02
00065 #define HTSMUX_I2C_DADDR 0x03
00066 #define HTSMUX_I2C_MADDR 0x04
00067 #define HTSMUX_CH_OFFSET 0x22
00068 #define HTSMUX_CH_ENTRY_SIZE 0x05
00069
00070 #define HTSMUX_ANALOG 0x36
00071 #define HTSMUX_AN_ENTRY_SIZE 0x02
00072
00073 #define HTSMUX_I2C_BUF 0x40
00074 #define HTSMUX_BF_ENTRY_SIZE 0x10
00075
00076
00077
00078 #define HTSMUX_CMD_HALT 0x00
00079 #define HTSMUX_CMD_AUTODETECT 0x01
00080 #define HTSMUX_CMD_RUN 0x02
00081
00082
00083 #define HTSMUX_STAT_NORMAL 0x00
00084 #define HTSMUX_STAT_BATT 0x01
00085 #define HTSMUX_STAT_BUSY 0x02
00086 #define HTSMUX_STAT_HALT 0x04
00087 #define HTSMUX_STAT_ERROR 0x08
00088 #define HTSMUX_STAT_NOTHING 0xFF
00089
00090
00091 #define HTSMUX_CHAN_NONE 0x00
00092 #define HTSMUX_CHAN_I2C 0x01
00093 #define HTSMUX_CHAN_9V 0x02
00094 #define HTSMUX_CHAN_DIG0_HIGH 0x04
00095 #define HTSMUX_CHAN_DIG1_HIGH 0x08
00096 #define HTSMUX_CHAN_I2C_SLOW 0x10
00097
00098
00099
00100 typedef enum {
00101 HTSMUXAnalogue = 0x00,
00102 HTSMUXLegoUS = 0x01,
00103 HTSMUXCompass = 0x02,
00104 HTSMUXColor = 0x03,
00105 HTSMUXAccel = 0x04,
00106 HTSMUXIRSeeker = 0x05,
00107 HTSMUXProto = 0x06,
00108 HTSMUXColorNew = 0x07,
00109 HTSMUXAngle = 0x08,
00110 HTSMUXIRSeekerNew = 0x09,
00111 HTSMUXSensorCustom = 0x0E,
00112 HTSMUXSensorNone = 0x0F
00113 } HTSMUXSensorType;
00114
00115
00116 typedef enum {
00117 msensor_S1_1 = 0,
00118 msensor_S1_2 = 1,
00119 msensor_S1_3 = 2,
00120 msensor_S1_4 = 3,
00121 msensor_S2_1 = 4,
00122 msensor_S2_2 = 5,
00123 msensor_S2_3 = 6,
00124 msensor_S2_4 = 7,
00125 msensor_S3_1 = 8,
00126 msensor_S3_2 = 9,
00127 msensor_S3_3 = 10,
00128 msensor_S3_4 = 11,
00129 msensor_S4_1 = 12,
00130 msensor_S4_2 = 13,
00131 msensor_S4_3 = 14,
00132 msensor_S4_4 = 15
00133 } tMUXSensor;
00134
00135
00136
00137 ubyte HTSMUXstatus[4] = {HTSMUX_STAT_NOTHING, HTSMUX_STAT_NOTHING, HTSMUX_STAT_NOTHING, HTSMUX_STAT_NOTHING};
00138
00139
00140 HTSMUXSensorType HTSMUXSensorTypes[16] = {HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone,
00141 HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone,
00142 HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone,
00143 HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone, HTSMUXSensorNone};
00144
00145 tByteArray HTSMUX_I2CRequest;
00146 tByteArray HTSMUX_I2CReply;
00147
00148 typedef ubyte tConfigParams[4];
00149
00150 tConfigParams Analogue_config = {HTSMUX_CHAN_NONE, 0, 0, 0};
00151
00152 byte HTSMUXreadStatus(tSensors link);
00153 HTSMUXSensorType HTSMUXreadSensorType(tMUXSensor muxsensor);
00154 bool HTSMUXsendCommand(tSensors link, byte command);
00155 bool HTSMUXreadPort(tMUXSensor muxsensor, tByteArray &result, int numbytes, int offset = 0);
00156 bool HTSMUXsetMode(tMUXSensor muxsensor, byte mode);
00157 bool HTSMUXsetAnalogueActive(tMUXSensor muxsensor);
00158 bool HTSMUXsetAnalogueInactive(tMUXSensor muxsensor);
00159 int HTSMUXreadAnalogue(tMUXSensor muxsensor);
00160 bool HTSMUXreadPowerStatus(tSensors link);
00161 bool HTSMUXconfigChannel(tMUXSensor muxsensor, tConfigParams &configparams);
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 byte HTSMUXreadStatus(tSensors link) {
00178 memset(HTSMUX_I2CRequest, 0, sizeof(tByteArray));
00179
00180 HTSMUX_I2CRequest[0] = 2;
00181 HTSMUX_I2CRequest[1] = HTSMUX_I2C_ADDR;
00182 HTSMUX_I2CRequest[2] = HTSMUX_STATUS;
00183
00184 if (!writeI2C(link, HTSMUX_I2CRequest, HTSMUX_I2CReply, 1))
00185 return -1;
00186
00187 return HTSMUX_I2CReply[0];
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 HTSMUXSensorType HTSMUXreadSensorType(tMUXSensor muxsensor) {
00197 return HTSMUXSensorTypes[muxsensor];
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 bool HTSMUXsetMode(tMUXSensor muxsensor, byte mode) {
00215 tSensors link = (tSensors)SPORT(muxsensor);
00216 byte channel = MPORT(muxsensor);
00217
00218
00219 if (HTSMUXstatus[link] == HTSMUX_STAT_BUSY) {
00220 return false;
00221 } else if (HTSMUXstatus[link] != HTSMUX_STAT_HALT) {
00222
00223 if (!HTSMUXsendCommand(link, HTSMUX_CMD_HALT))
00224 return false;
00225 wait1Msec(50);
00226 }
00227
00228 memset(HTSMUX_I2CRequest, 0, sizeof(tByteArray));
00229
00230 HTSMUX_I2CRequest[0] = 3;
00231 HTSMUX_I2CRequest[1] = HTSMUX_I2C_ADDR;
00232 HTSMUX_I2CRequest[2] = HTSMUX_CH_OFFSET + HTSMUX_MODE + (HTSMUX_CH_ENTRY_SIZE * channel);
00233 HTSMUX_I2CRequest[3] = mode;
00234
00235 return writeI2C(link, HTSMUX_I2CRequest);
00236 }
00237
00238
00239
00240
00241
00242
00243
00244 bool HTSMUXsetAnalogueActive(tMUXSensor muxsensor) {
00245 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00246 HTSMUXconfigChannel(muxsensor, Analogue_config);
00247
00248 if (!HTSMUXsetMode(muxsensor, HTSMUX_CHAN_DIG0_HIGH))
00249 return false;
00250
00251 return HTSMUXsendCommand((tSensors)SPORT(muxsensor), HTSMUX_CMD_RUN);
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 bool HTSMUXsetAnalogueInactive(tMUXSensor muxsensor) {
00261 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00262 HTSMUXconfigChannel(muxsensor, Analogue_config);
00263
00264 if (!HTSMUXsetMode(muxsensor, 0))
00265 return false;
00266
00267 return HTSMUXsendCommand((tSensors)SPORT(muxsensor), HTSMUX_CMD_RUN);
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 bool HTSMUXsendCommand(tSensors link, byte command) {
00285 memset(HTSMUX_I2CRequest, 0, sizeof(tByteArray));
00286
00287 HTSMUX_I2CRequest[0] = 3;
00288 HTSMUX_I2CRequest[1] = HTSMUX_I2C_ADDR;
00289 HTSMUX_I2CRequest[2] = HTSMUX_COMMAND;
00290 HTSMUX_I2CRequest[3] = command;
00291
00292 switch(command) {
00293 case HTSMUX_CMD_HALT:
00294 HTSMUXstatus[link] = HTSMUX_STAT_HALT;
00295 break;
00296 case HTSMUX_CMD_AUTODETECT:
00297 HTSMUXstatus[link] = HTSMUX_STAT_BUSY;
00298 break;
00299 case HTSMUX_CMD_RUN:
00300 HTSMUXstatus[link] = HTSMUX_STAT_NORMAL;
00301 break;
00302 }
00303
00304 return writeI2C(link, HTSMUX_I2CRequest);
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 bool HTSMUXreadPort(tMUXSensor muxsensor, tByteArray &result, int numbytes, int offset) {
00318 tSensors link = (tSensors)SPORT(muxsensor);
00319 byte channel = MPORT(muxsensor);
00320
00321 memset(HTSMUX_I2CRequest, 0, sizeof(tByteArray));
00322 if (HTSMUXstatus[link] != HTSMUX_STAT_NORMAL)
00323 HTSMUXsendCommand(link, HTSMUX_CMD_RUN);
00324
00325 HTSMUX_I2CRequest[0] = 2;
00326 HTSMUX_I2CRequest[1] = HTSMUX_I2C_ADDR;
00327 HTSMUX_I2CRequest[2] = HTSMUX_I2C_BUF + (HTSMUX_BF_ENTRY_SIZE * channel) + offset;
00328
00329 if (!writeI2C(link, HTSMUX_I2CRequest, HTSMUX_I2CReply, numbytes))
00330 return false;
00331
00332 memcpy(result, HTSMUX_I2CReply, sizeof(tByteArray));
00333
00334 return true;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344 int HTSMUXreadAnalogue(tMUXSensor muxsensor) {
00345 tSensors link = (tSensors)SPORT(muxsensor);
00346 byte channel = MPORT(muxsensor);
00347
00348 memset(HTSMUX_I2CRequest, 0, sizeof(tByteArray));
00349 if (HTSMUXstatus[link] != HTSMUX_STAT_NORMAL)
00350 HTSMUXsendCommand(link, HTSMUX_CMD_RUN);
00351
00352 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00353 HTSMUXconfigChannel(muxsensor, Analogue_config);
00354
00355 HTSMUX_I2CRequest[0] = 2;
00356 HTSMUX_I2CRequest[1] = HTSMUX_I2C_ADDR;
00357 HTSMUX_I2CRequest[2] = HTSMUX_ANALOG + (HTSMUX_AN_ENTRY_SIZE * channel);
00358
00359 if (!writeI2C(link, HTSMUX_I2CRequest, HTSMUX_I2CReply, 2))
00360 return -1;
00361
00362 return ((int)HTSMUX_I2CReply[0] * 4) + HTSMUX_I2CReply[1];
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372 void HTSMUXsensorTypeToString(HTSMUXSensorType muxsensor, string &sensorName) {
00373 switch(muxsensor) {
00374 case HTSMUXAnalogue: sensorName = "Analogue"; break;
00375 case HTSMUXLegoUS: sensorName = "Ultra Sonic"; break;
00376 case HTSMUXCompass: sensorName = "Compass"; break;
00377 case HTSMUXColor: sensorName = "Colour"; break;
00378 case HTSMUXColorNew: sensorName = "Colour New"; break;
00379 case HTSMUXAccel: sensorName = "Accel"; break;
00380 case HTSMUXIRSeeker: sensorName = "IR Seeker"; break;
00381 case HTSMUXProto: sensorName = "Proto Board"; break;
00382 case HTSMUXIRSeekerNew: sensorName = "IR Seeker V2"; break;
00383 case HTSMUXSensorNone : sensorName = "No sensor"; break;
00384 }
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394 bool HTSMUXreadPowerStatus(tSensors link) {
00395 if ((HTSMUXreadStatus(link) & HTSMUX_STAT_BATT) == HTSMUX_STAT_BATT)
00396 return true;
00397 else
00398 return false;
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 bool HTSMUXconfigChannel(tMUXSensor muxsensor, tConfigParams &configparams) {
00415 memset(HTSMUX_I2CRequest, 0, sizeof(tByteArray));
00416
00417
00418 if (!HTSMUXsendCommand((tSensors)SPORT(muxsensor), HTSMUX_CMD_HALT))
00419 return false;
00420 wait1Msec(50);
00421
00422 HTSMUX_I2CRequest[0] = 7;
00423 HTSMUX_I2CRequest[1] = HTSMUX_I2C_ADDR;
00424 HTSMUX_I2CRequest[2] = HTSMUX_CH_OFFSET + (HTSMUX_CH_ENTRY_SIZE * MPORT(muxsensor));
00425 HTSMUX_I2CRequest[3] = configparams[0];
00426 HTSMUX_I2CRequest[4] = 0x00;
00427 HTSMUX_I2CRequest[5] = configparams[1];
00428 HTSMUX_I2CRequest[6] = configparams[2];
00429 HTSMUX_I2CRequest[7] = configparams[3];
00430
00431 if (!writeI2C((tSensors)SPORT(muxsensor), HTSMUX_I2CRequest))
00432 return false;
00433
00434 HTSMUXSensorTypes[muxsensor] = HTSMUXSensorCustom;
00435 return HTSMUXsendCommand((tSensors)SPORT(muxsensor), HTSMUX_CMD_RUN);
00436 }
00437
00438
00439 #endif // __HTSMUX_H__
00440
00441
00442
00443
00444