-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwasm_mphal.c
167 lines (129 loc) · 3.24 KB
/
wasm_mphal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "py/mphal.h"
#include "py/runtime.h"
#include "extmod/misc.h"
#include <stdarg.h>
#ifdef __EMSCRIPTEN__
#include "emscripten.h"
#elif __CPP__
#define EMSCRIPTEN_KEEPALIVE
#endif
#include "upython.h"
#include <time.h>
void mp_hal_delay_ms(mp_uint_t ms) { }
void mp_hal_delay_us(mp_uint_t us) { }
struct timespec ts;
#define EPOCH_US 0
#if EPOCH_US
static unsigned long epoch_us = 0;
#endif
mp_uint_t mp_hal_ticks_ms(void) {
clock_gettime(CLOCK_MONOTONIC, &ts);
unsigned long now_us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
#if EPOCH_US
if (!epoch_us)
epoch_us = now_us -1000;
return (mp_uint_t)( (now_us - epoch_us) / 1000 ) ;
#else
return (mp_uint_t)(now_us / 1000);
#endif
}
mp_uint_t mp_hal_ticks_us(void) {
clock_gettime(CLOCK_MONOTONIC, &ts);
unsigned long now_us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
#if EPOCH_US
if (!epoch_us)
epoch_us = now_us-1;
return (mp_uint_t)(now_us - epoch_us);
#else
return (mp_uint_t)now_us;
#endif
}
// Receive single character
int mp_hal_stdin_rx_chr(void) {
fprintf(stderr,"mp_hal_stdin_rx_chr");
unsigned char c = fgetc(stdin);
return c;
}
static unsigned char last = 0;
#define HEXBUF 1
#if HEXBUF
extern rbb_t out_rbb;
unsigned char v2a(int c)
{
const unsigned char hex[] = "0123456789abcdef";
return hex[c];
}
unsigned char hex_hi(unsigned char b) {
return v2a((b >> 4) & 0x0F);
}
unsigned char hex_lo(unsigned char b) {
return v2a((b) & 0x0F);
}
unsigned char out_push(unsigned char c) {
if (last>127) {
if (c>127)
fprintf(stderr," -- utf-8(2/2) %u --\n", c );
} else {
if (c>127)
fprintf(stderr," -- utf-8(1/2) %u --\n", c );
}
rbb_append(&out_rbb, hex_hi(c));
rbb_append(&out_rbb, hex_lo(c));
return (unsigned char)c;
}
#endif
//FIXME: libc print with valid json are likely to pass and get interpreted by pts
//TODO: buffer all until render tick
//this one (over)cooks like _cooked
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
for(int i=0;i<len;i++) {
if ( (str[i] == 0x0a) && (last != 0x0d) ) {
#if HEXBUF
out_push( 0x0d );
#else
printf("{\"%c\":%u}\n",49, 0x0d );
#endif
}
#if HEXBUF
last = out_push( (unsigned char)str[i] );
#else
printf("{\"%c\":%u}\n",49,(unsigned char)str[i]);
last = (unsigned char)str[i];
#endif
}
}
EMSCRIPTEN_KEEPALIVE static PyObject *
embed_run_script(PyObject *self, PyObject *argv) {
char *runstr = NULL;
if (!PyArg_ParseTuple(argv, "s", &runstr)) {
return NULL;
}
emscripten_run_script( runstr );
Py_RETURN_NONE;
}
#if MICROPY_USE_READLINE
#else
char *prompt(char *p) {
fprintf(stderr,"61:simple read string\n");
static char buf[256];
//fputs(p, stdout);
fputs(p, stderr);
char *s = fgets(buf, sizeof(buf), stdin);
if (!s) {
return NULL;
}
int l = strlen(buf);
if (buf[l - 1] == '\n') {
buf[l - 1] = 0;
} else {
l++;
}
char *line = malloc(l);
memcpy(line, buf, l);
return line;
}
#endif