|
7 | 7 | #include "Arduino_LSM6DSOX.h" |
8 | 8 | #include <Arduino_LPS22HB.h> |
9 | 9 | #include <Arduino_HS300x.h> |
| 10 | +#include "Arduino_LTR381RGB.h" |
| 11 | +#include "Arduino.h" |
10 | 12 | //#include <SE05X.h> // need to provide a way to change Wire object |
11 | 13 |
|
12 | 14 | #ifndef ARDUINO_API_VERSION |
@@ -289,10 +291,13 @@ class ModulinoKnob : public Module { |
289 | 291 | uint8_t match[2] = { 0x74, 0x76 }; |
290 | 292 | }; |
291 | 293 |
|
| 294 | +extern ModulinoColor BLACK; |
292 | 295 | extern ModulinoColor RED; |
293 | 296 | extern ModulinoColor BLUE; |
294 | 297 | extern ModulinoColor GREEN; |
| 298 | +extern ModulinoColor YELLOW; |
295 | 299 | extern ModulinoColor VIOLET; |
| 300 | +extern ModulinoColor CYAN; |
296 | 301 | extern ModulinoColor WHITE; |
297 | 302 |
|
298 | 303 | class ModulinoMovement : public Module { |
@@ -394,7 +399,111 @@ class ModulinoPressure : public Module { |
394 | 399 | }; |
395 | 400 |
|
396 | 401 | class ModulinoLight : public Module { |
| 402 | +public: |
| 403 | + bool begin() { |
| 404 | + if (_light == nullptr) { |
| 405 | + _light = new LTR381RGBClass(*((TwoWire*)getWire()), 0x53); |
| 406 | + } |
| 407 | + initialized = _light->begin(); |
| 408 | + __increaseI2CPriority(); |
| 409 | + return initialized != 0; |
| 410 | + } |
| 411 | + operator bool() { |
| 412 | + return (initialized != 0); |
| 413 | + } |
| 414 | + bool update() { |
| 415 | + if (initialized) { |
| 416 | + return _light->readAllSensors(r, g, b, rawlux, lux, ir); |
| 417 | + } |
| 418 | + return 0; |
| 419 | + } |
| 420 | + ModulinoColor getColor() { |
| 421 | + return ModulinoColor(r, g, b); |
| 422 | + } |
| 423 | + String getColorApproximate() { |
| 424 | + String color = "UNKNOWN"; |
| 425 | + float h, s, l; |
| 426 | + _light->getHSL(r, g, b, h, s, l); |
397 | 427 |
|
| 428 | + if (l > 90.0) { |
| 429 | + return "WHITE"; |
| 430 | + } |
| 431 | + if (l < 10.0) { |
| 432 | + return "BLACK"; |
| 433 | + } |
| 434 | + if (s < 10.0) { |
| 435 | + if (l < 50.0) { |
| 436 | + return "DARK GRAY"; |
| 437 | + } else { |
| 438 | + return "LIGHT GRAY"; |
| 439 | + } |
| 440 | + } |
| 441 | + |
| 442 | + if (h < 0) { |
| 443 | + h = 360 + h; |
| 444 | + } |
| 445 | + if (h < 15 || h >= 345) { |
| 446 | + color = "RED"; |
| 447 | + } else if (h < 45) { |
| 448 | + color = "ORANGE"; |
| 449 | + } else if (h < 75) { |
| 450 | + color = "YELLOW"; |
| 451 | + } else if (h < 105) { |
| 452 | + color = "LIME"; |
| 453 | + } else if (h < 135) { |
| 454 | + color = "GREEN"; |
| 455 | + } else if (h < 165) { |
| 456 | + color = "SPRING GREEN"; |
| 457 | + } else if (h < 195) { |
| 458 | + color = "CYAN"; |
| 459 | + } else if (h < 225) { |
| 460 | + color = "AZURE"; |
| 461 | + } else if (h < 255) { |
| 462 | + color = "BLUE"; |
| 463 | + } else if (h < 285) { |
| 464 | + color = "VIOLET"; |
| 465 | + } else if (h < 315) { |
| 466 | + color = "MAGENTA"; |
| 467 | + } else { |
| 468 | + color = "ROSE"; |
| 469 | + } |
| 470 | + |
| 471 | + // Adjust color based on lightness |
| 472 | + if (l < 20.0) { |
| 473 | + color = "VERY DARK " + color; |
| 474 | + } else if (l < 40.0) { |
| 475 | + color = "DARK " + color; |
| 476 | + } else if (l > 80.0) { |
| 477 | + color = "VERY LIGHT " + color; |
| 478 | + } else if (l > 60.0) { |
| 479 | + color = "LIGHT " + color; |
| 480 | + } |
| 481 | + |
| 482 | + // Adjust color based on saturation |
| 483 | + if (s < 20.0) { |
| 484 | + color = "VERY PALE " + color; |
| 485 | + } else if (s < 40.0) { |
| 486 | + color = "PALE " + color; |
| 487 | + } else if (s > 80.0) { |
| 488 | + color = "VERY VIVID " + color; |
| 489 | + } else if (s > 60.0) { |
| 490 | + color = "VIVID " + color; |
| 491 | + } |
| 492 | + return color; |
| 493 | + } |
| 494 | + int getAL() { |
| 495 | + return rawlux; |
| 496 | + } |
| 497 | + int getLux() { |
| 498 | + return lux; |
| 499 | + } |
| 500 | + int getIR() { |
| 501 | + return ir; |
| 502 | + } |
| 503 | +private: |
| 504 | + LTR381RGBClass* _light = nullptr; |
| 505 | + int r, g, b, rawlux, lux, ir; |
| 506 | + int initialized = 0; |
398 | 507 | }; |
399 | 508 |
|
400 | 509 | class _distance_api { |
|
0 commit comments