Skip to content

Commit c008800

Browse files
committed
added support for modulino light
1 parent cf5e2b2 commit c008800

File tree

4 files changed

+301
-0
lines changed

4 files changed

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

src/Modulino.cpp

+3
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

+109
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "Arduino_LSM6DSOX.h"
88
#include <Arduino_LPS22HB.h>
99
#include <Arduino_HS300x.h>
10+
#include "Arduino_LTR381RGB.h"
11+
#include "Arduino.h"
1012
//#include <SE05X.h> // need to provide a way to change Wire object
1113

1214
#ifndef ARDUINO_API_VERSION
@@ -289,10 +291,13 @@ class ModulinoKnob : public Module {
289291
uint8_t match[2] = { 0x74, 0x76 };
290292
};
291293

294+
extern ModulinoColor BLACK;
292295
extern ModulinoColor RED;
293296
extern ModulinoColor BLUE;
294297
extern ModulinoColor GREEN;
298+
extern ModulinoColor YELLOW;
295299
extern ModulinoColor VIOLET;
300+
extern ModulinoColor CYAN;
296301
extern ModulinoColor WHITE;
297302

298303
class ModulinoMovement : public Module {
@@ -394,7 +399,111 @@ class ModulinoPressure : public Module {
394399
};
395400

396401
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);
397427

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;
398507
};
399508

400509
class _distance_api {

0 commit comments

Comments
 (0)