Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

217 lines
6.7KB

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