added comments to light classes

This commit is contained in:
Sebastian 2020-07-22 23:28:13 +02:00
parent c68e136985
commit 903e1e101a
2 changed files with 13 additions and 3 deletions

View File

@ -1,8 +1,10 @@
#include <header.h> #include <header.h>
// Bool to check if light is already active
bool lightActive = false; bool lightActive = false;
// Colors in Array: purple, blue, green, yellow, orange, red, white // 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}}; 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; int colorCounter = 6;
// Setting PWM properties // Setting PWM properties
@ -12,6 +14,7 @@ const int ledChannelGreen = 1;
const int ledChannelBlue= 2; const int ledChannelBlue= 2;
const int resolution = 8; const int resolution = 8;
// Setup method for PWM configurations
void setupPWM() { void setupPWM() {
// Set pins as output // Set pins as output
pinMode(LIGHT_LED_PIN_R, OUTPUT); pinMode(LIGHT_LED_PIN_R, OUTPUT);
@ -29,10 +32,12 @@ void setupPWM() {
ledcAttachPin(LIGHT_LED_PIN_B, ledChannelBlue); ledcAttachPin(LIGHT_LED_PIN_B, ledChannelBlue);
} }
// Takes a int value representing nanometers and sets color array pointer to appropriate color
void setValueNM(int NM) { void setValueNM(int NM) {
getColorBasedOnValueNM(NM); getColorBasedOnValueNM(NM);
} }
// Logic to set color array pointer
void getColorBasedOnValueNM(int valueNM) { void getColorBasedOnValueNM(int valueNM) {
if (valueNM <= 420) { //Purple if (valueNM <= 420) { //Purple
colorCounter = 0; colorCounter = 0;
@ -56,16 +61,15 @@ void getColorBasedOnValueNM(int valueNM) {
Serial.print(valueNM); Serial.print(valueNM);
} }
// Shuts down the light
bool shutdownLight() { bool shutdownLight() {
//digitalWrite(LIGHT_LED_PIN_R, LOW);
//digitalWrite(LIGHT_LED_PIN_G, LOW);
//digitalWrite(LIGHT_LED_PIN_B, LOW);
ledcWrite(ledChannelRed, 0); ledcWrite(ledChannelRed, 0);
ledcWrite(ledChannelGreen, 0); ledcWrite(ledChannelGreen, 0);
ledcWrite(ledChannelBlue, 0); ledcWrite(ledChannelBlue, 0);
return false; return false;
} }
// Activates the light based on colorValueArray
bool activateLight() { bool activateLight() {
ledcWrite(ledChannelRed, colorValueArray[colorCounter][0]); ledcWrite(ledChannelRed, colorValueArray[colorCounter][0]);
ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]); ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]);
@ -73,11 +77,13 @@ bool activateLight() {
return true; return true;
} }
// Activate light for given amount of time if not already activated
void lightTask(void *parameter) { void lightTask(void *parameter) {
takeSemaphore(); takeSemaphore();
unsigned long lightTimeoutTimer = millis(); unsigned long lightTimeoutTimer = millis();
if (lightActive == false) { if (lightActive == false) {
lightActive = activateLight(); lightActive = activateLight();
// If current time is below or equal to light timeout do nothing
while (millis() - lightTimeoutTimer <= MAX_LIGHT_TIMEOUT) { while (millis() - lightTimeoutTimer <= MAX_LIGHT_TIMEOUT) {
} }
lightActive = shutdownLight(); lightActive = shutdownLight();
@ -86,6 +92,7 @@ void lightTask(void *parameter) {
vTaskDelete(NULL); vTaskDelete(NULL);
} }
// Trigger for lightTask
void triggerLight() { void triggerLight() {
xTaskCreate( xTaskCreate(
lightTask, /* Task function. */ lightTask, /* Task function. */

View File

@ -1,13 +1,16 @@
#include <header.h> #include <header.h>
// New BH1750 class
BH1750 lightMeter; BH1750 lightMeter;
// Setup for reading light sensor (prepares i2c communication)
void setupLightSensor() { void setupLightSensor() {
Wire.begin(); Wire.begin();
lightMeter.begin(); lightMeter.begin();
Serial.println("Sensor started..."); Serial.println("Sensor started...");
} }
// Method to read and return the light value measured by BH1750 sensor
float readLightSensorValue() { float readLightSensorValue() {
int intensity = lightMeter.readLightLevel(); int intensity = lightMeter.readLightLevel();
return intensity; return intensity;