From 465a281744d964dd731498d36a8292c756d2f07d Mon Sep 17 00:00:00 2001 From: chai2010 Date: Fri, 20 Jun 2025 06:20:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20Arduino=20Nano,=202KB=20?= =?UTF-8?q?=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/appbuild/assets/arduino-host.cpp | 4 +- waroot/examples/arduino-lcd1602/src/main.wa | 53 +++++++++++++++++-- waroot/examples/arduino/src/main.wa | 1 + .../src => src/arduino/lcd1602}/lcd1602.wa | 4 ++ waroot/src/base_arduino.wat.ws | 2 +- waroot/src/src.go | 4 ++ 6 files changed, 62 insertions(+), 6 deletions(-) rename waroot/{examples/arduino-lcd1602/src => src/arduino/lcd1602}/lcd1602.wa (98%) diff --git a/internal/app/appbuild/assets/arduino-host.cpp b/internal/app/appbuild/assets/arduino-host.cpp index 6b752b83..e717478c 100644 --- a/internal/app/appbuild/assets/arduino-host.cpp +++ b/internal/app/appbuild/assets/arduino-host.cpp @@ -3,8 +3,8 @@ #include // 配置初始内存 -static uint8_t app_host_memory[{{.MemoryBytes}}]; -static int32_t app_host_memory_page_size = ({{.MemoryBytes}})/(1<<16); +static uint8_t app_host_memory[1014]; // Arduino Nano 只有2KB内存, 不足1页 +static int32_t app_host_memory_page_size = 0; // 初始化内存 extern "C" void app_memory_init(uint8_t** pp_memory, int32_t* page_size) { diff --git a/waroot/examples/arduino-lcd1602/src/main.wa b/waroot/examples/arduino-lcd1602/src/main.wa index 113a194c..11dc1f4b 100644 --- a/waroot/examples/arduino-lcd1602/src/main.wa +++ b/waroot/examples/arduino-lcd1602/src/main.wa @@ -1,10 +1,57 @@ // 版权 @2025 arduino-lcd1602 作者。保留所有权利。 +import ( + "arduino/lcd1602" + "syscall/arduino" +) + +global LED = arduino.GetPinLED() + func init { - LCDInit() - LCDPrint("Hello Wa-lang") + arduino.PinMode(LED, 1) + arduino.Print("凹语言(Wa)/Arduino is running ...\n") + arduino.Print("https://wa-lang.org\n") + arduino.Print("基于wat2c技术\n") + + lcd1602.LCDInit() } func Loop { - // + const s = "hello wa-lang!" + + arduino.DigitalWrite(LED, arduino.HIGH) + + lcd1602.LCDClear() + SayHello(0, 1, s) + SayHello(1, 0, s) + arduino.Delay(500) + arduino.DigitalWrite(LED, arduino.LOW) + + lcd1602.LCDClear() + SayHello(0, 1, s) + SayHello(1, 1, s) + arduino.Delay(500) + arduino.DigitalWrite(LED, arduino.HIGH) + + lcd1602.LCDClear() + SayHello(0, 1, s) + SayHello(1, 2, s) + arduino.Delay(500) + arduino.DigitalWrite(LED, arduino.LOW) + + lcd1602.LCDClear() + SayHello(0, 1, s) + SayHello(1, 1, s) + arduino.Delay(500) + arduino.DigitalWrite(LED, arduino.HIGH) +} + +func SayHello(row, col: i32, s: string) { + lcd1602.LCDSetCursor(row, col) + + // Arduino Nano 内存太小, 暂不支持 for range + + for i := 0; i < len(s); i++ { + lcd1602.LCDWriteChar(i32(s[i])) + } } diff --git a/waroot/examples/arduino/src/main.wa b/waroot/examples/arduino/src/main.wa index 580040ce..28f52c9a 100644 --- a/waroot/examples/arduino/src/main.wa +++ b/waroot/examples/arduino/src/main.wa @@ -8,6 +8,7 @@ func init { arduino.PinMode(LED, 1) arduino.Print("凹语言(Wa)/Arduino is running ...\n") arduino.Print("https://wa-lang.org\n") + arduino.Print("基于wat2c技术\n") } func Loop { diff --git a/waroot/examples/arduino-lcd1602/src/lcd1602.wa b/waroot/src/arduino/lcd1602/lcd1602.wa similarity index 98% rename from waroot/examples/arduino-lcd1602/src/lcd1602.wa rename to waroot/src/arduino/lcd1602/lcd1602.wa index 564ef32d..04f72957 100644 --- a/waroot/examples/arduino-lcd1602/src/lcd1602.wa +++ b/waroot/src/arduino/lcd1602/lcd1602.wa @@ -97,6 +97,10 @@ func LCDPrint(s: string) { } } +func LCDWriteChar(ch: rune) { + writeChar(byte(ch)) +} + // Claer screen. func LCDClear { command(0x01) diff --git a/waroot/src/base_arduino.wat.ws b/waroot/src/base_arduino.wat.ws index bcabaffe..448c2cfb 100644 --- a/waroot/src/base_arduino.wat.ws +++ b/waroot/src/base_arduino.wat.ws @@ -6,6 +6,6 @@ ;; | 0 <-- stack --> | <-- static-data --> | <-- heap --> | ;; +-----------------+---------------------+--------------+ -(global $__stack_ptr (mut i32) (i32.const 1024)) ;; 1024 +(global $__stack_ptr (mut i32) (i32.const 128)) ;; 基本用不上 (global $__heap_lfixed_cap i32 (i32.const 0)) ;; 固定尺寸空闲链表最大长度, 满时回收; 也可用于关闭 fixed 策略 diff --git a/waroot/src/src.go b/waroot/src/src.go index 8b6ccb17..4649e329 100644 --- a/waroot/src/src.go +++ b/waroot/src/src.go @@ -121,6 +121,9 @@ func GetStdTestPkgList() []string { if strings.HasPrefix(s, "syscall") { continue } + if strings.HasPrefix(s, "arduino/") { + continue + } if s == "js" || strings.HasPrefix(s, "js/") { continue } @@ -132,6 +135,7 @@ func GetStdTestPkgList() []string { var stdPkgs = []string{ "ai", // ? "apple", // 测试已覆盖, wat2wasm ok + "arduino/lcd1602", // Arduino 平台, LCD显示屏 "archive/txtar", // API 完整, wat2wasm ok "bufio", // API 完整, wat2wasm ok "bytes", // API 完整, wat2wasm ok