/**
 * @file    button.c
 * @brief   Reacting to a BUTTON press with a hardware read.
 *          A reading aid for the tutorial page "widget-types", not a complete
 *          plugin: the functions below would be wired into an IcPlugin
 *          (on_widget_event) or called from init()/a Worker callback.
 *
 * @note    ASCII-only and C89 on purpose (VC6 / CP950 safe, and it must pass
 *          the syntax-only guard). The widget ids match button.nxlay.
 */

#include <string.h>

#include "nexus_plugin_api.h"

/** @brief The HostAPI table, saved by init() (not shown here). */
static const NxHostApi *s_host = (const NxHostApi *)0;

/** @brief widget id of the BUTTON ([w.1] in button.nxlay). */
#define WID_READ 1

/**
 * @brief  Widget event handler.
 * @note   I2C Worker Thread: read the hardware right here with ExecSync().
 *         A BUTTON has no state; evt->val is the click position (ignored here).
 */
void NX_CALL button_on_event(NxPluginCtx ctx, NxByte panel_id, NxWord widget_id,
                     const NxWidgetEvent *evt)
{
    NxJobRequest req;
    NxByte       reg;

    NX_UNUSED_ARG(panel_id);
    if (widget_id != WID_READ || evt->kind != NX_WEVT_ACTIVATED)
    {
        return;
    }
    memset(&req, 0, sizeof(req));
    req.size_of_struct = (NxDword)sizeof(req);
    req.op     = NX_JOB_READ;
    req.offset = 0x00;
    req.length = 1;
    if (s_host->ExecSync(ctx, &req, &reg, (NxWord)sizeof(reg)) == ERR_OK)
    {
        s_host->LogWrite(ctx, NX_LOG_INFO, "button: read ok");
    }
}
