1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-21 16:12:18 +00:00
stmbl/shared/defines.h

84 lines
3.2 KiB
C
Raw Permalink Normal View History

2014-11-25 00:40:44 +00:00
/*
* This file is part of the stmbl project.
*
* Copyright (C) 2013-2015 Rene Hopf <renehopf@mac.com>
* Copyright (C) 2013-2015 Nico Stute <crinq@crinq.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <math.h>
2018-03-18 22:56:39 +00:00
#include <stdint.h>
2014-11-25 00:40:44 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2015-04-03 22:23:34 +00:00
#define NO 0
#define YES 1
2017-09-06 02:20:06 +00:00
#define ABS(a) (((a) < 0.0) ? -(a) : (a))
#define LIMIT(x, lowhigh) (((x) > (lowhigh)) ? (lowhigh) : (((x) < (-lowhigh)) ? (-lowhigh) : (x)))
#define SAT(x, lowhigh) (((x) > (lowhigh)) ? (1.0) : (((x) < (-lowhigh)) ? (-1.0) : (0.0)))
#define SAT2(x, low, high) (((x) > (high)) ? (1.0) : (((x) < (low)) ? (-1.0) : (0.0)))
#define STEP(from, to, step) (((from) < (to)) ? (MIN((from) + (step), (to))) : (MAX((from) - (step), (to))))
#define DEG(a) ((a)*M_PI / 180.0)
#define RAD(a) ((a)*180.0 / M_PI)
#define SIGN(a) (((a) < 0.0) ? (-1.0) : (((a) > 0.0) ? (1.0) : (0.0)))
2017-09-06 02:20:06 +00:00
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0), 1.0)
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
2017-01-14 02:30:26 +00:00
#define MIN3(a, b, c) MIN(a, MIN(b, c))
#define MAX3(a, b, c) MAX(a, MAX(b, c))
2015-03-05 21:58:25 +00:00
2017-09-06 02:20:06 +00:00
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
2016-09-12 20:24:12 +00:00
2017-09-06 02:20:06 +00:00
#define STATIC_ASSERT(x) _Static_assert(x, #x)
2016-09-12 20:24:12 +00:00
2017-09-06 02:20:06 +00:00
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
2016-09-12 20:24:12 +00:00
2018-03-18 22:56:39 +00:00
extern volatile uint64_t systime;
2018-04-06 23:36:17 +00:00
#define BLINK(N) \
({ \
2018-03-18 22:56:39 +00:00
int t = (systime / 300) % (2 * (unsigned int)(N) + 2); \
2018-04-06 23:36:17 +00:00
if(t < 2) { \
t = 0; \
} else { \
t = t % 2; \
} \
t; \
2018-03-18 22:56:39 +00:00
})
2016-09-07 17:41:24 +00:00
//TODO: change type to typeof()
2017-09-06 02:20:06 +00:00
//TODO: change __old_val__ to something more useful
2017-06-02 18:42:35 +00:00
//BUG: fails with multiple comps TODO: move to ctx
2017-09-06 02:20:06 +00:00
#define RISING_EDGE(sig) \
({static float __old_val__ = 0.0; uint8_t ret = (sig) > __old_val__; __old_val__ = (sig); ret; })
2016-09-07 17:41:24 +00:00
2017-09-06 02:20:06 +00:00
#define FALLING_EDGE(sig) \
({static float __old_val__ = 0.0; uint8_t ret = (sig) < __old_val__; __old_val__ = (sig); ret; })
2014-11-25 00:40:44 +00:00
2017-09-06 02:20:06 +00:00
#define EDGE(sig) \
({static float __old_val__ = 0.0; uint8_t ret = (sig) != __old_val__; __old_val__ = (sig); ret; })
2016-09-07 17:41:24 +00:00
2017-09-06 02:20:06 +00:00
#define LP_HZ(a) (((a) <= 0.0) ? (1.0) : (1.0 / (1.0 / period / ((a)*M_PI * 2.0) + 1.0)))
2014-11-25 00:40:44 +00:00
#ifdef __cplusplus
}
#endif