Files
CAN-IO/Test2/Core/Src/math.c
v0stap bd00d0025d test
2026-04-03 16:34:46 +02:00

34 lines
890 B
C
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 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);
}