MeshProtocolSimulator  1.0.0
Simulation.h
1 #pragma once
2 
3 #include <stdio.h>
4 #include <iostream>
5 #include <fstream>
6 #include <iomanip>
7 #include <time.h>
8 #include <stdlib.h>
9 #define NOMINMAX
10 #include <windows.h>
11 #include "Printable.h"
12 #include <chrono>
13 #include <ctime>
14 #include <string>
15 #include <mutex>
16 //#include <random>
17 #include "date.h"
18 #include "TextColors.h"
19 #include <realtimeapiset.h>
20 #include <processthreadsapi.h>
21 
22 #include <thread>
23 
24 
25 using namespace date;
26 
27 #define OSTREAM printStream
28 
29 #define STREAM OSTREAM
30 #define Print OSTREAM
31 
32 template <typename T>
34 {
35  template <typename U> // must be template to get SFINAE fall-through...
36  static auto test(const U* u) -> decltype(std::cout << *u);
37 
38  static auto test(...)->std::false_type;
39 
40 public:
41  enum { value = !std::is_same_v<decltype(test((T*)0)), std::false_type> };
42 };
43 
44 template<typename T> typename std::enable_if<std::is_floating_point<T>::value, char*>::type ftoa(T f, char * buf, int precision) {
45  char * ptr = buf;
46  char * p = ptr;
47  char * p1;
48  char c;
49  long intPart;
50 
51 #define MAX_PRECISION (10)
52  static const double rounders[MAX_PRECISION + 1] =
53  {
54  0.5, // 0
55  0.05, // 1
56  0.005, // 2
57  0.0005, // 3
58  0.00005, // 4
59  0.000005, // 5
60  0.0000005, // 6
61  0.00000005, // 7
62  0.000000005, // 8
63  0.0000000005, // 9
64  0.00000000005 // 10
65  };
66 
67  // check precision bounds
68  if (precision > MAX_PRECISION)
69  precision = MAX_PRECISION;
70 
71  // sign stuff
72  if (f < 0)
73  {
74  f = -f;
75  *ptr++ = '-';
76  }
77 
78  if (precision < 0) // negative precision == automatic precision guess
79  {
80  if (f < 1.0) precision = 6;
81  else if (f < 10.0) precision = 5;
82  else if (f < 100.0) precision = 4;
83  else if (f < 1000.0) precision = 3;
84  else if (f < 10000.0) precision = 2;
85  else if (f < 100000.0) precision = 1;
86  else precision = 0;
87  }
88 
89  // round value according the precision
90  if (precision)
91  f += rounders[precision];
92 
93  // integer part...
94  intPart = (long)f;
95  f -= intPart;
96 
97  if (!intPart)
98  *ptr++ = '0';
99  else
100  {
101  // save start pointer
102  p = ptr;
103 
104  // convert (reverse order)
105  while (intPart)
106  {
107  *p++ = '0' + intPart % 10;
108  intPart /= 10;
109  }
110 
111  // save end pos
112  p1 = p;
113 
114  // reverse result
115  while (p > ptr)
116  {
117  c = *--p;
118  *p = *ptr;
119  *ptr++ = c;
120  }
121 
122  // restore end pos
123  ptr = p1;
124  }
125 
126  // decimal part
127  if (precision)
128  {
129  // place decimal point
130  *ptr++ = '.';
131 
132  // convert
133  while (precision--)
134  {
135  f *= 10.0;
136  c = (char)f;
137  *ptr++ = '0' + c;
138  f -= c;
139  }
140  }
141 
142  // terminating zero
143  *ptr = 0;
144 
145  return buf;
146 }
147 
148 /*template<typename Clock, typename Duration>
149 std::ostream& operator<<(std::ostream& stream,
150  const std::chrono::time_point<Clock, Duration>& time_point) {
151  const time_t time = Clock::to_time_t(time_point);
152 #if __GNUC__ > 4 || \
153  ((__GNUC__ == 4) && __GNUC_MINOR__ > 8 && __GNUC_REVISION__ > 1)
154  // Maybe the put_time will be implemented later?
155  struct tm tm;
156  localtime_r(&time, &tm);
157  return stream << std::put_time(&tm, "%c"); // Print standard date&time
158 #else
159 #ifdef _WIN32
160  char buffer[26];
161  ctime_s(buffer , 26, &time);
162  buffer[24] = '\0'; // Removes the newline that is added
163  return stream << buffer;
164 #else
165  char buffer[26];
166  ctime_r(&time, buffer);
167  buffer[24] = '\0'; // Removes the newline that is added
168  return stream << buffer;
169 #endif
170 #endif
171 }*/
172 
173 #define HEX (16)
174 #define DEC (10)
175 #define OCT (8)
176 #define BIN (2)
177 
178 
180 public:
181  size_t print(const Printable& x);
182 
183  size_t print(char data);
184 
185  size_t print(int32_t data, uint8_t base = DEC);
186 
187  size_t print(uint32_t data, uint8_t base = DEC);
188 
189  size_t print(double data);
190 
191  size_t print(std::string data);
192 
193  size_t print(const char* data);
194 
195  template <typename T> size_t println(T data) {
196  size_t ret = print(data);
197  ret += println();
198  return ret;
199  }
200 
201  size_t println();
202 
203  size_t write(char val);
204 
205  size_t write(uint8_t* val, size_t count);
206 
207  size_t write(char* val, size_t count);
208 
209  printStream& operator<<(const Printable& data);
210 
211  printStream& operator<<(std::chrono::nanoseconds data);
212 
213  template<typename T> std::enable_if_t<is_streamable_cout<T>::value, printStream&> operator<<(T data)
214  {
215  std::cout << data;
216  return *this;
217  }
218 };
219 
220 class String : public std::string {
221 public:
222  String();
223 
224  String(String& str);
225 
226  String(std::string str);
227 
228  bool startsWith(const char* txt);
229 
230  bool startsWith(std::string txt);
231 
232  int indexOf(char c) const;
233 
234  int indexOf(char ch, unsigned int fromIndex) const;
235 
236  int indexOf(const char* s2) const {
237  return indexOf(String(s2));
238  }
239 
240  int indexOf(const char* s2, unsigned int fromIndex) const {
241  return indexOf(String(s2), fromIndex);
242  }
243 
244  int indexOf(const String& s2) const;
245 
246  int indexOf(const String& s2, unsigned int fromIndex) const;
247 
248  String substring(size_t from, size_t to);
249 
250  String substring(size_t from);
251 
252  String trimStart();
253 
254  String trimEnd();
255 
256  String trim();
257 };
258 
259 struct membuf : std::streambuf
260 {
261  membuf(char* p, size_t size)
262  {
263  setp(p, p + size); // set start end end pointers
264  }
265  size_t written() { return pptr() - pbase(); } // how many bytes were really written?
266 };
267 
268 int32_t random();
269 
270 int32_t random(int32_t to);
271 
272 int32_t random(int32_t from, int32_t to);
273 
283 class ConsoleItem {
284 public:
285 
286 };
287 
288 #define endl "\r\n"
289 
290 inline bool false_ret() {
291  return false;
292 }
293 
294 #define __get_PRIMASK false_ret
295 #define __disable_irq()
296 #define __enable_irq()
297 
298 extern printStream pout;
299 
300 
301 template <typename T>
303 {
304  template <typename U> // must be template to get SFINAE fall-through...
305  static auto test(const U* u) -> decltype(pout << *u);
306 
307  static auto test(...)->std::false_type;
308 
309 public:
310  enum { value = !std::is_same_v<decltype(test((T*)0)), std::false_type> };
311 };
ConsoleItem
Derive from this class to create class, which can be controlled through SerialConsole.
Definition: Simulation.h:283
membuf
Definition: Simulation.h:259
printStream
Definition: Simulation.h:179
String
Definition: Simulation.h:220
is_streamable_cout
Definition: Simulation.h:33
Printable
Definition: Printable.h:5
is_streamable
Definition: Simulation.h:302