Skip to content

Commit

Permalink
Added rgb dimming process
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkinyua committed Nov 6, 2024
1 parent 57cf459 commit c9fe4f1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
6 changes: 3 additions & 3 deletions firmware/Core/Inc/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
extern "C" {
#endif

#define RED_CHANNEL GPIO_NUM_5
#define GREEN_CHANNEL GPIO_NUM_4
#define BLUE_CHANNEL GPIO_NUM_6
#define RED_GPIO_PIN GPIO_NUM_5
#define GREEN_GPIO_PIN GPIO_NUM_4
#define BLUE_GPIO_PIN GPIO_NUM_6

void GPIO_init(void);

Expand Down
13 changes: 10 additions & 3 deletions firmware/Core/Inc/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
#define __INCLUDE_GUARD__PWM_H__

#include <stdint.h>
#include "driver/ledc.h"
#include "rgb.h"

#ifdef __cplusplus
extern "C"
{
#endif

void LED_PWM_Init(void);
uint8_t LED_set_dimming(uint32_t duty_percentage);
void LED_test_dimming(void);
#define RED_CHANNEL LEDC_CHANNEL_0
#define GREEN_CHANNEL LEDC_CHANNEL_1
#define BLUE_CHANNEL LEDC_CHANNEL_2

void LED_PWM_Init(void);
uint8_t LED_set_dimming(uint32_t duty_percentage);
void LED_test_dimming(void);
uint8_t LED_set_rgb_dimming(LED_RGB_t *rgb_values);

#ifdef __cplusplus
}
Expand Down
8 changes: 7 additions & 1 deletion firmware/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void app_main(void)
{
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second
ESP_LOGI("Main", "Running...");
LED_test_dimming();
// LED_test_dimming();

// TODO: Create an rtos thread to handle the dimming
LED_RGB_t rgb_values;
Expand All @@ -51,5 +51,11 @@ void app_main(void)
if(brightness_result == 0){
printf("Very Nice: Dimming Applied\n");
}

const uint8_t rgb_dimming_result = LED_set_rgb_dimming(&rgb_values);

if(rgb_dimming_result == 0){
printf("GREAT SUCCESS! Dimming worked\n");
}
}
}
62 changes: 55 additions & 7 deletions firmware/Core/Src/pwm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pwm.h"
#include "IO.h"
#include "rgb.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand Down Expand Up @@ -28,10 +29,10 @@ void LED_PWM_Init(void)
// Setting up the LEDC channel Configuration for Red Channel
ledc_channel_config_t LED_red_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.channel = RED_CHANNEL,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = RED_CHANNEL,
.gpio_num = RED_GPIO_PIN,
.duty = 0,
.hpoint = 0
};
Expand All @@ -46,10 +47,10 @@ void LED_PWM_Init(void)
// Setting up the LEDC channel Configuration for Green Channel
ledc_channel_config_t LED_green_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.channel = GREEN_CHANNEL,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = GREEN_CHANNEL,
.gpio_num = GREEN_GPIO_PIN,
.duty = 0,
.hpoint = 0};
ledc_channel_config(&LED_green_channel);
Expand All @@ -61,10 +62,10 @@ void LED_PWM_Init(void)
// Setting up the LEDC channel Configuration for Blue Channel
ledc_channel_config_t LED_blue_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.channel = BLUE_CHANNEL,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = BLUE_CHANNEL,
.gpio_num = BLUE_GPIO_PIN,
.duty = 0,
.hpoint = 0};
// ledc_channel_config(&LED_red_channel);
Expand Down Expand Up @@ -121,4 +122,51 @@ void LED_test_dimming(void)
}
vTaskDelay(500 / portTICK_PERIOD_MS); // Delay 500ms
}
}
}


/// @brief RGBW Dimming: Controls the dimming level of an LED using PWM.
/// @param rgb_values
/// - Arg 0: The struct containing the RGB values to be used in PWM dimming
/// @return 0 on success, > 0 on error
uint8_t LED_set_rgb_dimming(LED_RGB_t *rgb_values)
{
if (rgb_values == NULL)
{
// Error: Missing LED_RGB_t struct
return 1;
}

// Define duty variable for calculating each channel’s duty cycle
uint32_t duty;

// Set the duty cycle for the RED channel
duty = (8191 * rgb_values->RGB_red) / 255; // 8191 = 2^13 - 1 (13-bit resolution)

if (ledc_set_duty(LEDC_LOW_SPEED_MODE, RED_CHANNEL, duty) != ESP_OK ||
ledc_update_duty(LEDC_LOW_SPEED_MODE, RED_CHANNEL) != ESP_OK)
{
// Error setting or updating RED channel
return 2;
}

// Set the duty cycle for the GREEN channel
duty = (8191 * rgb_values->RGB_green) / 255;
if (ledc_set_duty(LEDC_LOW_SPEED_MODE, GREEN_CHANNEL, duty) != ESP_OK ||
ledc_update_duty(LEDC_LOW_SPEED_MODE, GREEN_CHANNEL) != ESP_OK)
{
// Error setting or updating GREEN channel
return 3;
}

// Set the duty cycle for the BLUE channel
duty = (8191 * rgb_values->RGB_blue) / 255;
if (ledc_set_duty(LEDC_LOW_SPEED_MODE, BLUE_CHANNEL, duty) != ESP_OK ||
ledc_update_duty(LEDC_LOW_SPEED_MODE, BLUE_CHANNEL) != ESP_OK)
{
// Error setting or updating BLUE channel
return 4;
}

return 0;
}

0 comments on commit c9fe4f1

Please sign in to comment.