init

Dependencies:   mbed C12832 EthernetInterface MQTT mbed-rtos picojson

Committer:
co838_mgl6
Date:
Thu May 05 12:05:16 2016 +0000
Revision:
1:1e45dd2c91fb
first step

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_mgl6 1:1e45dd2c91fb 1 //
co838_mgl6 1:1e45dd2c91fb 2 // Created by Marc Le Labourier
co838_mgl6 1:1e45dd2c91fb 3 // 14-Feb-16.
co838_mgl6 1:1e45dd2c91fb 4 //
co838_mgl6 1:1e45dd2c91fb 5
co838_mgl6 1:1e45dd2c91fb 6 #ifndef MATHALGO_H
co838_mgl6 1:1e45dd2c91fb 7 #define MATHALGO_H
co838_mgl6 1:1e45dd2c91fb 8
co838_mgl6 1:1e45dd2c91fb 9 #include <cmath>
co838_mgl6 1:1e45dd2c91fb 10 #include <sstream>
co838_mgl6 1:1e45dd2c91fb 11 #define VAR_PI 3.141592653589793238462643383279502884L /* pi */
co838_mgl6 1:1e45dd2c91fb 12 #define DIFF(x, y) (x - y)
co838_mgl6 1:1e45dd2c91fb 13 #define ABS(x) (x < 0 ? x * -1.0f : x)
co838_mgl6 1:1e45dd2c91fb 14
co838_mgl6 1:1e45dd2c91fb 15 /* Library of many useful peace of code */
co838_mgl6 1:1e45dd2c91fb 16
co838_mgl6 1:1e45dd2c91fb 17 enum Mode {
co838_mgl6 1:1e45dd2c91fb 18 Digital,
co838_mgl6 1:1e45dd2c91fb 19 Degree,
co838_mgl6 1:1e45dd2c91fb 20 Radian
co838_mgl6 1:1e45dd2c91fb 21 };
co838_mgl6 1:1e45dd2c91fb 22
co838_mgl6 1:1e45dd2c91fb 23 namespace mathalgo
co838_mgl6 1:1e45dd2c91fb 24 {
co838_mgl6 1:1e45dd2c91fb 25
co838_mgl6 1:1e45dd2c91fb 26 inline std::string modeToString(Mode m)
co838_mgl6 1:1e45dd2c91fb 27 {
co838_mgl6 1:1e45dd2c91fb 28 switch (m) {
co838_mgl6 1:1e45dd2c91fb 29 case Digital: return "DIGITAL";
co838_mgl6 1:1e45dd2c91fb 30 case Degree: return "DEGREE";
co838_mgl6 1:1e45dd2c91fb 31 case Radian: return "RADIAN";
co838_mgl6 1:1e45dd2c91fb 32 }
co838_mgl6 1:1e45dd2c91fb 33 return "DIGITAL";
co838_mgl6 1:1e45dd2c91fb 34 }
co838_mgl6 1:1e45dd2c91fb 35
co838_mgl6 1:1e45dd2c91fb 36 inline Mode modeFromString(const std::string& s)
co838_mgl6 1:1e45dd2c91fb 37 {
co838_mgl6 1:1e45dd2c91fb 38 if (s.compare("DEGREE") == 0)
co838_mgl6 1:1e45dd2c91fb 39 return Degree;
co838_mgl6 1:1e45dd2c91fb 40 else if (s.compare("RADIAN") == 0)
co838_mgl6 1:1e45dd2c91fb 41 return Radian;
co838_mgl6 1:1e45dd2c91fb 42 return Digital;
co838_mgl6 1:1e45dd2c91fb 43 }
co838_mgl6 1:1e45dd2c91fb 44
co838_mgl6 1:1e45dd2c91fb 45 // How to round a double to a given precision.
co838_mgl6 1:1e45dd2c91fb 46 // From: http://stackoverflow.com/questions/11208971/round-a-float-to-a-given-precision
co838_mgl6 1:1e45dd2c91fb 47 inline double pround(double x, int precision)
co838_mgl6 1:1e45dd2c91fb 48 {
co838_mgl6 1:1e45dd2c91fb 49 if (x == 0.)
co838_mgl6 1:1e45dd2c91fb 50 return x;
co838_mgl6 1:1e45dd2c91fb 51 int ex = (int)std::floor(std::log10(std::abs(x))) - precision + 1;
co838_mgl6 1:1e45dd2c91fb 52 double div = std::pow(10., ex);
co838_mgl6 1:1e45dd2c91fb 53 return std::floor(x / div + 0.5) * div;
co838_mgl6 1:1e45dd2c91fb 54 }
co838_mgl6 1:1e45dd2c91fb 55
co838_mgl6 1:1e45dd2c91fb 56 inline double limit(double val, double upper, double lower)
co838_mgl6 1:1e45dd2c91fb 57 {
co838_mgl6 1:1e45dd2c91fb 58 return (val > upper ? upper : (val < lower ? lower : val));
co838_mgl6 1:1e45dd2c91fb 59 }
co838_mgl6 1:1e45dd2c91fb 60
co838_mgl6 1:1e45dd2c91fb 61 // Conversion of digital value from [0, 1] to degree or rad.
co838_mgl6 1:1e45dd2c91fb 62 inline double digitalToDegree(double val)
co838_mgl6 1:1e45dd2c91fb 63 {
co838_mgl6 1:1e45dd2c91fb 64 return val * 360.0;
co838_mgl6 1:1e45dd2c91fb 65 }
co838_mgl6 1:1e45dd2c91fb 66
co838_mgl6 1:1e45dd2c91fb 67 inline double degreeToDigital(double val)
co838_mgl6 1:1e45dd2c91fb 68 {
co838_mgl6 1:1e45dd2c91fb 69 return val / 360.0;
co838_mgl6 1:1e45dd2c91fb 70 }
co838_mgl6 1:1e45dd2c91fb 71
co838_mgl6 1:1e45dd2c91fb 72 inline double digitalToRadian(double val)
co838_mgl6 1:1e45dd2c91fb 73 {
co838_mgl6 1:1e45dd2c91fb 74 return (digitalToDegree(val) * (VAR_PI / 180.0));
co838_mgl6 1:1e45dd2c91fb 75 }
co838_mgl6 1:1e45dd2c91fb 76
co838_mgl6 1:1e45dd2c91fb 77 inline double radianToDigital(double val)
co838_mgl6 1:1e45dd2c91fb 78 {
co838_mgl6 1:1e45dd2c91fb 79 return degreeToDigital(val / (VAR_PI / 180.0));
co838_mgl6 1:1e45dd2c91fb 80 }
co838_mgl6 1:1e45dd2c91fb 81
co838_mgl6 1:1e45dd2c91fb 82 // Convert float to string without c++11
co838_mgl6 1:1e45dd2c91fb 83 inline std::string stof (float number){
co838_mgl6 1:1e45dd2c91fb 84 std::ostringstream buff;
co838_mgl6 1:1e45dd2c91fb 85 buff << number;
co838_mgl6 1:1e45dd2c91fb 86 return buff.str();
co838_mgl6 1:1e45dd2c91fb 87 }
co838_mgl6 1:1e45dd2c91fb 88
co838_mgl6 1:1e45dd2c91fb 89 // Convert int to string without c++11
co838_mgl6 1:1e45dd2c91fb 90 inline std::string stoi (int number){
co838_mgl6 1:1e45dd2c91fb 91 std::ostringstream buff;
co838_mgl6 1:1e45dd2c91fb 92 buff << number;
co838_mgl6 1:1e45dd2c91fb 93 return buff.str();
co838_mgl6 1:1e45dd2c91fb 94 }
co838_mgl6 1:1e45dd2c91fb 95 }
co838_mgl6 1:1e45dd2c91fb 96
co838_mgl6 1:1e45dd2c91fb 97 #endif // MATHALGO_H