- Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathCommon.h
190 lines (151 loc) · 5.05 KB
/
Common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Common.h - Common definitions for Arduino core
Copyright (c) 2017 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include<stdint.h>
#include<stdbool.h>
#ifdef __cplusplus
extern"C"{
#endif
voidyield(void);
typedefenum {
LOW = 0,
HIGH = 1,
CHANGE = 2,
FALLING = 3,
RISING = 4,
} PinStatus;
typedefenum {
INPUT = 0x0,
OUTPUT = 0x1,
INPUT_PULLUP = 0x2,
INPUT_PULLDOWN = 0x3,
OUTPUT_OPENDRAIN = 0x4,
} PinMode;
typedefenum {
LSBFIRST = 0,
MSBFIRST = 1,
} BitOrder;
#definePI3.1415926535897932384626433832795
#defineHALF_PI1.5707963267948966192313216916398
#defineTWO_PI6.283185307179586476925286766559
#defineDEG_TO_RAD0.017453292519943295769236907684886
#defineRAD_TO_DEG57.295779513082320876798154814105
#defineEULER2.718281828459045235360287471352
#defineSERIAL0x0
#defineDISPLAY0x1
#ifndef constrain
#defineconstrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#endif
#ifndef radians
#defineradians(deg) ((deg)*DEG_TO_RAD)
#endif
#ifndef degrees
#definedegrees(rad) ((rad)*RAD_TO_DEG)
#endif
#ifndef sq
#definesq(x) ((x)*(x))
#endif
typedefvoid (*voidFuncPtr)(void);
typedefvoid (*voidFuncPtrParam)(void*);
// interrupts() / noInterrupts() must be defined by the core
#definelowByte(w) ((uint8_t) ((w) & 0xff))
#definehighByte(w) ((uint8_t) ((w) >> 8))
#definebitRead(value, bit) (((value) >> (bit)) & 0x01)
#definebitSet(value, bit) ((value) |= (1UL << (bit)))
#definebitClear(value, bit) ((value) &= ~(1UL << (bit)))
#definebitToggle(value, bit) ((value) ^= (1UL << (bit)))
#definebitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
#ifndef bit
#definebit(b) (1UL << (b))
#endif
/* TODO: request for removal */
typedefbool boolean;
typedefuint8_t byte;
typedefuint16_t word;
voidinit(void);
voidinitVariant(void);
#ifndef HOST
intatexit(void (*func)()) __attribute__((weak));
#endif
intmain() __attribute__((weak));
#ifdef EXTENDED_PIN_MODE
// Platforms who want to declare more than 256 pins need to define EXTENDED_PIN_MODE globally
typedefuint32_tpin_size_t;
#else
typedefuint8_tpin_size_t;
#endif
voidpinMode(pin_size_t pinNumber, PinMode pinMode);
voiddigitalWrite(pin_size_t pinNumber, PinStatus status);
PinStatus digitalRead(pin_size_t pinNumber);
intanalogRead(pin_size_t pinNumber);
voidanalogReference(uint8_t mode);
voidanalogWrite(pin_size_t pinNumber, int value);
unsignedlongmillis(void);
unsignedlongmicros(void);
voiddelay(unsignedlong);
voiddelayMicroseconds(unsignedint us);
unsignedlongpulseIn(pin_size_t pin, uint8_t state, unsignedlong timeout);
unsignedlongpulseInLong(pin_size_t pin, uint8_t state, unsignedlong timeout);
voidshiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val);
uint8_tshiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder);
voidattachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode);
voidattachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void* param);
voiddetachInterrupt(pin_size_t interruptNumber);
voidsetup(void);
voidloop(void);
#ifdef __cplusplus
} // extern "C"
#endif
#ifdef __cplusplus
template<classT, classL>
automin(const T& a, const L& b) -> decltype((b < a) ? b : a)
{
return (b < a) ? b : a;
}
template<classT, classL>
automax(const T& a, const L& b) -> decltype((b < a) ? b : a)
{
return (a < b) ? b : a;
}
#else
#ifndef min
#definemin(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#endif
#ifndef max
#definemax(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif
#endif
#ifdef __cplusplus
/* C++ prototypes */
uint16_tmakeWord(uint16_t w);
uint16_tmakeWord(byte h, byte l);
#defineword(...) makeWord(__VA_ARGS__)
unsignedlongpulseIn(uint8_t pin, uint8_t state, unsignedlong timeout = 1000000L);
unsignedlongpulseInLong(uint8_t pin, uint8_t state, unsignedlong timeout = 1000000L);
voidtone(uint8_t _pin, unsignedint frequency, unsignedlong duration = 0);
voidnoTone(uint8_t _pin);
// WMath prototypes
longrandom(long);
longrandom(long, long);
voidrandomSeed(unsignedlong);
longmap(long, long, long, long, long);
#endif// __cplusplus