From 39e424ed13d755e93d8ff717261ad2bb16ea8836 Mon Sep 17 00:00:00 2001 From: hhaupt Date: Sun, 28 Apr 2024 01:24:47 +0200 Subject: [PATCH 1/3] implemented basic handling of infrared --- example/example.ino | 6 +-- src/Dezibot.cpp | 73 +---------------------------- src/Dezibot.h | 3 +- src/infraredLight/InfraredLED.cpp | 22 +++++++++ src/infraredLight/InfraredLight.cpp | 6 +++ src/infraredLight/InfraredLight.h | 60 ++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 src/infraredLight/InfraredLED.cpp create mode 100644 src/infraredLight/InfraredLight.cpp create mode 100644 src/infraredLight/InfraredLight.h diff --git a/example/example.ino b/example/example.ino index 8999e5a..e454f79 100644 --- a/example/example.ino +++ b/example/example.ino @@ -1,13 +1,13 @@ -#include #include -#define GPIO_LED 48 - Dezibot dezibot = Dezibot(); void setup() { dezibot.begin(); + dezibot.infraredLight.front.turnOn(); + dezibot.infraredLight.bottom.turnOn(); } void loop() { + } diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index 3285444..b44cf7c 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -4,79 +4,8 @@ #include "Dezibot.h" -#include -#include -#include -#include -#define SCREEN_WIDTH 128 // OLED display width, in pixels -#define SCREEN_HEIGHT 64 // OLED display height, in pixels - -// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) -// The pins for I2C are defined by the Wire-library. -// On an arduino UNO: A4(SDA), A5(SCL) -// On an arduino MEGA 2560: 20(SDA), 21(SCL) -// On an arduino LEONARDO: 2(SDA), 3(SCL), ... -#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) -#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 - -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); - - } + infraredLight.begin(); } \ No newline at end of file diff --git a/src/Dezibot.h b/src/Dezibot.h index 07fe014..94b47b1 100644 --- a/src/Dezibot.h +++ b/src/Dezibot.h @@ -16,6 +16,7 @@ #include "colorDetection/ColorDetection.h" #include "multiColorLight/MultiColorLight.h" #include "motionDetection/MotionDetection.h" +#include "infraredLight/InfraredLight.h" class Dezibot { protected: @@ -26,7 +27,7 @@ public: ColorDetection colorDetection; MultiColorLight multiColorLight; MotionDetection motionDetection; - + InfraredLight infraredLight; void begin(void); /* Display display diff --git a/src/infraredLight/InfraredLED.cpp b/src/infraredLight/InfraredLED.cpp new file mode 100644 index 0000000..082add1 --- /dev/null +++ b/src/infraredLight/InfraredLED.cpp @@ -0,0 +1,22 @@ +#include "InfraredLight.h" + + +InfraredLED::InfraredLED(uint8_t pin){ + ledPin = pin; +}; + +void InfraredLED::begin(void){ + pinMode(ledPin,OUTPUT); +}; + +void InfraredLED::turnOn(void){ + InfraredLED::setState(true); +}; + +void InfraredLED::turnOff(void){ + InfraredLED::setState(false); +}; + +void InfraredLED::setState(bool state){ + digitalWrite(ledPin,state); +}; \ No newline at end of file diff --git a/src/infraredLight/InfraredLight.cpp b/src/infraredLight/InfraredLight.cpp new file mode 100644 index 0000000..d61b7e7 --- /dev/null +++ b/src/infraredLight/InfraredLight.cpp @@ -0,0 +1,6 @@ +#include "InfraredLight.h" + +void InfraredLight::begin(void){ + bottom.begin(); + front.begin(); +} \ No newline at end of file diff --git a/src/infraredLight/InfraredLight.h b/src/infraredLight/InfraredLight.h new file mode 100644 index 0000000..54c2523 --- /dev/null +++ b/src/infraredLight/InfraredLight.h @@ -0,0 +1,60 @@ +/** + * @file InfraredLight.h + * @author Hans Haupt (hans.haupt@dezibot.de) + * @brief Provides basic controls for the infrared LEDs of the robot. + * @version 0.1 + * @date 2024-04-27 + * + * @copyright Copyright (c) 2024 + * + */ +#ifndef InfraredLight_h +#define InfraredLight_h +#include +#include + +enum IRLeds{ + Bottom, + Front +}; + +class InfraredLED{ + public: + InfraredLED(uint8_t pin); + void begin(void); + /** + * @brief enables selected LED + * + * @param led + */ + void turnOn(void); + /** + * @brief disables selected LED + * + * @param led + */ + void turnOff(void); + /** + * @brief changes state of selected LED depending on the state + * + * @param led which led will be affected + * @param state true if led should be turned on, else false + */ + void setState(bool state); + protected: + uint8_t ledPin; +}; + +class InfraredLight{ +public: + InfraredLED bottom = InfraredLED(IRBottomPin); + InfraredLED front = InfraredLED(IRFrontPin); + void begin(void); + +protected: + static const uint8_t IRFrontPin = 14; + static const uint8_t IRBottomPin = 13; +}; + + +#endif //InfraredLight_h \ No newline at end of file From 1596b3883c21644e311566772c7860bb3d097b34 Mon Sep 17 00:00:00 2001 From: hhaupt Date: Wed, 1 May 2024 14:25:03 +0200 Subject: [PATCH 2/3] added the possibility of sending a specific frequency via IR --- example/example.ino | 13 --------- src/infraredLight/InfraredLED.cpp | 47 +++++++++++++++++++++++++++---- src/infraredLight/InfraredLight.h | 19 +++++++++++-- 3 files changed, 58 insertions(+), 21 deletions(-) delete mode 100644 example/example.ino diff --git a/example/example.ino b/example/example.ino deleted file mode 100644 index e454f79..0000000 --- a/example/example.ino +++ /dev/null @@ -1,13 +0,0 @@ -#include - -Dezibot dezibot = Dezibot(); - -void setup() { - dezibot.begin(); - dezibot.infraredLight.front.turnOn(); - dezibot.infraredLight.bottom.turnOn(); -} - -void loop() { - -} diff --git a/src/infraredLight/InfraredLED.cpp b/src/infraredLight/InfraredLED.cpp index 082add1..60ee6e7 100644 --- a/src/infraredLight/InfraredLED.cpp +++ b/src/infraredLight/InfraredLED.cpp @@ -1,12 +1,36 @@ #include "InfraredLight.h" - -InfraredLED::InfraredLED(uint8_t pin){ - ledPin = pin; +#define pwmSpeedMode LEDC_LOW_SPEED_MODE +#define fooPin 13 +#define footimer LEDC_TIMER_0 +#define foochannel LEDC_CHANNEL_0 +InfraredLED::InfraredLED(uint8_t pin,ledc_timer_t timer, ledc_channel_t channel){ + this->ledPin = pin; + this->timer = timer; + this->channel = channel; }; void InfraredLED::begin(void){ - pinMode(ledPin,OUTPUT); + //we want to change frequency instead of + pwmTimer = ledc_timer_config_t{ + .speed_mode = pwmSpeedMode, + .duty_resolution = LEDC_TIMER_10_BIT, + .timer_num = this->timer, + .freq_hz = 1, + .clk_cfg = LEDC_AUTO_CLK + }; + ledc_timer_config(&pwmTimer); + + pwmChannel = ledc_channel_config_t{ + .gpio_num = this->ledPin, + .speed_mode =pwmSpeedMode, + .channel = this->channel, + .intr_type = LEDC_INTR_DISABLE, + .timer_sel = this->timer, + .duty = 0, + .hpoint = 0 + }; + ledc_channel_config(&pwmChannel); }; void InfraredLED::turnOn(void){ @@ -18,5 +42,18 @@ void InfraredLED::turnOff(void){ }; void InfraredLED::setState(bool state){ - digitalWrite(ledPin,state); + ledc_set_freq(pwmSpeedMode,timer,1); + if (state) { + ledc_set_duty(pwmSpeedMode,channel,1023); + } else { + ledc_set_duty(pwmSpeedMode,channel,0); + } + ledc_update_duty(pwmSpeedMode,channel); + +}; + +void InfraredLED::sendFrequency(uint16_t frequency){ + ledc_set_freq(pwmSpeedMode,timer,frequency); + ledc_set_duty(pwmSpeedMode,channel,512); + ledc_update_duty(pwmSpeedMode,channel); }; \ No newline at end of file diff --git a/src/infraredLight/InfraredLight.h b/src/infraredLight/InfraredLight.h index 54c2523..d13390f 100644 --- a/src/infraredLight/InfraredLight.h +++ b/src/infraredLight/InfraredLight.h @@ -12,6 +12,7 @@ #define InfraredLight_h #include #include +#include "driver/ledc.h" enum IRLeds{ Bottom, @@ -20,7 +21,7 @@ enum IRLeds{ class InfraredLED{ public: - InfraredLED(uint8_t pin); + InfraredLED(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel); void begin(void); /** * @brief enables selected LED @@ -41,14 +42,26 @@ class InfraredLED{ * @param state true if led should be turned on, else false */ void setState(bool state); + /** + * @brief starts flashing the IRLed with a specific frequency + * Won't stop automatically, must be stopped by calling any other IR-Method + * + * @param frequency + */ + void sendFrequency(uint16_t frequency); protected: uint8_t ledPin; + ledc_timer_t timer; + ledc_channel_t channel; + ledc_timer_config_t pwmTimer; + ledc_channel_config_t pwmChannel; }; class InfraredLight{ public: - InfraredLED bottom = InfraredLED(IRBottomPin); - InfraredLED front = InfraredLED(IRFrontPin); + //Do something for correct resource sharing + InfraredLED bottom = InfraredLED(IRBottomPin,LEDC_TIMER_0,LEDC_CHANNEL_0); + InfraredLED front = InfraredLED(IRFrontPin,LEDC_TIMER_1,LEDC_CHANNEL_1); void begin(void); protected: From 6ec94ae0dc88cae917e0f5b2d2b0871df52b64bc Mon Sep 17 00:00:00 2001 From: hhaupt Date: Wed, 1 May 2024 14:26:26 +0200 Subject: [PATCH 3/3] moved example ino in separate directory --- example/example/example.ino | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 example/example/example.ino diff --git a/example/example/example.ino b/example/example/example.ino new file mode 100644 index 0000000..101f2a9 --- /dev/null +++ b/example/example/example.ino @@ -0,0 +1,16 @@ +#include + +Dezibot dezibot = Dezibot(); + +void setup() { + dezibot.begin(); + //dezibot.infraredLight.front.turnOn(); + //dezibot.infraredLight.bottom.turnOn(); +} + +void loop() { + dezibot.infraredLight.bottom.turnOn(); + delay(1000); + dezibot.infraredLight.bottom.turnOff(); + delay(1000); +}