Skip to content

Commit bcf0745

Browse files
Rocketctfacchinm
authored andcommitted
added support for modulino light
1 parent 5b84f16 commit bcf0745

File tree

4 files changed

+302
-0
lines changed

4 files changed

+302
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "Modulino.h"
2+
3+
ModulinoLight light;
4+
5+
int lux;
6+
int ir;
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
Modulino.begin();
11+
light.begin();
12+
}
13+
14+
void loop() {
15+
light.update();
16+
ModulinoColor color = light.getColor();
17+
String colorName = light.getColorApproximate();
18+
Serial.print("Color near to: ");
19+
Serial.print(colorName);
20+
21+
int r = (0xFF000000 & color) >> 24;
22+
int g = (0x00FF0000 & color) >> 16;
23+
int b = (0x0000FF00 & color) >> 8;
24+
25+
lux = light.getAL();
26+
ir = light.getIR();
27+
28+
Serial.print("light data: ");
29+
Serial.print("\tRed:\t");
30+
Serial.print(r);
31+
Serial.print("\tGreen:\t");
32+
Serial.print(g);
33+
Serial.print("\tBlue:\t");
34+
Serial.print(b);
35+
Serial.print("\tLux:\t");
36+
Serial.print(lux);
37+
Serial.print("\tIR\t");
38+
Serial.println(ir);
39+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "Modulino.h"
2+
3+
ModulinoLight light;
4+
5+
// before to run this sketch, change the encoders addresses in order to use more than one encoder
6+
// and set the addresses in the constructor as shown below
7+
ModulinoKnob knob_green;
8+
ModulinoKnob knob_red(0x09);
9+
ModulinoKnob knob_blue(0x0A);
10+
ModulinoPixels leds;
11+
12+
13+
int brightness = 100;
14+
int red = 0;
15+
int green = 0;
16+
int blue = 0;
17+
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
Modulino.begin();
22+
knob_green.begin();
23+
knob_red.begin();
24+
knob_blue.begin();
25+
26+
// set knobs initial positions to 0
27+
knob_red.set(0);
28+
knob_green.set(0);
29+
knob_blue.set(0);
30+
light.begin();
31+
leds.begin();
32+
}
33+
34+
unsigned long start = millis();
35+
void loop() {
36+
knobPressed();
37+
readColors();
38+
updatesColors();
39+
}
40+
41+
void readColors() {
42+
if (red > 255) {
43+
red = 255;
44+
knob_red.set(red);
45+
} else if (red < 0) {
46+
red = 0;
47+
knob_red.set(red);
48+
}
49+
50+
if (green > 255) {
51+
green = 255;
52+
knob_green.set(green);
53+
} else if (green < 0) {
54+
green = 0;
55+
knob_green.set(green);
56+
}
57+
58+
if (blue > 255) {
59+
blue = 255;
60+
knob_blue.set(blue);
61+
} else if (blue < 0) {
62+
blue = 0;
63+
knob_red.set(blue);
64+
}
65+
}
66+
67+
void knobPressed() {
68+
red = knob_red.get();
69+
green = knob_green.get();
70+
blue = knob_blue.get();
71+
bool red_click = knob_red.isPressed();
72+
bool green_click = knob_green.isPressed();
73+
bool blue_click = knob_blue.isPressed();
74+
75+
if (red_click) {
76+
red = 255;
77+
knob_red.set(red);
78+
}
79+
80+
if (green_click) {
81+
green = 255;
82+
knob_green.set(green);
83+
}
84+
85+
if (blue_click) {
86+
blue = 255;
87+
knob_blue.set(blue);
88+
}
89+
90+
if (green_click && blue_click && red_click ) {
91+
red = 0;
92+
knob_red.set(red);
93+
green = 0;
94+
knob_green.set(green);
95+
blue = 0;
96+
knob_blue.set(blue);
97+
} else if (red_click && green_click) {
98+
red = 255;
99+
knob_red.set(red);
100+
green = 255;
101+
knob_green.set(green);
102+
blue = 0;
103+
knob_blue.set(blue);
104+
} else if (red_click && blue_click) {
105+
red = 255;
106+
knob_red.set(red);
107+
green = 0;
108+
knob_green.set(green);
109+
blue = 255;
110+
knob_blue.set(blue);
111+
} else if (green_click && blue_click) {
112+
red = 0;
113+
knob_red.set(red);
114+
green = 255;
115+
knob_green.set(green);
116+
blue = 255;
117+
knob_blue.set(blue);
118+
}
119+
}
120+
121+
void updatesColors() {
122+
ModulinoColor COLOR(red, green, blue);
123+
for (int l = 0; l < 8; l++) {
124+
leds.set(l, COLOR, brightness);
125+
leds.show();
126+
}
127+
light.update();
128+
129+
if (millis() - start > 1000) {
130+
char buffer [50];
131+
int n, a = 3;
132+
n = sprintf (buffer, "%03d", red);
133+
134+
Serial.print("Red:\t");
135+
Serial.print(buffer);
136+
n = sprintf (buffer, "%03d", green);
137+
Serial.print("\tGreen:\t");
138+
Serial.print(buffer);
139+
n = sprintf (buffer, "%03d", blue);
140+
Serial.print("\tBlue:\t");
141+
Serial.print(buffer);
142+
143+
ModulinoColor color = light.getColor();
144+
String colorName = light.getColorApproximate();
145+
Serial.print("\tColor near to:\t");
146+
Serial.print(colorName);
147+
Serial.println();
148+
149+
start = millis();
150+
}
151+
}

