Skip to content

Commit 8bc736e

Browse files
Rocketctfacchinm
authored andcommitted
added support for modulino light
1 parent a913d08 commit 8bc736e

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
@@ -11,6 +11,8 @@
1111
#include "Arduino_LSM6DSOX.h"
1212
#include <Arduino_LPS22HB.h>
1313
#include <Arduino_HS300x.h>
14+
#include "Arduino_LTR381RGB.h"
15+
#include "Arduino.h"
1416
//#include <SE05X.h> // need to provide a way to change Wire object
1517

1618
#ifndef ARDUINO_API_VERSION
@@ -394,10 +396,13 @@ class ModulinoKnob : public Module {
394396
uint8_t match[2] = { 0x74, 0x76 };
395397
};
396398

399+
extern ModulinoColor BLACK;
397400
extern ModulinoColor RED;
398401
extern ModulinoColor BLUE;
399402
extern ModulinoColor GREEN;
403+
extern ModulinoColor YELLOW;
400404
extern ModulinoColor VIOLET;
405+
extern ModulinoColor CYAN;
401406
extern ModulinoColor WHITE;
402407

403408
class ModulinoMovement : public Module {
@@ -505,7 +510,111 @@ class ModulinoPressure : public Module {
505510
};
506511

507512
class ModulinoLight : public Module {
513+
public:
514+
bool begin() {
515+
if (_light == nullptr) {
516+
_light = new LTR381RGBClass(*((TwoWire*)getWire()), 0x53);
517+
}
518+
initialized = _light->begin();
519+
__increaseI2CPriority();
520+
return initialized != 0;
521+
}
522+
operator bool() {
523+
return (initialized != 0);
524+
}
525+
bool update() {
526+
if (initialized) {
527+
return _light->readAllSensors(r, g, b, rawlux, lux, ir);
528+
}
529+
return 0;
530+
}
531+
ModulinoColor getColor() {
532+
return ModulinoColor(r, g, b);
533+
}
534+
String getColorApproximate() {
535+
String color = "UNKNOWN";
536+
float h, s, l;
537+
_light->getHSL(r, g, b, h, s, l);
508538

539+
if (l > 90.0) {
540+
return "WHITE";
541+
}
542+
if (l < 10.0) {
543+
return "BLACK";
544+
}
545+
if (s < 10.0) {
546+
if (l < 50.0) {
547+
return "DARK GRAY";
548+
} else {
549+
return "LIGHT GRAY";
550+
}
551+
}
552+
553+
if (h < 0) {
554+
h = 360 + h;
555+
}
556+
if (h < 15 || h >= 345) {
557+
color = "RED";
558+
} else if (h < 45) {
559+
color = "ORANGE";
560+
} else if (h < 75) {
561+
color = "YELLOW";
562+
} else if (h < 105) {
563+
color = "LIME";
564+
} else if (h < 135) {
565+
color = "GREEN";
566+
} else if (h < 165) {
567+
color = "SPRING GREEN";
568+
} else if (h < 195) {
569+
color = "CYAN";
570+
} else if (h < 225) {
571+
color = "AZURE";
572+
} else if (h < 255) {
573+
color = "BLUE";
574+
} else if (h < 285) {
575+
color = "VIOLET";
576+
} else if (h < 315) {
577+
color = "MAGENTA";
578+
} else {
579+
color = "ROSE";
580+
}
581+
582+
// Adjust color based on lightness
583+
if (l < 20.0) {
584+
color = "VERY DARK " + color;
585+
} else if (l < 40.0) {
586+
color = "DARK " + color;
587+
} else if (l > 80.0) {
588+
color = "VERY LIGHT " + color;
589+
} else if (l > 60.0) {
590+
color = "LIGHT " + color;
591+
}
592+
593+
// Adjust color based on saturation
594+
if (s < 20.0) {
595+
color = "VERY PALE " + color;
596+
} else if (s < 40.0) {
597+
color = "PALE " + color;
598+
} else if (s > 80.0) {
599+
color = "VERY VIVID " + color;
600+
} else if (s > 60.0) {
601+
color = "VIVID " + color;
602+
}
603+
return color;
604+
}
605+
int getAL() {
606+
return rawlux;
607+
}
608+
int getLux() {
609+
return lux;
610+
}
611+
int getIR() {
612+
return ir;
613+
}
614+
private:
615+
LTR381RGBClass* _light = nullptr;
616+
int r, g, b, rawlux, lux, ir;
617+
int initialized = 0;
509618
};
510619

511620
class _distance_api {

0 commit comments

Comments
 (0)