MySensors Library & Examples  2.3.2-62-ge298769
PJON_LINUX_Interface.h
1 
2 /* PJON Linux Interface
3  13/04/2020 - callalilychen, Use termios2 for generic baudrate support
4  ___________________________________________________________________________
5 
6  Copyright 2018 Fred Larsen
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License. */
19 
20 #pragma once
21 
22 #if defined(LINUX) || defined(ANDROID)
23 
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 
36 extern "C" {
37  extern int tcflush (int __fd, int __queue_selector);
38 }
39 
40 #include <chrono>
41 #include <thread>
42 #include <sstream>
43 #include <math.h>
44 
45 #define OUTPUT 1
46 #define INPUT 0
47 #define HIGH 1
48 #define LOW 0
49 #define INPUT_PULLUP 0x2
50 #define LSBFIRST 1
51 #define MSBFIRST 2
52 
53 uint32_t micros();
54 
55 uint32_t millis();
56 
57 void delayMicroseconds(uint32_t delay_value);
58 
59 void delay(uint32_t delay_value_ms);
60 
61 /* Open serial port ----------------------------------------------------- */
62 
63 int serialOpen(const char *device, const int baud);
64 
65 /* Returns the number of bytes of data available to be read in the buffer */
66 
67 int serialDataAvailable(const int fd);
68 
69 /* Reads a character from the serial buffer ------------------------------- */
70 
71 int serialGetCharacter(const int fd);
72 
73 #ifndef PJON_LINUX_SEPARATE_DEFINITION
74 #include "PJON_LINUX_Interface.inl"
75 #endif
76 
77 /* Generic constants ---------------------------------------------------- */
78 
79 #ifndef A0
80 #define A0 0
81 #endif
82 
83 #ifndef LED_BUILTIN
84 #define LED_BUILTIN -1
85 #endif
86 
87 /* LINUX IO system calls ------------------------------------------------ */
88 
89 #if !defined(PJON_ANALOG_READ)
90 #define PJON_ANALOG_READ(P) 0
91 #endif
92 
93 #if !defined(PJON_IO_WRITE)
94 #define PJON_IO_WRITE(P, V)
95 #endif
96 
97 #if !defined(PJON_IO_READ)
98 #define PJON_IO_READ(P) 0
99 #endif
100 
101 #if !defined(PJON_IO_MODE)
102 #define PJON_IO_MODE(P, V)
103 #endif
104 
105 #if !defined(PJON_IO_PULL_DOWN)
106 #define PJON_IO_PULL_DOWN(P)
107 #endif
108 
109 /* Random --------------------------------------------------------------- */
110 
111 #ifndef PJON_RANDOM
112 #define PJON_RANDOM(randMax) (int)((1.0 + randMax) * rand() / ( RAND_MAX + 1.0 ) )
113 /* Scale rand()'s return value against RAND_MAX using doubles instead of
114  a pure modulus to have a more distributed result */
115 #endif
116 
117 #ifndef PJON_RANDOM_SEED
118 #define PJON_RANDOM_SEED srand
119 #endif
120 
121 /* Serial --------------------------------------------------------------- */
122 
123 #ifndef PJON_SERIAL_TYPE
124 #define PJON_SERIAL_TYPE int16_t
125 #endif
126 
127 #ifndef PJON_SERIAL_AVAILABLE
128 #define PJON_SERIAL_AVAILABLE(S) serialDataAvailable(S)
129 #endif
130 
131 #ifndef PJON_SERIAL_WRITE
132 #define PJON_SERIAL_WRITE(S, C) write(S, &C, 1)
133 #endif
134 
135 #ifndef PJON_SERIAL_READ
136 #define PJON_SERIAL_READ(S) serialGetCharacter(S)
137 #endif
138 
139 #ifndef PJON_SERIAL_FLUSH
140 #define PJON_SERIAL_FLUSH(S) tcflush(S, TCIOFLUSH)
141 #endif
142 
143 /* Timing --------------------------------------------------------------- */
144 
145 #ifndef PJON_DELAY
146 #define PJON_DELAY delay
147 #endif
148 
149 #ifndef PJON_DELAY_MICROSECONDS
150 #define PJON_DELAY_MICROSECONDS delayMicroseconds
151 #endif
152 
153 #ifndef PJON_MICROS
154 #define PJON_MICROS micros
155 #endif
156 
157 #ifndef PJON_MILLIS
158 #define PJON_MILLIS millis
159 #endif
160 #endif