2 #include <crsf_protocol.h>
7 static uint16_t fmapf(uint16_t x
, float in_min
, float in_max
, float out_min
, float out_max
) { return round((x
- in_min
) * (out_max
- out_min
) / (in_max
- in_min
) + out_min
); };
9 void test_fmap_consistent_with_float(void)
11 for (int i
= 172; i
<= 1811; i
++)
13 uint16_t iv
= fmap(i
, 172, 1811, 0, 1023);
14 uint16_t fv
= fmapf(i
, 172, 1811, 0, 1023);
16 // assert that the integer function is equivalent to the floating point function
17 TEST_ASSERT_EQUAL(fv
, iv
);
21 void test_fmap_consistent_bider(void)
23 for (int i
= 172; i
<= 1811; i
++)
25 uint16_t iv
= fmap(i
, 172, 1811, 0, 1023);
26 uint16_t iiv
= fmap(iv
, 0, 1023, 172, 1811);
27 uint16_t fiv
= fmapf(iv
, 0, 1023, 172, 1811);
29 // make sure that integer and floats return the same values
30 TEST_ASSERT_EQUAL(fiv
, iiv
);
32 // make sure that the full cycle value is +/- 1 error given that the input range is under 2x the output range
33 TEST_ASSERT_GREATER_OR_EQUAL(i
-1, iiv
);
34 TEST_ASSERT_LESS_OR_EQUAL(i
+1, iiv
);
38 // Unity setup/teardown
42 int main(int argc
, char **argv
)
45 RUN_TEST(test_fmap_consistent_with_float
);
46 RUN_TEST(test_fmap_consistent_bider
);