Arduino.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  33. #define DEFAULT 0
  34. #define EXTERNAL 1
  35. #define INTERNAL 2
  36. #else
  37. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
  38. #define INTERNAL1V1 2
  39. #define INTERNAL2V56 3
  40. #else
  41. #define INTERNAL 3
  42. #endif
  43. #define DEFAULT 1
  44. #define EXTERNAL 0
  45. #endif
  46. // undefine stdlib's abs if encountered
  47. #ifdef abs
  48. #undef abs
  49. #endif
  50. #define min(a,b) ((a)<(b)?(a):(b))
  51. #define max(a,b) ((a)>(b)?(a):(b))
  52. #define abs(x) ((x)>0?(x):-(x))
  53. #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  54. #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
  55. #define radians(deg) ((deg)*DEG_TO_RAD)
  56. #define degrees(rad) ((rad)*RAD_TO_DEG)
  57. #define sq(x) ((x)*(x))
  58. #define interrupts() sei()
  59. #define noInterrupts() cli()
  60. #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
  61. #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
  62. #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
  63. #define lowByte(w) ((uint8_t) ((w) & 0xff))
  64. #define highByte(w) ((uint8_t) ((w) >> 8))
  65. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  66. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  67. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  68. #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
  69. typedef unsigned int word;
  70. #define bit(b) (1UL << (b))
  71. typedef uint8_t boolean;
  72. typedef uint8_t byte;
  73. void init(void);
  74. void pinMode(uint8_t, uint8_t);
  75. void digitalWrite(uint8_t, uint8_t);
  76. int digitalRead(uint8_t);
  77. int analogRead(uint8_t);
  78. void analogReference(uint8_t mode);
  79. void analogWrite(uint8_t, int);
  80. unsigned long millis(void);
  81. unsigned long micros(void);
  82. void delay(unsigned long);
  83. void delayMicroseconds(unsigned int us);
  84. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
  85. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
  86. uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
  87. void attachInterrupt(uint8_t, void (*)(void), int mode);
  88. void detachInterrupt(uint8_t);
  89. void setup(void);
  90. void loop(void);
  91. // Get the bit location within the hardware port of the given virtual pin.
  92. // This comes from the pins_*.c file for the active board configuration.
  93. #define analogInPinToBit(P) (P)
  94. // On the ATmega1280, the addresses of some of the port registers are
  95. // greater than 255, so we can't store them in uint8_t's.
  96. extern const uint16_t PROGMEM port_to_mode_PGM[];
  97. extern const uint16_t PROGMEM port_to_input_PGM[];
  98. extern const uint16_t PROGMEM port_to_output_PGM[];
  99. extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
  100. // extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
  101. extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
  102. extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
  103. // Get the bit location within the hardware port of the given virtual pin.
  104. // This comes from the pins_*.c file for the active board configuration.
  105. //
  106. // These perform slightly better as macros compared to inline functions
  107. //
  108. #define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
  109. #define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
  110. #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
  111. #define analogInPinToBit(P) (P)
  112. #define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
  113. #define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
  114. #define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
  115. #define NOT_A_PIN 0
  116. #define NOT_A_PORT 0
  117. #ifdef ARDUINO_MAIN
  118. #define PA 1
  119. #define PB 2
  120. #define PC 3
  121. #define PD 4
  122. #define PE 5
  123. #define PF 6
  124. #define PG 7
  125. #define PH 8
  126. #define PJ 10
  127. #define PK 11
  128. #define PL 12
  129. #endif
  130. #define NOT_ON_TIMER 0
  131. #define TIMER0A 1
  132. #define TIMER0B 2
  133. #define TIMER1A 3
  134. #define TIMER1B 4
  135. #define TIMER2 5
  136. #define TIMER2A 6
  137. #define TIMER2B 7
  138. #define TIMER3A 8
  139. #define TIMER3B 9
  140. #define TIMER3C 10
  141. #define TIMER4A 11
  142. #define TIMER4B 12
  143. #define TIMER4C 13
  144. #define TIMER4D 14
  145. #define TIMER5A 15
  146. #define TIMER5B 16
  147. #define TIMER5C 17
  148. #ifdef __cplusplus
  149. } // extern "C"
  150. #endif
  151. #ifdef __cplusplus
  152. #include "WCharacter.h"
  153. #include "WString.h"
  154. #include "HardwareSerial.h"
  155. uint16_t makeWord(uint16_t w);
  156. uint16_t makeWord(byte h, byte l);
  157. #define word(...) makeWord(__VA_ARGS__)
  158. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
  159. void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
  160. void noTone(uint8_t _pin);
  161. // WMath prototypes
  162. long random(long);
  163. long random(long, long);
  164. void randomSeed(unsigned int);
  165. long map(long, long, long, long, long);
  166. #endif
  167. #include "pins_arduino.h"
  168. #endif