00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __HTCS2_H__
00013 #define __HTCS2_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 #pragma systemFile
00042
00043 #ifndef __COMMON_H__
00044 #include "common.h"
00045 #endif
00046
00047 #ifndef __LIGHT_COMMON_H__
00048 #include "common-light.h"
00049 #endif
00050
00051 #define HTCS2_I2C_ADDR 0x02
00052 #define HTCS2_CMD_REG 0x41
00053 #define HTCS2_OFFSET 0x42
00054
00055
00056 #define HTCS2_COLNUM_REG 0x00
00057 #define HTCS2_RED_REG 0x01
00058 #define HTCS2_GREEN_REG 0x02
00059 #define HTCS2_BLUE_REG 0x03
00060 #define HTCS2_WHITE_REG 0x04
00061 #define HTCS2_COL_INDEX_REG 0x05
00062 #define HTCS2_RED_NORM_REG 0x06
00063 #define HTCS2_GREEN_NORM_REG 0x07
00064 #define HTCS2_BLUE_NORM_REG 0x08
00065
00066
00067 #define HTCS2_RED_MSB 0x00
00068 #define HTCS2_RED_LSB 0x00
00069 #define HTCS2_GREEN_MSB 0x00
00070 #define HTCS2_GREEN_LSB 0x00
00071 #define HTCS2_BLUE_MSB 0x00
00072 #define HTCS2_BLUE_LSB 0x00
00073 #define HTCS2_WHITE_MSB 0x00
00074 #define HTCS2_WHITE_LSB 0x00
00075
00076
00077 #define HTCS2_MODE_ACTIVE 0x00
00078 #define HTCS2_MODE_PASSIVE 0x01
00079 #define HTCS2_MODE_RAW 0x03
00080 #define HTCS2_MODE_50HZ 0x35
00081 #define HTCS2_MODE_60HZ 0x36
00082
00083
00084 int HTCS2readColor(tSensors link);
00085 bool HTCS2readRGB(tSensors link, int &red, int &green, int &blue);
00086 bool HTCS2readHSV(tSensors link, float &hue, float &saturation, float &value);
00087 bool HTCS2readWhite(tSensors link, int &white);
00088 bool HTCS2readNormRGB(tSensors link, int &red, int &green, int &blue);
00089 bool HTCS2readRawRGB(tSensors link, bool passive, long &red, long &green, long &blue);
00090 bool HTCS2readRawWhite(tSensors link, bool passive, long &white);
00091 bool _HTCSsendCommand(tSensors link, byte command);
00092
00093 #ifdef __HTSMUX_SUPPORT__
00094 int HTCS2readColor(tMUXSensor muxsensor);
00095 bool HTCS2readRGB(tMUXSensor muxsensor, int &red, int &green, int &blue);
00096
00097 tConfigParams HTCS2_config = {HTSMUX_CHAN_I2C, 4, 0x02, 0x42};
00098 #endif // __HTSMUX_SUPPORT__
00099
00100 tByteArray HTCS2_I2CRequest;
00101 tByteArray HTCS2_I2CReply;
00102
00103
00104 signed byte active_mode[4] = {-1, -1, -1, -1};
00105
00106
00107
00108
00109
00110
00111
00112 int HTCS2readColor(tSensors link) {
00113 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00114
00115 if (active_mode[link] != HTCS2_MODE_ACTIVE)
00116 _HTCSsendCommand(link, HTCS2_MODE_ACTIVE);
00117
00118 HTCS2_I2CRequest[0] = 2;
00119 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00120 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_COLNUM_REG;
00121
00122 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 1))
00123 return -1;
00124
00125 return HTCS2_I2CReply[0];
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 #ifdef __HTSMUX_SUPPORT__
00135 int HTCS2readColor(tMUXSensor muxsensor) {
00136 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00137
00138 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00139 HTSMUXconfigChannel(muxsensor, HTCS2_config);
00140
00141 if (!HTSMUXreadPort(muxsensor, HTCS2_I2CReply, 1, HTCS2_COLNUM_REG)) {
00142 return -1;
00143 }
00144
00145 return HTCS2_I2CReply[0];
00146 }
00147 #endif // __HTSMUX_SUPPORT__
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 bool HTCS2readRGB(tSensors link, int &red, int &green, int &blue) {
00159 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00160
00161 if (active_mode[link] != HTCS2_MODE_ACTIVE)
00162 _HTCSsendCommand(link, HTCS2_MODE_ACTIVE);
00163
00164 HTCS2_I2CRequest[0] = 2;
00165 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00166 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_RED_REG;
00167
00168 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 3))
00169 return false;
00170
00171 red = HTCS2_I2CReply[0];
00172 green = HTCS2_I2CReply[1];
00173 blue = HTCS2_I2CReply[2];
00174
00175 return true;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 #ifdef __HTSMUX_SUPPORT__
00188 bool HTCS2readRGB(tMUXSensor muxsensor, int &red, int &green, int &blue) {
00189 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00190
00191 if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
00192 HTSMUXconfigChannel(muxsensor, HTCS2_config);
00193
00194 if (!HTSMUXreadPort(muxsensor, HTCS2_I2CReply, 3, HTCS2_RED_REG)) {
00195 return false;
00196 }
00197
00198 red = HTCS2_I2CReply[0];
00199 green = HTCS2_I2CReply[1];
00200 blue = HTCS2_I2CReply[2];
00201
00202 return true;
00203 }
00204 #endif // __HTSMUX_SUPPORT__
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 bool HTCS2readHSV(tSensors link, float &hue, float &saturation, float &value) {
00217 int red,green,blue;
00218
00219 bool ret = HTCS2readRGB(link, red, green, blue);
00220 RGBtoHSV(red,green,blue, hue, saturation, value);
00221
00222 return ret;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 #ifdef __HTSMUX_SUPPORT__
00235 bool HTCS2readHSV(tMUXSensor muxsensor, float &hue, float &saturation, float &value) {
00236 int red,green,blue;
00237
00238 bool ret = HTCS2readRGB(muxsensor, red, green, blue);
00239 RGBtoHSV(red,green,blue, hue, saturation, value);
00240
00241 return ret;
00242 }
00243 #endif // __HTSMUX_SUPPORT__
00244
00245
00246
00247
00248
00249
00250
00251
00252 bool HTCS2readWhite(tSensors link, int &white) {
00253 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00254
00255 if (active_mode[link] != HTCS2_MODE_ACTIVE)
00256 _HTCSsendCommand(link, HTCS2_MODE_ACTIVE);
00257
00258 HTCS2_I2CRequest[0] = 2;
00259 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00260 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_RED_REG;
00261
00262 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 1))
00263 return false;
00264
00265 white = HTCS2_I2CReply[0];
00266
00267 return true;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 bool HTCS2readNormRGB(tSensors link, int &red, int &green, int &blue) {
00282 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00283
00284 HTCS2_I2CRequest[0] = 2;
00285 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00286 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_RED_NORM_REG;
00287
00288 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 3))
00289 return false;
00290
00291 red = HTCS2_I2CReply[0];
00292 green = HTCS2_I2CReply[1];
00293 blue = HTCS2_I2CReply[2];
00294
00295 return true;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 bool HTCS2readRawRGB(tSensors link, bool passive, long &red, long &green, long &blue) {
00310 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00311
00312 if (passive && (active_mode[link] != HTCS2_MODE_PASSIVE))
00313 _HTCSsendCommand(link, HTCS2_MODE_PASSIVE);
00314 else if (!passive && (active_mode[link] != HTCS2_MODE_RAW))
00315 _HTCSsendCommand(link, HTCS2_MODE_RAW);
00316
00317 HTCS2_I2CRequest[0] = 2;
00318 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00319 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_RED_MSB;
00320
00321 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 8))
00322 return false;
00323
00324 red = (long)HTCS2_I2CReply[0] * 256 + HTCS2_I2CReply[1];
00325 green = (long)HTCS2_I2CReply[2] * 256 + HTCS2_I2CReply[3];
00326 blue = (long)HTCS2_I2CReply[4] * 256 + HTCS2_I2CReply[5];
00327
00328 return true;
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 bool HTCS2readRawWhite(tSensors link, bool passive, long &white) {
00341 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00342
00343 if (passive && (active_mode[link] != HTCS2_MODE_PASSIVE))
00344 _HTCSsendCommand(link, HTCS2_MODE_PASSIVE);
00345 else if (!passive && (active_mode[link] != HTCS2_MODE_RAW))
00346 _HTCSsendCommand(link, HTCS2_MODE_RAW);
00347
00348 HTCS2_I2CRequest[0] = 2;
00349 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00350 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_WHITE_MSB;
00351
00352 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 2))
00353 return false;
00354
00355 white = (long)HTCS2_I2CReply[0] * 256 + HTCS2_I2CReply[1];
00356
00357 return true;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 int HTCS2readColorIndex(tSensors link) {
00370 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00371
00372 if (active_mode[link] != HTCS2_MODE_ACTIVE)
00373 _HTCSsendCommand(link, HTCS2_MODE_ACTIVE);
00374
00375 HTCS2_I2CRequest[0] = 2;
00376 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00377 HTCS2_I2CRequest[2] = HTCS2_OFFSET + HTCS2_COL_INDEX_REG;
00378
00379 if (!writeI2C(link, HTCS2_I2CRequest, HTCS2_I2CReply, 1))
00380 return -1;
00381
00382 return HTCS2_I2CReply[0];
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392 bool _HTCSsendCommand(tSensors link, byte command) {
00393 memset(HTCS2_I2CRequest, 0, sizeof(tByteArray));
00394
00395 HTCS2_I2CRequest[0] = 3;
00396 HTCS2_I2CRequest[1] = HTCS2_I2C_ADDR;
00397 HTCS2_I2CRequest[2] = HTCS2_CMD_REG;
00398 HTCS2_I2CRequest[3] = command;
00399
00400 if (command < 30)
00401 active_mode[link] = command;
00402
00403 return writeI2C(link, HTCS2_I2CRequest);
00404 }
00405
00406 #endif // __HTCS2_H__
00407
00408
00409
00410
00411
00412