Skip to content

Commit

Permalink
Modified device config and created RGB apply brightness function
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkinyua committed Nov 5, 2024
1 parent 0f2e04c commit 57cf459
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
5 changes: 5 additions & 0 deletions firmware/Core/Inc/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#define __INCLUDE_GUARD__IO_H__

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

#ifdef __cplusplus
extern "C" {
#endif

#define RED_CHANNEL GPIO_NUM_5
#define GREEN_CHANNEL GPIO_NUM_4
#define BLUE_CHANNEL GPIO_NUM_6

void GPIO_init(void);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion firmware/Core/Inc/config/device_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C"
char dimming_type; // Type of dimming, e.g., "0_10V" or "RGBW_PWM"
uint8_t brightness; // Brightness level (0-100%)
LED_RGB_t rgb_values; // Color Intensity for each channel (0-255)
} dimming_control_command_t;
} LED_dimming_control_t;

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions firmware/Core/Inc/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern "C"
} 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_to_rgb(const uint8_t *LED_brightness, LED_RGB_t *rgb_result);

#ifdef __cplusplus
}
Expand Down
15 changes: 15 additions & 0 deletions 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 @@ -36,5 +37,19 @@ void app_main(void)
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second
ESP_LOGI("Main", "Running...");
LED_test_dimming();

// TODO: Create an rtos thread to handle the dimming
LED_RGB_t rgb_values;
const uint8_t hex_parse_result = RGB_convert_hex_to_rgb("#FFAABB", &rgb_values);

if(hex_parse_result == 0){
printf("Great Success\n");
}
uint8_t brightness = 50;
const uint8_t brightness_result = RGB_apply_brightness_to_rgb(&brightness,&rgb_values);

if(brightness_result == 0){
printf("Very Nice: Dimming Applied\n");
}
}
}
53 changes: 47 additions & 6 deletions firmware/Core/Src/pwm.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "pwm.h"
#include "IO.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand All @@ -18,19 +19,59 @@ void LED_PWM_Init(void)
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_13_BIT, // 13-bit resolution for the duty cycle
.freq_hz = 5000, // Frequency in Hertz
.clk_cfg = LEDC_AUTO_CLK};
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);



// Setting up the LEDC channel Configuration for GPIO pin 0
ledc_channel_config_t ledc_channel = {
// 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,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = GPIO_NUM_0,
.duty = 0, // Initially off
.gpio_num = RED_CHANNEL,
.duty = 0,
.hpoint = 0
};

// ledc_channel_config(&LED_red_channel);

if(ledc_channel_config(&LED_red_channel) == ESP_ERR_INVALID_ARG){
printf("Invalid Param for Red Channel");
}


// 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,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = GREEN_CHANNEL,
.duty = 0,
.hpoint = 0};
ledc_channel_config(&LED_green_channel);

if(ledc_channel_config(&LED_green_channel) == ESP_ERR_INVALID_ARG){
printf("Invalid Param for blue Channel");
}

// 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,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = BLUE_CHANNEL,
.duty = 0,
.hpoint = 0};
ledc_channel_config(&ledc_channel);
// ledc_channel_config(&LED_red_channel);

if(ledc_channel_config(&LED_blue_channel) == ESP_ERR_INVALID_ARG){
printf("Invalid Param for blue Channel");
}
}

/// @brief 0-10V Dimming: Controls the dimming level of an LED using PWM.
Expand Down
25 changes: 25 additions & 0 deletions firmware/Core/Src/rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,30 @@ 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 the brightness param to the RGB values.
/// @param LED_brightness - The brightness value to be applied (uint8_t)
/// @param rgb_result - The struct to store the parsed RGB values
/// @return 0 on success, > 0 on error
uint8_t RGB_apply_brightness_to_rgb(const uint8_t *LED_brightness, LED_RGB_t *rgb_result)
{
if(LED_brightness == NULL || *LED_brightness > 100){
// Error: Missing brightness value
return 1;
}

if(rgb_result == NULL){
// Error: Missing RGB value
return 2;
}

//Apply brightness to RGB Values
rgb_result->RGB_red = (rgb_result->RGB_red * (*LED_brightness)) / 100;
rgb_result->RGB_green = (rgb_result->RGB_green * (*LED_brightness)) / 100;
rgb_result->RGB_blue = (rgb_result->RGB_blue * (*LED_brightness)) /100;

return 0;
}

0 comments on commit 57cf459

Please sign in to comment.