Frequently used functions, algorithms, data structures ... in embedded C code.
In this repo, below things are taken into consideration.
- Embedded MCU often has little memory
- Data processed by MCU are often time series, which means data are feed one by one
- Embedded data structs are often small, so we use
uint8_t
as the default type for counter purpose instead of usingsize_t
.
Ifuint8_t
is too small for you, do not hesitate to copy/paste code and then replace the type to what you need.
Code convertions
- C99
- Use
jks_
,Jks
... as the name space - Use
uint8_t
if possible
Remane default-jks-cbed-config.h
as jks-cbed-config.h
and then dis/enable configs you (not) wanted.
- Increase: 0, 1, 2, ..., n-1, n, 0, 1, ...
- Decrease: n, n-1, ..., 3, 2, 1, 0, n, n-1, ...
- Increase: 0, 1, 2, ..., n-1, n, n, n, ...
- Decrease: n, n-1, ..., 3, 2, 1, 0, 0, 0, ...
- Header: jks-ring-buffer.h
- Head only
- Dependency: None
Ring buffer is very commonly used. But I do not think it is good to hardcode index updating.
Example:
#define N_BUFFER 55
uint8_t g_buffer[N_BUFFER] = {0};
struct jks_ring_buffer g_ring = {0};
void jks_ring_buffer_init(&g_ring, N_BUFFER);
uint8_t add_to_buffer(uint8_t new_value)
{
uint8_t eldest_value = g_buffer[g_ring.index];
g_buffer[g_ring.index] = new_value;
jks_ring_buffer_update_index(&g_ring);
return eldest_value;
}
- Header: jks-moving-average.h
- Src: jks-moving-average.c
- Dependency: Ring buffer
- Header: jks-moving-standard-deviation.h
- Src: jks-moving-standard-deviation.c
- Dependency: Ring buffer, Moving average