/* vm2 studios | t.me/v2vproj — MultiOS / MultiPulti.
 * Educational integration example, NOT a complete board driver.
 * The two board_* hooks MUST be implemented by the target BSP.
 * Syntax-checked only; no target hardware test is claimed.
 * Assumes one boot-frozen instance and platform-owned GPIO resources.
 */
#include "multios/modules.h"
#include "multios/buttons.h"

/* Return a debounced logical mask: NEXT=1, SELECT=2, PREVIOUS=4.
 * This example enables only NEXT/SELECT. Zero means all released.
 * Electrical polarity, GPIO setup and debounce belong to the BSP.
 */
extern unsigned board_buttons_debounced_mask(void *board);
/* Wait/yield between samples; must eventually allow input progress. */
extern void board_buttons_wait(void *board);

typedef struct {
    void *board;
    MultiButtons buttons;
} ButtonInput;
static ButtonInput input;

static int buttons_getc(void *ctx) {
    ButtonInput *s = (ButtonInput *)ctx;
    for (;;) {
        unsigned mask = board_buttons_debounced_mask(s->board);
        int key = multi_buttons_update(&s->buttons, mask);
        if (key != -2)
            return key; /* raw event; common code processes OSK once */
        board_buttons_wait(s->board);
    }
    /* Never return -1 just because no button is pressed: that means EOF. */
}
static int buttons_open(void *platform, const char *options, MultiDevice *d) {
    if (!platform || !options || !d || *options)
        return INVALID;
    input.board = platform;
    int result = multi_buttons_init(&input.buttons, 2);
    if (result < 0)
        return result;
    d->input = (MultiInput){ .ctx = &input, .getc = buttons_getc };
    return 0;
}
const MultiDriver example_two_buttons = MULTI_DRIVER(
    "example-two-buttons", MULTI_INPUT,
    "vm2 studios | t.me/v2vproj: two-button BSP integration example",
    buttons_open, 0
);
