Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.2KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef Wiring_h
  31. #define Wiring_h
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include "binary.h"
  35. #include "core_id.h"
  36. #include "core_pins.h"
  37. // type_traits interferes with min() and other defines
  38. // include it early, so we can define these later
  39. // for Arduino compatibility
  40. #ifdef __cplusplus
  41. #include <type_traits>
  42. // when the input number is an integer type, do all math as 32 bit signed long
  43. template <class T, class A, class B, class C, class D>
  44. long map(T _x, A _in_min, B _in_max, C _out_min, D _out_max, typename std::enable_if<std::is_integral<T>::value >::type* = 0)
  45. {
  46. long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max;
  47. // Arduino's traditional algorithm
  48. //return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  49. // st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889
  50. // more conversation:
  51. // https://forum.pjrc.com/threads/44503-map()-function-improvements
  52. if ((in_max - in_min) > (out_max - out_min)) {
  53. return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min;
  54. } else {
  55. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  56. }
  57. }
  58. // when the input is a float or double, do all math using the input's type
  59. template <class T, class A, class B, class C, class D>
  60. T map(T x, A in_min, B in_max, C out_min, D out_max, typename std::enable_if<std::is_floating_point<T>::value >::type* = 0)
  61. {
  62. return (x - (T)in_min) * ((T)out_max - (T)out_min) / ((T)in_max - (T)in_min) + (T)out_min;
  63. }
  64. //#include <algorithm> // this isn't really needed, is it? (slows down compiling)
  65. #include <utility>
  66. // https://forum.pjrc.com/threads/44596-Teensyduino-1-37-Beta-2-(Arduino-1-8-3-support)?p=145150&viewfull=1#post145150
  67. template<class A, class B>
  68. constexpr auto min(A&& a, B&& b) -> decltype(a < b ? std::forward<A>(a) : std::forward<B>(b)) {
  69. return a < b ? std::forward<A>(a) : std::forward<B>(b);
  70. }
  71. template<class A, class B>
  72. constexpr auto max(A&& a, B&& b) -> decltype(a < b ? std::forward<A>(a) : std::forward<B>(b)) {
  73. return a >= b ? std::forward<A>(a) : std::forward<B>(b);
  74. }
  75. #else // not C++
  76. #define min(a, b) ({ \
  77. typeof(a) _a = (a); \
  78. typeof(b) _b = (b); \
  79. (_a < _b) ? _a : _b; \
  80. })
  81. #define max(a, b) ({ \
  82. typeof(a) _a = (a); \
  83. typeof(b) _b = (b); \
  84. (_a > _b) ? _a : _b; \
  85. })
  86. #endif
  87. #ifdef PI
  88. #undef PI
  89. #endif
  90. #define PI 3.1415926535897932384626433832795
  91. #define HALF_PI 1.5707963267948966192313216916398
  92. #define TWO_PI 6.283185307179586476925286766559
  93. #define DEG_TO_RAD 0.017453292519943295769236907684886
  94. #define RAD_TO_DEG 57.295779513082320876798154814105
  95. #ifndef M_PI
  96. #define M_PI 3.1415926535897932384626433832795
  97. #endif
  98. #ifndef M_SQRT2
  99. #define M_SQRT2 1.4142135623730950488016887
  100. #endif
  101. #define SERIAL 0
  102. #define DISPLAY 1
  103. // undefine stdlib's abs if encountered
  104. #ifdef abs
  105. #undef abs
  106. #endif
  107. #if __cplusplus >= 201103L && defined(__STRICT_ANSI__)
  108. #define typeof(a) decltype(a)
  109. #endif
  110. #define abs(x) ({ \
  111. typeof(x) _x = (x); \
  112. (_x > 0) ? _x : -_x; \
  113. })
  114. #define constrain(amt, low, high) ({ \
  115. typeof(amt) _amt = (amt); \
  116. typeof(low) _low = (low); \
  117. typeof(high) _high = (high); \
  118. (_amt < _low) ? _low : ((_amt > _high) ? _high : _amt); \
  119. })
  120. #define round(x) ({ \
  121. typeof(x) _x = (x); \
  122. (_x>=0) ? (long)(_x+0.5) : (long)(_x-0.5); \
  123. })
  124. #define radians(deg) ((deg)*DEG_TO_RAD)
  125. #define degrees(rad) ((rad)*RAD_TO_DEG)
  126. #define sq(x) ({ \
  127. typeof(x) _x = (x); \
  128. _x * _x; \
  129. })
  130. #ifdef __cplusplus
  131. extern "C"{
  132. #endif
  133. extern double exp10(double x);
  134. extern float exp10f(float x);
  135. extern long double exp10l(long double x);
  136. extern double pow10(double x);
  137. extern float pow10f(float x);
  138. extern long double pow10l(long double x);
  139. #define stricmp(a, b) strcasecmp(a, b)
  140. #define sei() __enable_irq()
  141. #define cli() __disable_irq()
  142. #define interrupts() __enable_irq()
  143. #define noInterrupts() __disable_irq()
  144. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  145. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  146. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  147. #define lowByte(w) ((uint8_t)((w) & 0xFF))
  148. #define highByte(w) ((uint8_t)((w) >> 8))
  149. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  150. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  151. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  152. #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
  153. typedef unsigned int word;
  154. #define bit(b) (1UL << (b))
  155. typedef uint8_t byte;
  156. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout);
  157. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
  158. void setup(void);
  159. void loop(void);
  160. #ifdef __cplusplus
  161. } // extern "C"
  162. #endif
  163. // fix C++ boolean issue
  164. // https://github.com/arduino/Arduino/pull/2151
  165. #ifdef __cplusplus
  166. typedef bool boolean;
  167. #else
  168. typedef uint8_t boolean;
  169. #define false 0
  170. #define true (!false)
  171. #endif
  172. #endif