88#define PokemonAutomation_Controllers_JoystickTools_H
99
1010#include < stdint.h>
11+ #include < algorithm>
1112
1213namespace PokemonAutomation {
1314namespace JoystickTools {
@@ -23,45 +24,33 @@ inline double linear_u8_to_float(uint8_t x){
2324}
2425inline uint8_t linear_float_to_u8 (double f){
2526 if (f <= 0 ){
26- if (f <= -1 ){
27- return 0 ;
28- }
27+ f = std::max<double >(f, -1 );
2928 f = f * 128 + 128 ;
3029 return (uint8_t )(f + 0.5 );
3130 }else {
32- if (f >= 1 ){
33- return 255 ;
34- }
31+ f = std::min<double >(f, +1 );
3532 f = f * 127 + 128 ;
3633 return (uint8_t )(f + 0.5 );
3734 }
3835}
3936inline int16_t linear_float_to_s16 (double f){
4037 if (f <= 0 ){
41- if (f <= -1 ){
42- return -32768 ;
43- }
38+ f = std::max<double >(f, -1 );
4439 f = f * 32768 ;
4540 return (int16_t )(f + 0.5 );
4641 }else {
47- if (f >= 1 ){
48- return 32767 ;
49- }
42+ f = std::min<double >(f, +1 );
5043 f = f * 32767 ;
5144 return (int16_t )(f + 0.5 );
5245 }
5346}
5447inline uint16_t linear_float_to_u16 (double f){
5548 if (f <= 0 ){
56- if (f <= -1 ){
57- return 0 ;
58- }
49+ f = std::max<double >(f, -1 );
5950 f = f * 32768 + 32768 ;
6051 return (uint16_t )(f + 0.5 );
6152 }else {
62- if (f >= 1 ){
63- return 65535 ;
64- }
53+ f = std::min<double >(f, +1 );
6554 f = f * 32767 + 32768 ;
6655 return (uint16_t )(f + 0.5 );
6756 }
@@ -70,16 +59,14 @@ inline uint16_t linear_float_to_u12(double lo, double hi, double f){
7059 if (f == 0 ){
7160 return 2048 ;
7261 }else if (f <= 0 ){
73- if (f <= -1 ){
74- return 0 ;
75- }
62+ f = std::max<double >(f, -1 );
63+ // if (f < -1){ return 0; }
7664 f = f * (hi - lo) - lo;
7765 f = f * 2048 + 2048 ;
7866 return (uint16_t )(f + 0.5 );
7967 }else {
80- if (f >= 1 ){
81- return 4095 ;
82- }
68+ f = std::min<double >(f, +1 );
69+ // if (f > 1){ return 4095; }
8370 f = f * (hi - lo) + lo;
8471 f = f * 2047 + 2048 ;
8572 return (uint16_t )(f + 0.5 );
0 commit comments