MySensors Library & Examples  2.3.2-62-ge298769
PJON_WINX86_Interface.h
1 
2 /* PJON Windows x86 Interface
3  _____________________________________________________________________________
4 
5  Copyright 2018 Zbigniew Zasieczny [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(_WIN32)
22 
23 #include <chrono>
24 #include <thread>
25 #include <sstream>
26 #include "Serial/Serial.h"
27 
28 #define OUTPUT 1
29 #define INPUT 0
30 #define HIGH 1
31 #define LOW 0
32 #define INPUT_PULLUP 0x2
33 #define LSBFIRST 1
34 #define MSBFIRST 2
35 
36 auto start_ts = std::chrono::high_resolution_clock::now();
37 auto start_ts_ms = std::chrono::high_resolution_clock::now();
38 
39 uint32_t micros()
40 {
41  auto elapsed_usec =
42  std::chrono::duration_cast<std::chrono::microseconds>(
43  std::chrono::high_resolution_clock::now() - start_ts
44  ).count();
45 
46  if(elapsed_usec >= UINT32_MAX) {
47  start_ts = std::chrono::high_resolution_clock::now();
48  return 0;
49  } else {
50  return (uint32_t) elapsed_usec;
51  }
52 };
53 
54 uint32_t millis()
55 {
56  return (uint32_t)
57  std::chrono::duration_cast<std::chrono::milliseconds>(
58  std::chrono::high_resolution_clock::now() - start_ts_ms
59  ).count();
60 };
61 
62 
63 void delayMicroseconds(uint32_t delay_value)
64 {
65  auto begin_ts = std::chrono::high_resolution_clock::now();
66  while(true) {
67  auto elapsed_usec =
68  std::chrono::duration_cast<std::chrono::microseconds>(
69  std::chrono::high_resolution_clock::now() - begin_ts
70  ).count();
71  if(elapsed_usec >= delay_value) {
72  break;
73  }
74  std::this_thread::sleep_for(std::chrono::microseconds(50));
75  }
76 };
77 
78 void delay(uint32_t delay_value_ms)
79 {
80  std::this_thread::sleep_for(std::chrono::milliseconds(delay_value_ms));
81 }
82 
83 /* Generic constants ---------------------------------------------------- */
84 
85 #ifndef A0
86 #define A0 0
87 #endif
88 
89 #ifndef LED_BUILTIN
90 #define LED_BUILTIN -1
91 #endif
92 
93 /* Fallback to WINDOWS-specific functions -------------------------------- */
94 
95 #if !defined(PJON_ANALOG_READ)
96 #define PJON_ANALOG_READ(P) 0
97 #endif
98 
99 #if !defined(PJON_IO_WRITE)
100 // Avoid warning C4390 (empty controlled statement)
101 #define PJON_IO_WRITE(P, V) (void)0
102 #endif
103 
104 #if !defined(PJON_IO_READ)
105 #define PJON_IO_READ(P) 0
106 #endif
107 
108 #if !defined(PJON_IO_MODE)
109 // Avoid warning C4390 (empty controlled statement)
110 #define PJON_IO_MODE(P, V) (void)0
111 #endif
112 
113 #if !defined(PJON_IO_PULL_DOWN)
114 // Avoid warning C4390 (empty controlled statement)
115 #define PJON_IO_PULL_DOWN(P) (void)0
116 #endif
117 
118 /* Random ----------------------------------------------------------------- */
119 
120 #ifndef PJON_RANDOM
121 #define PJON_RANDOM(randMax) (int)((1.0 + randMax) * rand() / ( RAND_MAX + 1.0 ) )
122 /* Scale rand()'s return value against RAND_MAX using doubles instead of
123  a pure modulus to have a more distributed result */
124 #endif
125 
126 #ifndef PJON_RANDOM_SEED
127 #define PJON_RANDOM_SEED srand
128 #endif
129 
130 
131 /* Serial ----------------------------------------------------------------- */
132 
133 #ifndef PJON_SERIAL_TYPE
134 #define PJON_SERIAL_TYPE Serial *
135 #endif
136 
137 #ifndef PJON_SERIAL_AVAILABLE
138 #define PJON_SERIAL_AVAILABLE(S) S->serialDataAvail()
139 #endif
140 
141 #ifndef PJON_SERIAL_WRITE
142 #define PJON_SERIAL_WRITE(S, C) S->writeByte(&C)
143 #endif
144 
145 #ifndef PJON_SERIAL_READ
146 #define PJON_SERIAL_READ(S) S->getByte()
147 #endif
148 
149 #ifndef PJON_SERIAL_FLUSH
150 #define PJON_SERIAL_FLUSH(S) S->flush()
151 #endif
152 
153 
154 /* Timing ----------------------------------------------------------------- */
155 
156 #ifndef PJON_DELAY
157 #define PJON_DELAY delay
158 #endif
159 
160 #ifndef PJON_DELAY_MICROSECONDS
161 #define PJON_DELAY_MICROSECONDS delayMicroseconds
162 #endif
163 
164 #ifndef PJON_MICROS
165 #define PJON_MICROS micros
166 #endif
167 
168 #ifndef PJON_MILLIS
169 #define PJON_MILLIS millis
170 #endif
171 #endif