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

#include <stdio.h>

int high_pass(int alpha, int scale);
Copy link
Contributor

Choose a reason for hiding this comment

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

There are no functions for the user to pass a value through the filter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Documentation for the function should be in the header file and use doxygen, javadoc style comments.


#endif
52 changes: 52 additions & 0 deletions middleware/src/high_pass.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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"

//Samples can be modified
#define SAMPLES 12
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this modifiable by the user. They should not have to edit code in embedded base. This also should be available to be edited on a per high-pass filter level.


//Scale Var allows adjustment of output strength
//Alpha Var controls weight of prev input & sum difference
float high_pass(int alpha, int scale)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these values be ints? Floats might also be useful here.

{
static float buffer[SAMPLES] = { 0 }; //initialize changing buffer
static int index = 0;
static float prev_sum = 0.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This implementation does not allow for using multiple filters at once.


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