src/Modulino.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
// Build before other objects to fix the Wire object
88
ModulinoClass Modulino __attribute__ ((init_priority (101)));
99

10+
ModulinoColor BLACK(0, 0, 0);
1011
ModulinoColor RED(255, 0, 0);
1112
ModulinoColor BLUE(0, 0, 255);
1213
ModulinoColor GREEN(0, 255, 0);
14+
ModulinoColor YELLOW(255, 255, 0);
1315
ModulinoColor VIOLET(255, 0, 255);
16+
ModulinoColor CYAN(0, 255, 255);
1417
ModulinoColor WHITE(255, 255, 255);
1518

1619
#if __has_include("Arduino_LED_Matrix.h")

src/Modulino.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "Arduino_LSM6DSOX.h"
1717
#include <Arduino_LPS22HB.h>
1818
#include <Arduino_HS300x.h>
19+
#include "Arduino_LTR381RGB.h"
20+
#include "Arduino.h"
1921
//#include <SE05X.h> // need to provide a way to change Wire object
2022

2123
#ifndef ARDUINO_API_VERSION
@@ -440,10 +442,13 @@ class ModulinoKnob : public Module {
440442
uint8_t match[2] = { 0x74, 0x76 };
441443
};
442444

445+
extern ModulinoColor BLACK;
443446
extern ModulinoColor RED;
444447
extern ModulinoColor BLUE;
445448
extern ModulinoColor GREEN;
449+
extern ModulinoColor YELLOW;
446450
extern ModulinoColor VIOLET;
451+
extern ModulinoColor CYAN;
447452
extern ModulinoColor WHITE;
448453

449454
class ModulinoMovement : public Module {
@@ -563,7 +568,111 @@ class ModulinoPressure : public Module {
563568
};
564569

565570
class ModulinoLight : public Module {
571+
public:
572+
bool begin() {
573+
if (_light == nullptr) {
574+
_light = new LTR381RGBClass(*((TwoWire*)getWire()), 0x53);
575+
}
576+
initialized = _light->begin();
577+
__increaseI2CPriority();
578+
return initialized != 0;
579+
}
580+
operator bool() {
581+
return (initialized != 0);
582+
}
583+
bool update() {
584+
if (initialized) {
585+
return _light->readAllSensors(r, g, b, rawlux, lux, ir);
586+
}
587+
return 0;
588+
}
589+
ModulinoColor getColor() {
590+
return ModulinoColor(r, g, b);
591+
}
592+
String getColorApproximate() {
593+
String color = "UNKNOWN";
594+
float h, s, l;
595+
_light->getHSL(r, g, b, h, s, l);
566596

597+
if (l > 90.0) {
598+
return "WHITE";
599+
}
600+
if (l < 10.0) {
601+
return "BLACK";
602+
}
603+
if (s < 10.0) {
604+
if (l < 50.0) {
605+
return "DARK GRAY";
606+
} else {
607+
return "LIGHT GRAY";
608+
}
609+
}
610+
611+
if (h < 0) {
612+
h = 360 + h;
613+
}
614+
if (h < 15 || h >= 345) {
615+
color = "RED";
616+
} else if (h < 45) {
617+
color = "ORANGE";
618+
} else if (h < 75) {
619+
color = "YELLOW";
620+
} else if (h < 105) {
621+
color = "LIME";
622+
} else if (h < 135) {
623+
color = "GREEN";
624+
} else if (h < 165) {
625+
color = "SPRING GREEN";
626+
} else if (h < 195) {
627+
color = "CYAN";
628+
} else if (h < 225) {
629+
color = "AZURE";
630+
} else if (h < 255) {
631+
color = "BLUE";
632+
} else if (h < 285) {
633+
color = "VIOLET";
634+
} else if (h < 315) {
635+
color = "MAGENTA";
636+
} else {
637+
color = "ROSE";
638+
}
639+
640+
// Adjust color based on lightness
641+
if (l < 20.0) {
642+
color = "VERY DARK " + color;
643+
} else if (l < 40.0) {
644+
color = "DARK " + color;
645+
} else if (l > 80.0) {
646+
color = "VERY LIGHT " + color;
647+
} else if (l > 60.0) {
648+
color = "LIGHT " + color;
649+
}
650+
651+
// Adjust color based on saturation
652+
if (s < 20.0) {
653+
color = "VERY PALE " + color;
654+
} else if (s < 40.0) {
655+
color = "PALE " + color;
656+
} else if (s > 80.0) {
657+
color = "VERY VIVID " + color;
658+
} else if (s > 60.0) {
659+
color = "VIVID " + color;
660+
}
661+
return color;
662+
}
663+
int getAL() {
664+
return rawlux;
665+
}
666+
int getLux() {
667+
return lux;
668+
}
669+
int getIR() {
670+
return ir;
671+
}
672+
private:
673+
LTR381RGBClass* _light = nullptr;
674+
int r, g, b, rawlux, lux, ir;
675+
int initialized = 0;
567676
};
568677

569678
class _distance_api {

0 commit comments

Comments
 (0)