Modified version to work with EventQueue (outside of ISR)

MultiClick.cpp

Committer:
ohtsuka
Date:
2016-06-10
Revision:
0:9c5c674dcaea
Child:
1:67d677ad73e7

File content as of revision 0:9c5c674dcaea:

#include "MultiClick.h"

MultiClick::MultiClick(PinName p, PinMode m)
{
    _iin = new InterruptIn(p);
    _mode = m;
    
    _iin->mode(_mode);
 
    if(_mode == PullUp) {
         printf("isr fall.\r\n");
        _iin->fall(this, &MultiClick::isr);
    } else {
         printf("isr rise.\r\n");
        _iin->rise(this, &MultiClick::isr);
    }
    
    _timeout = new Timeout();

    _click_interval = 0.5;

}

void MultiClick::isr( void )
{
    _click_times++;
    printf("isr called:%d\r\n", _click_times);
    fflush(stdout);
    
    _timeout->detach();

    switch( _click_times ) {
    case 0:
        printf("error!");
        break;
    
    case 1:
        if(_c_callback_clicked != 0) {
            (*_c_callback_clicked)();
        }
        break;

    case 2:
        if(_c_callback_doubleclicked != 0) {
            (*_c_callback_doubleclicked)();
        }
        break;
    default:
        if(_c_callback_n_clicked != 0) {
            (*_c_callback_n_clicked)(_click_times);
        }
    }
    
    _timeout->attach(this, &MultiClick::click_timeout, _click_interval );
}

void MultiClick::click_timeout( void )
{
    printf("click time_out.\r\n");
    _click_times = 0;    
}

void MultiClick::attach_clicked(void (*function)(void)) { _c_callback_clicked = function; }

void MultiClick::attach_doubleclicked(void (*function)(void) = 0) { _c_callback_doubleclicked = function; }

void MultiClick::attach_n_clicked(void (*function)(int) = 0) { _c_callback_n_clicked = function; }