Skip to content

Commit

Permalink
Feature/getset bits (#211)
Browse files Browse the repository at this point in the history
* #173: casual lowpass filter

* #173: finalized file location

* #200: created macros for adding and setting bits

* #200: changed macro name to avoid redefinition error, and added if else clause for bit overflow

* #200 : moved endif to end of file

* #200 : pulled changes and fixed formatting issue

* Delete middleware/simple_ema.c
  • Loading branch information
DimitryP6 authored Jan 14, 2025
1 parent 94c5d89 commit 07a22e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
47 changes: 29 additions & 18 deletions middleware/include/c_utils.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#ifndef C_UTILS
#define C_UTILS

/*
* Will retrieve the container of a certain pointer given the container type and
* its pointer Trick pulled from Linux Kernel
*/
#define CONTAINER_OF(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))

#endif /* C_UTILS */

void endian_swap(void *ptr, int size);

/// @brief Reverses the bit order of a byte
/// @param b byte
/// @return the same byte but wuth the bits reversed
unsigned char reverse_bits(unsigned char b);
#ifndef C_UTILS
#define C_UTILS

/*
* Will retrieve the container of a certain pointer given the container type and
* its pointer Trick pulled from Linux Kernel
*/
#define CONTAINER_OF(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))

/*
* Gets a bit of a binary number at desired location
*/
#define NER_GET_BIT(num, bit) ((num >> bit) & 1)

/*
* Sets a bit of a binary number at desired location
*/
#define NER_SET_BIT(num, bit) \
bit < (sizeof(num) * 8) ? (num |= (1UL << bit)) : num

void endian_swap(void *ptr, int size);

/// @brief Reverses the bit order of a byte
/// @param b byte
/// @return the same byte but wuth the bits reversed
unsigned char reverse_bits(unsigned char b);

#endif /* C_UTILS */
38 changes: 19 additions & 19 deletions middleware/src/c_utils.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "c_utils.h"

void endian_swap(void *ptr, int size)
{
char *p = (char *)ptr;
char tmp;
for (int i = 0; i < size / 2; i++) {
tmp = p[i];
p[i] = p[size - i - 1];
p[size - i - 1] = tmp;
}
}

unsigned char reverse_bits(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
#include "c_utils.h"

void endian_swap(void *ptr, int size)
{
char *p = (char *)ptr;
char tmp;
for (int i = 0; i < size / 2; i++) {
tmp = p[i];
p[i] = p[size - i - 1];
p[size - i - 1] = tmp;
}
}

unsigned char reverse_bits(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}

0 comments on commit 07a22e3

Please sign in to comment.