代码拉取完成,页面将自动刷新
/* Sample app to test C and Rust WebAssembly Interoperability
Build sample Rust app: wasm/test_rust.html, test_rust.js, test_rust.wasm
emcc \
-g \
wasm/test_rust.c \
-s WASM=1 \
-s "EXPORTED_FUNCTIONS=[ '_main', '_test_c', '_test_c_set_buffer', '_test_c_get_buffer', '_test_c_buffer_address', '_test_rust', '_test_rust2', '_test_rust3', '_test_rust_set_buffer', '_test_rust_get_buffer' ]" \
-o wasm/test_rust.html \
target/wasm32-unknown-emscripten/debug/liblvgl_wasm_rust.a
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "../lvgl.h"
////////////////////////////////////////////////////////////////////
// Device and Display Buffers
/// RGBA WebAssembly Display Buffer that will be rendered to HTML Canvas
#define DISPLAY_BYTES_PER_PIXEL 4
uint8_t display_buffer[LV_HOR_RES_MAX * LV_VER_RES_MAX * DISPLAY_BYTES_PER_PIXEL];
/// Plot a pixel on the WebAssembly Display Buffer
void put_display_px(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
assert(x >= 0); assert(x < LV_HOR_RES_MAX);
assert(y >= 0); assert(y < LV_VER_RES_MAX);
// TODO: Map RGB565 to RGBA
int i = (y * LV_HOR_RES_MAX * DISPLAY_BYTES_PER_PIXEL) + (x * DISPLAY_BYTES_PER_PIXEL);
display_buffer[i++] = r; // Red
display_buffer[i++] = g; // Green
display_buffer[i++] = b; // Blue
display_buffer[i++] = a; // Alpha
}
/// Return the WebAssembly Address of the WebAssembly Display Buffer
unsigned get_display_buffer(void) {
uint8_t *p = &display_buffer[0];
return (unsigned) p;
}
/// Return the width of the WebAssembly Display Buffer
unsigned get_display_width(void) { return LV_HOR_RES_MAX; }
/// Return the height of the WebAssembly Display Buffer
unsigned get_display_height(void) { return LV_VER_RES_MAX; }
/// Render a colour box to the WebAssembly Display Buffer
int test_display(void) {
puts("In C: test_display()");
for (uint16_t x = 0; x < LV_HOR_RES_MAX; x++) {
for (uint16_t y = 0; y < LV_VER_RES_MAX; y++) {
uint8_t r = (x * 256 / 20) & 0xff;
uint8_t g = (y * 256 / 20) & 0xff;
uint8_t b = ((x + y) * 256 / 40) & 0xff;
uint8_t a = 0xff;
put_display_px(x, y, r, g, b, a);
}
}
return 0;
}
/*
/// RGB565 Device Buffer for PineTime
#define DEVICE_BYTES_PER_PIXEL 3 // TODO: Switch to RGB565
uint8_t device_buffer[LV_HOR_RES_MAX * LV_VER_RES_MAX * DEVICE_BYTES_PER_PIXEL];
/// Plot a pixel on the PineTime Device Buffer
void put_display_px(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
assert(x >= 0); assert(x < LV_HOR_RES_MAX);
assert(y >= 0); assert(y < LV_VER_RES_MAX);
int i = (y * LV_HOR_RES_MAX * DEVICE_BYTES_PER_PIXEL) + (x * DEVICE_BYTES_PER_PIXEL);
// TODO: Switch to RGB565
device_buffer[i++] = r; // Red
device_buffer[i++] = g; // Green
device_buffer[i++] = b; // Blue
}
*/
////////////////////////////////////////////////////////////////////
// Test
// Defined in rust/src/lib.rs
int test_rust(void);
// Export memory buffer
uint8_t test_rust_buffer[32] = "Test Rust Buffer";
////////////////////////////////////////////////////////////////////
// Main
int main(int argc, char **argv) {
puts("In C: main()");
int i = test_rust();
printf("In C: test_rust() returned %d\n", i);
uint8_t *p = &test_rust_buffer[0];
printf("In C: test_rust_buffer is 0x%x\n", (unsigned) p);
return 0;
}
int test_c(void) {
puts("In C: test_c()");
return 2407;
}
int test_c_set_buffer(void) {
int i = test_rust_buffer[0];
test_rust_buffer[0] = 'A';
return i;
}
int test_c_get_buffer(void) {
return test_rust_buffer[0];
}
unsigned test_c_buffer_address(void) {
uint8_t *p = &test_rust_buffer[0];
return (unsigned) p;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。