From 1d074f26d14895812d3024008b3bdde04e119aff Mon Sep 17 00:00:00 2001 From: hhaupt Date: Fri, 26 Apr 2024 14:58:20 +0200 Subject: [PATCH] add basic PT Reading --- example/example.ino | 5 ++ src/Dezibot.cpp | 54 +------------------- src/Dezibot.h | 1 + src/lightDetection/LightDetection.h | 77 ++++++++++++++++++++++++++++- 4 files changed, 83 insertions(+), 54 deletions(-) diff --git a/example/example.ino b/example/example.ino index 8999e5a..7f2768b 100644 --- a/example/example.ino +++ b/example/example.ino @@ -7,7 +7,12 @@ Dezibot dezibot = Dezibot(); void setup() { dezibot.begin(); + Serial.begin(9600); } void loop() { + Serial.println("FooBarBaz"); + Serial.println(dezibot.lightDetection.getValue(DL_FRONT)); + delay(1000); + } diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index 3285444..a961ddb 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -26,57 +26,5 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define GPIO_LED 48 void Dezibot::begin(void) { - Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(3, GPIO_LED, NEO_GRB + NEO_KHZ800); - Wire.begin(1, 2); - if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { - Serial.println("SSD1306 allocation failed"); - for (;;); // Don't proceed, loop forever - } - - display.display(); - vTaskDelay(2000); - display.clearDisplay(); - vTaskDelay(2000); - - // Draw a single pixel in white - display.drawPixel(10, 10, SSD1306_WHITE); - - // Show the display buffer on the screen. You MUST call display() after - // drawing commands to make them visible on screen! - display.display(); - vTaskDelay(2000); - - Serial.begin(9600); - Serial.println("start"); - - - vTaskDelay(1000); - - while (1) { - /* Blink off (output low) */ - ledStrip.setPixelColor(1, ledStrip.Color(100, 100, 100)); - ledStrip.show(); // Aktualisiere die Farbe des Pixels - vTaskDelay(1000); - /* Blink on (output high) */ - ledStrip.setPixelColor(1, ledStrip.Color(0, 0, 0)); - ledStrip.show(); // Aktualisiere die Farbe des Pixels - vTaskDelay(1000); - - struct timeval tv_now; - gettimeofday(&tv_now, NULL); - int64_t time_us = (int64_t) tv_now.tv_sec * 1000000L + (int64_t) tv_now.tv_usec; - - - Serial.println(time_us); - - display.clearDisplay(); - - display.setTextSize(2); // Draw 2X-scale text - display.setTextColor(SSD1306_WHITE); - display.setCursor(10, 0); - display.println(F("scroll")); - display.display(); // Show initial text - vTaskDelay(1000); - - } + lightDetection.begin(); } \ No newline at end of file diff --git a/src/Dezibot.h b/src/Dezibot.h index 07fe014..76b649d 100644 --- a/src/Dezibot.h +++ b/src/Dezibot.h @@ -17,6 +17,7 @@ #include "multiColorLight/MultiColorLight.h" #include "motionDetection/MotionDetection.h" + class Dezibot { protected: diff --git a/src/lightDetection/LightDetection.h b/src/lightDetection/LightDetection.h index 80827d8..ca10a98 100644 --- a/src/lightDetection/LightDetection.h +++ b/src/lightDetection/LightDetection.h @@ -1,8 +1,83 @@ +/** + * @file LightDetection.h + * @author Hans Haupt (hans.haupt@dezibot.de) + * @brief Class for Reading the values of the different Phototransistors, both IR, and DaylightSensors are supported. + * @version 0.1 + * @date 2024-04-26 + * + * @copyright Copyright (c) 2024 + * + */ + #ifndef LightDetection_h #define LightDetection_h +#include +#include + +enum photoTransistors{ + IR_LEFT, + IR_RIGHT, + IR_FRONT, + IR_BACK, + DL_FRONT, + DL_BOTTOM +}; + +enum ptType{ + IR, + DAYLIGHT +}; -//beinhaltet IR + Tageslicht class LightDetection{ +public: + /** + * @brief initialize the Lightdetection Compnent, must be called before the other methods are used. + * + */ + void begin(void); + + /** + * @brief reads the Value of the specified sensor + * + * @param sensor which sensor to read + * @return uint the reading of the sensor. between 0-4095 + */ + uint16_t getValue(photoTransistors sensor); + + /** + * @brief can be used to determine which sensor is exposed to the greatest amount of light + * Can distingish between IR and Daylight + * + * @param type select which PTTransistors to compare + * @return photoTransistors which sensor is exposed to the greatest amount of light + */ + photoTransistors getBrightest(ptType type); + + /** + * @brief Get the Average of multiple measurments of a single PT + * + * @param sensor Which Phototransistor should be read + * @param measurments how many measurements should be taken + * @param timeBetween which time should elapse between + * @return the average of all taken meaurments + */ + uint32_t getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween); +protected: + static const uint8_t IR_PT_FRONT_ADC = 3; + static const uint8_t IR_PT_LEFT_ADC = 4; + static const uint8_t IR_PT_RIGHT_ADC = 5; + static const uint8_t IR_PT_BACK_ADC = 6; + + static const uint8_t DL_PT_FRONT_ADC = 7; + static const uint8_t DL_PT_BOTTOM_ADC = 8; + + static const uint8_t DL_PT_ENABLE = 41; + static const uint8_t IR_PT_ENABLE = 40; + void beginInfrared(void); + void beginDaylight(void); + uint16_t readIRPT(photoTransistors sensor); + uint16_t readDLPT(photoTransistors sensor); + }; #endif //LightDetection_h \ No newline at end of file