adafruit / Adafruit_CircuitPython_MCP2515

A CircuitPython library for working with the MCP2515 CAN bus controller
MIT License
21 stars 14 forks source link

Making full use of all hardware masks and filters #19

Open xyx0826 opened 1 year ago

xyx0826 commented 1 year ago

It appears that the current behavior of calling listen with Matches will assign one (unused) mask and one (unused) filter for each Match provided, regardless if masks can be shared across multiple filters. This means the user can only utilize up to two filters out of the MCP2515's six. An improved behavior might roughly look like this:

MASKS = {
    _RXM0SIDH: None
    _RXM1SIDH: None
}

FILTERS = {
    _RXM0SIDH: {
        _RXF0SIDH: None
        _RXF1SIDH: None
    }
    # mask 1 ...
}

def listen(matches):
    for match in matches:
        mask_id = self._get_or_create_mask(match)
        # create filter ...

def _get_or_create_mask(match):
    mask_id = 0
    for reg, value in MASKS.values():
        if match.mask == value and has_free_filters:
            break # mask already exists, has free filters

        if value is None:
            break # otherwise use first available mask

If this looks good, I can help updating the current implementation.