Modified version to work with EventQueue (outside of ISR)

Committer:
ohtsuka
Date:
Mon Jun 13 08:30:29 2016 +0000
Revision:
1:67d677ad73e7
Parent:
0:9c5c674dcaea
Child:
2:6af39916a4a2
??????(debounce)??

Who changed what in which revision?

UserRevisionLine numberNew 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 1:67d677ad73e7 9
ohtsuka 0:9c5c674dcaea 10 if(_mode == PullUp) {
ohtsuka 1:67d677ad73e7 11 DBG_PRINT("isr fall.\r\n");
ohtsuka 1:67d677ad73e7 12 _iin->fall(this, &MultiClick::isr_pressed);
ohtsuka 1:67d677ad73e7 13 _iin->rise(this, &MultiClick::isr_released);
ohtsuka 0:9c5c674dcaea 14 } else {
ohtsuka 1:67d677ad73e7 15 DBG_PRINT("isr rise.\r\n");
ohtsuka 1:67d677ad73e7 16 _iin->fall(this, &MultiClick::isr_released);
ohtsuka 1:67d677ad73e7 17 _iin->rise(this, &MultiClick::isr_pressed);
ohtsuka 0:9c5c674dcaea 18 }
ohtsuka 0:9c5c674dcaea 19
ohtsuka 0:9c5c674dcaea 20 _timeout = new Timeout();
ohtsuka 1:67d677ad73e7 21 _debounce_timeout = new Timeout();
ohtsuka 1:67d677ad73e7 22 _t = new Timer();
ohtsuka 1:67d677ad73e7 23 _t->start();
ohtsuka 0:9c5c674dcaea 24
ohtsuka 1:67d677ad73e7 25 _click_interval_us = 300*1000;
ohtsuka 1:67d677ad73e7 26 _debounce_ignore_us =100*1000;
ohtsuka 1:67d677ad73e7 27 _debounce_ignore = false;
ohtsuka 0:9c5c674dcaea 28 }
ohtsuka 0:9c5c674dcaea 29
ohtsuka 1:67d677ad73e7 30 void MultiClick::isr_pressed( void )
ohtsuka 0:9c5c674dcaea 31 {
ohtsuka 1:67d677ad73e7 32 // --- チャタリング(debounce)チェック ------------------------------------------------
ohtsuka 1:67d677ad73e7 33 if(_debounce_ignore == true) {
ohtsuka 1:67d677ad73e7 34 DBG_PRINT("bounce. ");
ohtsuka 1:67d677ad73e7 35 fflush(stdout);
ohtsuka 1:67d677ad73e7 36
ohtsuka 1:67d677ad73e7 37 return;
ohtsuka 1:67d677ad73e7 38 } else {
ohtsuka 1:67d677ad73e7 39 DBG_PRINT("isr_pressed called:\r\n");
ohtsuka 1:67d677ad73e7 40 fflush(stdout);
ohtsuka 1:67d677ad73e7 41 }
ohtsuka 1:67d677ad73e7 42
ohtsuka 1:67d677ad73e7 43 // クリック認定
ohtsuka 0:9c5c674dcaea 44 _click_times++;
ohtsuka 0:9c5c674dcaea 45 switch( _click_times ) {
ohtsuka 0:9c5c674dcaea 46 case 0:
ohtsuka 1:67d677ad73e7 47 DBG_PRINT("error!");
ohtsuka 0:9c5c674dcaea 48 break;
ohtsuka 0:9c5c674dcaea 49
ohtsuka 0:9c5c674dcaea 50 case 1:
ohtsuka 0:9c5c674dcaea 51 if(_c_callback_clicked != 0) {
ohtsuka 0:9c5c674dcaea 52 (*_c_callback_clicked)();
ohtsuka 0:9c5c674dcaea 53 }
ohtsuka 0:9c5c674dcaea 54 break;
ohtsuka 0:9c5c674dcaea 55
ohtsuka 0:9c5c674dcaea 56 case 2:
ohtsuka 0:9c5c674dcaea 57 if(_c_callback_doubleclicked != 0) {
ohtsuka 0:9c5c674dcaea 58 (*_c_callback_doubleclicked)();
ohtsuka 0:9c5c674dcaea 59 }
ohtsuka 0:9c5c674dcaea 60 break;
ohtsuka 0:9c5c674dcaea 61 default:
ohtsuka 0:9c5c674dcaea 62 if(_c_callback_n_clicked != 0) {
ohtsuka 0:9c5c674dcaea 63 (*_c_callback_n_clicked)(_click_times);
ohtsuka 0:9c5c674dcaea 64 }
ohtsuka 0:9c5c674dcaea 65 }
ohtsuka 0:9c5c674dcaea 66
ohtsuka 1:67d677ad73e7 67 // チャタリングチェック期間を開始する設定(初回、押し下げ後一定時間は、押し下げを無視)
ohtsuka 1:67d677ad73e7 68 _debounce_ignore = true;
ohtsuka 1:67d677ad73e7 69 _debounce_timeout->detach();
ohtsuka 1:67d677ad73e7 70 _debounce_timeout->attach_us(this, &MultiClick::debounce_timeout, _debounce_ignore_us );
ohtsuka 1:67d677ad73e7 71
ohtsuka 1:67d677ad73e7 72 // マルチクリック検出用タイマーを設定
ohtsuka 1:67d677ad73e7 73 _timeout->detach();
ohtsuka 1:67d677ad73e7 74 _timeout->attach_us(this, &MultiClick::click_timeout, _click_interval_us );
ohtsuka 1:67d677ad73e7 75
ohtsuka 1:67d677ad73e7 76 }
ohtsuka 1:67d677ad73e7 77
ohtsuka 1:67d677ad73e7 78 void MultiClick::isr_released( void )
ohtsuka 1:67d677ad73e7 79 {
ohtsuka 1:67d677ad73e7 80
ohtsuka 1:67d677ad73e7 81 }
ohtsuka 1:67d677ad73e7 82
ohtsuka 1:67d677ad73e7 83 void MultiClick::debounce_timeout( void )
ohtsuka 1:67d677ad73e7 84 {
ohtsuka 1:67d677ad73e7 85 DBG_PRINT("--- debounce T.O.\r\n");
ohtsuka 1:67d677ad73e7 86 _debounce_ignore = false;
ohtsuka 0:9c5c674dcaea 87 }
ohtsuka 0:9c5c674dcaea 88
ohtsuka 0:9c5c674dcaea 89 void MultiClick::click_timeout( void )
ohtsuka 0:9c5c674dcaea 90 {
ohtsuka 1:67d677ad73e7 91 DBG_PRINT("===== multiclick T.O.\r\n\r\n");
ohtsuka 0:9c5c674dcaea 92 _click_times = 0;
ohtsuka 0:9c5c674dcaea 93 }
ohtsuka 0:9c5c674dcaea 94
ohtsuka 1:67d677ad73e7 95
ohtsuka 0:9c5c674dcaea 96 void MultiClick::attach_clicked(void (*function)(void)) { _c_callback_clicked = function; }
ohtsuka 0:9c5c674dcaea 97
ohtsuka 0:9c5c674dcaea 98 void MultiClick::attach_doubleclicked(void (*function)(void) = 0) { _c_callback_doubleclicked = function; }
ohtsuka 0:9c5c674dcaea 99
ohtsuka 0:9c5c674dcaea 100 void MultiClick::attach_n_clicked(void (*function)(int) = 0) { _c_callback_n_clicked = function; }