Arduino.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef Arduino_h
  2. #define Arduino_h
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <avr/pgmspace.h>
  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #include "binary.h"
  10. #ifdef __cplusplus
  11. extern "C"{
  12. #endif
  13. #define HIGH 0x1
  14. #define LOW 0x0
  15. #define INPUT 0x0
  16. #define OUTPUT 0x1
  17. #define INPUT_PULLUP 0x2
  18. #define true 0x1
  19. #define false 0x0
  20. #define PI 3.1415926535897932384626433832795
  21. #define HALF_PI 1.5707963267948966192313216916398
  22. #define TWO_PI 6.283185307179586476925286766559
  23. #define DEG_TO_RAD 0.017453292519943295769236907684886
  24. #define RAD_TO_DEG 57.295779513082320876798154814105
  25. #define SERIAL 0x0
  26. #define DISPLAY 0x1
  27. #define LSBFIRST 0
  28. #define MSBFIRST 1
  29. #define CHANGE 1
  30. #define FALLING 2
  31. #define RISING 3
  32. // analog voltage references
  33. #if defined(REFS2) // ATtiny of some sort
  34. #define DEFAULT 0
  35. #define EXTERNAL 1
  36. #define INTERNAL 2
  37. #else
  38. // Looks like the SFIOR register exists only on those chips without
  39. // 1.1V internal reference voltage. This is an educated guess inspired
  40. // by AppNote AVR097, page 5 bottom.
  41. #if !defined(SFIOR)
  42. #define INTERNAL1V1 2
  43. #define INTERNAL2V56 3
  44. #else
  45. #define INTERNAL 3
  46. #endif
  47. #define DEFAULT 1
  48. #define EXTERNAL 0
  49. #endif
  50. // undefine stdlib's abs if encountered
  51. #ifdef abs
  52. #undef abs
  53. #endif
  54. #define min(a,b) ((a)<(b)?(a):(b))
  55. #define max(a,b) ((a)>(b)?(a):(b))
  56. #define abs(x) ((x)>0?(x):-(x))
  57. #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  58. #if __GNUC__ <= 4 && __GNUC_MINOR__ < 5
  59. // this conflicts with avr-gcc's built in headers as of avr-gcc 4.5.3
  60. #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
  61. #endif
  62. #define radians(deg) ((deg)*DEG_TO_RAD)
  63. #define degrees(rad) ((rad)*RAD_TO_DEG)
  64. #define sq(x) ((x)*(x))
  65. #define interrupts() sei()
  66. #define noInterrupts() cli()
  67. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  68. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  69. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  70. #define lowByte(w) ((uint8_t) ((w) & 0xff))
  71. #define highByte(w) ((uint8_t) ((w) >> 8))
  72. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  73. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  74. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  75. #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
  76. typedef unsigned int word;
  77. #define bit(b) (1UL << (b))
  78. typedef uint8_t boolean;
  79. typedef uint8_t byte;
  80. void init(void);
  81. void pinMode(uint8_t, uint8_t);
  82. void digitalWrite(uint8_t, uint8_t);
  83. int digitalRead(uint8_t);
  84. int analogRead(uint8_t);
  85. void analogReference(uint8_t mode);
  86. void analogWrite(uint8_t, int);
  87. unsigned long millis(void);
  88. unsigned long micros(void);
  89. void delay(unsigned long);
  90. void delayMicroseconds(unsigned int us);
  91. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
  92. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
  93. uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
  94. void attachInterrupt(uint8_t, void (*)(void), int mode);
  95. void detachInterrupt(uint8_t);
  96. void setup(void);
  97. void loop(void);
  98. // Get the bit location within the hardware port of the given virtual pin.
  99. // This comes from the pins_*.c file for the active board configuration.
  100. #define analogInPinToBit(P) (P)
  101. // On the ATmega1280, the addresses of some of the port registers are
  102. // greater than 255, so we can't store them in uint8_t's.
  103. extern const uint16_t PROGMEM port_to_mode_PGM[];
  104. extern const uint16_t PROGMEM port_to_input_PGM[];
  105. extern const uint16_t PROGMEM port_to_output_PGM[];
  106. extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
  107. // extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
  108. extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
  109. extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
  110. // Get the bit location within the hardware port of the given virtual pin.
  111. // This comes from the pins_*.c file for the active board configuration.
  112. //
  113. // These perform slightly better as macros compared to inline functions
  114. //
  115. #define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
  116. #define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
  117. #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
  118. #define analogInPinToBit(P) (P)
  119. #define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
  120. #define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
  121. #define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
  122. #define NOT_A_PIN 0
  123. #define NOT_A_PORT 0
  124. #ifdef ARDUINO_MAIN
  125. #define PA 1
  126. #define PB 2
  127. #define PC 3
  128. #define PD 4
  129. #define PE 5
  130. #define PF 6
  131. #define PG 7
  132. #define PH 8
  133. #define PJ 10
  134. #define PK 11
  135. #define PL 12
  136. #endif
  137. #define NOT_ON_TIMER 0
  138. #define TIMER0A 1
  139. #define TIMER0B 2
  140. #define TIMER1A 3
  141. #define TIMER1B 4
  142. #define TIMER2 5
  143. #define TIMER2A 6
  144. #define TIMER2B 7
  145. #define TIMER3A 8
  146. #define TIMER3B 9
  147. #define TIMER3C 10
  148. #define TIMER4A 11
  149. #define TIMER4B 12
  150. #define TIMER4C 13
  151. #define TIMER4D 14
  152. #define TIMER5A 15
  153. #define TIMER5B 16
  154. #define TIMER5C 17
  155. #ifdef __cplusplus
  156. } // extern "C"
  157. #endif
  158. #ifdef __cplusplus
  159. #include "WCharacter.h"
  160. #include "WString.h"
  161. #include "HardwareSerial.h"
  162. uint16_t makeWord(uint16_t w);
  163. uint16_t makeWord(byte h, byte l);
  164. #define word(...) makeWord(__VA_ARGS__)
  165. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
  166. void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
  167. void noTone(uint8_t _pin);
  168. // WMath prototypes
  169. long random(long);
  170. long random(long, long);
  171. void randomSeed(unsigned int);
  172. long map(long, long, long, long, long);
  173. #endif
  174. #include "pins_arduino.h"
  175. #endif