LambLisp 01 Red Fox Alpha
Lisp For Real-Time Control
Loading...
Searching...
No Matches
ll_platform_generic.h
1#ifndef LL_PLATFORM_GENERIC_H
2#define LL_PLATFORM_GENERIC_H
3
4#include <stdio.h>
5#include <stdint.h>
6#include <stdarg.h>
7#include "assert.h"
8#include "unistd.h"
9
10#if LL_ARDUINO
11#include "Arduino.h"
12#endif
13
14#if LL_FAKE_ARDUINO
15#include <string.h>
16
17unsigned long millis();
18unsigned long micros();
19void delay_ms(unsigned long ms);
20
21static void delay(unsigned long ms) { delay_ms(ms); }
22
23typedef uint8_t byte;
24
25class LL_String {
26public:
27
28 LL_String() { ptr = 0; set(""); }
29 LL_String(const char *other) { ptr = 0; set(other); }
30 LL_String(const char *other, int n) { ptr = 0; set(other, n); }
31 LL_String(const LL_String &other) { ptr = 0; set(other.ptr); }
32
33 ~LL_String() { if (ptr) { delete[] ptr; ptr = 0; } }
34
35 void set(const char *s) { set(s, strlen(s)); }
36
37 void set(const char *s, int n) {
38 if (ptr) { delete[] ptr; ptr = 0; }
39 char *p = ptr = new char[n + 1];
40 while (n--) *p++ = *s++;
41 *p = '\0';
42 }
43
44 LL_String concat(const char *s1, const char *s2) {
45 int n1 = strlen(s1);
46 int n2 = strlen(s2);
47 char *s3 = new char[n1 + n2 + 1];
48
49 char *p = s3;
50 while (n1--) *p++ = *s1++;
51 while (n2--) *p++ = *s2++;
52 *p = '\0';
53 return s3;
54 }
55
56 LL_String &append(const char *s1) {
57 const char *s0 = ptr;
58 int n0 = s0 ? strlen(s0) : 0;
59 int n1 = strlen(s1);
60 char *s2 = new char[n0 + n1 + 1];
61
62 char *p = s2;
63 while (n0--) *p++ = *s0++;
64 while (n1--) *p++ = *s1++;
65 *p = '\0';
66
67 if (ptr) { delete[] ptr; ptr = 0; }
68 ptr = s2;
69 return *this;
70 }
71
72 LL_String substring(int start, int len) { return LL_String(&(ptr[start]), len); }
73
74 LL_String &operator=(const char *other) { set(other); return *this; }
75 LL_String &operator=(const LL_String &other) { set(other.ptr); return *this; }
76
77 bool operator==(const char *other) { return strcmp(ptr, other) == 0; }
78 bool operator!=(const char *other) { return strcmp(ptr, other) != 0; }
79 bool operator==(const LL_String &other) { return strcmp(ptr, other.ptr) == 0; }
80 bool operator!=(const LL_String &other) { return strcmp(ptr, other.ptr) != 0; }
81 char &operator[](int ix) { return ptr[ix]; }
82
83 LL_String operator+(const char *other) { return concat(ptr, other); }
84 LL_String operator+(const LL_String &other) { return concat(ptr, other.ptr); }
85 LL_String operator+(char c) { char cstr[2]; cstr[0] = c; cstr[1] = '\0'; return concat(ptr, cstr); }
86
87 LL_String &operator+=(const char *other) { return append(other); }
88 LL_String &operator+=(const LL_String &other) { return append(other.ptr); }
89 LL_String &operator+=(char c) { char cstr[2]; cstr[0] = c; cstr[1] = '\0'; return append(cstr); }
90
91 char *c_str() const { return ptr; }
92 int length() { return strlen(ptr); }
93
94private:
95 char *ptr;
96};
97
98typedef LL_String String;
99#endif
100
103#define NOTUSED __attribute__((__unused__))
104#define INLINE __attribute__((__inline__))
105#define NOINLINE __attribute__((__noinline__))
106#define CHECKPRINTF __attribute__((format(printf, 1, 2)))
107#define CHECKPRINTF_pos2 __attribute__((format(printf, 2, 3)))
108#define CHECKPRINTF_pos3 __attribute__((format(printf, 3, 4)))
109#define CHECKPRINTF_pos4 __attribute__((format(printf, 4, 5)))
110
113const unsigned long toString_MAX_LENGTH = 8192;
114String toString(const char *fmt, ...) CHECKPRINTF;
116
117
118#define ME(_me_) NOTUSED const char me[] = _me_
119#define isdef(sym) (#sym[0])
120
121void global_printf(const char *fmt, ...);
122
124
143
144typedef unsigned char Byte_t;
145typedef bool Bool_t;
146typedef char Char_t;
147
148typedef Char_t const *Charst_t;
149typedef Byte_t const *Bytest_t;
150typedef Char_t *CharVec_t;
151typedef Byte_t *ByteVec_t;
152
153#if LL_AMD64
154typedef unsigned long Word_t;
155typedef int Int_t;
156typedef double Real_t;
157typedef void *Ptr_t;
158#endif
159
160#if LL_ARM64
161typedef unsigned long Word_t;
162typedef int Int_t;
163typedef float Real_t;
164typedef void *Ptr_t;
165#endif
166
167#if LL_ESP32
168typedef unsigned Word_t;
169typedef int Int_t;
170typedef double Real_t;
171typedef void *Ptr_t;
172#endif
173
174static_assert(sizeof(Word_t) == sizeof(Ptr_t), "Word_t size must be == Ptr_t size\n");
175static_assert(sizeof(Word_t) >= sizeof(Int_t), "Word_t size must be >= Int_t size\n");
176static_assert(sizeof(Real_t) <= 2*sizeof(Word_t), "Real_t size must be <= 2 * Word_t size\n");
178
179//The AsciiConverter class mitigates printf formatting problems caused by different int sizes on different processors, when using printf-style format string (%u, %d %lu %ld etc).
180class AsciiConverter {
181public:
182
183 char *dec(Word_t n) {
184 Int_t ix = buffer_size - 1;
185 char *ptr = &(buffer[ix]);
186
187 *ptr = '\0';
188 do {
189 Word_t d = n % 10;
190 n /= 10;
191 char c = d + '0';
192 *--ptr = c;
193 } while (n);
194 return ptr;
195 }
196
197 char *dec(Int_t n) {
198 if (n >= 0) return dec((Word_t) n);
199 n = -n;
200 char *ptr = dec((Word_t) n);
201 *--ptr = '-';
202 return ptr;
203 }
204
205 char *hex(Word_t n) {
206 Int_t ix = buffer_size - 1;
207 char *ptr = &(buffer[ix]);
208 Int_t nibs = sizeof(Word_t) * 2;
209
210 *ptr = '\0';
211 while (nibs--) {
212 Word_t d = n & 0x0f;
213 n >>= 4;
214 char c = (d < 10) ? (d + '0') : (d - 10 + 'a');
215 *--ptr = c;
216 }
217 return ptr;
218
219 }
220
221 char *dec(Real_t n) {
222 snprintf(buffer, buffer_size, "%f", (double) n);
223 return buffer;
224 }
225
226private:
227 static const Int_t buffer_size = 128;
228 char buffer[buffer_size];
229};
230
231extern AsciiConverter ascii;
232
233class LambPlatform {
234public:
235
238 LambPlatform() {}
239 ~LambPlatform() { end(); }
240
241 void begin(void);
242 void loop(void);
243 void end(void);
244
245 void reboot(void);
246
247 const char *name();
248 void identification(void);
249
250 Int_t free_heap();
251 Int_t free_stack();
252 Bool_t heap_integrity_check(Bool_t complain=false);
253
255 Real_t rand11() {
256 const int max_int = (~((unsigned int) 0)) >> 1;
257 const int min_int = -max_int - 1;
258 const Real_t min = (Real_t) min_int;
259
260 Int_t n;
261 rand((byte *) &n, sizeof(n));
262 return (n / min);
263 }
264
265 Real_t rand01() { return (rand11() + 1.0) / 2.0; }
266
267 void rand(byte *buf, Int_t len);
268 void rand11(Real_t *buf, Int_t n) { while (n--) *buf++ = rand11(); }
269 void rand01(Real_t *buf, Int_t n) { while (n--) *buf++ = rand01(); }
270
271 Int_t loop_elapsed_ms() { return millis() - loop_start_ms; }
272 Int_t loop_elapsed_us() { return micros() - loop_start_us; }
273
275
276private:
277 Int_t loop_start_ms;
278 Int_t loop_start_us;
279};
280
281extern LambPlatform lambPlatform;
282
285#if LL_SPIFFS
286#include "SPIFFS.h"
287typedef File File_Native;
288#endif
289
290#if LL_POSIX
291#include <stdio.h>
292typedef FILE* File_Native;
293#endif
295
306class LL_File {
307public:
308
309 LL_File();
310 ~LL_File();
311
312 bool isOpen() { return _path != ""; }
313 int read(void);
314 int write(byte b);
315 int seek(unsigned long target, int whence=SEEK_SET);
316 int tell();
317 int size();
318 int close();
319
320 int read(byte *b, int n) {
321 Int_t nread = 0;
322 while (n--) {
323 int ch = read();
324 if (ch == EOF) return nread;
325 b[nread++] = ch;
326 }
327 return nread;
328 }
329
330 int read(char *s, int n) { return read((byte *) s, n); }
331 int write(const byte *b, int n) { while (n--) write(*b++); return n; }
332 int write(const char *s, int n) { while (n--) write((byte) *s++); return n; }
333 int peek();
334
335 File_Native _theFile;
336 String _path;
337 String _mode;
338
339private:
340
341};
342
343
351public:
352 LL_File *open(const char *path, const char *mode);
353 int rm(const char *path);
354 int mv(const char *from, const char *to);
355
356private:
357};
358
359extern LL_File_System ll_file_system;
360
364#if LL_WIRE
365#include "Wire.h"
366extern TwoWire *LL_Wire;
367#endif
368
369#if LL_WIFI
370#include "WiFi.h"
371extern WiFiClass *LL_WiFi;
372#endif
374
382public:
383 int setTxBufferSize(int n);
384 int setRxBufferSize(int n);
385
386 void begin(void) { begin(115200); }
387 void begin(unsigned long baudrate);
388 void end();
389 int available(void);
390 int availableForWrite(void);
391 int read(void);
392 int write(uint8_t c);
393 int write(char c) { return write((uint8_t) c); }
394
395 void flush(void);
396
397 int read(byte *buf, int max) {
398 int nread = 0;
399 while (max--) {
400 int b = read();
401 if (b == EOF) break;
402 *buf++ = b;
403 }
404 return nread;
405 }
406
407 int read(char *s, int max) { return read((uint8_t *) s, max); }
408 int write(const char *s, int n) { return write((uint8_t *) s, n); }
409 int write(const byte *b, size_t n) { int i=n; while (i--) write(*b++); return n; }
410 int write(const char *s) { int i=0; while (*s) { write(*s++); i++; } return i; }
411
412 operator bool() { return true; }
413};
414
415extern LambStdioClass LambStdio;
416
417typedef byte uuid_t[16];
418
420#define once(_once_something) do { \
421 static bool _visited_ = false; \
422 if (!_visited_) { \
423 _visited_ = true; \
424 { _once_something; } \
425 } \
426 } while (0) \
427 //
428//
429
431#define every(_every_so_often_ms, _every_something_to_do) do { \
432 static unsigned long _every_next = 0; \
433 unsigned long _every_now = millis(); \
434 if (_every_now >= _every_next) { \
435 { _every_something_to_do; } \
436 _every_next = _every_now + (_every_so_often_ms); \
437 } \
438 } while (0) \
439 //
440//
441
447void embedded_debug_catcher();
448#define ll_debug_catcher embedded_debug_catcher()
449//#define ll_debug_catcher ll_term.flush()
450
454#define ll_try try
455
456#define ll_catch(__code_before_rethrow__) \
457 catch (Sexpr_t __err__) { \
458 if (__err__->type() != Cell::T_ERROR) \
459 throw NIL->mk_error("ll_catch() BUG in %s bad type %s", me, __err__->dump().c_str()); \
460 \
461 global_printf("\r[%d] %s ll_catch(): %s\n", millis(), me, __err__->error_get_chars()); \
462 \
463 ll_debug_catcher; \
464 __code_before_rethrow__; \
465 throw __err__; \
466 } \
467 //
469
470
471#endif
Definition ll_platform_generic.h:350
Definition ll_platform_generic.h:306
Definition ll_platform_generic.h:381