GPIO Driver
These functions allow you to configure and interact with GPIO pins on the ESP32 from within a WebAssembly module.
Set GPIO Level
result = wasm_gpio_set_level(gpio_num, level);
- Parameters:
gpio_num
: GPIO pin number.level
: GPIO level (e.g.,0
for LOW,1
for HIGH).
- Returns:
ESP_OK
on success, or an error code on failure. - Description: Sets the output level of a GPIO pin.
Reset GPIO Pin
result = wasm_gpio_reset_pin(gpio_num);
- Parameters:
gpio_num
: GPIO pin number.
- Returns:
ESP_OK
on success, or an error code on failure. - Description: Resets the GPIO pin to its default state.
Set GPIO Direction
result = wasm_gpio_set_direction(gpio_num, mode);
- Parameters:
gpio_num
: GPIO pin number.mode
: GPIO mode (e.g.,GPIO_MODE_INPUT
,GPIO_MODE_OUTPUT
,GPIO_MODE_INPUT_OUTPUT
).
- Returns:
ESP_OK
on success, or an error code on failure. - Description: Configures the direction of a GPIO pin.
Example Usage
Configure GPIO and Set Level
// Set GPIO 2 as output
result = wasm_gpio_set_direction(2, GPIO_MODE_OUTPUT);
if (result != ESP_OK) {
println("Failed to set GPIO direction");
return;
}
// Set GPIO 2 to HIGH
result = wasm_gpio_set_level(2, 1);
if (result != ESP_OK) {
println("Failed to set GPIO level");
return;
}
// Reset GPIO 2 to its default state
result = wasm_gpio_reset_pin(2);
if (result != ESP_OK) {
println("Failed to reset GPIO pin");
return;
}
Explanation
-
Set Direction:
- Configures GPIO 2 as an output pin using
wasm_gpio_set_direction
.
- Configures GPIO 2 as an output pin using
-
Set Level:
- Sets the GPIO pin to HIGH using
wasm_gpio_set_level
.
- Sets the GPIO pin to HIGH using
-
Reset Pin:
- Resets the GPIO pin to its default state using
wasm_gpio_reset_pin
.
- Resets the GPIO pin to its default state using
This example demonstrates how to configure and control GPIO pins using the provided bindings.