kyanit.button module

This module publishes the Button class, which can be used to monitor a button and catch button press events.

Here"s a simple example in code.py, that will do something on button press on Pin(5):

import kyanit
from kyanit import runner
from kyanit.button import Button

async def do_something_on_click(button):
    while True:
        if button.check() == 'click':
            # do something on button press here
            ...
        await runner.sleep_ms(100)

@kyanit.controls()
def main():
    my_button = Button(5, True)  # active-low button on Pin(5)
    runner.create_task('my_button_monitor', my_button.monitor)  # start monitoring task
    runner.create_task('on_click', do_something_on_click, my_button)

@kyanit.controls(brightness=0.1)
def cleanup(exception):
    pass

See the Button class documentation for details on usage.

Classes

class Button (pin_number, invert=True)

Instantiate this class with a pin number to catch button-press events on the pin. The pin number is the GPIO number on the controller.

If the button is active-high (ie. it's logical high on press, and logical low by default), set invert to False.

Once instantiated, the monitor task must be started for button monitorization. After that, check the last button press event with the check function.

NOTE: You should check button events frequently, because if more than 1 event occurs between checks, only the last event will be returned. Frequent checks will also make the button more responsive. A 100ms wait between checks should be a good choice.

Methods

def check(self)

Return the last new event on the button.

Supported events:

  • 'click' (the button was pressed for shorter than 250ms)
  • 'long3s' (the button was pressed for at least 3 seconds)

Return value will be one of the supported event names.

async def monitor(self)

This task continuously monitors the button. It must be started for check to work.