green_copper_runtime::modules::io

Module gpio

source
Expand description

gpio module

this module may be loaded in an EsRuntime initialized by green_copper_runtime::new_greco_runtime() by loading ‘greco://gpio’

§Example

  • Blink an Led
async function test_gpio() {
    // load the module
    let gpio_mod = await import('greco://gpio');
    // create a new PinSet
    let pin_set = new gpio_mod.PinSet();
    // init a single pin
    await pin_set.init('/dev/gpiochip0', 'out', [13]);
    // set the state of that pin to 1 (e.g. turn on a led)
    await pin_set.setState(1);
}
test_gpio().then(() => {
    console.log("done testing GPIO");
}).catch((ex) => {
    console.error("GPIO test failed: %s", "" + ex);
});
  • Listen for button press
async function test_gpio() {
    // load the module
    let gpio_mod = await import('greco://gpio');
    // create a new PinSet
    let pin_set = new gpio_mod.PinSet();
    // init two pins to listen to
    await pin_set.init('/dev/gpiochip0', 'in', [12, 13]);
    // add an event listener
    pin_set.addEventListener('rising', (evt) => {
        console.log("Pin state went to rising for %s", evt.pin);
    });
    pin_set.addEventListener('falling', (evt) => {
        console.log("Pin state went to falling for %s", evt.pin);//!     
    });
}
test_gpio().then(() => {
    console.log("done testing GPIO");
}).catch((ex) => {
    console.error("GPIO test failed: %s", "" + ex);
});

Modules§