-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_wasm3_clockenv.cpp
360 lines (289 loc) · 8.79 KB
/
test_wasm3_clockenv.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "Arduino.h"
#include "SPIFFS.h"
#include "esp_log.h"
#include "esp_err.h"
#include "esp_heap_caps.h"
#include <stdint.h>
#include "wasm3.h"
#include "m3_env.h"
#include "m3_api_libc.h"
#include "c3dev_board.h"
#include "test_freetype.h"
#ifdef CONFIG_GPIO1819_I2C
#include "test_i2c_gpio1819.h"
/**
* Unit ENV III member
*/
unitenv_t unitenv;
/**
* Unit UltraSonic
*/
unit_ultrasonic_t ultrasonic;
#endif
static const char *TAG = "test_wasm3_clockenv.cpp";
/**
* Wasm3 member
*/
IM3Environment wasm3_env;
IM3Runtime wasm3_runtime;
IM3Module wasm3_module;
IM3Function wasm3_func_tick;
IM3Function wasm3_func_collect;
#define WASM3_STACK_SIZE 16384
#define FREETYPE_FONT_SIZE 9
/**
* SPIFFS member
*/
fs::SPIFFSFS SPIFFS_WASM;
/**
* FreeType member
*/
font_render_t wasm_font_render;
/**
* as_gc_collect.
*/
void as_gc_collect(void)
{
M3Result result = m3Err_none;
result = m3_Call(wasm3_func_collect, 0, nullptr);
if (result) {
ESP_LOGE(TAG, "m3_Call:as_gc_collect: %s", result);
}
}
// (import "env" "seed" (func $env.seed (type $t3)))
// (type $t3 (func (result f64)))
m3ApiRawFunction(c3dev_random) {
m3ApiReturnType(float_t) // 32bit
m3ApiReturn(esp_random()); // uint32_t */
}
// (import "env" "abort" (func $env.abort (type $t0)))
// (type $t0 (func (param i32 i32 i32 i32)))
m3ApiRawFunction(c3dev_abort)
{
m3ApiGetArgMem(char16_t *, message)
m3ApiGetArgMem(char16_t *, fileName)
m3ApiGetArg(int32_t, lineNumber)
m3ApiGetArg(int32_t, columnNumber)
ESP_LOGE(TAG, "c3dev_abort: %ls %ls %d %d", message, fileName, lineNumber, columnNumber);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_now)
{
m3ApiReturnType(int64_t)
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
int64_t time_ms = (int64_t)tv_now.tv_sec * 1000L + /* UTC+9 */ 3600000 * 9;
// ESP_LOGI(TAG, "c3dev_now: %d", time_ms);
m3ApiReturn(time_ms);
}
m3ApiRawFunction(c3dev_delay) {
m3ApiGetArg(int32_t, wait)
delay(wait);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_start_write)
{
tft.startWrite();
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_draw_pixel)
{
m3ApiGetArg(int32_t, x)
m3ApiGetArg(int32_t, y)
m3ApiGetArg(int32_t, color)
tft.drawPixel(x, y, color);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_write_pixel)
{
m3ApiGetArg(int32_t, x)
m3ApiGetArg(int32_t, y)
m3ApiGetArg(int32_t, color)
tft.writePixel(x, y, color);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_draw_line)
{
m3ApiGetArg(int32_t, x0)
m3ApiGetArg(int32_t, y0)
m3ApiGetArg(int32_t, x1)
m3ApiGetArg(int32_t, y1)
m3ApiGetArg(int32_t, color)
tft.drawLine(x0, y0, x1, y1, color);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_end_write)
{
tft.endWrite();
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_log)
{
m3ApiGetArgMem(char *, utf8_null_terminated_string)
ESP_LOGI(TAG, "c3dev_log: %s", utf8_null_terminated_string);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_draw_string)
{
m3ApiGetArg(int32_t, x)
m3ApiGetArg(int32_t, y)
m3ApiGetArg(int32_t, color)
m3ApiGetArgMem(char *, utf8_null_terminated_string)
draw_freetype_string(utf8_null_terminated_string, x, y + FREETYPE_FONT_SIZE, color, &wasm_font_render);
// ESP_LOGI(TAG, "draw_freetype_string(%d, %d, %d, %s)", x, y, color, utf8_null_terminated_string);
m3ApiSuccess();
}
m3ApiRawFunction(c3dev_get_env_tmp) {
m3ApiReturnType(float_t)
#ifdef CONFIG_GPIO1819_I2C
m3ApiReturn(unitenv.tmp);
#else
m3ApiReturn(/* dummy */20.0);
#endif
}
m3ApiRawFunction(c3dev_get_env_hum) {
m3ApiReturnType(float_t)
#ifdef CONFIG_GPIO1819_I2C
m3ApiReturn(unitenv.hum);
#else
m3ApiReturn(/* dummy */40.0);
#endif
}
m3ApiRawFunction(c3dev_get_env_pressure) {
m3ApiReturnType(float_t)
#ifdef CONFIG_GPIO1819_I2C
m3ApiReturn(unitenv.pressure);
#else
m3ApiReturn(/* dummy */1000.0);
#endif
}
m3ApiRawFunction(c3dev_get_ultrasonic_distance) {
m3ApiReturnType(float_t)
#ifdef CONFIG_GPIO1819_I2C
m3ApiReturn(ultrasonic.distance);
#else
m3ApiReturn(/* dummy */20.0);
#endif
}
M3Result link_c3dev(IM3Runtime runtime) {
IM3Module module = runtime->modules;
m3_LinkRawFunction(module, "env", "seed", "F()", &c3dev_random);
m3_LinkRawFunction(module, "env", "abort", "v(**ii)", &c3dev_abort);
m3_LinkRawFunction(module, "c3dev", "now", "I()", &c3dev_now);
m3_LinkRawFunction(module, "c3dev", "delay", "v(i)", &c3dev_delay);
m3_LinkRawFunction(module, "c3dev", "start_write", "v()", &c3dev_start_write);
m3_LinkRawFunction(module, "c3dev", "draw_pixel", "v(iii)", &c3dev_draw_pixel);
m3_LinkRawFunction(module, "c3dev", "draw_line", "v(iiiii)", &c3dev_draw_line);
m3_LinkRawFunction(module, "c3dev", "write_pixel", "v(iii)", &c3dev_write_pixel);
m3_LinkRawFunction(module, "c3dev", "end_write", "v()", &c3dev_end_write);
m3_LinkRawFunction(module, "c3dev", "draw_string", "v(iii*)", &c3dev_draw_string);
m3_LinkRawFunction(module, "c3dev", "get_env_tmp", "f()", &c3dev_get_env_tmp);
m3_LinkRawFunction(module, "c3dev", "get_env_hum", "f()", &c3dev_get_env_hum);
m3_LinkRawFunction(module, "c3dev", "get_env_pressure", "f()", &c3dev_get_env_pressure);
m3_LinkRawFunction(module, "c3dev", "get_ultrasonic_distance", "f()", &c3dev_get_ultrasonic_distance);
m3_LinkRawFunction(module, "c3dev", "log", "v(*)", &c3dev_log);
return m3Err_none;
}
esp_err_t load_wasm(uint8_t *wasm_binary, size_t wasm_size)
{
M3Result result = m3Err_none;
ESP_LOGI(TAG, "heap_caps_get_free_size: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT));
ESP_LOGI(TAG, "Loading WebAssembly...");
wasm3_env = m3_NewEnvironment();
if (!wasm3_env) {
ESP_LOGE(TAG, "m3_NewEnvironment failed");
return ESP_FAIL;
}
wasm3_runtime = m3_NewRuntime(wasm3_env, WASM3_STACK_SIZE, NULL);
if (!wasm3_runtime) {
ESP_LOGE(TAG, "m3_NewRuntime failed");
return ESP_FAIL;
}
result = m3_ParseModule(wasm3_env, &wasm3_module, wasm_binary, wasm_size);
if (result) {
ESP_LOGE(TAG, "m3_ParseModule: %s", result);
return ESP_FAIL;
}
result = m3_LoadModule(wasm3_runtime, wasm3_module);
if (result) {
ESP_LOGE(TAG, "m3_LoadModule: %s", result);
return ESP_FAIL;
}
// link c3dev function
result = link_c3dev(wasm3_runtime);
if (result) {
ESP_LOGE(TAG, "link_c3dev: %s", result);
return ESP_FAIL;
}
ESP_LOGI(TAG, "Running...");
// Get AssemblyScript GC interface
result = m3_FindFunction(&wasm3_func_collect, wasm3_runtime, "__collect");
if (result) {
ESP_LOGE(TAG, "m3_FindFunction:wasm3_func_collect: %s", result);
return ESP_FAIL;
}
// Draw Clock
IM3Function clock;
result = m3_FindFunction(&clock, wasm3_runtime, "clock");
if (result) {
ESP_LOGE(TAG, "m3_FindFunction: %s", result);
return ESP_FAIL;
}
// export function clock(x: u32, y: u32, r: u32): void;
const char* i_argv[4] = { "80", "64", "63" };
result = m3_CallArgv(clock, 3, i_argv);
if (result) {
ESP_LOGE(TAG, "m3_Call: %s", result);
return ESP_FAIL;
}
// Get tick function
result = m3_FindFunction(&wasm3_func_tick, wasm3_runtime, "tick");
if (result) {
ESP_LOGE(TAG, "m3_FindFunction: %s", result);
return ESP_FAIL;
}
ESP_LOGI(TAG, "Wasm3 Initialized");
ESP_LOGI(TAG, "heap_caps_get_free_size: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT));
return ESP_OK;
}
esp_err_t clockenv_init_wasm(void)
{
SPIFFS_WASM.begin(false, "/wasm", 4, "wasm");
File wasm_file = SPIFFS_WASM.open("/clockenv.wasm", "rb");
size_t wasm_size = wasm_file.size();
ESP_LOGI(TAG, "clockenv.wasm: %d", wasm_size);
// Read .wasm
uint8_t *wasm_binary = (uint8_t *)malloc(sizeof(uint8_t) * wasm_size);
if(wasm_binary == nullptr) {
ESP_LOGE(TAG, "Memory alloc error");
return ESP_FAIL;
}
if(wasm_file.read(wasm_binary, wasm_size) != wasm_size) {
ESP_LOGE(TAG, "SPIFFS read error");
return ESP_FAIL;
}
wasm_file.close();
SPIFFS_WASM.end();
// Setup Font render
wasm_font_render = create_freetype_render(FREETYPE_FONT_SIZE, /* font cache */ 64);
// Clear LCD
tft.fillScreen(ST77XX_BLACK);
// Load WebAssembly on Wasm3
return load_wasm(wasm_binary, wasm_size);
}
esp_err_t clockenv_tick_wasm(void)
{
M3Result result = m3Err_none;
#ifdef CONFIG_GPIO1819_I2C
// Get Unit ENV III date
get_i2c_unit_data(&unitenv, &ultrasonic);
#endif
result = m3_Call(wasm3_func_tick, 0, nullptr);
if (result) {
ESP_LOGE(TAG, "m3_Call: %s", result);
return ESP_FAIL;
}
// GC by tick for AssemblyScript --runtime minimal
as_gc_collect();
return ESP_OK;
}