Arduino.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Arduino.h - Main include file for the Arduino SDK
  3. Copyright (c) 2005-2013 Arduino Team. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #ifndef Arduino_h
  17. #define Arduino_h
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <avr/pgmspace.h>
  23. #include <avr/io.h>
  24. #include <avr/interrupt.h>
  25. #include "binary.h"
  26. #ifdef __cplusplus
  27. extern "C"{
  28. #endif
  29. void yield(void);
  30. #define HIGH 0x1
  31. #define LOW 0x0
  32. #define INPUT 0x0
  33. #define OUTPUT 0x1
  34. #define INPUT_PULLUP 0x2
  35. #define PI 3.1415926535897932384626433832795
  36. #define HALF_PI 1.5707963267948966192313216916398
  37. #define TWO_PI 6.283185307179586476925286766559
  38. #define DEG_TO_RAD 0.017453292519943295769236907684886
  39. #define RAD_TO_DEG 57.295779513082320876798154814105
  40. #define EULER 2.718281828459045235360287471352
  41. #define SERIAL 0x0
  42. #define DISPLAY 0x1
  43. #define LSBFIRST 0
  44. #define MSBFIRST 1
  45. #define CHANGE 1
  46. #define FALLING 2
  47. #define RISING 3
  48. #if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  49. #define DEFAULT 0
  50. #define EXTERNAL 1
  51. #define INTERNAL 2
  52. #else
  53. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
  54. #define INTERNAL1V1 2
  55. #define INTERNAL2V56 3
  56. #else
  57. #define INTERNAL 3
  58. #endif
  59. #define DEFAULT 1
  60. #define EXTERNAL 0
  61. #endif
  62. // undefine stdlib's abs if encountered
  63. #ifdef abs
  64. #undef abs
  65. #endif
  66. #define min(a,b) ((a)<(b)?(a):(b))
  67. #define max(a,b) ((a)>(b)?(a):(b))
  68. #define abs(x) ((x)>0?(x):-(x))
  69. #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  70. #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
  71. #define radians(deg) ((deg)*DEG_TO_RAD)
  72. #define degrees(rad) ((rad)*RAD_TO_DEG)
  73. #define sq(x) ((x)*(x))
  74. #define interrupts() sei()
  75. #define noInterrupts() cli()
  76. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  77. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  78. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  79. #define lowByte(w) ((uint8_t) ((w) & 0xff))
  80. #define highByte(w) ((uint8_t) ((w) >> 8))
  81. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  82. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  83. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  84. #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
  85. // avr-libc defines _NOP() since 1.6.2
  86. #ifndef _NOP
  87. #define _NOP() do { __asm__ volatile ("nop"); } while (0)
  88. #endif
  89. typedef unsigned int word;
  90. #define bit(b) (1UL << (b))
  91. typedef bool boolean;
  92. typedef uint8_t byte;
  93. void init(void);
  94. void initVariant(void);
  95. int atexit(void (*func)()) __attribute__((weak));
  96. void pinMode(uint8_t, uint8_t);
  97. void digitalWrite(uint8_t, uint8_t);
  98. int digitalRead(uint8_t);
  99. int analogRead(uint8_t);
  100. void analogReference(uint8_t mode);
  101. void analogWrite(uint8_t, int);
  102. unsigned long millis(void);
  103. unsigned long micros(void);
  104. void delay(unsigned long);
  105. void delayMicroseconds(unsigned int us);
  106. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
  107. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
  108. uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
  109. void attachInterrupt(uint8_t, void (*)(void), int mode);
  110. void detachInterrupt(uint8_t);
  111. void setup(void);
  112. void loop(void);
  113. // Get the bit location within the hardware port of the given virtual pin.
  114. // This comes from the pins_*.c file for the active board configuration.
  115. #define analogInPinToBit(P) (P)
  116. // On the ATmega1280, the addresses of some of the port registers are
  117. // greater than 255, so we can't store them in uint8_t's.
  118. extern const uint16_t PROGMEM port_to_mode_PGM[];
  119. extern const uint16_t PROGMEM port_to_input_PGM[];
  120. extern const uint16_t PROGMEM port_to_output_PGM[];
  121. extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
  122. // extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
  123. extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
  124. extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
  125. // Get the bit location within the hardware port of the given virtual pin.
  126. // This comes from the pins_*.c file for the active board configuration.
  127. //
  128. // These perform slightly better as macros compared to inline functions
  129. //
  130. #define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
  131. #define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
  132. #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
  133. #define analogInPinToBit(P) (P)
  134. #define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
  135. #define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
  136. #define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
  137. #define NOT_A_PIN 0
  138. #define NOT_A_PORT 0
  139. #define NOT_AN_INTERRUPT -1
  140. #ifdef ARDUINO_MAIN
  141. #define PA 1
  142. #define PB 2
  143. #define PC 3
  144. #define PD 4
  145. #define PE 5
  146. #define PF 6
  147. #define PG 7
  148. #define PH 8
  149. #define PJ 10
  150. #define PK 11
  151. #define PL 12
  152. #endif
  153. #define NOT_ON_TIMER 0
  154. #define TIMER0A 1
  155. #define TIMER0B 2
  156. #define TIMER1A 3
  157. #define TIMER1B 4
  158. #define TIMER1C 5
  159. #define TIMER2 6
  160. #define TIMER2A 7
  161. #define TIMER2B 8
  162. #define TIMER3A 9
  163. #define TIMER3B 10
  164. #define TIMER3C 11
  165. #define TIMER4A 12
  166. #define TIMER4B 13
  167. #define TIMER4C 14
  168. #define TIMER4D 15
  169. #define TIMER5A 16
  170. #define TIMER5B 17
  171. #define TIMER5C 18
  172. #ifdef __cplusplus
  173. } // extern "C"
  174. #endif
  175. #ifdef __cplusplus
  176. #include "WCharacter.h"
  177. #include "WString.h"
  178. #include "HardwareSerial.h"
  179. #include "USBAPI.h"
  180. #if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
  181. #error "Targets with both UART0 and CDC serial not supported"
  182. #endif
  183. uint16_t makeWord(uint16_t w);
  184. uint16_t makeWord(byte h, byte l);
  185. #define word(...) makeWord(__VA_ARGS__)
  186. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
  187. void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
  188. void noTone(uint8_t _pin);
  189. // WMath prototypes
  190. long random(long);
  191. long random(long, long);
  192. void randomSeed(unsigned int);
  193. long map(long, long, long, long, long);
  194. #endif
  195. #include "pins_arduino.h"
  196. #endif