Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

184 líneas
5.3KB

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