MySensors Library & Examples  2.3.2-62-ge298769
PJON_ARDUINO_Interface.h
1 
2 /* PJON Arduino Interface
3  ___________________________________________________________________________
4 
5  Copyright 2018 Giovanni Blu Mitolo [email protected]
6 
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0
12 
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License. */
18 
19 #pragma once
20 
21 #if defined(ARDUINO)
22 #ifdef ARDUINO_ESP8266_WEMOS_D1MINI
23 #define PJON_ESP // WeMos mini and D1 R2
24 #elif ARDUINO_ESP8266_ESP01
25 #define PJON_ESP // Generic ESP's use for 01's
26 #elif ARDUINO_ESP8266_NODEMCU
27 #define PJON_ESP // Wio Link and NodeMCU 1.0 (also 0.9), use for ESP12
28 #elif ESP8266
29 #define PJON_ESP
30 #elif defined(ESP32)
31 #define PJON_ESP
32 #endif
33 
34 #include <Arduino.h>
35 #include "PJON_IO.h"
36 
37 #ifdef __STM32F1__
38 #ifndef A0
39 #define A0 0
40 #endif
41 #endif
42 
43 /* Generic constants ---------------------------------------------------- */
44 
45 #ifndef LED_BUILTIN
46 #define LED_BUILTIN -1
47 #endif
48 
49 /* Arduino IO system calls ---------------------------------------------- */
50 
51 #ifndef PJON_ANALOG_READ
52 #define PJON_ANALOG_READ analogRead
53 #endif
54 
55 #ifndef PJON_IO_WRITE
56 #define PJON_IO_WRITE digitalWrite
57 #endif
58 
59 #ifndef PJON_IO_READ
60 #define PJON_IO_READ digitalRead
61 #endif
62 
63 #ifndef PJON_IO_MODE
64 #define PJON_IO_MODE pinMode
65 #endif
66 
67 #ifndef PJON_IO_PULL_DOWN
68 #define PJON_IO_PULL_DOWN(P) \
69  do { \
70  digitalWrite(P, LOW); \
71  pinMode(P, INPUT); \
72  } while(0)
73 #endif
74 
75 /* Random --------------------------------------------------------------- */
76 
77 #ifdef __STM32F1__
78 #ifndef PJON_RANDOM
79 #define PJON_RANDOM(R) random(R ? R : R + 1)
80 #endif
81 #else
82 #ifndef PJON_RANDOM
83 #define PJON_RANDOM random
84 #endif
85 #endif
86 
87 #ifndef PJON_RANDOM_SEED
88 #define PJON_RANDOM_SEED randomSeed
89 #endif
90 
91 /* Serial --------------------------------------------------------------- */
92 
93 #ifndef PJON_SERIAL_TYPE
94 #define PJON_SERIAL_TYPE Stream *
95 #endif
96 
97 #ifndef PJON_SERIAL_AVAILABLE
98 #define PJON_SERIAL_AVAILABLE(S) S->available()
99 #endif
100 
101 #ifndef PJON_SERIAL_WRITE
102 #define PJON_SERIAL_WRITE(S, C) S->write(C)
103 #endif
104 
105 #ifndef PJON_SERIAL_READ
106 #define PJON_SERIAL_READ(S) S->read()
107 #endif
108 
109 #ifndef PJON_SERIAL_FLUSH
110 #define PJON_SERIAL_FLUSH(S) S->flush()
111 #endif
112 
113 /* Timing --------------------------------------------------------------- */
114 
115 #ifndef PJON_DELAY
116 #define PJON_DELAY delay
117 #endif
118 
119 #ifndef PJON_DELAY_MICROSECONDS
120 #define PJON_DELAY_MICROSECONDS delayMicroseconds
121 #endif
122 
123 #ifndef PJON_MICROS
124 #define PJON_MICROS micros
125 #endif
126 
127 #ifndef PJON_MILLIS
128 #define PJON_MILLIS millis
129 #endif
130 
131 /* Set ADC prescale functions ------------------------------------------- */
132 
133 #ifndef cbi
134 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
135 #endif
136 
137 #ifndef sbi
138 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
139 #endif
140 
141 /* Sockets -------------------------------------------------------------- */
142 
143 #define HAS_ETHERNETUDP
144 
145 /* Byte order translation functions ------------------------------------- */
146 
147 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && !defined(PJON_ESP)
148 
149 #ifndef htons // Host to network short
150 #define htons(x) ( ((uint16_t)(x) << 8 & 0xFF00) | \
151  ((uint16_t)(x) >> 8 & 0x00FF) )
152 #endif
153 
154 #ifndef ntohs // Network to host short
155 #define ntohs(x) htons(x)
156 #endif
157 
158 #ifndef htonl // Host to network long
159 #define htonl(x) ( ((uint32_t)(x) << 24 & 0xFF000000UL) | \
160  ((uint32_t)(x) << 8 & 0x00FF0000UL) | \
161  ((uint32_t)(x) >> 8 & 0x0000FF00UL) | \
162  ((uint32_t)(x) >> 24 & 0x000000FFUL) )
163 #endif
164 
165 #ifndef ntohl // Network to host long
166 #define ntohl(x) htonl(x)
167 #endif
168 #endif
169 
170 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && !defined(PJON_ESP)
171 #define htons(x) (x)
172 #define htonl(x) (x)
173 #define ntohs(x) (x)
174 #define ntohl(x) (x)
175 #endif
176 
177 #endif