added comments to light classes
This commit is contained in:
parent
c68e136985
commit
903e1e101a
@ -1,8 +1,10 @@
|
||||
#include <header.h>
|
||||
|
||||
// Bool to check if light is already active
|
||||
bool lightActive = false;
|
||||
// Colors in Array: purple, blue, green, yellow, orange, red, white
|
||||
int colorValueArray[][3] = {{125,0,125}, {0,0,255}, {0,255,0}, {255,255,0}, {255,140,0}, {255,0,0}, {255,255,255}};
|
||||
// Pointer for color array
|
||||
int colorCounter = 6;
|
||||
|
||||
// Setting PWM properties
|
||||
@ -12,6 +14,7 @@ const int ledChannelGreen = 1;
|
||||
const int ledChannelBlue= 2;
|
||||
const int resolution = 8;
|
||||
|
||||
// Setup method for PWM configurations
|
||||
void setupPWM() {
|
||||
// Set pins as output
|
||||
pinMode(LIGHT_LED_PIN_R, OUTPUT);
|
||||
@ -29,10 +32,12 @@ void setupPWM() {
|
||||
ledcAttachPin(LIGHT_LED_PIN_B, ledChannelBlue);
|
||||
}
|
||||
|
||||
// Takes a int value representing nanometers and sets color array pointer to appropriate color
|
||||
void setValueNM(int NM) {
|
||||
getColorBasedOnValueNM(NM);
|
||||
}
|
||||
|
||||
// Logic to set color array pointer
|
||||
void getColorBasedOnValueNM(int valueNM) {
|
||||
if (valueNM <= 420) { //Purple
|
||||
colorCounter = 0;
|
||||
@ -56,16 +61,15 @@ void getColorBasedOnValueNM(int valueNM) {
|
||||
Serial.print(valueNM);
|
||||
}
|
||||
|
||||
// Shuts down the light
|
||||
bool shutdownLight() {
|
||||
//digitalWrite(LIGHT_LED_PIN_R, LOW);
|
||||
//digitalWrite(LIGHT_LED_PIN_G, LOW);
|
||||
//digitalWrite(LIGHT_LED_PIN_B, LOW);
|
||||
ledcWrite(ledChannelRed, 0);
|
||||
ledcWrite(ledChannelGreen, 0);
|
||||
ledcWrite(ledChannelBlue, 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Activates the light based on colorValueArray
|
||||
bool activateLight() {
|
||||
ledcWrite(ledChannelRed, colorValueArray[colorCounter][0]);
|
||||
ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]);
|
||||
@ -73,11 +77,13 @@ bool activateLight() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Activate light for given amount of time if not already activated
|
||||
void lightTask(void *parameter) {
|
||||
takeSemaphore();
|
||||
unsigned long lightTimeoutTimer = millis();
|
||||
if (lightActive == false) {
|
||||
lightActive = activateLight();
|
||||
// If current time is below or equal to light timeout do nothing
|
||||
while (millis() - lightTimeoutTimer <= MAX_LIGHT_TIMEOUT) {
|
||||
}
|
||||
lightActive = shutdownLight();
|
||||
@ -86,6 +92,7 @@ void lightTask(void *parameter) {
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// Trigger for lightTask
|
||||
void triggerLight() {
|
||||
xTaskCreate(
|
||||
lightTask, /* Task function. */
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
#include <header.h>
|
||||
|
||||
// New BH1750 class
|
||||
BH1750 lightMeter;
|
||||
|
||||
// Setup for reading light sensor (prepares i2c communication)
|
||||
void setupLightSensor() {
|
||||
Wire.begin();
|
||||
lightMeter.begin();
|
||||
Serial.println("Sensor started...");
|
||||
}
|
||||
|
||||
// Method to read and return the light value measured by BH1750 sensor
|
||||
float readLightSensorValue() {
|
||||
int intensity = lightMeter.readLightLevel();
|
||||
return intensity;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user