From 4e9db2f09e2db55b1c7038745beeb538280c9989 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 10 Nov 2024 16:35:08 -0500 Subject: [PATCH 01/10] changes finalized --- general/include/pca9539.h | 68 ++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 72a392a..75f8766 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -5,8 +5,7 @@ #include /* -PCA 9539 16 bit GPIO expander. Datasheet: -https://www.ti.com/lit/ds/symlink/pca9539.pdf?ts=1716785085909 +PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca9539.pdf?ts=1716785085909 */ /// Possible I2C addresses, see comment below @@ -36,31 +35,56 @@ PCA 9539 16 bit GPIO expander. Datasheet: #define PCA_DIRECTION_0_REG 0x06 #define PCA_DIRECTION_1_REG 0x07 -typedef struct { - I2C_HandleTypeDef *i2c_handle; +//return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, +//I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); + +typedef int (*WritePtr)(void *handler, uint16_t dev_addr, uint16_t mem_address, + uint16_t mem_add_size, uint8_t *data, uint16_t size, + int delay); +typedef int (*ReadPtr)(void *handler, uint16_t dev_addr, uint16_t mem_address, + uint16_t mem_add_size, uint8_t *data, uint16_t size, + int delay); + +{ + //int i2c_handler; + void *i2c_handler; + //I2C_HandleTypeDef *i2c_handle; + + WritePtr write; + ReadPtr read; + uint16_t dev_addr; -} pca9539_t; +} +pca9539_t; + +void pca9539_init(pca9539_t *pca, void *i2c_handler, uint8_t dev_addr); + +int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf); +int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t *buf); + +int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf); + +int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t buf); + +/*IGNORE THIS CODE - SERVES AS A REFERENCE /// Init PCA9539, a 16 bit I2C GPIO expander -void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, - uint8_t dev_addr); +void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr); -/// @brief Read all pins on a bus, for example using reg_type input to get -/// incoming logic level +/// @brief Read all pins on a bus, for example using reg_type input to get incoming logic level HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t *buf); -/// @brief Read a specific pin on a bus, do not iterate over this, use read_pins -/// instead + uint8_t *buf); +/// @brief Read a specific pin on a bus, do not iterate over this, use read_pins instead HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, - uint8_t pin, uint8_t *buf); - -/// @brief Write all pins on a bus, for example using reg_type OUTPUT to set -/// logic level or DIRECTION to set as output -HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t buf); -/// @brief Write a specific pin on a bus, do not iterate over this, use -/// write_pins instead -HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, - uint8_t pin, uint8_t buf); + uint8_t pin, uint8_t *buf); +/// @brief Write all pins on a bus, for example using reg_type OUTPUT to set logic level or DIRECTION to set as +/// output +HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf); +/// @brief Write a specific pin on a bus, do not iterate over this, use write_pins instead +HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t buf); +*/ #endif \ No newline at end of file From e5441af497302ddd038d163c8f2b849778d68b64 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 10 Nov 2024 16:35:54 -0500 Subject: [PATCH 02/10] finalized 2 --- general/src/pca9539.c | 121 +++++++++++++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 19 deletions(-) diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 6e62f2f..42810ca 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -4,30 +4,112 @@ #define REG_SIZE_BITS 8 -HAL_StatusTypeDef pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) +//RETURNS THE WRITE POINTER INSTEAD +int pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) +{ + //Parameters in the HAL Function + uint16_t mem_add_size = 8; + uint16_t data_size = 1; + int delay = 0xFFFFFFFFU; + + return pca->write(pca->i2c_handler, pca->dev_addr, address, + mem_add_size, data, data_size, delay); +} + +/*IGNORE THIS CODE - LEFT AS A REFERENCE +HAL_StatusTypeDef pca_write_reg(pca9539_t* pca, uint16_t address, uint8_t* data) { // ensure shifting left one, HAL adds the write bit - return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, - I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); + return HAL_I2C_Mem_Write(pca->i2c_handler, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1, + HAL_MAX_DELAY); +}*/ + +int pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) +{ + uint16_t mem_add_size = 8; + uint16_t data_size = 1; + int delay = 0xFFFFFFFFU; + + return pca->read(pca->i2c_handler, pca->dev_addr, address, mem_add_size, + data, data_size, delay); } -HAL_StatusTypeDef pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) +/* IGNORE THIS CODE - LEFT AS A REFERENCE +HAL_StatusTypeDef pca_read_reg(pca9539_t* pca, uint16_t address, uint8_t* data) { - return HAL_I2C_Mem_Read(pca->i2c_handle, pca->dev_addr, address, - I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); + + return HAL_I2C_Mem_Read(pca->i2c_handler, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1, + HAL_MAX_DELAY); +}*/ + +//Intializes the struct +void pca9539_init(pca9539_t *pca, void *i2c_handler, WritePtr writeFunc, + ReadPtr readFunc, uint8_t dev_addr) +{ + pca->i2c_handler = i2c_handler; + pca->dev_addr = dev_addr << 1u; + + pca->write = writeFunc; + pca->read = readFunc; } -void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, - uint8_t dev_addr) +/*IGNORE THIS CODE - LEFT AS A REFERENCE +void pca9539_init(pca9539_t* pca, I2C_HandleTypeDef* i2c_handle, uint8_t dev_addr) { pca->i2c_handle = i2c_handle; - pca->dev_addr = dev_addr - << 1u; /* shifted one to the left cuz STM says so */ + pca->dev_addr = dev_addr << 1u; shifted one to the left cuz STM says so +} +*/ +int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf) +{ + int status = pca_read_reg(pca, reg_type, buf); + if (status) { + return status; + } + + return status; +} + +int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t *buf) +{ + uint8_t data; + int status = pca_read_reg(pca, reg_type, &data); + if (status) { + return status; + } + + *buf = (data & (1 << pin)) > 0; + + return status; } -HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t *buf) +int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf) { + return pca_write_reg(pca, reg_type, &buf); +} + +int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t buf) +{ + uint8_t data; + uint8_t data_new; + + int status = pca_read_reg(pca, reg_type, &data); + if (status) { + return status; + } + + data_new = (data & ~(1u << pin)) | (buf << pin); + + return pca_write_reg(pca, reg_type, &data_new); +} + +//IGNORE THIS CODE - JUST LEFT AS REFERENCE, WILL DELETE ONCE APPROVED +/* +HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* buf) +{ + HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, buf); if (status) { return status; @@ -36,8 +118,7 @@ HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, return status; } -HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, - uint8_t pin, uint8_t *buf) +HAL_StatusTypeDef pca9539_read_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t* buf) { uint8_t data; HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); @@ -49,16 +130,17 @@ HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, return status; } +*/ -HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t buf) +/* +HAL_StatusTypeDef pca9539_write_reg(pca9539_t* pca, uint8_t reg_type, uint8_t buf) { + return pca_write_reg(pca, reg_type, &buf); } - -HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, - uint8_t pin, uint8_t buf) +HAL_StatusTypeDef pca9539_write_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t buf) { + uint8_t data; uint8_t data_new; @@ -71,3 +153,4 @@ HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, return pca_write_reg(pca, reg_type, &data_new); } +*/ \ No newline at end of file From 824f3efc12d3d7c667c8c1ae9459c60002a00c8e Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 10 Nov 2024 16:40:16 -0500 Subject: [PATCH 03/10] struct fix --- general/include/pca9539.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 75f8766..fb2a5de 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -45,7 +45,7 @@ typedef int (*ReadPtr)(void *handler, uint16_t dev_addr, uint16_t mem_address, uint16_t mem_add_size, uint8_t *data, uint16_t size, int delay); -{ +typedef struct { //int i2c_handler; void *i2c_handler; //I2C_HandleTypeDef *i2c_handle; @@ -54,8 +54,7 @@ typedef int (*ReadPtr)(void *handler, uint16_t dev_addr, uint16_t mem_address, ReadPtr read; uint16_t dev_addr; -} -pca9539_t; +} pca9539_t; void pca9539_init(pca9539_t *pca, void *i2c_handler, uint8_t dev_addr); From dbf535813b9028ed8c459dff2c94ca7d3b2b4294 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 10 Nov 2024 16:42:21 -0500 Subject: [PATCH 04/10] final final --- general/include/pca9539.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index fb2a5de..bbd6878 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -56,7 +56,8 @@ typedef struct { uint16_t dev_addr; } pca9539_t; -void pca9539_init(pca9539_t *pca, void *i2c_handler, uint8_t dev_addr); +void pca9539_init(pca9539_t *pca, void *i2c_handler, WritePtr writeFunc, + ReadPtr readFunc, uint8_t dev_addr); int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf); From c526b7c568a803af3d14fdf827851e3eafeb972a Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 10 Nov 2024 17:05:38 -0500 Subject: [PATCH 05/10] removing the i2c_handler, preping for cerb changes --- general/include/pca9539.h | 10 +++++----- general/src/pca9539.c | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index bbd6878..a75fde2 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -38,16 +38,16 @@ PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca //return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, //I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); -typedef int (*WritePtr)(void *handler, uint16_t dev_addr, uint16_t mem_address, +typedef int (*WritePtr)(uint16_t dev_addr, uint16_t mem_address, uint16_t mem_add_size, uint8_t *data, uint16_t size, int delay); -typedef int (*ReadPtr)(void *handler, uint16_t dev_addr, uint16_t mem_address, +typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t mem_address, uint16_t mem_add_size, uint8_t *data, uint16_t size, int delay); typedef struct { //int i2c_handler; - void *i2c_handler; + //void *i2c_handler; //I2C_HandleTypeDef *i2c_handle; WritePtr write; @@ -56,8 +56,8 @@ typedef struct { uint16_t dev_addr; } pca9539_t; -void pca9539_init(pca9539_t *pca, void *i2c_handler, WritePtr writeFunc, - ReadPtr readFunc, uint8_t dev_addr); +void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc, + uint8_t dev_addr); int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf); diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 42810ca..8fb578f 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -12,8 +12,8 @@ int pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) uint16_t data_size = 1; int delay = 0xFFFFFFFFU; - return pca->write(pca->i2c_handler, pca->dev_addr, address, - mem_add_size, data, data_size, delay); + return pca->write(pca->dev_addr, address, mem_add_size, data, data_size, + delay); } /*IGNORE THIS CODE - LEFT AS A REFERENCE @@ -30,8 +30,8 @@ int pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) uint16_t data_size = 1; int delay = 0xFFFFFFFFU; - return pca->read(pca->i2c_handler, pca->dev_addr, address, mem_add_size, - data, data_size, delay); + return pca->read(pca->dev_addr, address, mem_add_size, data, data_size, + delay); } /* IGNORE THIS CODE - LEFT AS A REFERENCE @@ -43,10 +43,10 @@ HAL_StatusTypeDef pca_read_reg(pca9539_t* pca, uint16_t address, uint8_t* data) }*/ //Intializes the struct -void pca9539_init(pca9539_t *pca, void *i2c_handler, WritePtr writeFunc, - ReadPtr readFunc, uint8_t dev_addr) +void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc, + uint8_t dev_addr) { - pca->i2c_handler = i2c_handler; + //pca->i2c_handler = i2c_handler; pca->dev_addr = dev_addr << 1u; pca->write = writeFunc; From bab8efec1b363114247320461455f2c2010c92f5 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 24 Nov 2024 14:42:40 -0500 Subject: [PATCH 06/10] high pass filter implementation --- middleware/include/high_pass.h | 8 ++++++ middleware/src/high_pass.c | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 middleware/include/high_pass.h create mode 100644 middleware/src/high_pass.c diff --git a/middleware/include/high_pass.h b/middleware/include/high_pass.h new file mode 100644 index 0000000..8abc59b --- /dev/null +++ b/middleware/include/high_pass.h @@ -0,0 +1,8 @@ +#ifndef HIGH_PASS_H +#define HIGH_PASS_H + +#include + +int high_pass(int alpha, int scale); + +#endif diff --git a/middleware/src/high_pass.c b/middleware/src/high_pass.c new file mode 100644 index 0000000..b5e6b5d --- /dev/null +++ b/middleware/src/high_pass.c @@ -0,0 +1,52 @@ +/* +Understanding High-Pass Filter Implementation + +Goal is to customize the cutoff frequency/strength + +One style: +-alpha factor -> smoothing factor used to make the changes smoother +-scale factor -> scales the output given by algo +these two params correlate to strength + +Or user could pass desired cutoff frequency and that would be used + +Algorithm: +1. first possibility +output = alpha * last_output + (1-alpha) * input + +output = alpha * last_output + alpha * (avg - prev_avg) + +*/ +#include "high_pass.h" + +//Samples can be modified +#define SAMPLES 12 + +//Scale Var allows adjustment of output strength +//Alpha Var controls weight of prev input & sum difference +float high_pass(int alpha, int scale) +{ + static float buffer[SAMPLES] = { 0 }; //initialize changing buffer + static int index = 0; + static float prev_sum = 0.0; + + //buffer[index] = freq; //-> adding the freq input into the + + index = (index + 1) % SAMPLES; + + float sum = 0.0; + + //Iterates through all indices + for (int i = 0; i < SAMPLES; i++) { + sum += buffer[i]; + } + + //Algorithm + // + float output = + scale * (alpha * buffer[index - 1] + alpha * (sum - prev_sum)); + + prev_sum = sum; //updates the prev_sum + + return output; +} From 25814f70e88f4a03a93c7ef336fe15ef48bdf3e8 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 24 Nov 2024 17:38:02 -0500 Subject: [PATCH 07/10] new additions --- middleware/include/high_pass.h | 19 ++++++++++++++++- middleware/src/high_pass.c | 37 +++++++++++++--------------------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/middleware/include/high_pass.h b/middleware/include/high_pass.h index 8abc59b..1a397c8 100644 --- a/middleware/include/high_pass.h +++ b/middleware/include/high_pass.h @@ -3,6 +3,23 @@ #include -int high_pass(int alpha, int scale); +typedef struct { + //Function Parameters + float alpha; + float scale; + + float prev_output; + +} high_pass_st; + +void high_pass_init(float alpha, float scale, high_pass_st filter); +/** +@brief Initialization for high pass values + */ + +float high_pass(high_pass_st filter, float input); +/** +@brief Function for high pass filter + */ #endif diff --git a/middleware/src/high_pass.c b/middleware/src/high_pass.c index b5e6b5d..90cae67 100644 --- a/middleware/src/high_pass.c +++ b/middleware/src/high_pass.c @@ -17,36 +17,27 @@ output = alpha * last_output + (1-alpha) * input output = alpha * last_output + alpha * (avg - prev_avg) */ -#include "high_pass.h" -//Samples can be modified -#define SAMPLES 12 +#include "high_pass.h" -//Scale Var allows adjustment of output strength -//Alpha Var controls weight of prev input & sum difference -float high_pass(int alpha, int scale) +void high_pass_init(float alpha, float scale, high_pass_st filter) { - static float buffer[SAMPLES] = { 0 }; //initialize changing buffer - static int index = 0; - static float prev_sum = 0.0; - - //buffer[index] = freq; //-> adding the freq input into the - - index = (index + 1) % SAMPLES; + filter->alpha = alpha; + filter->scale = scale; - float sum = 0.0; - - //Iterates through all indices - for (int i = 0; i < SAMPLES; i++) { - sum += buffer[i]; - } + filter->prev_output = 0.0; +} - //Algorithm - // +//y[n]=x[n]−SMA[n] +//The output is equal to the input - the moving average +float high_pass(high_pass_st filter, float input) +{ float output = - scale * (alpha * buffer[index - 1] + alpha * (sum - prev_sum)); + filter->scale(input - (filter->alpha * input + + (1 - filter->alpha) * prev_output)); - prev_sum = sum; //updates the prev_sum + filter->prev_output = + filter->alpha * input + (1 - filter->alpha) * prev_output; return output; } From 389d3d1e5a871f9d1e4e107d693b4b7fff9ac789 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Wed, 27 Nov 2024 21:52:14 -0500 Subject: [PATCH 08/10] added doxygen comments, added proper syntax --- middleware/include/high_pass.h | 18 +++++++++++++----- middleware/src/high_pass.c | 26 ++------------------------ 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/middleware/include/high_pass.h b/middleware/include/high_pass.h index 1a397c8..d301ebb 100644 --- a/middleware/include/high_pass.h +++ b/middleware/include/high_pass.h @@ -10,16 +10,24 @@ typedef struct { float prev_output; -} high_pass_st; +} high_pass_t; -void high_pass_init(float alpha, float scale, high_pass_st filter); /** -@brief Initialization for high pass values + * @brief + * + * @param alpha + * @param scale + * @param filter */ +void high_pass_init(float alpha, float scale, high_pass_t *filter); -float high_pass(high_pass_st filter, float input); /** -@brief Function for high pass filter + * @brief + * + * @param filter + * @param input + * @return float */ +float high_pass(high_pass_t *filter, float input); #endif diff --git a/middleware/src/high_pass.c b/middleware/src/high_pass.c index 90cae67..c9bcb88 100644 --- a/middleware/src/high_pass.c +++ b/middleware/src/high_pass.c @@ -1,26 +1,6 @@ -/* -Understanding High-Pass Filter Implementation - -Goal is to customize the cutoff frequency/strength - -One style: --alpha factor -> smoothing factor used to make the changes smoother --scale factor -> scales the output given by algo -these two params correlate to strength - -Or user could pass desired cutoff frequency and that would be used - -Algorithm: -1. first possibility -output = alpha * last_output + (1-alpha) * input - -output = alpha * last_output + alpha * (avg - prev_avg) - -*/ - #include "high_pass.h" -void high_pass_init(float alpha, float scale, high_pass_st filter) +void high_pass_init(float alpha, float scale, high_pass_t *filter) { filter->alpha = alpha; filter->scale = scale; @@ -28,9 +8,7 @@ void high_pass_init(float alpha, float scale, high_pass_st filter) filter->prev_output = 0.0; } -//y[n]=x[n]−SMA[n] -//The output is equal to the input - the moving average -float high_pass(high_pass_st filter, float input) +float high_pass(high_pass_t *filter, float input) { float output = filter->scale(input - (filter->alpha * input + From d86aea266b43e1b39a5eaa5756c5d2ac43a590e7 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Wed, 27 Nov 2024 22:01:19 -0500 Subject: [PATCH 09/10] removed pca commits from this branch --- general/include/pca9539.h | 64 +++++++------------- general/src/pca9539.c | 123 +++++++------------------------------- 2 files changed, 40 insertions(+), 147 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index a75fde2..72a392a 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -5,7 +5,8 @@ #include /* -PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca9539.pdf?ts=1716785085909 +PCA 9539 16 bit GPIO expander. Datasheet: +https://www.ti.com/lit/ds/symlink/pca9539.pdf?ts=1716785085909 */ /// Possible I2C addresses, see comment below @@ -35,56 +36,31 @@ PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca #define PCA_DIRECTION_0_REG 0x06 #define PCA_DIRECTION_1_REG 0x07 -//return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, -//I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); - -typedef int (*WritePtr)(uint16_t dev_addr, uint16_t mem_address, - uint16_t mem_add_size, uint8_t *data, uint16_t size, - int delay); -typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t mem_address, - uint16_t mem_add_size, uint8_t *data, uint16_t size, - int delay); - typedef struct { - //int i2c_handler; - //void *i2c_handler; - //I2C_HandleTypeDef *i2c_handle; - - WritePtr write; - ReadPtr read; - + I2C_HandleTypeDef *i2c_handle; uint16_t dev_addr; } pca9539_t; -void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc, - uint8_t dev_addr); - -int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf); - -int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, - uint8_t *buf); - -int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf); - -int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, - uint8_t buf); - -/*IGNORE THIS CODE - SERVES AS A REFERENCE /// Init PCA9539, a 16 bit I2C GPIO expander -void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr); +void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, + uint8_t dev_addr); -/// @brief Read all pins on a bus, for example using reg_type input to get incoming logic level +/// @brief Read all pins on a bus, for example using reg_type input to get +/// incoming logic level HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t *buf); -/// @brief Read a specific pin on a bus, do not iterate over this, use read_pins instead + uint8_t *buf); +/// @brief Read a specific pin on a bus, do not iterate over this, use read_pins +/// instead HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, - uint8_t pin, uint8_t *buf); + uint8_t pin, uint8_t *buf); + +/// @brief Write all pins on a bus, for example using reg_type OUTPUT to set +/// logic level or DIRECTION to set as output +HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, + uint8_t buf); +/// @brief Write a specific pin on a bus, do not iterate over this, use +/// write_pins instead +HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, + uint8_t pin, uint8_t buf); -/// @brief Write all pins on a bus, for example using reg_type OUTPUT to set logic level or DIRECTION to set as -/// output -HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf); -/// @brief Write a specific pin on a bus, do not iterate over this, use write_pins instead -HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, - uint8_t buf); -*/ #endif \ No newline at end of file diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 8fb578f..6e85e52 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -4,112 +4,30 @@ #define REG_SIZE_BITS 8 -//RETURNS THE WRITE POINTER INSTEAD -int pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) -{ - //Parameters in the HAL Function - uint16_t mem_add_size = 8; - uint16_t data_size = 1; - int delay = 0xFFFFFFFFU; - - return pca->write(pca->dev_addr, address, mem_add_size, data, data_size, - delay); -} - -/*IGNORE THIS CODE - LEFT AS A REFERENCE -HAL_StatusTypeDef pca_write_reg(pca9539_t* pca, uint16_t address, uint8_t* data) +HAL_StatusTypeDef pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) { // ensure shifting left one, HAL adds the write bit - return HAL_I2C_Mem_Write(pca->i2c_handler, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1, - HAL_MAX_DELAY); -}*/ - -int pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) -{ - uint16_t mem_add_size = 8; - uint16_t data_size = 1; - int delay = 0xFFFFFFFFU; - - return pca->read(pca->dev_addr, address, mem_add_size, data, data_size, - delay); + return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, + I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); } -/* IGNORE THIS CODE - LEFT AS A REFERENCE -HAL_StatusTypeDef pca_read_reg(pca9539_t* pca, uint16_t address, uint8_t* data) +HAL_StatusTypeDef pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) { - - return HAL_I2C_Mem_Read(pca->i2c_handler, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1, - HAL_MAX_DELAY); -}*/ - -//Intializes the struct -void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc, - uint8_t dev_addr) -{ - //pca->i2c_handler = i2c_handler; - pca->dev_addr = dev_addr << 1u; - - pca->write = writeFunc; - pca->read = readFunc; + return HAL_I2C_Mem_Read(pca->i2c_handle, pca->dev_addr, address, + I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); } -/*IGNORE THIS CODE - LEFT AS A REFERENCE -void pca9539_init(pca9539_t* pca, I2C_HandleTypeDef* i2c_handle, uint8_t dev_addr) +void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, + uint8_t dev_addr) { pca->i2c_handle = i2c_handle; - pca->dev_addr = dev_addr << 1u; shifted one to the left cuz STM says so + pca->dev_addr = dev_addr + << 1u; /* shifted one to the left cuz STM says so */ } -*/ -int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf) -{ - int status = pca_read_reg(pca, reg_type, buf); - if (status) { - return status; - } - return status; -} - -int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, - uint8_t *buf) +HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, + uint8_t *buf) { - uint8_t data; - int status = pca_read_reg(pca, reg_type, &data); - if (status) { - return status; - } - - *buf = (data & (1 << pin)) > 0; - - return status; -} - -int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf) -{ - return pca_write_reg(pca, reg_type, &buf); -} - -int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, - uint8_t buf) -{ - uint8_t data; - uint8_t data_new; - - int status = pca_read_reg(pca, reg_type, &data); - if (status) { - return status; - } - - data_new = (data & ~(1u << pin)) | (buf << pin); - - return pca_write_reg(pca, reg_type, &data_new); -} - -//IGNORE THIS CODE - JUST LEFT AS REFERENCE, WILL DELETE ONCE APPROVED -/* -HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* buf) -{ - HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, buf); if (status) { return status; @@ -118,7 +36,8 @@ HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* bu return status; } -HAL_StatusTypeDef pca9539_read_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t* buf) +HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, + uint8_t pin, uint8_t *buf) { uint8_t data; HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); @@ -130,17 +49,16 @@ HAL_StatusTypeDef pca9539_read_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin return status; } -*/ -/* -HAL_StatusTypeDef pca9539_write_reg(pca9539_t* pca, uint8_t reg_type, uint8_t buf) +HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, + uint8_t buf) { - return pca_write_reg(pca, reg_type, &buf); } -HAL_StatusTypeDef pca9539_write_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t buf) -{ +HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, + uint8_t pin, uint8_t buf) +{ uint8_t data; uint8_t data_new; @@ -152,5 +70,4 @@ HAL_StatusTypeDef pca9539_write_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pi data_new = (data & ~(1u << pin)) | (buf << pin); return pca_write_reg(pca, reg_type, &data_new); -} -*/ \ No newline at end of file +} \ No newline at end of file From a21e9dbf384046b38fd1b4004ec1a53ac746f557 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Thu, 5 Dec 2024 13:15:01 -0500 Subject: [PATCH 10/10] added doxygen comments --- middleware/include/high_pass.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/middleware/include/high_pass.h b/middleware/include/high_pass.h index d301ebb..5c1cc4b 100644 --- a/middleware/include/high_pass.h +++ b/middleware/include/high_pass.h @@ -13,20 +13,20 @@ typedef struct { } high_pass_t; /** - * @brief + * @brief Initiailzing the high pass filter with filter coefficient & desired scale * - * @param alpha - * @param scale - * @param filter + * @param alpha filter coefficient controlling freq response + * @param scale desired scaling for filter + * @param filter pointer to a new high pass struct */ void high_pass_init(float alpha, float scale, high_pass_t *filter); /** - * @brief + * @brief Function applying filter to a new sample, returning the filtered output * - * @param filter - * @param input - * @return float + * @param filter passing pointer to initialized high pass struct + * @param input new sample to be filtered + * @return float Filtered & Scaled output value based on prev values */ float high_pass(high_pass_t *filter, float input);