1#ifndef LL_PLATFORM_GENERIC_H
2#define LL_PLATFORM_GENERIC_H
17unsigned long millis();
18unsigned long micros();
19void delay_ms(
unsigned long ms);
21static void delay(
unsigned long ms) { delay_ms(ms); }
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); }
33 ~LL_String() {
if (ptr) {
delete[] ptr; ptr = 0; } }
35 void set(
const char *s) { set(s, strlen(s)); }
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++;
44 LL_String concat(
const char *s1,
const char *s2) {
47 char *s3 =
new char[n1 + n2 + 1];
50 while (n1--) *p++ = *s1++;
51 while (n2--) *p++ = *s2++;
56 LL_String &append(
const char *s1) {
58 int n0 = s0 ? strlen(s0) : 0;
60 char *s2 =
new char[n0 + n1 + 1];
63 while (n0--) *p++ = *s0++;
64 while (n1--) *p++ = *s1++;
67 if (ptr) {
delete[] ptr; ptr = 0; }
72 LL_String substring(
int start,
int len) {
return LL_String(&(ptr[start]), len); }
74 LL_String &operator=(
const char *other) { set(other);
return *
this; }
75 LL_String &operator=(
const LL_String &other) { set(other.ptr);
return *
this; }
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]; }
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); }
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); }
91 char *c_str()
const {
return ptr; }
92 int length() {
return strlen(ptr); }
98typedef LL_String String;
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)))
113const unsigned long toString_MAX_LENGTH = 8192;
114String toString(
const char *fmt, ...) CHECKPRINTF;
118#define ME(_me_) NOTUSED const char me[] = _me_
119#define isdef(sym) (#sym[0])
121void global_printf(
const char *fmt, ...);
144typedef unsigned char Byte_t;
148typedef Char_t
const *Charst_t;
149typedef Byte_t
const *Bytest_t;
150typedef Char_t *CharVec_t;
151typedef Byte_t *ByteVec_t;
154typedef unsigned long Word_t;
156typedef double Real_t;
161typedef unsigned long Word_t;
168typedef unsigned Word_t;
170typedef double Real_t;
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");
180class AsciiConverter {
183 char *dec(Word_t n) {
184 Int_t ix = buffer_size - 1;
185 char *ptr = &(buffer[ix]);
198 if (n >= 0)
return dec((Word_t) n);
200 char *ptr = dec((Word_t) n);
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;
214 char c = (d < 10) ? (d +
'0') : (d - 10 +
'a');
221 char *dec(Real_t n) {
222 snprintf(buffer, buffer_size,
"%f", (
double) n);
227 static const Int_t buffer_size = 128;
228 char buffer[buffer_size];
231extern AsciiConverter ascii;
239 ~LambPlatform() { end(); }
248 void identification(
void);
252 Bool_t heap_integrity_check(Bool_t complain=
false);
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;
261 rand((
byte *) &n,
sizeof(n));
265 Real_t rand01() {
return (rand11() + 1.0) / 2.0; }
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(); }
271 Int_t loop_elapsed_ms() {
return millis() - loop_start_ms; }
272 Int_t loop_elapsed_us() {
return micros() - loop_start_us; }
281extern LambPlatform lambPlatform;
287typedef File File_Native;
292typedef FILE* File_Native;
312 bool isOpen() {
return _path !=
""; }
315 int seek(
unsigned long target,
int whence=SEEK_SET);
320 int read(
byte *b,
int n) {
324 if (ch == EOF)
return nread;
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; }
335 File_Native _theFile;
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);
366extern TwoWire *LL_Wire;
371extern WiFiClass *LL_WiFi;
383 int setTxBufferSize(
int n);
384 int setRxBufferSize(
int n);
386 void begin(
void) { begin(115200); }
387 void begin(
unsigned long baudrate);
390 int availableForWrite(
void);
392 int write(uint8_t c);
393 int write(
char c) {
return write((uint8_t) c); }
397 int read(
byte *buf,
int max) {
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; }
412 operator bool() {
return true; }
417typedef byte uuid_t[16];
420#define once(_once_something) do { \
421 static bool _visited_ = false; \
424 { _once_something; } \
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); \
447void embedded_debug_catcher();
448#define ll_debug_catcher embedded_debug_catcher()
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()); \
461 global_printf("\r[%d] %s ll_catch(): %s\n", millis(), me, __err__->error_get_chars()); \
464 __code_before_rethrow__; \
Definition ll_platform_generic.h:350
Definition ll_platform_generic.h:306
Definition ll_platform_generic.h:381