PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

203 rindas
6.5KB

  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 <stdbool.h>
  35. #include "binary.h"
  36. #include "core_id.h"
  37. #include "core_pins.h"
  38. // type_traits interferes with min() and other defines
  39. // include it early, so we can define these later
  40. // for Arduino compatibility
  41. #ifdef __cplusplus
  42. #include <type_traits>
  43. // when the input number is an integer type, do all math as 32 bit signed long
  44. template <class T, class A, class B, class C, class D>
  45. 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)
  46. {
  47. long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max;
  48. // Arduino's traditional algorithm
  49. //return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  50. // st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889
  51. // more conversation:
  52. // https://forum.pjrc.com/threads/44503-map()-function-improvements
  53. if ((in_max - in_min) > (out_max - out_min)) {
  54. return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min;
  55. } else {
  56. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  57. }
  58. }
  59. // when the input is a float or double, do all math using the input's type
  60. template <class T, class A, class B, class C, class D>
  61. 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)
  62. {
  63. return (x - (T)in_min) * ((T)out_max - (T)out_min) / ((T)in_max - (T)in_min) + (T)out_min;
  64. }
  65. //#include <algorithm> // this isn't really needed, is it? (slows down compiling)
  66. #include <utility>
  67. // https://forum.pjrc.com/threads/44596-Teensyduino-1-37-Beta-2-(Arduino-1-8-3-support)?p=145150&viewfull=1#post145150
  68. template<class A, class B>
  69. constexpr auto min(A&& a, B&& b) -> decltype(a < b ? std::forward<A>(a) : std::forward<B>(b)) {
  70. return a < b ? std::forward<A>(a) : std::forward<B>(b);
  71. }
  72. template<class A, class B>
  73. constexpr auto max(A&& a, B&& b) -> decltype(a < b ? std::forward<A>(a) : std::forward<B>(b)) {
  74. return a >= b ? std::forward<A>(a) : std::forward<B>(b);
  75. }
  76. #else // not C++
  77. #define min(a, b) ({ \
  78. typeof(a) _a = (a); \
  79. typeof(b) _b = (b); \
  80. (_a < _b) ? _a : _b; \
  81. })
  82. #define max(a, b) ({ \
  83. typeof(a) _a = (a); \
  84. typeof(b) _b = (b); \
  85. (_a > _b) ? _a : _b; \
  86. })
  87. #endif
  88. #ifdef PI
  89. #undef PI
  90. #endif
  91. #define PI 3.1415926535897932384626433832795
  92. #define HALF_PI 1.5707963267948966192313216916398
  93. #define TWO_PI 6.283185307179586476925286766559
  94. #define DEG_TO_RAD 0.017453292519943295769236907684886
  95. #define RAD_TO_DEG 57.295779513082320876798154814105
  96. #ifndef M_PI
  97. #define M_PI 3.1415926535897932384626433832795
  98. #endif
  99. #ifndef M_SQRT2
  100. #define M_SQRT2 1.4142135623730950488016887
  101. #endif
  102. #define SERIAL 0
  103. #define DISPLAY 1
  104. // undefine stdlib's abs if encountered
  105. #ifdef abs
  106. #undef abs
  107. #endif
  108. #if __cplusplus >= 201103L && defined(__STRICT_ANSI__)
  109. #define typeof(a) decltype(a)
  110. #endif
  111. #if __cplusplus < 201703L
  112. #define abs(x) ({ \
  113. typeof(x) _x = (x); \
  114. (_x > 0) ? _x : -_x; \
  115. })
  116. #define round(x) \
  117. ({ \
  118. typeof(x) _x = (x); \
  119. (_x >= 0) ? (long)(_x + 0.5) : (long)(_x - 0.5); \
  120. })
  121. #endif
  122. #define constrain(amt, low, high) ({ \
  123. typeof(amt) _amt = (amt); \
  124. typeof(low) _low = (low); \
  125. typeof(high) _high = (high); \
  126. (_amt < _low) ? _low : ((_amt > _high) ? _high : _amt); \
  127. })
  128. #define radians(deg) ((deg)*DEG_TO_RAD)
  129. #define degrees(rad) ((rad)*RAD_TO_DEG)
  130. #define sq(x) ({ \
  131. typeof(x) _x = (x); \
  132. _x * _x; \
  133. })
  134. #ifdef __cplusplus
  135. extern "C"{
  136. #endif
  137. extern double exp10(double x);
  138. extern float exp10f(float x);
  139. extern long double exp10l(long double x);
  140. extern double pow10(double x);
  141. extern float pow10f(float x);
  142. extern long double pow10l(long double x);
  143. #define stricmp(a, b) strcasecmp(a, b)
  144. #define sei() __enable_irq()
  145. #define cli() __disable_irq()
  146. #define interrupts() __enable_irq()
  147. #define noInterrupts() __disable_irq()
  148. #define clockCyclesPerMicrosecond() ( F_CPU_ACTUAL / 1000000L )
  149. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  150. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  151. #define lowByte(w) ((uint8_t)((w) & 0xFF))
  152. #define highByte(w) ((uint8_t)((w) >> 8))
  153. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  154. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  155. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  156. #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
  157. typedef unsigned int word;
  158. #define bit(b) (1UL << (b))
  159. typedef uint8_t byte;
  160. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout);
  161. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
  162. void setup(void);
  163. void loop(void);
  164. #ifdef __cplusplus
  165. } // extern "C"
  166. #endif
  167. // fix C++ boolean issue
  168. // https://github.com/arduino/Arduino/pull/2151
  169. #ifdef __cplusplus
  170. typedef bool boolean;
  171. #else
  172. typedef uint8_t boolean;
  173. #endif
  174. #endif