Debian Codegraph @

cores/arduino/Arduino.h

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