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.

186 lines
5.4KB

  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. #endif
  55. #ifdef __cplusplus
  56. extern "C"{
  57. #endif
  58. #ifdef PI
  59. #undef PI
  60. #endif
  61. #define PI 3.1415926535897932384626433832795
  62. #define HALF_PI 1.5707963267948966192313216916398
  63. #define TWO_PI 6.283185307179586476925286766559
  64. #define DEG_TO_RAD 0.017453292519943295769236907684886
  65. #define RAD_TO_DEG 57.295779513082320876798154814105
  66. #ifndef M_PI
  67. #define M_PI 3.1415926535897932384626433832795
  68. #endif
  69. #ifndef M_SQRT2
  70. #define M_SQRT2 1.4142135623730950488016887
  71. #endif
  72. #define SERIAL 0
  73. #define DISPLAY 1
  74. // undefine stdlib's abs if encountered
  75. #ifdef abs
  76. #undef abs
  77. #endif
  78. #if __cplusplus >= 201103L && defined(__STRICT_ANSI__)
  79. #define typeof(a) decltype(a)
  80. #endif
  81. #define min(a, b) ({ \
  82. typeof(a) _a = (a); \
  83. typeof(b) _b = (b); \
  84. (_a < _b) ? _a : _b; \
  85. })
  86. #define max(a, b) ({ \
  87. typeof(a) _a = (a); \
  88. typeof(b) _b = (b); \
  89. (_a > _b) ? _a : _b; \
  90. })
  91. #define abs(x) ({ \
  92. typeof(x) _x = (x); \
  93. (_x > 0) ? _x : -_x; \
  94. })
  95. #define constrain(amt, low, high) ({ \
  96. typeof(amt) _amt = (amt); \
  97. typeof(low) _low = (low); \
  98. typeof(high) _high = (high); \
  99. (_amt < _low) ? _low : ((_amt > _high) ? _high : _amt); \
  100. })
  101. #define round(x) ({ \
  102. typeof(x) _x = (x); \
  103. (_x>=0) ? (long)(_x+0.5) : (long)(_x-0.5); \
  104. })
  105. #define radians(deg) ((deg)*DEG_TO_RAD)
  106. #define degrees(rad) ((rad)*RAD_TO_DEG)
  107. #define sq(x) ({ \
  108. typeof(x) _x = (x); \
  109. _x * _x; \
  110. })
  111. extern double exp10(double x);
  112. extern float exp10f(float x);
  113. extern long double exp10l(long double x);
  114. extern double pow10(double x);
  115. extern float pow10f(float x);
  116. extern long double pow10l(long double x);
  117. #define stricmp(a, b) strcasecmp(a, b)
  118. #define sei() __enable_irq()
  119. #define cli() __disable_irq()
  120. #define interrupts() __enable_irq()
  121. #define noInterrupts() __disable_irq()
  122. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  123. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  124. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  125. #define lowByte(w) ((uint8_t)((w) & 0xFF))
  126. #define highByte(w) ((uint8_t)((w) >> 8))
  127. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  128. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  129. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  130. #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
  131. typedef unsigned int word;
  132. #define bit(b) (1UL << (b))
  133. typedef uint8_t byte;
  134. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout);
  135. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
  136. //void tone(uint8_t pin, uint16_t frequency, uint32_t duration);
  137. //void noTone(uint8_t pin);
  138. //void attachInterrupt(uint8_t, void (*)(void), uint8_t mode);
  139. //void detachInterrupt(uint8_t);
  140. void setup(void);
  141. void loop(void);
  142. #ifdef __cplusplus
  143. } // extern "C"
  144. #endif
  145. // fix C++ boolean issue
  146. // https://github.com/arduino/Arduino/pull/2151
  147. #ifdef __cplusplus
  148. typedef bool boolean;
  149. #else
  150. typedef uint8_t boolean;
  151. #define false 0
  152. #define true (!false)
  153. #endif
  154. #endif