Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

197 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 <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. #define abs(x) ({ \
  112. typeof(x) _x = (x); \
  113. (_x > 0) ? _x : -_x; \
  114. })
  115. #define constrain(amt, low, high) ({ \
  116. typeof(amt) _amt = (amt); \
  117. typeof(low) _low = (low); \
  118. typeof(high) _high = (high); \
  119. (_amt < _low) ? _low : ((_amt > _high) ? _high : _amt); \
  120. })
  121. #define round(x) ({ \
  122. typeof(x) _x = (x); \
  123. (_x>=0) ? (long)(_x+0.5) : (long)(_x-0.5); \
  124. })
  125. #define radians(deg) ((deg)*DEG_TO_RAD)
  126. #define degrees(rad) ((rad)*RAD_TO_DEG)
  127. #define sq(x) ({ \
  128. typeof(x) _x = (x); \
  129. _x * _x; \
  130. })
  131. #ifdef __cplusplus
  132. extern "C"{
  133. #endif
  134. extern double exp10(double x);
  135. extern float exp10f(float x);
  136. extern long double exp10l(long double x);
  137. extern double pow10(double x);
  138. extern float pow10f(float x);
  139. extern long double pow10l(long double x);
  140. #define stricmp(a, b) strcasecmp(a, b)
  141. #define sei() __enable_irq()
  142. #define cli() __disable_irq()
  143. #define interrupts() __enable_irq()
  144. #define noInterrupts() __disable_irq()
  145. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  146. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  147. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  148. #define lowByte(w) ((uint8_t)((w) & 0xFF))
  149. #define highByte(w) ((uint8_t)((w) >> 8))
  150. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  151. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  152. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  153. #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
  154. typedef unsigned int word;
  155. #define bit(b) (1UL << (b))
  156. typedef uint8_t byte;
  157. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout);
  158. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
  159. void setup(void);
  160. void loop(void);
  161. #ifdef __cplusplus
  162. } // extern "C"
  163. #endif
  164. // fix C++ boolean issue
  165. // https://github.com/arduino/Arduino/pull/2151
  166. #ifdef __cplusplus
  167. typedef bool boolean;
  168. #else
  169. typedef uint8_t boolean;
  170. #endif
  171. #endif