libeasymcp2221 2.0.3
Classes | Macros | Enumerations | Functions
mcp2221_gpio_poll.h File Reference

Stateful GPIO change and edge-event polling helpers. More...

#include <stdint.h>
#include <stddef.h>
#include "mcp2221.h"
Include dependency graph for mcp2221_gpio_poll.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mcp2221_gpio_change_t
 Change information for one GP pin. More...
 
struct  mcp2221_gpio_poll_state_t
 Persistent state used by the GPIO polling helpers. More...
 
struct  mcp2221_gpio_event_t
 One GPIO edge event produced by mcp2221_gpio_poll_events(). More...
 

Macros

#define MCP2221_GPIO_POLL_MASK_RISE(pin)   (1u << ((pin) * 2))
 Build the filter-mask bit for a rising edge on a GP pin. More...
 
#define MCP2221_GPIO_POLL_MASK_FALL(pin)   (1u << ((pin) * 2 + 1))
 Build the filter-mask bit for a falling edge on a GP pin. More...
 

Enumerations

enum  mcp2221_gpio_event_type_t { MCP2221_GPIO_EVENT_RISE = 0 , MCP2221_GPIO_EVENT_FALL = 1 }
 GPIO edge-event type. More...
 

Functions

void mcp2221_gpio_poll_init (mcp2221_gpio_poll_state_t *st)
 Initialize a GPIO polling state object. More...
 
void mcp2221_gpio_poll_set_filter_mask (mcp2221_gpio_poll_state_t *st, uint16_t mask)
 Set the persistent edge-event filter mask. More...
 
mcp2221_error_code_t mcp2221_gpio_poll (mcp2221_t *dev, mcp2221_gpio_poll_state_t *st, mcp2221_gpio_change_t out[4])
 Poll GP0 through GP3 and report per-pin state changes. More...
 
int mcp2221_gpio_poll_events (mcp2221_t *dev, mcp2221_gpio_poll_state_t *st, const uint16_t *filter_mask_opt, mcp2221_gpio_event_t *out_events, size_t max_events)
 Poll GPIO changes and emit filtered rise/fall events. More...
 

Detailed Description

Stateful GPIO change and edge-event polling helpers.

Definition in file mcp2221_gpio_poll.h.

Macro Definition Documentation

◆ MCP2221_GPIO_POLL_MASK_FALL

#define MCP2221_GPIO_POLL_MASK_FALL (   pin)    (1u << ((pin) * 2 + 1))

Build the filter-mask bit for a falling edge on a GP pin.

Parameters
pinGP pin number from 0 through 3.

Definition at line 120 of file mcp2221_gpio_poll.h.

◆ MCP2221_GPIO_POLL_MASK_RISE

#define MCP2221_GPIO_POLL_MASK_RISE (   pin)    (1u << ((pin) * 2))

Build the filter-mask bit for a rising edge on a GP pin.

Parameters
pinGP pin number from 0 through 3.

Definition at line 114 of file mcp2221_gpio_poll.h.

Enumeration Type Documentation

◆ mcp2221_gpio_event_type_t

GPIO edge-event type.

Enumerator
MCP2221_GPIO_EVENT_RISE 

Low-to-high transition.

MCP2221_GPIO_EVENT_FALL 

High-to-low transition.

Definition at line 80 of file mcp2221_gpio_poll.h.

Function Documentation

◆ mcp2221_gpio_poll()

mcp2221_error_code_t mcp2221_gpio_poll ( mcp2221_t dev,
mcp2221_gpio_poll_state_t st,
mcp2221_gpio_change_t  out[4] 
)

Poll GP0 through GP3 and report per-pin state changes.

On the first successful call, the function initializes the previous-state snapshot and reports changed == 0 for every pin. Each successful call also updates the timestamp shared with mcp2221_gpio_poll_events().

A sampled value of -1 indicates that the corresponding pin is not currently configured as GPIO.

Parameters
[in]devOpen MCP2221 device handle.
[in,out]stInitialized polling state.
[out]outFour-element array receiving GP0 through GP3 changes.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for invalid arguments, or another mcp2221_error_code_t value on failure.

◆ mcp2221_gpio_poll_events()

int mcp2221_gpio_poll_events ( mcp2221_t dev,
mcp2221_gpio_poll_state_t st,
const uint16_t *  filter_mask_opt,
mcp2221_gpio_event_t out_events,
size_t  max_events 
)

Poll GPIO changes and emit filtered rise/fall events.

If the polling snapshot has not yet been initialized by either GPIO polling helper, the first successful call initializes it and returns zero events. Transitions are emitted only when both the previous and current samples are valid GPIO states.

If filter_mask_opt is NULL, the filter already stored in st is preserved. Otherwise *filter_mask_opt becomes the new persistent filter; a value of 0 accepts all events.

At most max_events events are written. The polling state advances after every successful call even when more matching transitions occurred than fit in out_events, so excess events are discarded rather than returned by a later poll.

Parameters
[in]devOpen MCP2221 device handle.
[in,out]stInitialized polling state.
[in]filter_mask_optOptional replacement filter mask, or NULL to preserve the existing filter.
[out]out_eventsEvent buffer. May be NULL only when max_events is 0.
[in]max_eventsMaximum number of events that may be written.
Returns
Number of events written, from 0 through max_events, on success; otherwise a negative mcp2221_error_code_t value.
Note
Event timestamps use wall-clock time rather than a monotonic elapsed time source.

◆ mcp2221_gpio_poll_init()

void mcp2221_gpio_poll_init ( mcp2221_gpio_poll_state_t st)

Initialize a GPIO polling state object.

The filter is reset to 0, which accepts all edge events. Passing NULL is a no-op.

Parameters
[out]stPolling state to initialize.

◆ mcp2221_gpio_poll_set_filter_mask()

void mcp2221_gpio_poll_set_filter_mask ( mcp2221_gpio_poll_state_t st,
uint16_t  mask 
)

Set the persistent edge-event filter mask.

A mask of 0 accepts all events. Passing NULL is a no-op.

Parameters
[in,out]stPolling state whose filter is updated.
[in]maskNew event filter mask.