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