diff --git a/src/colorDetection/ColorDetection.cpp b/src/colorDetection/ColorDetection.cpp index b41bb16..a19ec0a 100644 --- a/src/colorDetection/ColorDetection.cpp +++ b/src/colorDetection/ColorDetection.cpp @@ -1,6 +1,7 @@ #include "ColorDetection.h" void ColorDetection::begin(void){ + Power::waitForCurrentAllowance(PowerParameters::PowerConsumers::RGBW_SENSOR, PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW, COLOR_DETECTION_MAX_EXECUTION_DELAY_MS, NULL); ColorDetection::configure(VEML_CONFIG{.mode = AUTO,.enabled = true,.exposureTime=MS40}); }; void ColorDetection::configure(VEML_CONFIG config){ diff --git a/src/colorDetection/ColorDetection.h b/src/colorDetection/ColorDetection.h index 6fd284b..d3dcc98 100644 --- a/src/colorDetection/ColorDetection.h +++ b/src/colorDetection/ColorDetection.h @@ -11,9 +11,10 @@ #ifndef ColorDetection_h #define ColorDetection_h -#include -#include +#include "../power/Power.h" #include +#include +#include //Definitions for I2c #define I2C_MASTER_SCL_IO 2 /*!< GPIO number used for I2C master clock */ #define I2C_MASTER_SDA_IO 1 /*!< GPIO number used for I2C master data */ @@ -28,6 +29,8 @@ #define REG_BLUE 0x0A #define REG_WHITE 0x0B +#define COLOR_DETECTION_MAX_EXECUTION_DELAY_MS 1 + enum duration{ MS40, diff --git a/src/infraredLight/InfraredLED.cpp b/src/infraredLight/InfraredLED.cpp index 7298603..e5e11d9 100644 --- a/src/infraredLight/InfraredLED.cpp +++ b/src/infraredLight/InfraredLED.cpp @@ -1,6 +1,9 @@ #include "InfraredLight.h" #define pwmSpeedMode LEDC_LOW_SPEED_MODE +#define IR_FRONT_PIN 14 +#define IR_BOTTOM_PIN 13 +#define DUTY_RESOLUTION LEDC_TIMER_10_BIT InfraredLED::InfraredLED(uint8_t pin,ledc_timer_t timer, ledc_channel_t channel){ this->ledPin = pin; @@ -12,7 +15,7 @@ void InfraredLED::begin(void){ //we want to change frequency instead of pwmTimer = ledc_timer_config_t{ .speed_mode = pwmSpeedMode, - .duty_resolution = LEDC_TIMER_10_BIT, + .duty_resolution = DUTY_RESOLUTION, .timer_num = this->timer, .freq_hz = 800, .clk_cfg = LEDC_AUTO_CLK @@ -42,8 +45,24 @@ void InfraredLED::turnOff(void){ void InfraredLED::setState(bool state){ ledc_set_freq(pwmSpeedMode,timer,1); if (state) { + if (this->ledPin == IR_BOTTOM_PIN) { + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::LED_IR_BOTTOM, + PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM, + IR_LED_MAX_EXECUTION_DELAY_MS, NULL); + } else if (this->ledPin == IR_FRONT_PIN) { + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::LED_IR_FRONT, + PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT, + IR_LED_MAX_EXECUTION_DELAY_MS, NULL); + } ledc_set_duty(pwmSpeedMode,channel,1023); } else { + if (this->ledPin == IR_BOTTOM_PIN) { + Power::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_BOTTOM); + } else { + Power::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT); + } ledc_set_duty(pwmSpeedMode,channel,0); } ledc_update_duty(pwmSpeedMode,channel); @@ -51,7 +70,27 @@ void InfraredLED::setState(bool state){ }; void InfraredLED::sendFrequency(uint16_t frequency){ + constexpr uint32_t duty = 512; + // Float to force float division without casting + constexpr float resolution = 1 << DUTY_RESOLUTION; + if (this->ledPin == IR_BOTTOM_PIN) { + float currentConsumption = + (duty / resolution) * + PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM; + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::LED_IR_BOTTOM, + currentConsumption, + IR_LED_MAX_EXECUTION_DELAY_MS, NULL); + } else if (this->ledPin == IR_FRONT_PIN) { + float currentConsumption = + (duty / resolution) * + PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT; + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::LED_IR_FRONT, + currentConsumption, + IR_LED_MAX_EXECUTION_DELAY_MS, NULL); + } ledc_set_freq(pwmSpeedMode,timer,frequency); - ledc_set_duty(pwmSpeedMode,channel,512); + ledc_set_duty(pwmSpeedMode,channel,duty); ledc_update_duty(pwmSpeedMode,channel); }; \ No newline at end of file diff --git a/src/infraredLight/InfraredLight.h b/src/infraredLight/InfraredLight.h index 024e10a..beaabf9 100644 --- a/src/infraredLight/InfraredLight.h +++ b/src/infraredLight/InfraredLight.h @@ -10,9 +10,12 @@ */ #ifndef InfraredLight_h #define InfraredLight_h -#include -#include +#include "../power/Power.h" #include "driver/ledc.h" +#include +#include + +#define IR_LED_MAX_EXECUTION_DELAY_MS 1 class InfraredLED{ diff --git a/src/lightDetection/LightDetection.cpp b/src/lightDetection/LightDetection.cpp index 8934941..622caa7 100644 --- a/src/lightDetection/LightDetection.cpp +++ b/src/lightDetection/LightDetection.cpp @@ -64,6 +64,10 @@ uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measu }; void LightDetection::beginInfrared(void){ + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::PT_IR, + PowerParameters::CurrentConsumptions::CURRENT_PT * 4, + LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL); digitalWrite(IR_PT_ENABLE,true); pinMode(IR_PT_ENABLE, OUTPUT); pinMode(IR_PT_FRONT_ADC, INPUT); @@ -73,6 +77,10 @@ void LightDetection::beginInfrared(void){ }; void LightDetection::beginDaylight(void){ + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::PT_DL, + PowerParameters::CurrentConsumptions::CURRENT_PT * 2, + LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL); digitalWrite(DL_PT_ENABLE,true); pinMode(DL_PT_ENABLE, OUTPUT); pinMode(DL_PT_BOTTOM_ADC, INPUT); @@ -99,6 +107,7 @@ uint16_t LightDetection::readIRPT(photoTransistors sensor){ default: break; } + //Power::releaseCurrent(PowerParameters::PowerConsumers::PT_IR); //digitalWrite(IR_PT_ENABLE,LOW); return result; }; @@ -117,6 +126,7 @@ uint16_t LightDetection::readDLPT(photoTransistors sensor){ default: break; } + Power::releaseCurrent(PowerParameters::PowerConsumers::PT_DL); digitalWrite(DL_PT_ENABLE,LOW); return result; }; \ No newline at end of file diff --git a/src/lightDetection/LightDetection.h b/src/lightDetection/LightDetection.h index 32e25f3..2c6a0db 100644 --- a/src/lightDetection/LightDetection.h +++ b/src/lightDetection/LightDetection.h @@ -11,10 +11,11 @@ #ifndef LightDetection_h #define LightDetection_h -#include +#include "../power/Power.h" #include +#include - +#define LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS 1 enum photoTransistors{ IR_LEFT,