arduino-emulator
Loading...
Searching...
No Matches
Common.h
1/*
2 Common.h - Common definitions for Arduino core
3 Copyright (c) 2017 Arduino LLC. 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#pragma once
21#include <stdint.h>
22#include <stdbool.h>
23
24#ifdef __cplusplus
25extern "C"{
26#endif
27
28void yield(void);
29
30typedef enum {
31 LOW = 0,
32 HIGH = 1,
33 CHANGE = 2,
34 FALLING = 3,
35 RISING = 4,
36} PinStatus;
37
38typedef enum {
39 INPUT = 0x0,
40 OUTPUT = 0x1,
41 INPUT_PULLUP = 0x2,
42 INPUT_PULLDOWN = 0x3,
43 OUTPUT_OPENDRAIN = 0x4,
44} PinMode;
45
46typedef enum {
47 LSBFIRST = 0,
48 MSBFIRST = 1,
49} BitOrder;
50
51#define PI 3.1415926535897932384626433832795
52#define HALF_PI 1.5707963267948966192313216916398
53#define TWO_PI 6.283185307179586476925286766559
54#define DEG_TO_RAD 0.017453292519943295769236907684886
55#define RAD_TO_DEG 57.295779513082320876798154814105
56#define EULER 2.718281828459045235360287471352
57
58#define SERIAL 0x0
59#define DISPLAY 0x1
60
61#ifndef constrain
62#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
63#endif
64
65#ifndef radians
66#define radians(deg) ((deg)*DEG_TO_RAD)
67#endif
68
69#ifndef degrees
70#define degrees(rad) ((rad)*RAD_TO_DEG)
71#endif
72
73#ifndef sq
74#define sq(x) ((x)*(x))
75#endif
76
77typedef void (*voidFuncPtr)(void);
78typedef void (*voidFuncPtrParam)(void*);
79
80// interrupts() / noInterrupts() must be defined by the core
81
82#define lowByte(w) ((uint8_t) ((w) & 0xff))
83#define highByte(w) ((uint8_t) ((w) >> 8))
84
85#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
86#define bitSet(value, bit) ((value) |= (1UL << (bit)))
87#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
88#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
89#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
90
91#ifndef bit
92#define bit(b) (1UL << (b))
93#endif
94
95/* TODO: request for removal */
96typedef bool boolean;
97typedef uint8_t byte;
98typedef uint16_t word;
99
100void init(void);
101void initVariant(void);
102
103#ifndef HOST
104int atexit(void (*func)()) __attribute__((weak));
105#endif
106int main() __attribute__((weak));
107
108#ifdef EXTENDED_PIN_MODE
109// Platforms who want to declare more than 256 pins need to define EXTENDED_PIN_MODE globally
110typedef uint32_t pin_size_t;
111#else
112typedef uint8_t pin_size_t;
113#endif
114
115void pinMode(pin_size_t pinNumber, PinMode pinMode);
116void digitalWrite(pin_size_t pinNumber, PinStatus status);
117PinStatus digitalRead(pin_size_t pinNumber);
118int analogRead(pin_size_t pinNumber);
119void analogReference(uint8_t mode);
120void analogWrite(pin_size_t pinNumber, int value);
121
122unsigned long millis(void);
123unsigned long micros(void);
124void delay(unsigned long);
125void delayMicroseconds(unsigned int us);
126unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout);
127unsigned long pulseInLong(pin_size_t pin, uint8_t state, unsigned long timeout);
128
129void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val);
130uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder);
131
132void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode);
133void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void* param);
134void detachInterrupt(pin_size_t interruptNumber);
135
136void setup(void);
137void loop(void);
138
139#ifdef __cplusplus
140} // extern "C"
141#endif
142
143#ifdef __cplusplus
144 template<class T, class L>
145 auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
146 {
147 return (b < a) ? b : a;
148 }
149
150 template<class T, class L>
151 auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
152 {
153 return (a < b) ? b : a;
154 }
155#else
156#ifndef min
157#define min(a,b) \
158 ({ __typeof__ (a) _a = (a); \
159 __typeof__ (b) _b = (b); \
160 _a < _b ? _a : _b; })
161#endif
162#ifndef max
163#define max(a,b) \
164 ({ __typeof__ (a) _a = (a); \
165 __typeof__ (b) _b = (b); \
166 _a > _b ? _a : _b; })
167#endif
168#endif
169
170#ifdef __cplusplus
171
172/* C++ prototypes */
173uint16_t makeWord(uint16_t w);
174uint16_t makeWord(byte h, byte l);
175
176#define word(...) makeWord(__VA_ARGS__)
177
178unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
179unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
180
181void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
182void noTone(uint8_t _pin);
183
184// WMath prototypes
185long random(long);
186long random(long, long);
187void randomSeed(unsigned long);
188long map(long, long, long, long, long);
189
190#endif // __cplusplus