https://github.com/hybridgroup/mechanoid Skip to content Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} hybridgroup / mechanoid Public * Notifications * Fork 2 * Star 32 * Mechanoid is a framework for WebAssembly applications on embedded systems. mechanoid.io License Apache-2.0 license 32 stars 2 forks Branches Tags Activity Star Notifications * Code * Issues 1 * Pull requests 0 * Discussions * Actions * Security * Insights Additional navigation options * Code * Issues * Pull requests * Discussions * Actions * Security * Insights hybridgroup/mechanoid This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 147 Commits .github/workflows .github/ workflows cmd/mecha cmd/mecha convert convert devices/hardware devices/hardware engine engine filestore filestore interp interp .gitignore .gitignore ARCHITECTURE.md ARCHITECTURE.md LICENSE LICENSE README.md README.md debug.go debug.go go.mod go.mod go.sum go.sum log.go log.go mechanoid.go mechanoid.go nodebug.go nodebug.go version.go version.go View all files Repository files navigation * README * Apache-2.0 license Mechanoid Mechanoid is a framework for WebAssembly applications on embedded systems. What is Mechanoid? Mechanoid is an open source framework for building and running WebAssembly applications on small embedded systems. It is intended to make it easier to create applications that are secure and extendable, and take advantage of all of the latest developments in both WebAssembly and embedded development. Mechanoid includes a command line interface tool that helps you create, test, and run applications on either simulators or actual hardware, in part thanks to being written using Go and TinyGo. Why would you want to do this? * Devices that are extensible. Think app stores, downloadable add-ons, or end-user programmability. * Environment is sandboxed, so bricking the device is less likely. * Code you write being compiled to WASM is very compact. * Devices that need a reliable way to update them. * OTA updates via slow/high latency are more viable. * Specific APIs can be provided by the host application to guest modules, so application-specific code matches the kind of code you are trying to write. Games, industrial control systems. * Develop code in Go/Rust/Zig or any language that can compile to WASM, and run it on the same hardware, using the same APIs. Getting started * Install the Mechanoid command line tool Use export GOPRIVATE (only needed until the repo is public) export GOPRIVATE=github.com/hybridgroup/mecha* Use go install to install the mecha CLI go install github.com/hybridgroup/mechanoid/cmd/mecha@latest * Create a new project mecha new example.com/myproject * Make something amazing! Example Here is an example of an application built using Mechanoid. It consists of a host application that runs on a microcontroller, and a separate WebAssembly module that will be run by the host application on that same microcontroller. The host application loads the WASM and then executes it, sending the output to the serial interface on the board. This way we can see the output on your computer. flowchart LR subgraph Computer end subgraph Microcontroller subgraph Application Pong end subgraph ping.wasm Ping end Ping-->Pong Application-->Ping end Application--Serial port-->Computer Here is how you create it using Mechanoid: mecha new project -template=simple example.com/myproject cd myproject mecha new module -template=ping ping WebAssembly guest program This is the Go code for the ping.wasm module. It exports a ping function, that calls a function pong that has been imported from the host application. //go:build tinygo package main //go:wasmimport hosted pong func pong() //go:export ping func ping() { pong() } func main() {} You can compile this program to WASM using the mecha build command: $ mecha build Building module ping code data bss | flash ram 9 0 0 | 9 0 Mechanoid host application This is the Go code for the Mechanoid host application that runs directly on the hardware. It loads the ping.wasm WebAssembly module and then runs it by calling the module's Ping() function. That Ping() function will then call the host's exported Pong() function: package main import ( "bytes" _ "embed" "time" "github.com/hybridgroup/mechanoid/engine" "github.com/hybridgroup/mechanoid/interp" "github.com/orsinium-labs/wypes" ) //go:embed modules/ping.wasm var wasmCode []byte func main() { time.Sleep(2 * time.Second) println("Mechanoid engine starting...") eng := engine.NewEngine() eng.UseInterpreter(interp.NewInterpreter()) println("Initializing engine using interpreter", eng.Interpreter.Name()) if err := eng.Init(); err != nil { println(err.Error()) return } println("Defining host function...") modules := wypes.Modules{ "hosted": wypes.Module{ "pong": wypes.H0(pongFunc), }, } if err := eng.Interpreter.SetModules(modules); err != nil { println(err.Error()) return } println("Loading and running WASM code...") ins, err := eng.LoadAndRun(bytes.NewReader(wasmCode)) if err != nil { println(err.Error()) return } for { println("Calling ping...") if _, err := ins.Call("ping"); err != nil { println(err.Error()) } time.Sleep(1 * time.Second) } } func pongFunc() wypes.Void { println("pong") return wypes.Void{} } You can compile and flash the application and the WASM program onto an Adafruit PyBadge (an ARM 32-bit microcontroller with 192k of RAM) with the mecha flash command: $ mecha flash -i wazero -m pybadge Building module ping Done. code data bss | flash ram 9 0 0 | 9 0 Application built. Now flashing... code data bss | flash ram 328988 66056 7112 | 395044 73168 Connected to /dev/ttyACM0. Press Ctrl-C to exit. Mechanoid engine starting... Initializing engine using interpreter wazero Defining host function... Loading and running WASM code... Calling ping... pong Calling ping... pong Calling ping... pong ... There are more examples available here: https://github.com/ hybridgroup/mechanoid-examples How it works See ARCHITECTURE.md for more information. Supported Runtime Interpreters * wazero - requires the https://github.com/orsinium-forks/wazero fork * wasman - requires the https://github.com/hybridgroup/wasman fork Goals * [*] Able to run small WASM modules designed for specific embedded runtime interfaces. * [*] Hot loading/unloading of WASM modules. * [*] Local storage system for WASM modules. * [ ] Allow the engine to be used/extended for different embedded application use cases, e.g. CLI, WASM4 runtime, others. - IN PROGRESS * [ ] Configurable system to allow the bridge interface to host capabilities to be defined per application. - IN PROGRESS About Mechanoid is a framework for WebAssembly applications on embedded systems. mechanoid.io Resources Readme License Apache-2.0 license Activity Custom properties Stars 32 stars Watchers 9 watching Forks 2 forks Report repository Releases 2 0.1.1 Latest Mar 14, 2024 + 1 release Packages 0 No packages published Contributors 2 * @deadprogram deadprogram Ron Evans * @orsinium orsinium Gram Languages * Go 100.0% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.