Modified version to work with EventQueue (outside of ISR)
MultiClick.cpp@0:9c5c674dcaea, 2016-06-10 (annotated)
- Committer:
- ohtsuka
- Date:
- Fri Jun 10 08:09:22 2016 +0000
- Revision:
- 0:9c5c674dcaea
- Child:
- 1:67d677ad73e7
first commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ohtsuka | 0:9c5c674dcaea | 1 | #include "MultiClick.h" |
ohtsuka | 0:9c5c674dcaea | 2 | |
ohtsuka | 0:9c5c674dcaea | 3 | MultiClick::MultiClick(PinName p, PinMode m) |
ohtsuka | 0:9c5c674dcaea | 4 | { |
ohtsuka | 0:9c5c674dcaea | 5 | _iin = new InterruptIn(p); |
ohtsuka | 0:9c5c674dcaea | 6 | _mode = m; |
ohtsuka | 0:9c5c674dcaea | 7 | |
ohtsuka | 0:9c5c674dcaea | 8 | _iin->mode(_mode); |
ohtsuka | 0:9c5c674dcaea | 9 | |
ohtsuka | 0:9c5c674dcaea | 10 | if(_mode == PullUp) { |
ohtsuka | 0:9c5c674dcaea | 11 | printf("isr fall.\r\n"); |
ohtsuka | 0:9c5c674dcaea | 12 | _iin->fall(this, &MultiClick::isr); |
ohtsuka | 0:9c5c674dcaea | 13 | } else { |
ohtsuka | 0:9c5c674dcaea | 14 | printf("isr rise.\r\n"); |
ohtsuka | 0:9c5c674dcaea | 15 | _iin->rise(this, &MultiClick::isr); |
ohtsuka | 0:9c5c674dcaea | 16 | } |
ohtsuka | 0:9c5c674dcaea | 17 | |
ohtsuka | 0:9c5c674dcaea | 18 | _timeout = new Timeout(); |
ohtsuka | 0:9c5c674dcaea | 19 | |
ohtsuka | 0:9c5c674dcaea | 20 | _click_interval = 0.5; |
ohtsuka | 0:9c5c674dcaea | 21 | |
ohtsuka | 0:9c5c674dcaea | 22 | } |
ohtsuka | 0:9c5c674dcaea | 23 | |
ohtsuka | 0:9c5c674dcaea | 24 | void MultiClick::isr( void ) |
ohtsuka | 0:9c5c674dcaea | 25 | { |
ohtsuka | 0:9c5c674dcaea | 26 | _click_times++; |
ohtsuka | 0:9c5c674dcaea | 27 | printf("isr called:%d\r\n", _click_times); |
ohtsuka | 0:9c5c674dcaea | 28 | fflush(stdout); |
ohtsuka | 0:9c5c674dcaea | 29 | |
ohtsuka | 0:9c5c674dcaea | 30 | _timeout->detach(); |
ohtsuka | 0:9c5c674dcaea | 31 | |
ohtsuka | 0:9c5c674dcaea | 32 | switch( _click_times ) { |
ohtsuka | 0:9c5c674dcaea | 33 | case 0: |
ohtsuka | 0:9c5c674dcaea | 34 | printf("error!"); |
ohtsuka | 0:9c5c674dcaea | 35 | break; |
ohtsuka | 0:9c5c674dcaea | 36 | |
ohtsuka | 0:9c5c674dcaea | 37 | case 1: |
ohtsuka | 0:9c5c674dcaea | 38 | if(_c_callback_clicked != 0) { |
ohtsuka | 0:9c5c674dcaea | 39 | (*_c_callback_clicked)(); |
ohtsuka | 0:9c5c674dcaea | 40 | } |
ohtsuka | 0:9c5c674dcaea | 41 | break; |
ohtsuka | 0:9c5c674dcaea | 42 | |
ohtsuka | 0:9c5c674dcaea | 43 | case 2: |
ohtsuka | 0:9c5c674dcaea | 44 | if(_c_callback_doubleclicked != 0) { |
ohtsuka | 0:9c5c674dcaea | 45 | (*_c_callback_doubleclicked)(); |
ohtsuka | 0:9c5c674dcaea | 46 | } |
ohtsuka | 0:9c5c674dcaea | 47 | break; |
ohtsuka | 0:9c5c674dcaea | 48 | default: |
ohtsuka | 0:9c5c674dcaea | 49 | if(_c_callback_n_clicked != 0) { |
ohtsuka | 0:9c5c674dcaea | 50 | (*_c_callback_n_clicked)(_click_times); |
ohtsuka | 0:9c5c674dcaea | 51 | } |
ohtsuka | 0:9c5c674dcaea | 52 | } |
ohtsuka | 0:9c5c674dcaea | 53 | |
ohtsuka | 0:9c5c674dcaea | 54 | _timeout->attach(this, &MultiClick::click_timeout, _click_interval ); |
ohtsuka | 0:9c5c674dcaea | 55 | } |
ohtsuka | 0:9c5c674dcaea | 56 | |
ohtsuka | 0:9c5c674dcaea | 57 | void MultiClick::click_timeout( void ) |
ohtsuka | 0:9c5c674dcaea | 58 | { |
ohtsuka | 0:9c5c674dcaea | 59 | printf("click time_out.\r\n"); |
ohtsuka | 0:9c5c674dcaea | 60 | _click_times = 0; |
ohtsuka | 0:9c5c674dcaea | 61 | } |
ohtsuka | 0:9c5c674dcaea | 62 | |
ohtsuka | 0:9c5c674dcaea | 63 | void MultiClick::attach_clicked(void (*function)(void)) { _c_callback_clicked = function; } |
ohtsuka | 0:9c5c674dcaea | 64 | |
ohtsuka | 0:9c5c674dcaea | 65 | void MultiClick::attach_doubleclicked(void (*function)(void) = 0) { _c_callback_doubleclicked = function; } |
ohtsuka | 0:9c5c674dcaea | 66 | |
ohtsuka | 0:9c5c674dcaea | 67 | void MultiClick::attach_n_clicked(void (*function)(int) = 0) { _c_callback_n_clicked = function; } |