Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for modulino light #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/Modulino_Light/Basic/Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "Modulino.h"

ModulinoLight light;

int lux;
int ir;

void setup() {
Serial.begin(9600);
Modulino.begin();
light.begin();
}

void loop() {
light.update();
ModulinoColor color = light.getColor();
String colorName = light.getColorApproximate();
Serial.print("Color near to: ");
Serial.print(colorName);

int r = (0xFF000000 & color) >> 24;
int g = (0x00FF0000 & color) >> 16;
int b = (0x0000FF00 & color) >> 8;

lux = light.getAL();
ir = light.getIR();

Serial.print("light data: ");
Serial.print("\tRed:\t");
Serial.print(r);
Serial.print("\tGreen:\t");
Serial.print(g);
Serial.print("\tBlue:\t");
Serial.print(b);
Serial.print("\tLux:\t");
Serial.print(lux);
Serial.print("\tIR\t");
Serial.println(ir);
}
151 changes: 151 additions & 0 deletions examples/Modulino_Light/ColorDetection/ColorDetection.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include "Modulino.h"

ModulinoLight light;

// before to run this sketch, change the encoders addresses in order to use more than one encoder
// and set the addresses in the constructor as shown below
ModulinoKnob knob_green;
ModulinoKnob knob_red(0x09);
ModulinoKnob knob_blue(0x0A);
ModulinoPixels leds;


int brightness = 100;
int red = 0;
int green = 0;
int blue = 0;


void setup() {
Serial.begin(9600);
Modulino.begin();
knob_green.begin();
knob_red.begin();
knob_blue.begin();

// set knobs initial positions to 0
knob_red.set(0);
knob_green.set(0);
knob_blue.set(0);
light.begin();
leds.begin();
}

unsigned long start = millis();
void loop() {
knobPressed();
readColors();
updatesColors();
}

void readColors() {
if (red > 255) {
red = 255;
knob_red.set(red);
} else if (red < 0) {
red = 0;
knob_red.set(red);
}

if (green > 255) {
green = 255;
knob_green.set(green);
} else if (green < 0) {
green = 0;
knob_green.set(green);
}

if (blue > 255) {
blue = 255;
knob_blue.set(blue);
} else if (blue < 0) {
blue = 0;
knob_red.set(blue);
}
}

void knobPressed() {
red = knob_red.get();
green = knob_green.get();
blue = knob_blue.get();
bool red_click = knob_red.isPressed();
bool green_click = knob_green.isPressed();
bool blue_click = knob_blue.isPressed();

if (red_click) {
red = 255;
knob_red.set(red);
}

if (green_click) {
green = 255;
knob_green.set(green);
}

if (blue_click) {
blue = 255;
knob_blue.set(blue);
}

if (green_click && blue_click && red_click ) {
red = 0;
knob_red.set(red);
green = 0;
knob_green.set(green);
blue = 0;
knob_blue.set(blue);
} else if (red_click && green_click) {
red = 255;
knob_red.set(red);
green = 255;
knob_green.set(green);
blue = 0;
knob_blue.set(blue);
} else if (red_click && blue_click) {
red = 255;
knob_red.set(red);
green = 0;
knob_green.set(green);
blue = 255;
knob_blue.set(blue);
} else if (green_click && blue_click) {
red = 0;
knob_red.set(red);
green = 255;
knob_green.set(green);
blue = 255;
knob_blue.set(blue);
}
}

void updatesColors() {
ModulinoColor COLOR(red, green, blue);
for (int l = 0; l < 8; l++) {
leds.set(l, COLOR, brightness);
leds.show();
}
light.update();

if (millis() - start > 1000) {
char buffer [50];
int n, a = 3;
n = sprintf (buffer, "%03d", red);

Serial.print("Red:\t");
Serial.print(buffer);
n = sprintf (buffer, "%03d", green);
Serial.print("\tGreen:\t");
Serial.print(buffer);
n = sprintf (buffer, "%03d", blue);
Serial.print("\tBlue:\t");
Serial.print(buffer);

ModulinoColor color = light.getColor();
String colorName = light.getColorApproximate();
Serial.print("\tColor near to:\t");
Serial.print(colorName);
Serial.println();

start = millis();
}
}
3 changes: 3 additions & 0 deletions src/Modulino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
// Build before other objects to fix the Wire object
ModulinoClass Modulino __attribute__ ((init_priority (101)));

