Skip to content

Commit

Permalink
add file redefining horror macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Amado Antonini committed Apr 24, 2020
1 parent f299c49 commit b0b2b69
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
105 changes: 105 additions & 0 deletions cpp_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#ifndef CPP_UTILS_H_INCLUDED
#define CPP_UTILS_H_INCLUDED

/*
* As far as this file is located with parts of GCC C++ standard library
* and contains some functions copied form it it is distributed under the
* same licanse as GCC.
*/

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.

// Do not change anything for plain C users
#ifdef __cplusplus

// Undef macro horror

#undef min
#undef max
#undef abs
#undef constrain
#undef radians
#undef degrees
#undef sq

// So as to stay as much API compatible as possible min, max and abs
// are not placed in std namespace

template<typename Tpa, typename Tpb>
inline auto min(const Tpa& a, const Tpb& b) -> decltype(a < b ? a : b) {
return b < a ? b : a;
}

template<typename Tpa, typename Tpb>
inline auto max(const Tpa& a, const Tpb& b) -> decltype(b > a ? b : a) {
return b > a ? b : a;
}

inline double abs(double x) {
return __builtin_fabs(x);
}

inline float abs(float x) {
return __builtin_fabsf(x);
}

inline long double abs(long double x) {
return __builtin_fabsl(x);
}

inline long abs(long i) {
return labs(i);
}

inline long long abs(long long x) {
return x >= 0 ? x : -x;
}

// abs for int is defined by C library
//inline int abs(int i)

template<typename Tpa, typename Tpb, typename Tpc>
inline auto constrain(
const Tpa& amt,
const Tpb& low,
const Tpc& high) -> decltype(min(max(amt, low), high)) {
return min(max(amt, low), high);
}

inline float radians(float deg) {
return deg * DEG_TO_RAD;
}

inline double radians(double deg) {
return deg * DEG_TO_RAD;
}

inline float degrees(float deg) {
return deg * RAD_TO_DEG;
}

inline double degrees(double deg) {
return deg * RAD_TO_DEG;
}

/// This one requires C++11 sintax
template<typename Tp>
inline auto sqr(const Tp& x) -> decltype(x * x) {
return x * x;
}

// We have malloc and free. Why not to have new and delete?
// Use them with caution so as not to end up with laggy application.
inline void * operator new(size_t size) { return malloc(size); }
inline void operator delete(void* ptr) { free(ptr); }

#endif // __cplusplus

#endif // CPP_UTILS_H_INCLUDED
7 changes: 5 additions & 2 deletions e-vent.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ enum States {
#include <LiquidCrystal.h>
#include "src/thirdparty/RoboClaw/RoboClaw.h"

#include "cpp_utils.h" // Redefines macros min, max, abs, etc. into proper functions,
// should be included after third-party code, before E-Vent includes

#include "Alarms.h"
#include "Buttons.h"
#include "Display.h"
Expand Down Expand Up @@ -101,14 +104,14 @@ const struct{float a, b, c;} COEFFS{1.29083271e-03, 4.72985182e-01, -7.35403067e
* Converts motor position in ticks to volume in mL
*/
float ticks2volume(const float& vol_ticks) {
return COEFFS.a * sq(vol_ticks) + COEFFS.b * vol_ticks + COEFFS.c;
return COEFFS.a * sqr(vol_ticks) + COEFFS.b * vol_ticks + COEFFS.c;
}

/**
* Converts volume in mL to motor position in ticks
*/
float volume2ticks(const float& vol_ml) {
return (-COEFFS.b + sqrt(sq(COEFFS.b) -4 * COEFFS.a * (COEFFS.c - vol_ml))) / (2 * COEFFS.a);
return (-COEFFS.b + sqrt(sqr(COEFFS.b) -4 * COEFFS.a * (COEFFS.c - vol_ml))) / (2 * COEFFS.a);
}

//Setup States
Expand Down

0 comments on commit b0b2b69

Please sign in to comment.