MoonBit ESP32 Binding
moon new hello-esp32$ idf.py --help
Usage: idf.py [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...moon updatemoon add moonbitlang/esp32This command will automatically generate a template project .moonbit_esp32, configure the ESP IDF toolchain path, and generate a Makefile."import": [
"moonbitlang/esp32/ctypes",
"moonbitlang/esp32/components/gpio",
"moonbitlang/esp32/components/task"
],Then write the following code in src/main/main.mbt:///|
const LED_GPIO_PIN = 12
///|
pub fn moonbit_main() -> Unit {
let _ = @gpio.gpio_config?(
pin_bit_mask=1UL << LED_GPIO_PIN,
mode=@gpio.GPIO_MODE_OUTPUT,
pull_up_en=@gpio.GPIO_PULLUP_ENABLE,
pull_down_en=@gpio.GPIO_PULLDOWN_DISABLE,
intr_type=@gpio.GPIO_INTR_DISABLE,
)
let _ = @task.xTaskCreate(
fn(_) {
for {
@task.vTaskDelay(@task.pdMS_TO_TICKS(1000))
let _ = @gpio.gpio_set_level?(LED_GPIO_PIN, 1)
@task.vTaskDelay(@task.pdMS_TO_TICKS(1000))
let _ = @gpio.gpio_set_level?(LED_GPIO_PIN, 0)
}
},
@ctypes.CString::from_bytes(b"LED Blink\x00"),
2048,
5,
)
for {
for {
println("Hello, Moonbit!")
@task.vTaskDelay(@task.pdMS_TO_TICKS(1000))
}
}
}
///|
fn main {
moonbit_main()
}The above code creates two tasks: one for blinking the LED and another for printing "Hello, Moonbit!".idf.py -C ./.moonbit_esp32 set-target esp32c3make buildThis command will package your MoonBit project into a .a file and copy it to the template project folder .moonbit_esp32.idf.py -C ./.moonbit_esp32 build
idf.py -C ./.moonbit_esp32 flash monitoridf.py -C ./.moonbit_esp32 qemu monitorYou can find the installation instructions for QEMU here.MoonBit ESP32 Binding