ModulinoColor BLACK(0, 0, 0);
ModulinoColor RED(255, 0, 0);
ModulinoColor BLUE(0, 0, 255);
ModulinoColor GREEN(0, 255, 0);
ModulinoColor YELLOW(255, 255, 0);
ModulinoColor VIOLET(255, 0, 255);
ModulinoColor CYAN(0, 255, 255);
ModulinoColor WHITE(255, 255, 255);

#if __has_include("Arduino_LED_Matrix.h")
Expand Down
109 changes: 109 additions & 0 deletions src/Modulino.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Arduino_LSM6DSOX.h"
#include <Arduino_LPS22HB.h>
#include <Arduino_HS300x.h>
#include "Arduino_LTR381RGB.h"
#include "Arduino.h"
//#include <SE05X.h> // need to provide a way to change Wire object

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

extern ModulinoColor BLACK;
extern ModulinoColor RED;
extern ModulinoColor BLUE;
extern ModulinoColor GREEN;
extern ModulinoColor YELLOW;
extern ModulinoColor VIOLET;
extern ModulinoColor CYAN;
extern ModulinoColor WHITE;

class ModulinoMovement : public Module {
Expand Down Expand Up @@ -394,7 +399,111 @@ class ModulinoPressure : public Module {
};

class ModulinoLight : public Module {
public:
bool begin() {
if (_light == nullptr) {
_light = new LTR381RGBClass(*((TwoWire*)getWire()), 0x53);
}
initialized = _light->begin();
__increaseI2CPriority();
return initialized != 0;
}
operator bool() {
return (initialized != 0);
}
bool update() {
if (initialized) {
return _light->readAllSensors(r, g, b, rawlux, lux, ir);
}
return 0;
}
ModulinoColor getColor() {
return ModulinoColor(r, g, b);
}
String getColorApproximate() {
String color = "UNKNOWN";
float h, s, l;
_light->getHSL(r, g, b, h, s, l);

if (l > 90.0) {
return "WHITE";
}
if (l < 10.0) {
return "BLACK";
}
if (s < 10.0) {
if (l < 50.0) {
return "DARK GRAY";
} else {
return "LIGHT GRAY";
}
}

if (h < 0) {
h = 360 + h;
}
if (h < 15 || h >= 345) {
color = "RED";
} else if (h < 45) {
color = "ORANGE";
} else if (h < 75) {
color = "YELLOW";
} else if (h < 105) {
color = "LIME";
} else if (h < 135) {
color = "GREEN";
} else if (h < 165) {
color = "SPRING GREEN";
} else if (h < 195) {
color = "CYAN";
} else if (h < 225) {
color = "AZURE";
} else if (h < 255) {
color = "BLUE";
} else if (h < 285) {
color = "VIOLET";
} else if (h < 315) {
color = "MAGENTA";
} else {
color = "ROSE";
}

// Adjust color based on lightness
if (l < 20.0) {
color = "VERY DARK " + color;
} else if (l < 40.0) {
color = "DARK " + color;
} else if (l > 80.0) {
color = "VERY LIGHT " + color;
} else if (l > 60.0) {
color = "LIGHT " + color;
}

// Adjust color based on saturation
if (s < 20.0) {
color = "VERY PALE " + color;
} else if (s < 40.0) {
color = "PALE " + color;
} else if (s > 80.0) {
color = "VERY VIVID " + color;
} else if (s > 60.0) {
color = "VIVID " + color;
}
return color;
}
int getAL() {
return rawlux;
}
int getLux() {
return lux;
}
int getIR() {
return ir;
}
private:
LTR381RGBClass* _light = nullptr;
int r, g, b, rawlux, lux, ir;
int initialized = 0;
};

class _distance_api {
Expand Down
Loading