Modified version to work with EventQueue (outside of ISR)
MultiClick.cpp@5:cb4d45f41e17, 2016-06-15 (annotated)
- Committer:
- ohtsuka
- Date:
- Wed Jun 15 08:55:09 2016 +0000
- Revision:
- 5:cb4d45f41e17
- Parent:
- 4:8d6883cdc3c8
- Child:
- 6:7f1142cc196a
fall??????????????????
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 | 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 | 0:9c5c674dcaea | 13 | } else { |
ohtsuka | 1:67d677ad73e7 | 14 | DBG_PRINT("isr rise.\r\n"); |
ohtsuka | 1:67d677ad73e7 | 15 | _iin->rise(this, &MultiClick::isr_pressed); |
ohtsuka | 0:9c5c674dcaea | 16 | } |
ohtsuka | 0:9c5c674dcaea | 17 | |
ohtsuka | 5:cb4d45f41e17 | 18 | _click_detect_timeout = new Timeout(); |
ohtsuka | 5:cb4d45f41e17 | 19 | _press_check = new Ticker(); |
ohtsuka | 1:67d677ad73e7 | 20 | _t = new Timer(); |
ohtsuka | 1:67d677ad73e7 | 21 | _t->start(); |
ohtsuka | 0:9c5c674dcaea | 22 | |
ohtsuka | 5:cb4d45f41e17 | 23 | _press_check_interval_us = 25*1000; |
ohtsuka | 5:cb4d45f41e17 | 24 | _shortpress_num = 1; |
ohtsuka | 5:cb4d45f41e17 | 25 | _longpress_num = 20; |
ohtsuka | 1:67d677ad73e7 | 26 | _click_interval_us = 300*1000; |
ohtsuka | 4:8d6883cdc3c8 | 27 | |
ohtsuka | 5:cb4d45f41e17 | 28 | _pressed_count = 0; |
ohtsuka | 3:c7f97bebf2f7 | 29 | _longpressed = false; |
ohtsuka | 0:9c5c674dcaea | 30 | } |
ohtsuka | 0:9c5c674dcaea | 31 | |
ohtsuka | 1:67d677ad73e7 | 32 | void MultiClick::isr_pressed( void ) |
ohtsuka | 0:9c5c674dcaea | 33 | { |
ohtsuka | 5:cb4d45f41e17 | 34 | DBG_PRINT("isr_pressed called:\r\n"); |
ohtsuka | 5:cb4d45f41e17 | 35 | fflush(stdout); |
ohtsuka | 4:8d6883cdc3c8 | 36 | |
ohtsuka | 5:cb4d45f41e17 | 37 | if(_pressed_count > 0) { // 「プレス中のプレス」 |
ohtsuka | 5:cb4d45f41e17 | 38 | DBG_PRINT("_"); |
ohtsuka | 5:cb4d45f41e17 | 39 | press_check_func(); |
ohtsuka | 5:cb4d45f41e17 | 40 | } else { // 通常時 |
ohtsuka | 5:cb4d45f41e17 | 41 | |
ohtsuka | 5:cb4d45f41e17 | 42 | // プレス判定処理開始 |
ohtsuka | 5:cb4d45f41e17 | 43 | _pressed_count = 0; |
ohtsuka | 5:cb4d45f41e17 | 44 | _press_check->detach(); |
ohtsuka | 5:cb4d45f41e17 | 45 | _press_check->attach_us(this, &MultiClick::press_check_func, _press_check_interval_us ); |
ohtsuka | 5:cb4d45f41e17 | 46 | |
ohtsuka | 3:c7f97bebf2f7 | 47 | _longpressed = false; |
ohtsuka | 5:cb4d45f41e17 | 48 | |
ohtsuka | 3:c7f97bebf2f7 | 49 | } |
ohtsuka | 2:6af39916a4a2 | 50 | } |
ohtsuka | 2:6af39916a4a2 | 51 | |
ohtsuka | 5:cb4d45f41e17 | 52 | void MultiClick::press_check_func( void ) |
ohtsuka | 2:6af39916a4a2 | 53 | { |
ohtsuka | 5:cb4d45f41e17 | 54 | int curr_state; |
ohtsuka | 5:cb4d45f41e17 | 55 | curr_state = _iin->read(); |
ohtsuka | 4:8d6883cdc3c8 | 56 | |
ohtsuka | 5:cb4d45f41e17 | 57 | if(curr_state == 0) { // 押されている場合 |
ohtsuka | 5:cb4d45f41e17 | 58 | DBG_PRINT("."); |
ohtsuka | 5:cb4d45f41e17 | 59 | _pressed_count++; |
ohtsuka | 5:cb4d45f41e17 | 60 | // 長押しチェック |
ohtsuka | 5:cb4d45f41e17 | 61 | if(_pressed_count > _longpress_num) { |
ohtsuka | 5:cb4d45f41e17 | 62 | if(_longpressed != true) { |
ohtsuka | 5:cb4d45f41e17 | 63 | if(_c_callback_longpressed != 0) { |
ohtsuka | 5:cb4d45f41e17 | 64 | (*_c_callback_longpressed)(); |
ohtsuka | 5:cb4d45f41e17 | 65 | } |
ohtsuka | 5:cb4d45f41e17 | 66 | _longpressed = true; |
ohtsuka | 5:cb4d45f41e17 | 67 | } |
ohtsuka | 5:cb4d45f41e17 | 68 | } |
ohtsuka | 5:cb4d45f41e17 | 69 | } else { // 押されていない場合 |
ohtsuka | 5:cb4d45f41e17 | 70 | DBG_PRINT("# _pressed_count=%d\r\n", _pressed_count); |
ohtsuka | 2:6af39916a4a2 | 71 | |
ohtsuka | 5:cb4d45f41e17 | 72 | if(_pressed_count >= _shortpress_num) { |
ohtsuka | 5:cb4d45f41e17 | 73 | _click_times++; |
ohtsuka | 5:cb4d45f41e17 | 74 | } |
ohtsuka | 5:cb4d45f41e17 | 75 | DBG_PRINT("# _click_times=%d\r\n", _click_times); |
ohtsuka | 5:cb4d45f41e17 | 76 | |
ohtsuka | 5:cb4d45f41e17 | 77 | _press_check->detach(); // プレスチェックを停止 |
ohtsuka | 3:c7f97bebf2f7 | 78 | |
ohtsuka | 5:cb4d45f41e17 | 79 | _click_detect_timeout->detach(); // マルチクリック検出用タイマーを設定 |
ohtsuka | 5:cb4d45f41e17 | 80 | _click_detect_timeout->attach_us(this, &MultiClick::click_detect_timeout, _click_interval_us ); |
ohtsuka | 5:cb4d45f41e17 | 81 | |
ohtsuka | 5:cb4d45f41e17 | 82 | } |
ohtsuka | 1:67d677ad73e7 | 83 | |
ohtsuka | 1:67d677ad73e7 | 84 | } |
ohtsuka | 1:67d677ad73e7 | 85 | |
ohtsuka | 5:cb4d45f41e17 | 86 | void MultiClick::click_detect_timeout( void ) |
ohtsuka | 1:67d677ad73e7 | 87 | { |
ohtsuka | 5:cb4d45f41e17 | 88 | DBG_PRINT("-> click_detect T.O.\r\n"); |
ohtsuka | 4:8d6883cdc3c8 | 89 | |
ohtsuka | 5:cb4d45f41e17 | 90 | if(_longpressed) { // 長押し時は何もしない |
ohtsuka | 5:cb4d45f41e17 | 91 | DBG_PRINT("-> skip n click C.B. \r\n"); |
ohtsuka | 5:cb4d45f41e17 | 92 | } else { // クリックイベントを通知 |
ohtsuka | 5:cb4d45f41e17 | 93 | switch( _click_times ) { |
ohtsuka | 5:cb4d45f41e17 | 94 | case 0: |
ohtsuka | 5:cb4d45f41e17 | 95 | DBG_PRINT("error!"); |
ohtsuka | 5:cb4d45f41e17 | 96 | break; |
ohtsuka | 5:cb4d45f41e17 | 97 | |
ohtsuka | 5:cb4d45f41e17 | 98 | case 1: |
ohtsuka | 5:cb4d45f41e17 | 99 | if(_c_callback_clicked != 0) { |
ohtsuka | 5:cb4d45f41e17 | 100 | (*_c_callback_clicked)(); |
ohtsuka | 5:cb4d45f41e17 | 101 | } |
ohtsuka | 5:cb4d45f41e17 | 102 | break; |
ohtsuka | 4:8d6883cdc3c8 | 103 | |
ohtsuka | 5:cb4d45f41e17 | 104 | case 2: |
ohtsuka | 5:cb4d45f41e17 | 105 | if(_c_callback_doubleclicked != 0) { |
ohtsuka | 5:cb4d45f41e17 | 106 | (*_c_callback_doubleclicked)(); |
ohtsuka | 5:cb4d45f41e17 | 107 | } |
ohtsuka | 5:cb4d45f41e17 | 108 | break; |
ohtsuka | 5:cb4d45f41e17 | 109 | default: |
ohtsuka | 5:cb4d45f41e17 | 110 | if(_c_callback_n_clicked != 0) { |
ohtsuka | 5:cb4d45f41e17 | 111 | (*_c_callback_n_clicked)(_click_times); |
ohtsuka | 5:cb4d45f41e17 | 112 | } |
ohtsuka | 4:8d6883cdc3c8 | 113 | } |
ohtsuka | 4:8d6883cdc3c8 | 114 | } |
ohtsuka | 5:cb4d45f41e17 | 115 | |
ohtsuka | 5:cb4d45f41e17 | 116 | _click_times = 0; |
ohtsuka | 5:cb4d45f41e17 | 117 | _pressed_count = 0; |
ohtsuka | 0:9c5c674dcaea | 118 | } |
ohtsuka | 0:9c5c674dcaea | 119 | |
ohtsuka | 3:c7f97bebf2f7 | 120 | void MultiClick::attach_clicked(void (*function)(void) = 0) { _c_callback_clicked = function; } |
ohtsuka | 0:9c5c674dcaea | 121 | |
ohtsuka | 0:9c5c674dcaea | 122 | void MultiClick::attach_doubleclicked(void (*function)(void) = 0) { _c_callback_doubleclicked = function; } |
ohtsuka | 0:9c5c674dcaea | 123 | |
ohtsuka | 0:9c5c674dcaea | 124 | void MultiClick::attach_n_clicked(void (*function)(int) = 0) { _c_callback_n_clicked = function; } |
ohtsuka | 3:c7f97bebf2f7 | 125 | |
ohtsuka | 3:c7f97bebf2f7 | 126 | void MultiClick::attach_longpressed(void (*function)(void) = 0) { _c_callback_longpressed = function; } |