From 226b185beb40c5d4315c113120d3ae39b9b2883b Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Mon, 28 Oct 2024 04:14:58 -0600 Subject: [PATCH 01/10] WIP: Created hex to rgb converter. Need to add unit tests --- firmware/Core/Inc/rgb.h | 21 ++++++++++++++++ firmware/Core/Src/rgb.c | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 firmware/Core/Inc/rgb.h create mode 100644 firmware/Core/Src/rgb.c diff --git a/firmware/Core/Inc/rgb.h b/firmware/Core/Inc/rgb.h new file mode 100644 index 0000000..a563df2 --- /dev/null +++ b/firmware/Core/Inc/rgb.h @@ -0,0 +1,21 @@ +#ifndef __INCLUDE_GUARD__RGB_H__ +#define __INCLUDE_GUARD__RGB_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/// @brief Struct to hold RGB values. +typedef struct { + uint8_t RGB_red; // Red component (0-255) + uint8_t RGB_green; // Green component (0-255) + uint8_t RGB_blue; // Blue component (0-255) +} LED_RGB_t; + +#ifdef __cplusplus +} +#endif + +#endif // __INCLUDE_GUARD__RGB_H__ \ No newline at end of file diff --git a/firmware/Core/Src/rgb.c b/firmware/Core/Src/rgb.c new file mode 100644 index 0000000..7f1f66d --- /dev/null +++ b/firmware/Core/Src/rgb.c @@ -0,0 +1,55 @@ +#include "rgb.h" + +#include +#include +#include + +/// @brief Converts HEX color code value to RGB values. +/// @param hex_color_code +/// - Arg 0: The Hex color code to be parsed (string) +/// @return 0 on success, > 0 on error +uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result) { + + // The string, hex_color_code, should be in the form of #FFAABB + + // Check if the string is empty + if (hex_color_code == NULL || hex_color_code[0] == '\0'){ + + // Empty or NULL string, return error + return 1; + } + + // Check if the string starts with "#" + + //Finding the "#" character in the hex_color_code string + const char *sync_char = strchr(hex_color_code,'#'); + + if(!sync_char || hex_color_code[0] != '#'){ + // Error: Missing sync_char and incorrect index of the sync_char + return 2; + } + + // Check if the string contains all RGB values ie check length of the char passed + const uint8_t hex_code_length = strlen(hex_color_code); + + if(hex_code_length != 7){ + // Error: Invalide length- missing hex values for the RGB + return 3; + } + + // Parse the string and pass the values into the struct holding the RBG Values + + // Extracting the string RGB values from hex_color_code + char red[3] = { hex_color_code[1], hex_color_code[2], '\0' }; + char green[3] = { hex_color_code[3], hex_color_code[4], '\0' }; + char blue[3] = { hex_color_code[5], hex_color_code[6], '\0' }; + + // Converting the string values into decimals and passing them into the struct + + char *endptr; + rgb_result->RGB_red = (uint8_t) strtol(red,&endptr,10); + rgb_result->RGB_green = (uint8_t) strtol(green,&endptr,10); + rgb_result->RGB_blue = (uint8_t) strtol(blue,&endptr,10); + + return 0; +} \ No newline at end of file From 128ec35c2e8b3ab45a38e5637d8c6e4173b78cff Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Mon, 28 Oct 2024 04:19:49 -0600 Subject: [PATCH 02/10] Update Schematic_0-10V-Dimming-Circuit_Rev1.pdf --- .../Schematic_0-10V-Dimming-Circuit_Rev1.pdf | Bin 23878 -> 28223 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/docs/Schematics/0-10V Dimming Circuit/Schematic_0-10V-Dimming-Circuit_Rev1.pdf b/docs/Schematics/0-10V Dimming Circuit/Schematic_0-10V-Dimming-Circuit_Rev1.pdf index edac7e73179656426e3ba3cc686cec578f5eeb36..2964382d1d957198869495e6b9eadce78e55488f 100644 GIT binary patch delta 7158 zcmai3O>A996_%CQe!lP7b?Tqz{P?|V8>fzcUhkcs`=eBee`2EI5{KBeghFT{7b=h) zK`0VbYRw9ERihCS5=Cs;pe(v$!v-NlsX$_dgb>{zu|Q&rgaqfz%$b?{uAOeK-<&yf zzH`o;`Oe(y-~MXg*MA-O;K8|~mppx#p3f3dKK*ey0mCFt)2zh5YX!sUPp_ZkFglJS zmS!MN;sXAn=*US}q6L9YY9vRINSRrbm_?xDM9vOLyd9FDofD0fCFmWXdqYm-43Wee zB8hX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cD&FKRzpEa3cX8)`H2t~i{uq@zOV6keEVU@y39A;Cwf4cBL?`{hVs zn>3>$EA4_MBxiVddM7O0Y?u+-o0)4+9A3xI%dua9v>OL3MjH#IA&o!kZdpV z!PvU55W_x>k2SQlMCwj(`QWM};v>OZp^NQWr-i3$JGQ9OVW&Jv5XAAlO*2Ac0plPD zr)FhU%3^CNyZ%7d1rATTb^Hf#wxF`9bpb#YoWk=hQ2 zVZq~_po}a{oQzyJ@KLz$=;5G6zTvOraJ#y4uDO{iisN-nUBcg;`b9WL@yecD{M3|{4^J2 z#oDI9!-ZR28F~{;UR=0BUztDVj|nG{L#%0-=N>zoTzvkyE0=rl;+^wzsw}$&!xAn0eGx)2?(( zBx~2An`LOpYVyi%j)wG-gsoenGQW)fKi@p1|h1l-&2 Date: Mon, 28 Oct 2024 21:51:50 -0600 Subject: [PATCH 03/10] Create Getting_Started.md --- docs/Getting_Started.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 docs/Getting_Started.md diff --git a/docs/Getting_Started.md b/docs/Getting_Started.md new file mode 100644 index 0000000..08202fe --- /dev/null +++ b/docs/Getting_Started.md @@ -0,0 +1,28 @@ +# Getting Started! + +Welcome! This guide will help you get started with the Dimmer firmware. This firmware runs on the ESPF32-C3 DevKit C-02 microcontroller. + +## VS Code Setup Procedure + +**TLDR**: Watch this link and note these changes below: [ESP-IDF Setup Video](https://www.youtube.com/watch?v=XDDcS7HQNlI) +1. COM Port: Check the one that is available on your computer by checking the device manager. +2. Esspressif Device Target: Set it to esp32-c3. If you don't, it won't flash onto the microcontroller. Also, make sure you are using UART. + +Steps: +1. On Windows, install the "ESP-IDF" extension in the extensions tab. + 1. In the Command Palette (Ctrl+Shift+P) search for "Configure ESP-IDF Extension." + 2. Click on the matching result and select express installation option. + 3. Select the latest version and click install. +3. Clone this repo. +4. Open this repo by clicking File>Open Folder.. and navigate to where you cloned the repo. +5. Open an ESP-IDF Terminal through one of the tabs at the bottom of the VS Code Screen (Left of ESP-IDF: Build, Flash and Monitor Tab) +6. Type `idf.py menuconfig` and press enter +7. Navigate to the Serial flasher config section using the K,J,H and L keys and press enter +8. Navigate to the Flash Size tab and press enter and change it to `4 MB` + +### Troubleshooting + +1. Check that your MicroUSB cable supports data transfer. Many MicroUSB cables are power-only. +2. Disable your antivirus. +3. Ensure that you are using the correct COM Port. You can verify this by checking your Device Manager under `Ports` +4. Ensure that the Esspressif Device Target is set to `esp32c3` From 56b511709f3268ec141060b2cafab27e124ce79b Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Mon, 28 Oct 2024 22:39:50 -0600 Subject: [PATCH 04/10] Added check for valid hexadecimal representation in function arg helpers --- firmware/Core/Inc/function_arg_helpers.h | 16 +++++++++++ firmware/Core/Inc/rgb.h | 3 ++ firmware/Core/Src/function_arg_helpers.c | 31 +++++++++++++++++++++ firmware/Core/Src/rgb.c | 35 ++++++++++++------------ 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 firmware/Core/Inc/function_arg_helpers.h create mode 100644 firmware/Core/Src/function_arg_helpers.c diff --git a/firmware/Core/Inc/function_arg_helpers.h b/firmware/Core/Inc/function_arg_helpers.h new file mode 100644 index 0000000..11c4322 --- /dev/null +++ b/firmware/Core/Inc/function_arg_helpers.h @@ -0,0 +1,16 @@ +#ifndef __INCLUDE_GUARD__FUNCTION_ARG_HELPERS_H__ +#define __INCLUDE_GUARD__FUNCTION_ARG_HELPERS_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len); + +#ifdef __cplusplus +} +#endif + +#endif // __INCLUDE_GUARD__FUNCTION_ARG_HELPERS_H__ \ No newline at end of file diff --git a/firmware/Core/Inc/rgb.h b/firmware/Core/Inc/rgb.h index a563df2..6c15a64 100644 --- a/firmware/Core/Inc/rgb.h +++ b/firmware/Core/Inc/rgb.h @@ -14,6 +14,9 @@ typedef struct { uint8_t RGB_blue; // Blue component (0-255) } LED_RGB_t; + +uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result); + #ifdef __cplusplus } #endif diff --git a/firmware/Core/Src/function_arg_helpers.c b/firmware/Core/Src/function_arg_helpers.c new file mode 100644 index 0000000..265936d --- /dev/null +++ b/firmware/Core/Src/function_arg_helpers.c @@ -0,0 +1,31 @@ +#include "function_arg_helpers.h" + +#include +#include +#include + +/// @brief Checks if the passed string is a proper hexadecimal representation. +/// @param hex_string - The HEX string to be checked (const char *) +/// @param string_len - The length of the passed string to be checked (uint8_t) +/// @return 0 on success, > 0 on error +uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len){ + + if (hex_string == NULL || string_len == 0 || *hex_string == '\0') { + // Error if string and string_len is NULL or empty + return 1; + } + + for(uint8_t i = 0; i < string_len; i++){ + char character = *hex_string; + + // Checking if the character is between 0-9, A-F, or a-f + if(!(isdigit(character) || (toupper(character) >= 'A' && toupper(character) <= 'F'))){ + // Error: Invalid hex character within the string + return 2; + } + + hex_string++; + } + + return 0; +} \ No newline at end of file diff --git a/firmware/Core/Src/rgb.c b/firmware/Core/Src/rgb.c index 7f1f66d..db1f84c 100644 --- a/firmware/Core/Src/rgb.c +++ b/firmware/Core/Src/rgb.c @@ -1,55 +1,54 @@ #include "rgb.h" +#include "function_arg_helpers.h" #include #include #include /// @brief Converts HEX color code value to RGB values. -/// @param hex_color_code -/// - Arg 0: The Hex color code to be parsed (string) +/// @param hex_color_code - The Hex color code to be parsed (string) +/// @param rgb_result - The struct to store the parsed RGB values /// @return 0 on success, > 0 on error uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result) { - // The string, hex_color_code, should be in the form of #FFAABB + // The string, hex_color_code, should be in the form of #RRGGBB // Check if the string is empty if (hex_color_code == NULL || hex_color_code[0] == '\0'){ - // Empty or NULL string, return error return 1; } // Check if the string starts with "#" - - //Finding the "#" character in the hex_color_code string - const char *sync_char = strchr(hex_color_code,'#'); - - if(!sync_char || hex_color_code[0] != '#'){ - // Error: Missing sync_char and incorrect index of the sync_char + if(hex_color_code[0] != '#'){ + // Error: Missing sync_char "#" return 2; } - // Check if the string contains all RGB values ie check length of the char passed + // Check if the string contains all RGB values (length should be 7 for "#RRGGBB") const uint8_t hex_code_length = strlen(hex_color_code); - if(hex_code_length != 7){ // Error: Invalide length- missing hex values for the RGB return 3; } - // Parse the string and pass the values into the struct holding the RBG Values + // Check if the string contains valid hex values (skip the first character "#") + const uint8_t hex_check = is_valid_hex_string(hex_color_code + 1,hex_code_length-1); + if(hex_check > 0 ){ + // Error: Invalid hexadecimal within the string + return 4; + } + // Parse the string and pass the values into the struct holding the RBG Values // Extracting the string RGB values from hex_color_code char red[3] = { hex_color_code[1], hex_color_code[2], '\0' }; char green[3] = { hex_color_code[3], hex_color_code[4], '\0' }; char blue[3] = { hex_color_code[5], hex_color_code[6], '\0' }; // Converting the string values into decimals and passing them into the struct - - char *endptr; - rgb_result->RGB_red = (uint8_t) strtol(red,&endptr,10); - rgb_result->RGB_green = (uint8_t) strtol(green,&endptr,10); - rgb_result->RGB_blue = (uint8_t) strtol(blue,&endptr,10); + rgb_result->RGB_red = (uint8_t) strtol(red,NULL,16); + rgb_result->RGB_green = (uint8_t) strtol(green,NULL,16); + rgb_result->RGB_blue = (uint8_t) strtol(blue,NULL,16); return 0; } \ No newline at end of file From 0601d59d14c5d46409b7b038a54f3c61270aaec9 Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Mon, 28 Oct 2024 04:14:58 -0600 Subject: [PATCH 05/10] WIP: Created hex to rgb converter. Need to add unit tests --- firmware/Core/Inc/rgb.h | 21 ++++++++++++++++ firmware/Core/Src/rgb.c | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 firmware/Core/Inc/rgb.h create mode 100644 firmware/Core/Src/rgb.c diff --git a/firmware/Core/Inc/rgb.h b/firmware/Core/Inc/rgb.h new file mode 100644 index 0000000..a563df2 --- /dev/null +++ b/firmware/Core/Inc/rgb.h @@ -0,0 +1,21 @@ +#ifndef __INCLUDE_GUARD__RGB_H__ +#define __INCLUDE_GUARD__RGB_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/// @brief Struct to hold RGB values. +typedef struct { + uint8_t RGB_red; // Red component (0-255) + uint8_t RGB_green; // Green component (0-255) + uint8_t RGB_blue; // Blue component (0-255) +} LED_RGB_t; + +#ifdef __cplusplus +} +#endif + +#endif // __INCLUDE_GUARD__RGB_H__ \ No newline at end of file diff --git a/firmware/Core/Src/rgb.c b/firmware/Core/Src/rgb.c new file mode 100644 index 0000000..7f1f66d --- /dev/null +++ b/firmware/Core/Src/rgb.c @@ -0,0 +1,55 @@ +#include "rgb.h" + +#include +#include +#include + +/// @brief Converts HEX color code value to RGB values. +/// @param hex_color_code +/// - Arg 0: The Hex color code to be parsed (string) +/// @return 0 on success, > 0 on error +uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result) { + + // The string, hex_color_code, should be in the form of #FFAABB + + // Check if the string is empty + if (hex_color_code == NULL || hex_color_code[0] == '\0'){ + + // Empty or NULL string, return error + return 1; + } + + // Check if the string starts with "#" + + //Finding the "#" character in the hex_color_code string + const char *sync_char = strchr(hex_color_code,'#'); + + if(!sync_char || hex_color_code[0] != '#'){ + // Error: Missing sync_char and incorrect index of the sync_char + return 2; + } + + // Check if the string contains all RGB values ie check length of the char passed + const uint8_t hex_code_length = strlen(hex_color_code); + + if(hex_code_length != 7){ + // Error: Invalide length- missing hex values for the RGB + return 3; + } + + // Parse the string and pass the values into the struct holding the RBG Values + + // Extracting the string RGB values from hex_color_code + char red[3] = { hex_color_code[1], hex_color_code[2], '\0' }; + char green[3] = { hex_color_code[3], hex_color_code[4], '\0' }; + char blue[3] = { hex_color_code[5], hex_color_code[6], '\0' }; + + // Converting the string values into decimals and passing them into the struct + + char *endptr; + rgb_result->RGB_red = (uint8_t) strtol(red,&endptr,10); + rgb_result->RGB_green = (uint8_t) strtol(green,&endptr,10); + rgb_result->RGB_blue = (uint8_t) strtol(blue,&endptr,10); + + return 0; +} \ No newline at end of file From d0edc354c83e56904b209bcd8d60ce6dced20e43 Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Mon, 28 Oct 2024 04:19:49 -0600 Subject: [PATCH 06/10] Update Schematic_0-10V-Dimming-Circuit_Rev1.pdf --- .../Schematic_0-10V-Dimming-Circuit_Rev1.pdf | Bin 23878 -> 28223 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/docs/Schematics/0-10V Dimming Circuit/Schematic_0-10V-Dimming-Circuit_Rev1.pdf b/docs/Schematics/0-10V Dimming Circuit/Schematic_0-10V-Dimming-Circuit_Rev1.pdf index edac7e73179656426e3ba3cc686cec578f5eeb36..2964382d1d957198869495e6b9eadce78e55488f 100644 GIT binary patch delta 7158 zcmai3O>A996_%CQe!lP7b?Tqz{P?|V8>fzcUhkcs`=eBee`2EI5{KBeghFT{7b=h) zK`0VbYRw9ERihCS5=Cs;pe(v$!v-NlsX$_dgb>{zu|Q&rgaqfz%$b?{uAOeK-<&yf zzH`o;`Oe(y-~MXg*MA-O;K8|~mppx#p3f3dKK*ey0mCFt)2zh5YX!sUPp_ZkFglJS zmS!MN;sXAn=*US}q6L9YY9vRINSRrbm_?xDM9vOLyd9FDofD0fCFmWXdqYm-43Wee zB8hXTNJPIKCvtX3qV1gM=t<-ZrQl7!B`VQiTdK$74WNp${?qBIq_ zFIBBnR%Ng!W}!$0?TIx-lE4y)5RWX8%voaB(xT54vy#A+7O9PM6Sr+?7&jQ}Uup#5 zO0Pmb;3~CoX;M8q*Bi*y-_E_JpPTCVe4~*k@&239(x!SkRI1MTMoU78g~Dc<_GA)s z+yXT=h-=3NH`UJu8^cyjeY!HEZVu1m@@A?xM%#4qlcC|k0t$|)n?qk1z~{R9{ZJEr z%?+;67*#!M{3zN}@3(|{+>F4#?>E0RkgAWHj|PNVYu$zCd#yVl{{((B)fqj2-`^d* z2fu$edIx6PW3R*WchF^iZql9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kyE0=rl;+^wzsw}$&!xAn0eGx)2?(( zBx~2An`LOpYVyi%j)wG-gsoenGQW)fKi@p1|h1l-&2 Date: Mon, 28 Oct 2024 21:51:50 -0600 Subject: [PATCH 07/10] Create Getting_Started.md --- docs/Getting_Started.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 docs/Getting_Started.md diff --git a/docs/Getting_Started.md b/docs/Getting_Started.md new file mode 100644 index 0000000..08202fe --- /dev/null +++ b/docs/Getting_Started.md @@ -0,0 +1,28 @@ +# Getting Started! + +Welcome! This guide will help you get started with the Dimmer firmware. This firmware runs on the ESPF32-C3 DevKit C-02 microcontroller. + +## VS Code Setup Procedure + +**TLDR**: Watch this link and note these changes below: [ESP-IDF Setup Video](https://www.youtube.com/watch?v=XDDcS7HQNlI) +1. COM Port: Check the one that is available on your computer by checking the device manager. +2. Esspressif Device Target: Set it to esp32-c3. If you don't, it won't flash onto the microcontroller. Also, make sure you are using UART. + +Steps: +1. On Windows, install the "ESP-IDF" extension in the extensions tab. + 1. In the Command Palette (Ctrl+Shift+P) search for "Configure ESP-IDF Extension." + 2. Click on the matching result and select express installation option. + 3. Select the latest version and click install. +3. Clone this repo. +4. Open this repo by clicking File>Open Folder.. and navigate to where you cloned the repo. +5. Open an ESP-IDF Terminal through one of the tabs at the bottom of the VS Code Screen (Left of ESP-IDF: Build, Flash and Monitor Tab) +6. Type `idf.py menuconfig` and press enter +7. Navigate to the Serial flasher config section using the K,J,H and L keys and press enter +8. Navigate to the Flash Size tab and press enter and change it to `4 MB` + +### Troubleshooting + +1. Check that your MicroUSB cable supports data transfer. Many MicroUSB cables are power-only. +2. Disable your antivirus. +3. Ensure that you are using the correct COM Port. You can verify this by checking your Device Manager under `Ports` +4. Ensure that the Esspressif Device Target is set to `esp32c3` From 9ff39244dfc14fb7933da226c093973467cb24a7 Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Mon, 28 Oct 2024 22:39:50 -0600 Subject: [PATCH 08/10] Added check for valid hexadecimal representation in function arg helpers --- firmware/Core/Inc/function_arg_helpers.h | 16 +++++++++++ firmware/Core/Inc/rgb.h | 3 ++ firmware/Core/Src/function_arg_helpers.c | 31 +++++++++++++++++++++ firmware/Core/Src/rgb.c | 35 ++++++++++++------------ 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 firmware/Core/Inc/function_arg_helpers.h create mode 100644 firmware/Core/Src/function_arg_helpers.c diff --git a/firmware/Core/Inc/function_arg_helpers.h b/firmware/Core/Inc/function_arg_helpers.h new file mode 100644 index 0000000..11c4322 --- /dev/null +++ b/firmware/Core/Inc/function_arg_helpers.h @@ -0,0 +1,16 @@ +#ifndef __INCLUDE_GUARD__FUNCTION_ARG_HELPERS_H__ +#define __INCLUDE_GUARD__FUNCTION_ARG_HELPERS_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len); + +#ifdef __cplusplus +} +#endif + +#endif // __INCLUDE_GUARD__FUNCTION_ARG_HELPERS_H__ \ No newline at end of file diff --git a/firmware/Core/Inc/rgb.h b/firmware/Core/Inc/rgb.h index a563df2..6c15a64 100644 --- a/firmware/Core/Inc/rgb.h +++ b/firmware/Core/Inc/rgb.h @@ -14,6 +14,9 @@ typedef struct { uint8_t RGB_blue; // Blue component (0-255) } LED_RGB_t; + +uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result); + #ifdef __cplusplus } #endif diff --git a/firmware/Core/Src/function_arg_helpers.c b/firmware/Core/Src/function_arg_helpers.c new file mode 100644 index 0000000..265936d --- /dev/null +++ b/firmware/Core/Src/function_arg_helpers.c @@ -0,0 +1,31 @@ +#include "function_arg_helpers.h" + +#include +#include +#include + +/// @brief Checks if the passed string is a proper hexadecimal representation. +/// @param hex_string - The HEX string to be checked (const char *) +/// @param string_len - The length of the passed string to be checked (uint8_t) +/// @return 0 on success, > 0 on error +uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len){ + + if (hex_string == NULL || string_len == 0 || *hex_string == '\0') { + // Error if string and string_len is NULL or empty + return 1; + } + + for(uint8_t i = 0; i < string_len; i++){ + char character = *hex_string; + + // Checking if the character is between 0-9, A-F, or a-f + if(!(isdigit(character) || (toupper(character) >= 'A' && toupper(character) <= 'F'))){ + // Error: Invalid hex character within the string + return 2; + } + + hex_string++; + } + + return 0; +} \ No newline at end of file diff --git a/firmware/Core/Src/rgb.c b/firmware/Core/Src/rgb.c index 7f1f66d..db1f84c 100644 --- a/firmware/Core/Src/rgb.c +++ b/firmware/Core/Src/rgb.c @@ -1,55 +1,54 @@ #include "rgb.h" +#include "function_arg_helpers.h" #include #include #include /// @brief Converts HEX color code value to RGB values. -/// @param hex_color_code -/// - Arg 0: The Hex color code to be parsed (string) +/// @param hex_color_code - The Hex color code to be parsed (string) +/// @param rgb_result - The struct to store the parsed RGB values /// @return 0 on success, > 0 on error uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result) { - // The string, hex_color_code, should be in the form of #FFAABB + // The string, hex_color_code, should be in the form of #RRGGBB // Check if the string is empty if (hex_color_code == NULL || hex_color_code[0] == '\0'){ - // Empty or NULL string, return error return 1; } // Check if the string starts with "#" - - //Finding the "#" character in the hex_color_code string - const char *sync_char = strchr(hex_color_code,'#'); - - if(!sync_char || hex_color_code[0] != '#'){ - // Error: Missing sync_char and incorrect index of the sync_char + if(hex_color_code[0] != '#'){ + // Error: Missing sync_char "#" return 2; } - // Check if the string contains all RGB values ie check length of the char passed + // Check if the string contains all RGB values (length should be 7 for "#RRGGBB") const uint8_t hex_code_length = strlen(hex_color_code); - if(hex_code_length != 7){ // Error: Invalide length- missing hex values for the RGB return 3; } - // Parse the string and pass the values into the struct holding the RBG Values + // Check if the string contains valid hex values (skip the first character "#") + const uint8_t hex_check = is_valid_hex_string(hex_color_code + 1,hex_code_length-1); + if(hex_check > 0 ){ + // Error: Invalid hexadecimal within the string + return 4; + } + // Parse the string and pass the values into the struct holding the RBG Values // Extracting the string RGB values from hex_color_code char red[3] = { hex_color_code[1], hex_color_code[2], '\0' }; char green[3] = { hex_color_code[3], hex_color_code[4], '\0' }; char blue[3] = { hex_color_code[5], hex_color_code[6], '\0' }; // Converting the string values into decimals and passing them into the struct - - char *endptr; - rgb_result->RGB_red = (uint8_t) strtol(red,&endptr,10); - rgb_result->RGB_green = (uint8_t) strtol(green,&endptr,10); - rgb_result->RGB_blue = (uint8_t) strtol(blue,&endptr,10); + rgb_result->RGB_red = (uint8_t) strtol(red,NULL,16); + rgb_result->RGB_green = (uint8_t) strtol(green,NULL,16); + rgb_result->RGB_blue = (uint8_t) strtol(blue,NULL,16); return 0; } \ No newline at end of file From c1ce1c70891592fd2489672d41874a15a95648c5 Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Thu, 31 Oct 2024 02:16:23 -0600 Subject: [PATCH 09/10] Added unit test for rgb function --- firmware/Core/Inc/rgb.h | 19 ++++---- firmware/Core/Inc/unit_tests/unit_test_rgb.h | 8 ++++ firmware/Core/Src/rgb.c | 31 +++++++------ .../Core/Src/unit_tests/unit_test_inventory.c | 5 +++ firmware/Core/Src/unit_tests/unit_test_rgb.c | 44 +++++++++++++++++++ 5 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 firmware/Core/Inc/unit_tests/unit_test_rgb.h create mode 100644 firmware/Core/Src/unit_tests/unit_test_rgb.c diff --git a/firmware/Core/Inc/rgb.h b/firmware/Core/Inc/rgb.h index 6c15a64..e3add35 100644 --- a/firmware/Core/Inc/rgb.h +++ b/firmware/Core/Inc/rgb.h @@ -4,18 +4,19 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/// @brief Struct to hold RGB values. -typedef struct { - uint8_t RGB_red; // Red component (0-255) - uint8_t RGB_green; // Green component (0-255) - uint8_t RGB_blue; // Blue component (0-255) -} LED_RGB_t; + /// @brief Struct to hold RGB values. + typedef struct + { + uint8_t RGB_red; // Red component (0-255) + uint8_t RGB_green; // Green component (0-255) + uint8_t RGB_blue; // Blue component (0-255) + } LED_RGB_t; - -uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result); + uint8_t RGB_convert_hex_to_rgb(const char *hex_color_code, LED_RGB_t *rgb_result); #ifdef __cplusplus } diff --git a/firmware/Core/Inc/unit_tests/unit_test_rgb.h b/firmware/Core/Inc/unit_tests/unit_test_rgb.h new file mode 100644 index 0000000..a35b1a5 --- /dev/null +++ b/firmware/Core/Inc/unit_tests/unit_test_rgb.h @@ -0,0 +1,8 @@ +#ifndef __INCLUDE_GUARD__UNIT_TEST_RGB_H__ +#define __INCLUDE_GUARD__UNIT_TEST_RGB_H__ + +#include + +uint8_t TEST_EXEC__RGB_convert_hex_to_rgb(); + +#endif // __INCLUDE_GUARD__UNIT_TEST_PWM_H__ \ No newline at end of file diff --git a/firmware/Core/Src/rgb.c b/firmware/Core/Src/rgb.c index db1f84c..988c298 100644 --- a/firmware/Core/Src/rgb.c +++ b/firmware/Core/Src/rgb.c @@ -9,46 +9,51 @@ /// @param hex_color_code - The Hex color code to be parsed (string) /// @param rgb_result - The struct to store the parsed RGB values /// @return 0 on success, > 0 on error -uint8_t RGB_convert_hex_to_rgb(char *hex_color_code, LED_RGB_t *rgb_result) { +uint8_t RGB_convert_hex_to_rgb(const char *hex_color_code, LED_RGB_t *rgb_result) +{ // The string, hex_color_code, should be in the form of #RRGGBB // Check if the string is empty - if (hex_color_code == NULL || hex_color_code[0] == '\0'){ + if (hex_color_code == NULL || hex_color_code[0] == '\0') + { // Empty or NULL string, return error return 1; } // Check if the string starts with "#" - if(hex_color_code[0] != '#'){ + if (hex_color_code[0] != '#') + { // Error: Missing sync_char "#" return 2; } // Check if the string contains all RGB values (length should be 7 for "#RRGGBB") const uint8_t hex_code_length = strlen(hex_color_code); - if(hex_code_length != 7){ - // Error: Invalide length- missing hex values for the RGB + if (hex_code_length != 7) + { + // Error: Invalid length- missing hex values for the RGB return 3; } // Check if the string contains valid hex values (skip the first character "#") - const uint8_t hex_check = is_valid_hex_string(hex_color_code + 1,hex_code_length-1); - if(hex_check > 0 ){ + const uint8_t hex_check = is_valid_hex_string(hex_color_code + 1, hex_code_length - 1); + if (hex_check > 0) + { // Error: Invalid hexadecimal within the string return 4; } // Parse the string and pass the values into the struct holding the RBG Values // Extracting the string RGB values from hex_color_code - char red[3] = { hex_color_code[1], hex_color_code[2], '\0' }; - char green[3] = { hex_color_code[3], hex_color_code[4], '\0' }; - char blue[3] = { hex_color_code[5], hex_color_code[6], '\0' }; + char red[3] = {hex_color_code[1], hex_color_code[2], '\0'}; + char green[3] = {hex_color_code[3], hex_color_code[4], '\0'}; + char blue[3] = {hex_color_code[5], hex_color_code[6], '\0'}; // Converting the string values into decimals and passing them into the struct - rgb_result->RGB_red = (uint8_t) strtol(red,NULL,16); - rgb_result->RGB_green = (uint8_t) strtol(green,NULL,16); - rgb_result->RGB_blue = (uint8_t) strtol(blue,NULL,16); + rgb_result->RGB_red = (uint8_t)strtol(red, NULL, 16); + rgb_result->RGB_green = (uint8_t)strtol(green, NULL, 16); + rgb_result->RGB_blue = (uint8_t)strtol(blue, NULL, 16); return 0; } \ No newline at end of file diff --git a/firmware/Core/Src/unit_tests/unit_test_inventory.c b/firmware/Core/Src/unit_tests/unit_test_inventory.c index 62f3579..2b2ac5f 100644 --- a/firmware/Core/Src/unit_tests/unit_test_inventory.c +++ b/firmware/Core/Src/unit_tests/unit_test_inventory.c @@ -1,6 +1,7 @@ #include "unit_tests/unit_test_helpers.h" #include "unit_tests/unit_test_inventory.h" #include "unit_tests/unit_test_pwm.h" +#include "unit_tests/unit_test_rgb.h" // extern const TEST_Definition_t TEST_definitions[] = { @@ -9,6 +10,10 @@ const TEST_Definition_t TEST_definitions[] = { .test_file = "unit_tests/unit_test_pwm", .test_func_name = "LED_set_dimming"}, + {.test_func = TEST_EXEC__RGB_convert_hex_to_rgb, + .test_file = "unit_tests/unit_test_rgb", + .test_func_name = "RGB_convert_hex_to_rgb"}, + }; // extern diff --git a/firmware/Core/Src/unit_tests/unit_test_rgb.c b/firmware/Core/Src/unit_tests/unit_test_rgb.c new file mode 100644 index 0000000..7ce0081 --- /dev/null +++ b/firmware/Core/Src/unit_tests/unit_test_rgb.c @@ -0,0 +1,44 @@ +#include "unit_tests/unit_test_helpers.h" +#include "unit_tests/unit_test_rgb.h" +#include "rgb.h" + +#include + +uint8_t TEST_EXEC__RGB_convert_hex_to_rgb() +{ + char hex_color_code[10] = "\0"; + LED_RGB_t rgb_result; + uint8_t status; + + // Fail Criteria + + // Empty string + status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result); + TEST_ASSERT_TRUE(status == 1); + + // Missing "#" + strcpy(hex_color_code, "FFAABB"); + status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result); + TEST_ASSERT_TRUE(status == 2); + + // Invalid Length - Missing hex value + strcpy(hex_color_code, "#FFAAB"); + status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result); + TEST_ASSERT_TRUE(status == 3); + + // Invalid Hex Value + strcpy(hex_color_code, "#K2AABB"); + status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result); + TEST_ASSERT_TRUE(status == 4); + + // Pass Criteria + strcpy(hex_color_code, "#D2AABB"); + status = RGB_convert_hex_to_rgb(hex_color_code, &rgb_result); + TEST_ASSERT_TRUE(status == 0); + + TEST_ASSERT_TRUE(rgb_result.RGB_red == 0xD2); + TEST_ASSERT_TRUE(rgb_result.RGB_green == 0xAA); + TEST_ASSERT_TRUE(rgb_result.RGB_blue == 0xBB); + + return 0; +} From 58dec44aee4a45cb1d188b14ff2725a46749a097 Mon Sep 17 00:00:00 2001 From: christopherkinyua Date: Thu, 31 Oct 2024 02:41:19 -0600 Subject: [PATCH 10/10] Added unit test for function_arg_helpers --- .../unit_test_function_arg_helpers.h | 8 +++++ firmware/Core/Src/function_arg_helpers.c | 16 ++++++---- .../unit_test_function_arg_helpers.c | 29 +++++++++++++++++++ .../Core/Src/unit_tests/unit_test_inventory.c | 5 ++++ 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 firmware/Core/Inc/unit_tests/unit_test_function_arg_helpers.h create mode 100644 firmware/Core/Src/unit_tests/unit_test_function_arg_helpers.c diff --git a/firmware/Core/Inc/unit_tests/unit_test_function_arg_helpers.h b/firmware/Core/Inc/unit_tests/unit_test_function_arg_helpers.h new file mode 100644 index 0000000..a691a26 --- /dev/null +++ b/firmware/Core/Inc/unit_tests/unit_test_function_arg_helpers.h @@ -0,0 +1,8 @@ +#ifndef __INCLUDE_GUARD__UNIT_TEST_FUNCTION_ARG_HELPERS_H__ +#define __INCLUDE_GUARD__UNIT_TEST_FUNCTION_ARG_HELPERS_H__ + +#include + +uint8_t TEST__is_valid_hex_string(); + +#endif // __INCLUDE_GUARD__UNIT_TEST_FUNCTION_ARG_HELPERS_H__ \ No newline at end of file diff --git a/firmware/Core/Src/function_arg_helpers.c b/firmware/Core/Src/function_arg_helpers.c index 265936d..7b11904 100644 --- a/firmware/Core/Src/function_arg_helpers.c +++ b/firmware/Core/Src/function_arg_helpers.c @@ -1,4 +1,4 @@ -#include "function_arg_helpers.h" +#include "function_arg_helpers.h" #include #include @@ -8,18 +8,22 @@ /// @param hex_string - The HEX string to be checked (const char *) /// @param string_len - The length of the passed string to be checked (uint8_t) /// @return 0 on success, > 0 on error -uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len){ +uint8_t is_valid_hex_string(const char *hex_string, uint8_t string_len) +{ - if (hex_string == NULL || string_len == 0 || *hex_string == '\0') { + if (hex_string == NULL || string_len == 0 || *hex_string == '\0') + { // Error if string and string_len is NULL or empty - return 1; + return 1; } - for(uint8_t i = 0; i < string_len; i++){ + for (uint8_t i = 0; i < string_len; i++) + { char character = *hex_string; // Checking if the character is between 0-9, A-F, or a-f - if(!(isdigit(character) || (toupper(character) >= 'A' && toupper(character) <= 'F'))){ + if (!(isdigit(character) || (toupper(character) >= 'A' && toupper(character) <= 'F'))) + { // Error: Invalid hex character within the string return 2; } diff --git a/firmware/Core/Src/unit_tests/unit_test_function_arg_helpers.c b/firmware/Core/Src/unit_tests/unit_test_function_arg_helpers.c new file mode 100644 index 0000000..ef76090 --- /dev/null +++ b/firmware/Core/Src/unit_tests/unit_test_function_arg_helpers.c @@ -0,0 +1,29 @@ +#include "unit_tests/unit_test_helpers.h" +#include "unit_tests/unit_test_function_arg_helpers.h" +#include "function_arg_helpers.h" + +#include + +uint8_t TEST__is_valid_hex_string() +{ + char hex_color_code[20] = "\0"; + uint8_t status; + + // Fail Criteria + + // Empty string + status = is_valid_hex_string(hex_color_code, strlen(hex_color_code)); + TEST_ASSERT_TRUE(status == 1); + + // Invalid Hex Value ie "#" is not a hex value + strcpy(hex_color_code, "#FFAABB"); + status = is_valid_hex_string(hex_color_code, strlen(hex_color_code)); + TEST_ASSERT_TRUE(status == 2); + + // Pass Criteria + strcpy(hex_color_code, "1234567890ABCDEF"); + status = is_valid_hex_string(hex_color_code, strlen(hex_color_code)); + TEST_ASSERT_TRUE(status == 0); + + return 0; +} diff --git a/firmware/Core/Src/unit_tests/unit_test_inventory.c b/firmware/Core/Src/unit_tests/unit_test_inventory.c index 2b2ac5f..119f764 100644 --- a/firmware/Core/Src/unit_tests/unit_test_inventory.c +++ b/firmware/Core/Src/unit_tests/unit_test_inventory.c @@ -2,10 +2,15 @@ #include "unit_tests/unit_test_inventory.h" #include "unit_tests/unit_test_pwm.h" #include "unit_tests/unit_test_rgb.h" +#include "unit_tests/unit_test_function_arg_helpers.h" // extern const TEST_Definition_t TEST_definitions[] = { + {.test_func = TEST__is_valid_hex_string, + .test_file = "unit_tests/unit_test_function_arg_helpers", + .test_func_name = "is_valid_hex_string"}, + {.test_func = TEST_EXEC__LED_set_dimming, .test_file = "unit_tests/unit_test_pwm", .test_func_name = "LED_set_dimming"},