Skip to content

Commit 6f1b8a8

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

File tree

4 files changed

+302
-0
lines changed

4 files changed

+302
-0
lines changed

Diff for: examples/Modulino_Light/Basic/Basic.ino

+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+
}
+151
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+
}

Diff for: 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")

Diff for: 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)