Skip to content

Commit

Permalink
WIP: RGB Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkinyua committed Nov 3, 2024
1 parent 9928b32 commit 329891f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 21 deletions.
6 changes: 6 additions & 0 deletions firmware/Core/Inc/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#define __INCLUDE_GUARD__IO_H__

#include <stdint.h>
#include "driver/gpio.h"

#ifdef __cplusplus
extern "C" {
#endif

#define RED_GPIO_PIN GPIO_NUM_5
#define BLUE_GPIO_PIN GPIO_NUM_6
#define GREEN_GPIO_PIN GPIO_NUM_4
#define WHITE_GPIO_PIN GPIO_NUM_7

void GPIO_init(void);

#ifdef __cplusplus
Expand Down
3 changes: 3 additions & 0 deletions firmware/Core/Inc/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

#ifdef __cplusplus
extern "C"
Expand All @@ -12,11 +13,13 @@ extern "C"
#define RED_CHANNEL LEDC_CHANNEL_0
#define GREEN_CHANNEL LEDC_CHANNEL_1
#define BLUE_CHANNEL LEDC_CHANNEL_2
#define WHITE_CHANNEL LEDC_CHANNEL_3


void LED_PWM_Init(void);
uint8_t LED_set_dimming(ledc_channel_t channel, uint32_t duty_percentage);
uint8_t LED_channel_ramp(ledc_channel_t channel, uint32_t delay_ms);
uint8_t LED_set_rgb(const LED_RGB_t *rgb_values);
void LED_test_dimming(void);

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions firmware/Core/Inc/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ extern "C"
uint8_t RGB_red; // Red component (0-255)
uint8_t RGB_green; // Green component (0-255)
uint8_t RGB_blue; // Blue component (0-255)
uint8_t RGB_brightness; // Brightness value (0-100)
} LED_RGB_t;

uint8_t RGB_convert_hex_to_rgb(const char *hex_color_code, LED_RGB_t *rgb_result);
uint8_t RGB_apply_brightness(const uint8_t *rgb_brightness, LED_RGB_t *rgb_result);

#ifdef __cplusplus
}
Expand Down
39 changes: 38 additions & 1 deletion firmware/Core/Src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "main.h"
#include "IO.h"
#include "pwm.h"
#include "rgb.h"
#include "unit_tests/unit_test_executor.h"

#include <stdio.h>
Expand Down Expand Up @@ -35,6 +36,42 @@ void app_main(void)
{
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second
ESP_LOGI("Main", "Running...");
LED_test_dimming();
// LED_test_dimming();

// char rgb_string[10] = "#FF6347";
LED_RGB_t rgb_values;
// const uint8_t LED_brightness = 100;

// uint8_t rgb_conversion_result = RGB_convert_hex_to_rgb("#F542E6",&rgb_values);

// if(rgb_conversion_result == 0){
// printf("RGB Conversion Done\n");
// // continue;
// }

// uint8_t brightness_application = RGB_apply_brightness(LED_brightness, &rgb_values);
// printf(brightness_application);

// if(brightness_application == 0){
// printf("Brightness applied\n");
// continue;
// }

rgb_values.RGB_red = 245;
rgb_values.RGB_green = 66;
rgb_values.RGB_blue = 230;
uint8_t dimming_result = LED_set_rgb(&rgb_values);


if(dimming_result == 0){
printf("Success");
continue;
}

// rgb_conversion_result = RGB_convert_hex_to_rgb("#2B93C7",&rgb_values);
// brightness_application = RGB_apply_brightness((uint8_t)50, &rgb_values);
// dimming_result = LED_set_rgb(&rgb_values);


}
}
72 changes: 52 additions & 20 deletions firmware/Core/Src/pwm.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "pwm.h"
#include "IO.h"
#include "rgb.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand All @@ -22,45 +24,45 @@ void LED_PWM_Init(void)
ledc_timer_config(&ledc_timer);

// Setting up the LEDC channel Configuration for GPIO pin 0
ledc_channel_config_t ledc_channel_4 = {
ledc_channel_config_t RGB_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 = GPIO_NUM_4,
.gpio_num = RED_GPIO_PIN,
.duty = 0, // Initially off
.hpoint = 0};
ledc_channel_config(&ledc_channel_4);
ledc_channel_config(&RGB_red_channel);

ledc_channel_config_t ledc_channel_5 = {
ledc_channel_config_t RGB_green_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_1,
.channel = GREEN_CHANNEL,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = GPIO_NUM_5,
.gpio_num = GREEN_GPIO_PIN,
.duty = 0, // Initially off
.hpoint = 0};
ledc_channel_config(&ledc_channel_5);
ledc_channel_config(&RGB_green_channel);

