-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
executable file
·33 lines (29 loc) · 966 Bytes
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* EGG OpenSource EBike firmware
*
* Copyright (C) Casainho, 2015, 2106, 2017.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include "stm8s.h"
int32_t map (int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max)
{
// if input is smaller/bigger than expected return the min/max out ranges value
if (x < in_min)
return out_min;
else if (x > in_max)
return out_max;
// map the input to the output range.
// round up if mapping bigger ranges to smaller ranges
else if ((in_max - in_min) > (out_max - out_min))
return (x - in_min) * (out_max - out_min + 1) / (in_max - in_min + 1) + out_min;
// round down if mapping smaller ranges to bigger ranges
else
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
uint8_t ui8_min (uint8_t value_a, uint8_t value_b)
{
if (value_a < value_b) return value_a;
else return value_b;
}