Test the set param mode

Dependencies:   FastPWM3

Committer:
eatinghsieh
Date:
Fri Mar 26 05:28:00 2021 +0000
Revision:
55:6ab83cbbbca7
Parent:
47:e1196a851f76
Test set parameter mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 20:bf9ea5125d52 1
benkatz 47:e1196a851f76 2 #include "../math_ops.h"
benkatz 20:bf9ea5125d52 3
benkatz 20:bf9ea5125d52 4
benkatz 20:bf9ea5125d52 5 float fmaxf(float x, float y){
benkatz 25:f5741040c4bb 6 /// Returns maximum of x, y ///
benkatz 20:bf9ea5125d52 7 return (((x)>(y))?(x):(y));
benkatz 20:bf9ea5125d52 8 }
benkatz 20:bf9ea5125d52 9
benkatz 20:bf9ea5125d52 10 float fminf(float x, float y){
benkatz 25:f5741040c4bb 11 /// Returns minimum of x, y ///
benkatz 20:bf9ea5125d52 12 return (((x)<(y))?(x):(y));
benkatz 20:bf9ea5125d52 13 }
benkatz 20:bf9ea5125d52 14
benkatz 20:bf9ea5125d52 15 float fmaxf3(float x, float y, float z){
benkatz 25:f5741040c4bb 16 /// Returns maximum of x, y, z ///
benkatz 20:bf9ea5125d52 17 return (x > y ? (x > z ? x : z) : (y > z ? y : z));
benkatz 20:bf9ea5125d52 18 }
benkatz 20:bf9ea5125d52 19
benkatz 20:bf9ea5125d52 20 float fminf3(float x, float y, float z){
benkatz 25:f5741040c4bb 21 /// Returns minimum of x, y, z ///
benkatz 20:bf9ea5125d52 22 return (x < y ? (x < z ? x : z) : (y < z ? y : z));
benkatz 20:bf9ea5125d52 23 }
benkatz 34:51647c6c500d 24
benkatz 34:51647c6c500d 25 float roundf(float x){
benkatz 34:51647c6c500d 26 /// Returns nearest integer ///
benkatz 34:51647c6c500d 27
benkatz 34:51647c6c500d 28 return x < 0.0f ? ceilf(x - 0.5f) : floorf(x + 0.5f);
benkatz 34:51647c6c500d 29 }
benkatz 20:bf9ea5125d52 30
benkatz 20:bf9ea5125d52 31 void limit_norm(float *x, float *y, float limit){
benkatz 25:f5741040c4bb 32 /// Scales the lenght of vector (x, y) to be <= limit ///
benkatz 20:bf9ea5125d52 33 float norm = sqrt(*x * *x + *y * *y);
benkatz 20:bf9ea5125d52 34 if(norm > limit){
benkatz 20:bf9ea5125d52 35 *x = *x * limit/norm;
benkatz 20:bf9ea5125d52 36 *y = *y * limit/norm;
benkatz 20:bf9ea5125d52 37 }
benkatz 20:bf9ea5125d52 38 }
benkatz 47:e1196a851f76 39
benkatz 47:e1196a851f76 40 void limit(float *x, float min, float max){
benkatz 47:e1196a851f76 41 *x = fmaxf(fminf(*x, max), min);
benkatz 47:e1196a851f76 42 }
benkatz 26:2b865c00d7e9 43
benkatz 26:2b865c00d7e9 44 int float_to_uint(float x, float x_min, float x_max, int bits){
benkatz 47:e1196a851f76 45 /// Converts a float to an unsigned int, given range and number of bits ///
benkatz 26:2b865c00d7e9 46 float span = x_max - x_min;
benkatz 26:2b865c00d7e9 47 float offset = x_min;
benkatz 47:e1196a851f76 48 return (int) ((x-offset)*((float)((1<<bits)-1))/span);
benkatz 26:2b865c00d7e9 49 }
benkatz 26:2b865c00d7e9 50
benkatz 47:e1196a851f76 51
benkatz 26:2b865c00d7e9 52 float uint_to_float(int x_int, float x_min, float x_max, int bits){
benkatz 47:e1196a851f76 53 /// converts unsigned int to float, given range and number of bits ///
benkatz 26:2b865c00d7e9 54 float span = x_max - x_min;
benkatz 26:2b865c00d7e9 55 float offset = x_min;
benkatz 26:2b865c00d7e9 56 return ((float)x_int)*span/((float)((1<<bits)-1)) + offset;
benkatz 26:2b865c00d7e9 57 }