Skip to content

Commit 0d62a57

Browse files
committed
Update examples
1 parent 51d3736 commit 0d62a57

File tree

3 files changed

+34
-51
lines changed

3 files changed

+34
-51
lines changed

examples/Wasm_Blink/Wasm_Blink.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ void wasm_task(void*)
199199

200200
Serial.println("Running WebAssembly...");
201201

202-
const char* i_argv[1] = { NULL };
203-
result = m3_CallWithArgs (f, 0, i_argv);
202+
result = m3_CallV (f);
204203

205204
// Should not arrive here
206205

examples/Wasm_Fibonacci/Wasm_Fibonacci.ino

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
*/
66

77
#include <wasm3.h>
8-
#include <m3_env.h>
9-
10-
/*
11-
* Configuration
12-
*/
13-
#define FIB_ARG "24"
14-
#define WASM_STACK_SLOTS 1024
8+
#include <m3_config.h>
159

1610
/*
1711
* WebAssembly app (recursive Fibonacci)
@@ -44,7 +38,7 @@ void wasm_task(void*)
4438
IM3Environment env = m3_NewEnvironment ();
4539
if (!env) FATAL("NewEnvironment", "failed");
4640

47-
IM3Runtime runtime = m3_NewRuntime (env, WASM_STACK_SLOTS, NULL);
41+
IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL);
4842
if (!runtime) FATAL("NewRuntime", "failed");
4943

5044
IM3Module module;
@@ -60,17 +54,19 @@ void wasm_task(void*)
6054

6155
TFINISH("Init");
6256

63-
Serial.println("Running fib(" FIB_ARG ")...");
57+
Serial.println("Running fib(24)...");
6458

6559
TSTART();
6660

67-
const char* i_argv[2] = { FIB_ARG , NULL };
68-
result = m3_CallWithArgs (f, 1, i_argv);
61+
result = m3_CallV (f, 24);
6962

7063
TFINISH("Done");
7164

7265
if (result == m3Err_none) {
73-
uint32_t value = *(uint32_t*)(runtime->stack);
66+
uint32_t value = 0;
67+
result = m3_GetResultsV (f, &value);
68+
if (result) FATAL("GetResults: %s", result);
69+
7470
Serial.print("Result: ");
7571
Serial.println(value);
7672
} else {
@@ -117,4 +113,3 @@ void loop()
117113
{
118114
delay(100);
119115
}
120-

examples/Wasm_PyBadge_Dino/Wasm_PyBadge_Dino.ino

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "Adafruit_Arcada.h"
88

99
#include <wasm3.h>
10-
#include <m3_env.h>
1110
#include <m3_api_defs.h>
1211

1312
/*
@@ -35,7 +34,7 @@
3534
m3ApiRawFunction(Math_random)
3635
{
3736
m3ApiReturnType (float)
38-
37+
3938
float r = (float)random(INT_MAX)/INT_MAX;
4039
//Serial.print("Random: "); Serial.println(r);
4140

@@ -49,22 +48,13 @@ m3ApiRawFunction(Dino_memcpy)
4948
m3ApiGetArgMem (uint8_t *, src)
5049
m3ApiGetArgMem (uint8_t *, dstend)
5150

52-
unsigned len = dstend-dst;
53-
memcpy(dst, src, len ? len : 1);
51+
do {
52+
*dst++ = *src++;
53+
} while (dst < dstend);
5454

5555
m3ApiSuccess();
5656
}
5757

58-
M3Result LinkImports (IM3Runtime runtime)
59-
{
60-
IM3Module module = runtime->modules;
61-
62-
m3_LinkRawFunction (module, "Math", "random", "f()", &Math_random);
63-
m3_LinkRawFunction (module, "Dino", "memcpy", "v(iii)", &Dino_memcpy);
64-
65-
return m3Err_none;
66-
}
67-
6858
Adafruit_Arcada arcada;
6959

7060
IM3Environment env;
@@ -93,25 +83,22 @@ void load_wasm()
9383
result = m3_LoadModule (runtime, module);
9484
if (result) FATAL("LoadModule", result);
9585

96-
result = LinkImports (runtime);
97-
if (result) FATAL("LinkImports", result);
86+
m3_LinkRawFunction (module, "Math", "random", "f()", &Math_random);
87+
m3_LinkRawFunction (module, "Dino", "memcpy", "v(iii)", &Dino_memcpy);
9888

9989
mem = m3_GetMemory (runtime, NULL, 0);
10090
if (!mem) FATAL("GetMemory", "failed");
101-
91+
10292
result = m3_FindFunction (&func_run, runtime, "run");
10393
if (result) FATAL("FindFunction", result);
10494
}
10595

106-
void init_random()
96+
void init_device()
10797
{
10898
// Try to randomize seed
10999
randomSeed((analogRead(A5) << 16) + analogRead(A4));
110100
Serial.print("Random: 0x"); Serial.println(random(INT_MAX), HEX);
111-
}
112101

113-
void init_arcada()
114-
{
115102
if (!arcada.arcadaBegin()) {
116103
FATAL("arcadaBegin", "failed");
117104
}
@@ -127,7 +114,7 @@ void display_info()
127114
arcada.display->setCursor(0, 5);
128115
arcada.display->println(" Wasm3 v" M3_VERSION " (" M3_ARCH "@" + String(F_CPU/1000000) + "MHz)\n");
129116
arcada.display->println(" Dino game");
130-
arcada.display->println(" by Ben Smith (binji)");
117+
arcada.display->println(" by Ben Smith (binji)");
131118
}
132119

133120
void setup()
@@ -136,14 +123,15 @@ void setup()
136123

137124
// Wait for serial port to connect
138125
// Needed for native USB port only
139-
//while(!Serial) {}
126+
while(!Serial) {}
127+
128+
Serial.println("\nWasm3 v" M3_VERSION " (" M3_ARCH "), build " __DATE__ " " __TIME__);
140129

141130
uint32_t tend, tstart;
142131
TSTART();
143-
init_random();
144-
init_arcada();
132+
init_device();
145133
display_info();
146-
TFINISH("Arcada init");
134+
TFINISH("Device init");
147135

148136
TSTART();
149137
load_wasm();
@@ -152,24 +140,22 @@ void setup()
152140
Serial.println("Running WebAssembly...");
153141

154142
M3Result result;
155-
const char* i_argv[1] = { NULL };
143+
uint64_t last_fps_print = 0;
156144

157145
while (true) {
158-
const uint32_t framestart = millis();
146+
const uint64_t framestart = micros();
159147

160148
// Process inputs
161149
uint32_t pressed_buttons = arcada.readButtons();
162150
if (arcada.justPressedButtons() & ARCADA_BUTTONMASK_START) {
163151
//NVIC_SystemReset();
164152

165153
// Restart Dino game
166-
init_random();
167154
display_info();
168155
load_wasm();
169156
}
170157

171158
uint32_t* input = (uint32_t*)(mem + 0x0000);
172-
173159
*input = 0;
174160
if (pressed_buttons & ARCADA_BUTTONMASK_A) { // Up
175161
*input |= 0x1;
@@ -179,20 +165,23 @@ void setup()
179165
}
180166

181167
// Render frame
182-
result = m3_CallWithArgs (func_run, 0, i_argv);
168+
result = m3_CallV (func_run);
183169
if (result) break;
184170

185171
// Output to display
186172
arcada.display->drawRGBBitmap(0, 40, (uint16_t*)(mem+0x5000), 160, 75);
187173

188-
const uint32_t frametime = millis() - framestart;
189-
//Serial.print("FPS: "); Serial.println(1000/frametime);
174+
const uint64_t frametime = micros() - framestart;
190175

191176
// Limit to 50..70 fps, depending on CPU/overclock setting (120..200MHz)
192-
//const int target_frametime = 1000/map(F_CPU/1000000, 120, 200, 50, 70);
193-
const uint32_t target_frametime = 1000/50;
177+
//const int target_frametime = 1000000/map(F_CPU/1000000, 120, 200, 50, 70);
178+
const uint32_t target_frametime = 1000000/50;
194179
if (target_frametime > frametime) {
195-
delay(target_frametime - frametime);
180+
delay((target_frametime - frametime)/1000);
181+
}
182+
if (framestart - last_fps_print > 1000000) {
183+
Serial.print("FPS: "); Serial.println((uint32_t)(1000000/frametime));
184+
last_fps_print = framestart;
196185
}
197186
}
198187

0 commit comments

Comments
 (0)