00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __HTIRS2_H__
00013 #define __HTIRS2_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #pragma systemFile
00045
00046 #ifndef __COMMON_H__
00047 #include "common.h"
00048 #endif
00049
00050 #define HTIRS2_I2C_ADDR 0x10
00051 #define HTIRS2_DSP_MODE 0x41
00052 #define HTIRS2_OFFSET 0x42
00053 #define HTIRS2_DC_DIR 0x00
00054 #define HTIRS2_DC_SSTR1 0x01
00055 #define HTIRS2_DC_SSTR2 0x02
00056 #define HTIRS2_DC_SSTR3 0x03
00057 #define HTIRS2_DC_SSTR4 0x04
00058 #define HTIRS2_DC_SSTR5 0x05
00059 #define HTIRS2_DC_SAVG 0x06
00060 #define HTIRS2_AC_DIR 0x07
00061 #define HTIRS2_AC_SSTR1 0x08
00062 #define HTIRS2_AC_SSTR2 0x09
00063 #define HTIRS2_AC_SSTR3 0x0A
00064 #define HTIRS2_AC_SSTR4 0x0B
00065 #define HTIRS2_AC_SSTR5 0x0C
00066
00067
00068
00069 typedef enum {
00070 DSP_1200 = 0,
00071 DSP_600 = 1
00072 } tHTIRS2DSPMode;
00073
00074
00075 int HTIRS2readDCDir(tSensors link);
00076 bool HTIRS2readAllDCStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00077 int HTIRS2readDCAverage(tSensors link);
00078
00079 bool HTIRS2setDSPMode(tSensors link, tHTIRS2DSPMode mode);
00080 int HTIRS2readACDir(tSensors link);
00081 bool HTIRS2readAllACStrength(tSensors link, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5);
00082
00083 bool HTIRS2readEnhanced(tSensors link, int &dir, int &strength);
00084
00085 #ifdef __HTSMUX_SUPPORT__
00086
00087 int HTIRS2readDCDir(tMUXSensor muxsensor);
00088 bool HTIRS2readAllDCStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
00089 int HTIRS2readDCAverage(tMUXSensor muxsensor);
00090
00091 int HTIRS2readACDir(tMUXSensor muxsensor);
00092 bool HTIRS2readAllACStrength(tMUXSensor muxsensor, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5);
00093
00094 bool HTIRS2readEnhanced(tMUXSensor muxsensor, int &dir, int &strength);
00095
00096 tConfigParams HTIRS2_config = {HTSMUX_CHAN_I2C, 13, 0x10, 0x42};
00097 #endif // __HTSMUX_SUPPORT__
00098
00099 tByteArray HTIRS2_I2CRequest;
00100 tByteArray HTIRS2_I2CReply;
00101
00102
00103
00104
00105
00106
00107
00108
00109 int HTIRS2readDCDir(tSensors link) {
00110 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00111
00112 HTIRS2_I2CRequest[0] = 2;
00113 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00114 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_DIR;
00115
00116 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 1))
00117 return -1;
00118
00119 return HTIRS2_I2CReply[0];
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 #ifdef __HTSMUX_SUPPORT__
00129 int HTIRS2readDCDir(tMUXSensor muxsensor) {
00130 memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00131
00132 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00133 HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00134
00135 if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_DC_DIR)) {
00136 return -1;
00137 }
00138
00139 return HTIRS2_I2CReply[0];
00140 }
00141 #endif // __HTSMUX_SUPPORT__
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 bool HTIRS2readAllDCStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00155 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00156
00157 HTIRS2_I2CRequest[0] = 2;
00158 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00159 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SSTR1;
00160
00161 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 5))
00162 return false;
00163
00164
00165
00166
00167 dcS1 = HTIRS2_I2CReply[0];
00168 dcS2 = HTIRS2_I2CReply[1];
00169 dcS3 = HTIRS2_I2CReply[2];
00170 dcS4 = HTIRS2_I2CReply[3];
00171 dcS5 = HTIRS2_I2CReply[4];
00172
00173 return true;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 #ifdef __HTSMUX_SUPPORT__
00188 bool HTIRS2readAllDCStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
00189 memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00190
00191 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00192 HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00193
00194 if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 5, HTIRS2_DC_SSTR1)) {
00195 return false;
00196 }
00197
00198 dcS1 = HTIRS2_I2CReply[0];
00199 dcS2 = HTIRS2_I2CReply[1];
00200 dcS3 = HTIRS2_I2CReply[2];
00201 dcS4 = HTIRS2_I2CReply[3];
00202 dcS5 = HTIRS2_I2CReply[4];
00203
00204 return true;
00205 }
00206 #endif // __HTSMUX_SUPPORT__
00207
00208
00209
00210
00211
00212
00213
00214 int HTIRS2readDCAverage(tSensors link) {
00215 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00216
00217 HTIRS2_I2CRequest[0] = 2;
00218 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00219 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SAVG;
00220
00221 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 1))
00222 return -1;
00223
00224 return HTIRS2_I2CReply[0];
00225 }
00226
00227
00228
00229
00230
00231
00232
00233 #ifdef __HTSMUX_SUPPORT__
00234 int HTIRS2readDCAverage(tMUXSensor muxsensor) {
00235 memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00236
00237 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00238 HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00239
00240 if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_DC_SAVG)) {
00241 return -1;
00242 }
00243
00244 return HTIRS2_I2CReply[0];
00245 }
00246 #endif // __HTSMUX_SUPPORT__
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 bool HTIRS2setDSPMode(tSensors link, tHTIRS2DSPMode mode) {
00262 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00263
00264 HTIRS2_I2CRequest[0] = 3;
00265 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00266 HTIRS2_I2CRequest[2] = HTIRS2_DSP_MODE;
00267 HTIRS2_I2CRequest[3] = (ubyte)mode;
00268
00269 return writeI2C(link, HTIRS2_I2CRequest);
00270 }
00271
00272
00273
00274
00275
00276
00277 int HTIRS2readACDir(tSensors link) {
00278 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00279
00280 HTIRS2_I2CRequest[0] = 2;
00281 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00282 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_DIR;
00283
00284 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 1))
00285 return -1;
00286
00287 return HTIRS2_I2CReply[0];
00288 }
00289
00290
00291
00292
00293
00294
00295
00296 #ifdef __HTSMUX_SUPPORT__
00297 int HTIRS2readACDir(tMUXSensor muxsensor) {
00298 memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00299
00300 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00301 HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00302
00303 if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_AC_DIR)) {
00304 return -1;
00305 }
00306
00307 return HTIRS2_I2CReply[0];
00308 }
00309 #endif // __HTSMUX_SUPPORT__
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 bool HTIRS2readAllACStrength(tSensors link, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5) {
00323 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00324
00325 HTIRS2_I2CRequest[0] = 2;
00326 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00327 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_SSTR1;
00328
00329 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 5))
00330 return false;
00331
00332 acS1 = HTIRS2_I2CReply[0];
00333 acS2 = HTIRS2_I2CReply[1];
00334 acS3 = HTIRS2_I2CReply[2];
00335 acS4 = HTIRS2_I2CReply[3];
00336 acS5 = HTIRS2_I2CReply[4];
00337
00338 return true;
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 #ifdef __HTSMUX_SUPPORT__
00353 bool HTIRS2readAllACStrength(tMUXSensor muxsensor, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5) {
00354 memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00355
00356 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00357 HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00358
00359 if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 5, HTIRS2_AC_SSTR1)) {
00360 return false;
00361 }
00362
00363 acS1 = HTIRS2_I2CReply[0];
00364 acS2 = HTIRS2_I2CReply[1];
00365 acS3 = HTIRS2_I2CReply[2];
00366 acS4 = HTIRS2_I2CReply[3];
00367 acS5 = HTIRS2_I2CReply[4];
00368
00369 return true;
00370 }
00371 #endif // __HTSMUX_SUPPORT__
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 bool HTIRS2readEnhanced(tSensors link, int &dir, int &strength)
00383 {
00384 ubyte iMax = 0;
00385 long dcSigSum = 0;
00386
00387
00388 memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
00389
00390 HTIRS2_I2CRequest[0] = 2;
00391 HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR;
00392 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SSTR1;
00393
00394 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 6))
00395 return false;
00396
00397
00398 for (int i = 1; i < 5; i++)
00399 {
00400 if (HTIRS2_I2CReply[i] > HTIRS2_I2CReply[iMax])
00401 {
00402 iMax = i;
00403 }
00404 }
00405
00406
00407 dir = iMax * 2 + 1;
00408
00409 dcSigSum = HTIRS2_I2CReply[iMax] + HTIRS2_I2CReply[5];
00410
00411
00412 if ((iMax > 0) && (HTIRS2_I2CReply[iMax - 1] > (ubyte)(HTIRS2_I2CReply[iMax] / 2)))
00413 {
00414 dir--;
00415 dcSigSum += HTIRS2_I2CReply[iMax - 1];
00416 }
00417
00418 if ((iMax < 4) && (HTIRS2_I2CReply[iMax + 1] > (ubyte)(HTIRS2_I2CReply[iMax] / 2)))
00419 {
00420 dir++;
00421 dcSigSum += HTIRS2_I2CReply[iMax + 1];
00422 }
00423
00424
00425
00426 strength = sqrt(dcSigSum * 500);
00427
00428
00429 if (strength <= 200)
00430 {
00431 writeDebugStreamLine("switching to AC");
00432
00433 HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_DIR;
00434
00435 if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 6))
00436 return false;
00437
00438 dir = HTIRS2_I2CReply[0];
00439
00440
00441 if (dir > 0)
00442 {
00443 strength = HTIRS2_I2CReply[1] + HTIRS2_I2CReply[2] +
00444 HTIRS2_I2CReply[3] + HTIRS2_I2CReply[4] +
00445 HTIRS2_I2CReply[5];
00446 }
00447 }
00448 return true;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 #ifdef __HTSMUX_SUPPORT__
00460 bool HTIRS2readEnhanced(tMUXSensor muxsensor, int &dir, int &strength)
00461 {
00462 ubyte iMax = 0;
00463 long dcSigSum = 0;
00464
00465 memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
00466
00467 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00468 HTSMUXconfigChannel(muxsensor, HTIRS2_config);
00469
00470 if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 13, HTIRS2_DC_DIR)) {
00471 return false;
00472 }
00473
00474
00475 for (int i = 1; i < 5; i++)
00476 {
00477 if (HTIRS2_I2CReply[HTIRS2_DC_SSTR1+i] > HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax])
00478 {
00479 iMax = i;
00480 }
00481 }
00482
00483
00484 dir = iMax * 2 + 1;
00485
00486 dcSigSum = HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax] + HTIRS2_I2CReply[HTIRS2_DC_SSTR1+5];
00487
00488
00489 if ((iMax > 0) && (HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax - 1] > (ubyte)(HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax] / 2)))
00490 {
00491 dir--;
00492 dcSigSum += HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax - 1];
00493 }
00494
00495 if ((iMax < 4) && (HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax + 1] > (ubyte)(HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax] / 2)))
00496 {
00497 dir++;
00498 dcSigSum += HTIRS2_I2CReply[HTIRS2_DC_SSTR1+iMax + 1];
00499 }
00500
00501
00502
00503 strength = sqrt(dcSigSum * 500);
00504
00505
00506 if (strength <= 200)
00507 {
00508 writeDebugStreamLine("switching to AC");
00509
00510 dir = HTIRS2_I2CReply[HTIRS2_AC_DIR];
00511
00512
00513 if (dir > 0)
00514 {
00515 strength = HTIRS2_I2CReply[HTIRS2_AC_SSTR1] + HTIRS2_I2CReply[HTIRS2_AC_SSTR2] +
00516 HTIRS2_I2CReply[HTIRS2_AC_SSTR3] + HTIRS2_I2CReply[HTIRS2_AC_SSTR4] +
00517 HTIRS2_I2CReply[HTIRS2_AC_SSTR5];
00518 }
00519 }
00520 return true;
00521 }
00522 #endif // __HTSMUX_SUPPORT__
00523
00524 #endif // __HTIRS2_H__
00525
00526
00527
00528
00529
00530