tdse-tp4_04-led_ldr

FIUBA - Electrónica - Taller de Sistemas Embebidos - Trabajo Práctico N°: 4 - LED & LDR
Index Commits Files Refs README
commit ef169ef675b81966c8604c522b15eca3459a4550
parent 3280bdf86d2d0b3f84175646ad18244a4c712033
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Sat, 25 Oct 2025 19:15:38 -0300

Implement `task_pwm_update`

Diffstat:
Mapp/src/task_pwm.c | 13++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/app/src/task_pwm.c b/app/src/task_pwm.c
@@ -47,11 +47,13 @@
 /* Application & Tasks includes. */
 #include "board.h"
 #include "app.h"
+#include <math.h>
 
 /********************** macros and definitions *******************************/
 
-#define STEP (2048)
-#define PERIOD (65535)
+#define ADC_MAX_VALUE (4096)
+#define PERIOD        (65535)
+#define min(a, b) a < b ? a : b
 
 /********************** internal data declaration ****************************/
 
@@ -78,17 +80,14 @@ void task_pwm_init(void *parameters)
     LOGGER_LOG("  %s is running - %s\r\n", GET_NAME(task_pwm_init), p_task_pwm);
 }
 
-float x_norm;
 void task_pwm_update(void *parameters)
 {
     shared_data_type *shared_data = (shared_data_type *) parameters;
-
-    x_norm = (shared_data->adc_value/4096.0f);
-    shared_data->pwm_active = x_norm*65535;
+    float x_norm = min(((float)shared_data->adc_value/ADC_MAX_VALUE)*8, 1);
+    shared_data->pwm_active = PERIOD*(1 - pow(x_norm, 1.5));
     setPWM(htim3, TIM_CHANNEL_1, PERIOD, shared_data->pwm_active);
 }
 
-
 void setPWM(TIM_HandleTypeDef timer, uint32_t channel,
             uint16_t period, uint16_t pulse) {
   HAL_TIM_PWM_Stop(&timer, channel);