Integrate color sensor, infrared LEDs and phototransistors into Power Management

This commit is contained in:
2025-02-12 21:26:29 +01:00
parent e5ff1e7610
commit 5407543658
6 changed files with 65 additions and 8 deletions

View File

@@ -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);
};

View File

@@ -10,9 +10,12 @@
*/
#ifndef InfraredLight_h
#define InfraredLight_h
#include <stdint.h>
#include <Arduino.h>
#include "../power/Power.h"
#include "driver/ledc.h"
#include <Arduino.h>
#include <stdint.h>
#define IR_LED_MAX_EXECUTION_DELAY_MS 1
class InfraredLED{