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.

199 lines
5.9KB

  1. /*
  2. wiring.h - Partial implementation of the Wiring API for the ATmega8.
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General
  14. Public License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. $Id: wiring.h 387 2008-03-08 21:30:00Z mellis $
  18. */
  19. #ifndef Wiring_h
  20. #define Wiring_h
  21. //#include <avr/io.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include "binary.h"
  25. #include "core_id.h"
  26. #include "core_pins.h"
  27. // type_traits interferes with min() and other defines
  28. // include it early, so we can define these later
  29. // for Arduino compatibility
  30. #ifdef __cplusplus
  31. #include <type_traits>
  32. // when the input number is an integer type, do all math as 32 bit signed long
  33. template <class T, class A, class B, class C, class D>
  34. 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)
  35. {
  36. long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max;
  37. // Arduino's traditional algorithm
  38. //return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  39. // st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889
  40. // more conversation:
  41. // https://forum.pjrc.com/threads/44503-map()-function-improvements
  42. if ((in_max - in_min) > (out_max - out_min)) {
  43. return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min;
  44. } else {
  45. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  46. }
  47. }
  48. // when the input is a float or double, do all math using the input's type
  49. template <class T, class A, class B, class C, class D>
  50. 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)
  51. {
  52. return (x - (T)in_min) * ((T)out_max - (T)out_min) / ((T)in_max - (T)in_min) + (T)out_min;
  53. }
  54. #include <algorithm> // c++ min, max
  55. #include <utility>
  56. // https://forum.pjrc.com/threads/44596-Teensyduino-1-37-Beta-2-(Arduino-1-8-3-support)?p=145150&viewfull=1#post145150
  57. template<class A, class B>
  58. constexpr auto min(A&& a, B&& b) -> decltype(a < b ? std::forward<A>(a) : std::forward<B>(b)) {
  59. return a < b ? std::forward<A>(a) : std::forward<B>(b);
  60. }
  61. template<class A, class B>
  62. constexpr auto max(A&& a, B&& b) -> decltype(a < b ? std::forward<A>(a) : std::forward<B>(b)) {
  63. return a >= b ? std::forward<A>(a) : std::forward<B>(b);
  64. }
  65. #else // not C++
  66. #define min(a, b) ({ \
  67. typeof(a) _a = (a); \
  68. typeof(b) _b = (b); \
  69. (_a < _b) ? _a : _b; \
  70. })
  71. #define max(a, b) ({ \
  72. typeof(a) _a = (a); \
  73. typeof(b) _b = (b); \
  74. (_a > _b) ? _a : _b; \
  75. })
  76. #endif
  77. #ifdef PI
  78. #undef PI
  79. #endif
  80. #define PI 3.1415926535897932384626433832795
  81. #define HALF_PI 1.5707963267948966192313216916398
  82. #define TWO_PI 6.283185307179586476925286766559
  83. #define DEG_TO_RAD 0.017453292519943295769236907684886
  84. #define RAD_TO_DEG 57.295779513082320876798154814105
  85. #ifndef M_PI
  86. #define M_PI 3.1415926535897932384626433832795
  87. #endif
  88. #ifndef M_SQRT2
  89. #define M_SQRT2 1.4142135623730950488016887
  90. #endif
  91. #define SERIAL 0
  92. #define DISPLAY 1
  93. // undefine stdlib's abs if encountered
  94. #ifdef abs
  95. #undef abs
  96. #endif
  97. #if __cplusplus >= 201103L && defined(__STRICT_ANSI__)
  98. #define typeof(a) decltype(a)
  99. #endif
  100. #define abs(x) ({ \
  101. typeof(x) _x = (x); \
  102. (_x > 0) ? _x : -_x; \
  103. })
  104. #define constrain(amt, low, high) ({ \
  105. typeof(amt) _amt = (amt); \
  106. typeof(low) _low = (low); \
  107. typeof(high) _high = (high); \
  108. (_amt < _low) ? _low : ((_amt > _high) ? _high : _amt); \
  109. })
  110. #define round(x) ({ \
  111. typeof(x) _x = (x); \
  112. (_x>=0) ? (long)(_x+0.5) : (long)(_x-0.5); \
  113. })
  114. #define radians(deg) ((deg)*DEG_TO_RAD)
  115. #define degrees(rad) ((rad)*RAD_TO_DEG)
  116. #define sq(x) ({ \
  117. typeof(x) _x = (x); \
  118. _x * _x; \
  119. })
  120. #ifdef __cplusplus
  121. extern "C"{
  122. #endif
  123. extern double exp10(double x);
  124. extern float exp10f(float x);
  125. extern long double exp10l(long double x);
  126. extern double pow10(double x);
  127. extern float pow10f(float x);
  128. extern long double pow10l(long double x);
  129. #define stricmp(a, b) strcasecmp(a, b)
  130. #define sei() __enable_irq()
  131. #define cli() __disable_irq()
  132. #define interrupts() __enable_irq()
  133. #define noInterrupts() __disable_irq()
  134. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  135. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  136. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  137. #define lowByte(w) ((uint8_t)((w) & 0xFF))
  138. #define highByte(w) ((uint8_t)((w) >> 8))
  139. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  140. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  141. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  142. #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
  143. typedef unsigned int word;
  144. #define bit(b) (1UL << (b))
  145. typedef uint8_t byte;
  146. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout);
  147. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
  148. //void tone(uint8_t pin, uint16_t frequency, uint32_t duration);
  149. //void noTone(uint8_t pin);
  150. //void attachInterrupt(uint8_t, void (*)(void), uint8_t mode);
  151. //void detachInterrupt(uint8_t);
  152. void setup(void);
  153. void loop(void);
  154. #ifdef __cplusplus
  155. } // extern "C"
  156. #endif
  157. // fix C++ boolean issue
  158. // https://github.com/arduino/Arduino/pull/2151
  159. #ifdef __cplusplus
  160. typedef bool boolean;
  161. #else
  162. typedef uint8_t boolean;
  163. #define false 0
  164. #define true (!false)
  165. #endif
  166. #endif