34 lines
890 B
C
34 lines
890 B
C
/*
|
||
* math.c
|
||
*
|
||
* Created on: 2 февр. 2026 г.
|
||
* Author: v0stap
|
||
*/
|
||
#include "main.h"
|
||
|
||
uint16_t map16_t(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) {
|
||
// Input validation: avoid division by zero
|
||
if (in_max == in_min) {
|
||
fprintf(stderr, "Error: Input range cannot be zero.\n");
|
||
return out_min;
|
||
} else {
|
||
// Perform mapping
|
||
return (uint16_t)(out_min + (x - in_min) * (out_max - out_min) / (in_max - in_min));
|
||
}
|
||
}
|
||
|
||
uint16_t LPF(unsigned short lpf_c, unsigned short value, unsigned short old_value) {
|
||
// Averageing filtering
|
||
|
||
float tmp;
|
||
|
||
tmp = ((float) (value - old_value) * ((float) (lpf_c) / (float) 1000.0)); // filter
|
||
if (tmp > 0)
|
||
tmp += (float) 0.5; // roundup
|
||
else
|
||
tmp -= (float) 0.5;
|
||
|
||
return (uint16_t)((signed int) old_value + (signed int) tmp);
|
||
|
||
}
|