Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/highpass #206

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 44 additions & 20 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include <stdint.h>

/*
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PCA changes shouldn't be in this PR

*/

/// Possible I2C addresses, see comment below
Expand Down Expand Up @@ -36,31 +35,56 @@ PCA 9539 16 bit GPIO expander. Datasheet:
#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 {
I2C_HandleTypeDef *i2c_handle;
//int i2c_handler;
//void *i2c_handler;
//I2C_HandleTypeDef *i2c_handle;

WritePtr write;
ReadPtr read;

uint16_t dev_addr;
} pca9539_t;

/// Init PCA9539, a 16 bit I2C GPIO expander
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle,
void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc,
uint8_t dev_addr);

/// @brief Read all pins on a bus, for example using reg_type input to get
/// incoming logic level
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);

/// @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
121 changes: 102 additions & 19 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
// 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);
//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);
}

HAL_StatusTypeDef pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
/*IGNORE THIS CODE - LEFT AS A REFERENCE
HAL_StatusTypeDef pca_write_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);
// 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);
}

void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle,
/* 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_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;
}

/*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;
}

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);
}

HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type,
uint8_t *buf)
//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;
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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);
}
*/
25 changes: 25 additions & 0 deletions middleware/include/high_pass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef HIGH_PASS_H
#define HIGH_PASS_H

#include <stdio.h>

typedef struct {
//Function Parameters
float alpha;
float scale;

float prev_output;

} high_pass_st;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention when defining a type (atleast in our codebases) is to append _t to the type, so this should be high_pass_t


void high_pass_init(float alpha, float scale, high_pass_st filter);
/**
@brief Initialization for high pass values
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doxygen comments go above the function, and explain the parameters and what the function returns (if it returns anything)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fill in doc comments


float high_pass(high_pass_st filter, float input);
/**
@brief Function for high pass filter
*/

#endif
43 changes: 43 additions & 0 deletions middleware/src/high_pass.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete notes from this if they won't be relevant to future programmers.

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)
{
filter->alpha = alpha;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect syntax and will not compile

filter->scale = scale;

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 output =
filter->scale(input - (filter->alpha * input +
(1 - filter->alpha) * prev_output));

filter->prev_output =
filter->alpha * input + (1 - filter->alpha) * prev_output;

return output;
}
Loading