ledc_channel_config_t ledc_channel_6 = {
ledc_channel_config_t RGB_blue_channel= {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_2,
.channel = BLUE_CHANNEL,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = GPIO_NUM_6,
.gpio_num = BLUE_GPIO_PIN,
.duty = 0, // Initially off
.hpoint = 0};
ledc_channel_config(&ledc_channel_6);
ledc_channel_config(&RGB_blue_channel);

ledc_channel_config_t ledc_channel_7 = {
ledc_channel_config_t RGB_white_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_3,
.channel = WHITE_CHANNEL,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = GPIO_NUM_7,
.gpio_num = WHITE_GPIO_PIN,
.duty = 0, // Initially off
.hpoint = 0};
ledc_channel_config(&ledc_channel_7);
ledc_channel_config(&RGB_white_channel);

ledc_channel_config_t ledc_channel_8 = {
.speed_mode = LEDC_LOW_SPEED_MODE,
Expand Down Expand Up @@ -135,9 +137,39 @@ uint8_t LED_channel_ramp(ledc_channel_t channel, uint32_t delay_ms)
/// TODO: Implement the error checks obtained from LED_set_dimming
void LED_test_dimming(void)
{
LED_channel_ramp(LEDC_CHANNEL_0, 100); // Red
LED_channel_ramp(LEDC_CHANNEL_1, 100); // Blue
LED_channel_ramp(LEDC_CHANNEL_2, 100); // Green
LED_channel_ramp(LEDC_CHANNEL_3, 100); // Not Sure
LED_channel_ramp(RED_CHANNEL, 100); // Red
LED_channel_ramp(BLUE_CHANNEL, 100); // Blue
LED_channel_ramp(GREEN_CHANNEL, 100); // Green
LED_channel_ramp(WHITE_CHANNEL, 100); // White
LED_channel_ramp(LEDC_CHANNEL_4, 100); // Not Sure
}


/// @brief Sets the PWM duty cycles based on RGB values with brightness adjustment
/// @param rgb_values The struct containing RGB values and brightness
/// @return 0 on success, > 0 on error
uint8_t LED_set_rgb(const LED_RGB_t *rgb_values)
{
// Validate the brightness level
if (rgb_values->RGB_brightness > 100)
{
return 1; // Error: Invalid brightness value
}

// Calculate duty cycles for each color component based on brightness
uint32_t duty_red = (8191 * rgb_values->RGB_red) / (255);
uint32_t duty_green = (8191 * rgb_values->RGB_green ) / (255);
uint32_t duty_blue = (8191 * rgb_values->RGB_blue ) / (255);

// Apply the duty cycles to the respective LED channels
ledc_set_duty(LEDC_LOW_SPEED_MODE, RED_CHANNEL, duty_red);
ledc_update_duty(LEDC_LOW_SPEED_MODE, RED_CHANNEL);

ledc_set_duty(LEDC_LOW_SPEED_MODE, GREEN_CHANNEL, duty_green);
ledc_update_duty(LEDC_LOW_SPEED_MODE, GREEN_CHANNEL);

ledc_set_duty(LEDC_LOW_SPEED_MODE, BLUE_CHANNEL, duty_blue);
ledc_update_duty(LEDC_LOW_SPEED_MODE, BLUE_CHANNEL);

return 0; // Success
}
27 changes: 27 additions & 0 deletions firmware/Core/Src/rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,32 @@ uint8_t RGB_convert_hex_to_rgb(const char *hex_color_code, LED_RGB_t *rgb_result
rgb_result->RGB_green = (uint8_t)strtol(green, NULL, 16);
rgb_result->RGB_blue = (uint8_t)strtol(blue, NULL, 16);

return 0;
}


/// @brief Applies brightness level to the RGB values.
/// @param rgb_brightness - The brightness level to be applied (uint8_t)
/// @param rgb_result - The struct to store the brightness value
/// @return 0 on success, > 0 on error
uint8_t RGB_apply_brightness(const uint8_t *rgb_brightness, LED_RGB_t *rgb_result)
{

// Check if the brightness is empty
// if (rgb_brightness == NULL || rgb_brightness > 100)
// {
// // Invalid brightness value
// return 1;
// }

rgb_result->RGB_brightness = *rgb_brightness;

// Apply the brightness to the rgb values

// Converting the string values into decimals and passing them into the struct
rgb_result->RGB_red = (rgb_result->RGB_red * (*rgb_brightness))/ 100;
rgb_result->RGB_green = (rgb_result->RGB_green * (*rgb_brightness))/ 100;
rgb_result->RGB_blue = (rgb_result->RGB_blue * (*rgb_brightness))/ 100;

return 0;
}

0 comments on commit 329891f

Please sign in to comment.