This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

Buttons

Digital Input Management Class (mwx::periph_buttons)
    Detects changes in digital input. This class detects changes when the same detected value is obtained multiple times. It is effective in reducing the effects of mechanical button chatter.

    Methods

    setup()

    void setup(uint8_t max_history);

    The parameter max_history is the maximum number of reference counts that can be set in begin(). Memory allocation and initialization are performed here.

    begin()

    void begin(uint32_t bmPortMask,
    				   uint8_t u8HistoryCount,
    				   uint16_t tick_delta);

    Starts the operation of Buttons. The first parameter bmPortMask specifies the bitmap of digital inputs to be monitored. bit 0 corresponds to DIO 0, …, bit N corresponds to DIO N. Multiple bits can be specified. The second parameter u8HistoryCount is the number of times required to confirm a value. The third parameter tick_delta specifies the interval for checking the value in milliseconds.

    The confirmation of the value takes u8HistoryCount * tick_delta [ms]. For example, if u8HistoryCount=5 and tick_delta=4, it takes at least about 20 ms to confirm the state.

    The check is performed in the event handler of TickTimer. Since it is not an interrupt handler, it is affected by processing delays, but it is sufficient for suppressing chatter of mechanical buttons and the like.

    end()

    void end()

    Stops the operation of Buttons.

    available()

    inline bool available()

    Returns true when a change is detected. It is cleared when read() is executed.

    read()

    bool read(uint32_t& u32port, uint32_t& u32changed)

    Called when available is true. u32port is the bitmap of the current input DIO, and u32changed is the bitmap of DIOs where changes were detected.

    Returns false if Buttons is not operating.

    Operation Details

    Initial Value Confirmation

    At the time when Buttons starts operating, the input state of DIO is not confirmed. It becomes available when the value is confirmed. At this time, the MSB (bit 31) of the bitmap read by read() is set to 1.

    Since operation confirmation is required, it cannot be used for monitoring ports where the input value changes constantly.

    Sleep

    If Buttons is running before sleep, it will resume after waking up. After resuming, the initial confirmation is performed.