Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
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,
35 RISING = 4,
36} PinStatus;
37
38typedef enum {
39 INPUT = 0x0,
40 OUTPUT = 0x1,
44} PinMode;
45
46typedef enum {
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
116void digitalWrite(pin_size_t pinNumber, PinStatus status);
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
@ LSBFIRST
Definition Common.h:47
@ MSBFIRST
Definition Common.h:48
@ OUTPUT
Definition Common.h:40
@ INPUT_PULLDOWN
Definition Common.h:42
@ INPUT_PULLUP
Definition Common.h:41
@ OUTPUT_OPENDRAIN
Definition Common.h:43
@ INPUT
Definition Common.h:39
@ HIGH
Definition Common.h:32
@ LOW
Definition Common.h:31
@ CHANGE
Definition Common.h:33
@ FALLING
Definition Common.h:34
@ RISING
Definition Common.h:35
unsigned long pulseInLong(pin_size_t pin, uint8_t state, unsigned long timeout)
Same as pulseIn(); kept for Arduino compatibility.
void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val)
Sends a byte one bit at a time, pulsing the clock pin after each bit.
uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder)
Reads a byte one bit at a time, pulsing the clock pin for each bit.
unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout)
Measures how long a pulse on a pin lasts.
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration=0)
Plays a square wave (50 % duty) on a pin, for a buzzer or speaker.
void noTone(uint8_t _pin)
Stops the tone, if one is playing on this pin.
void analogReference(uint8_t mode)
Does nothing: the references are fixed on Phyduino boards.
int analogRead(pin_size_t pinNumber)
Reads the voltage on an analog pin.
void analogWrite(pin_size_t pinNumber, int value)
Outputs a PWM signal: a square wave whose duty cycle sets the average voltage.
#define word(...)
Same as makeWord().
Definition Common.h:176
uint16_t makeWord(uint16_t w)
Returns w as a 16-bit word.
void digitalWrite(pin_size_t pinNumber, PinStatus status)
Sets an output pin HIGH (3.3 V) or LOW (0 V).
PinStatus digitalRead(pin_size_t pinNumber)
Reads the level on a pin.
void pinMode(pin_size_t pinNumber, PinMode pinMode)
Sets a pin as an input or an output.
void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode)
Calls a function when the level on a pin changes.
void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void *param)
Same as attachInterrupt(), but the function is called with param.
void detachInterrupt(pin_size_t interruptNumber)
Stops calling the function attached to a pin.
long map(long, long, long, long, long)
Re-maps a number from one range to another, e.g. 0-1023 to 0-255.
auto max(const T &a, const L &b) -> decltype((b< a) ? b :a)
The larger of two values.
Definition Common.h:151
auto min(const T &a, const L &b) -> decltype((b< a) ? b :a)
The smaller of two values.
Definition Common.h:145
long random(long)
Pseudo-random number from 0 up to, but not including, the argument.
void randomSeed(unsigned long)
Starts the random sequence at a new point.
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void init(void)
Core start-up, before setup(): sets the CPU speed (Tools > CPU Speed), the ADC, USB and the inter-cor...
void initVariant(void)
Board start-up, after init(): the on-board ADC and the USB-PD controller.
void yield(void)
Called while delay() or USB Serial is waiting.
void setup(void)
Written by the sketch: runs once after reset, on core 0.
unsigned long millis(void)
Milliseconds since the board started.
void delayMicroseconds(unsigned int us)
Waits a number of microseconds, exactly, by busy-waiting.
unsigned long micros(void)
Microseconds since the board started, with 1 µs resolution (4 µs on an Uno).
void delay(unsigned long)
Waits a number of milliseconds.
BitOrder
Bit order for shiftOut(), shiftIn() and SPISettings.
Definition Common.h:46
PinMode
Pin modes for pinMode().
Definition Common.h:38
PinStatus
Pin levels for digitalWrite() and digitalRead(), and interrupt modes for attachInterrupt().
Definition Common.h:30
void(* voidFuncPtrParam)(void *)
Function taking a void*, as used by attachInterruptParam().
Definition Common.h:78
uint8_t pin_size_t
Type for Arduino pin numbers (uint8_t).
Definition Common.h:112
uint8_t byte
8-bit unsigned number, 0-255 (uint8_t).
Definition Common.h:97
bool boolean
Old name for bool.
Definition Common.h:96
void(* voidFuncPtr)(void)
Function with no arguments and no return value, as used by attachInterrupt().
Definition Common.h:77