Modified version to work with EventQueue (outside of ISR)
MultiClick.cpp@8:633350efee38, 2020-04-17 (annotated)
- Committer:
- peekpt
- Date:
- Fri Apr 17 22:33:15 2020 +0000
- Revision:
- 8:633350efee38
- Parent:
- 7:7d56935ba84d
clean code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ohtsuka | 7:7d56935ba84d | 1 | /* |
ohtsuka | 7:7d56935ba84d | 2 | MultiClick.cpp: |
ohtsuka | 7:7d56935ba84d | 3 | For one button operation. |
ohtsuka | 7:7d56935ba84d | 4 | This library suports these events, |
ohtsuka | 7:7d56935ba84d | 5 | * Single click |
ohtsuka | 7:7d56935ba84d | 6 | * Double click |
ohtsuka | 7:7d56935ba84d | 7 | * N times click (over 3 times click) |
ohtsuka | 7:7d56935ba84d | 8 | * Long press |
ohtsuka | 7:7d56935ba84d | 9 | |
ohtsuka | 7:7d56935ba84d | 10 | The MIT License (MIT) |
ohtsuka | 7:7d56935ba84d | 11 | |
peekpt | 8:633350efee38 | 12 | Copyright (c) 2016 Uematsu Electric Co.,Ltd. Toru OHTSUKA |
peekpt | 8:633350efee38 | 13 | <t-ohtsuka@jupiter.ocn.ne.jp> |
ohtsuka | 7:7d56935ba84d | 14 | |
ohtsuka | 7:7d56935ba84d | 15 | Permission is hereby granted, free of charge, to any person obtaining a copy |
ohtsuka | 7:7d56935ba84d | 16 | of this software and associated documentation files (the "Software"), to deal |
ohtsuka | 7:7d56935ba84d | 17 | in the Software without restriction, including without limitation the rights |
ohtsuka | 7:7d56935ba84d | 18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ohtsuka | 7:7d56935ba84d | 19 | copies of the Software, and to permit persons to whom the Software is |
ohtsuka | 7:7d56935ba84d | 20 | furnished to do so, subject to the following conditions: |
ohtsuka | 7:7d56935ba84d | 21 | |
ohtsuka | 7:7d56935ba84d | 22 | The above copyright notice and this permission notice shall be included in |
ohtsuka | 7:7d56935ba84d | 23 | all copies or substantial portions of the Software. |
ohtsuka | 7:7d56935ba84d | 24 | |
ohtsuka | 7:7d56935ba84d | 25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ohtsuka | 7:7d56935ba84d | 26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ohtsuka | 7:7d56935ba84d | 27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ohtsuka | 7:7d56935ba84d | 28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ohtsuka | 7:7d56935ba84d | 29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ohtsuka | 7:7d56935ba84d | 30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ohtsuka | 7:7d56935ba84d | 31 | THE SOFTWARE. |
ohtsuka | 7:7d56935ba84d | 32 | */ |
ohtsuka | 7:7d56935ba84d | 33 | |
peekpt | 8:633350efee38 | 34 | /* |
peekpt | 8:633350efee38 | 35 | * Modified version to work with an EventQueue |
peekpt | 8:633350efee38 | 36 | * Removed deprecated code |
peekpt | 8:633350efee38 | 37 | */ |
peekpt | 8:633350efee38 | 38 | |
ohtsuka | 0:9c5c674dcaea | 39 | #include "MultiClick.h" |
ohtsuka | 0:9c5c674dcaea | 40 | |
peekpt | 8:633350efee38 | 41 | MultiClick::MultiClick(PinName p, PinMode m, EventQueue *q) { |
peekpt | 8:633350efee38 | 42 | _iin = new InterruptIn(p); |
peekpt | 8:633350efee38 | 43 | _mode = m; |
peekpt | 8:633350efee38 | 44 | |
peekpt | 8:633350efee38 | 45 | _iin->mode(_mode); |
peekpt | 8:633350efee38 | 46 | |
peekpt | 8:633350efee38 | 47 | _queue = q; |
peekpt | 8:633350efee38 | 48 | |
peekpt | 8:633350efee38 | 49 | if (_mode == PullUp) { |
peekpt | 8:633350efee38 | 50 | |
peekpt | 8:633350efee38 | 51 | _iin->fall(callback(this, &MultiClick::isr_pressed)); |
peekpt | 8:633350efee38 | 52 | } else { |
ohtsuka | 1:67d677ad73e7 | 53 | |
peekpt | 8:633350efee38 | 54 | _iin->rise(callback(this, &MultiClick::isr_pressed)); |
peekpt | 8:633350efee38 | 55 | } |
peekpt | 8:633350efee38 | 56 | |
peekpt | 8:633350efee38 | 57 | _click_detect_timeout = new Timeout(); |
peekpt | 8:633350efee38 | 58 | _press_check = new Ticker(); |
ohtsuka | 0:9c5c674dcaea | 59 | |
peekpt | 8:633350efee38 | 60 | _press_check_interval_us = 25 * 1000; |
peekpt | 8:633350efee38 | 61 | _shortpress_num = 1; |
peekpt | 8:633350efee38 | 62 | _longpress_num = 20; |
peekpt | 8:633350efee38 | 63 | _click_interval_us = 300 * 1000; |
peekpt | 8:633350efee38 | 64 | |
peekpt | 8:633350efee38 | 65 | _pressed_count = 0; |
peekpt | 8:633350efee38 | 66 | _longpressed = false; |
ohtsuka | 0:9c5c674dcaea | 67 | } |
ohtsuka | 0:9c5c674dcaea | 68 | |
peekpt | 8:633350efee38 | 69 | void MultiClick::isr_pressed(void) { |
ohtsuka | 4:8d6883cdc3c8 | 70 | |
peekpt | 8:633350efee38 | 71 | if (_pressed_count > 0) { |
peekpt | 8:633350efee38 | 72 | press_check_func(); |
peekpt | 8:633350efee38 | 73 | } else { |
peekpt | 8:633350efee38 | 74 | _pressed_count = 0; |
peekpt | 8:633350efee38 | 75 | _press_check->detach(); |
peekpt | 8:633350efee38 | 76 | _press_check->attach_us(callback(this, &MultiClick::press_check_func), |
peekpt | 8:633350efee38 | 77 | _press_check_interval_us); |
peekpt | 8:633350efee38 | 78 | |
peekpt | 8:633350efee38 | 79 | _longpressed = false; |
peekpt | 8:633350efee38 | 80 | } |
ohtsuka | 2:6af39916a4a2 | 81 | } |
ohtsuka | 2:6af39916a4a2 | 82 | |
peekpt | 8:633350efee38 | 83 | void MultiClick::press_check_func(void) { |
peekpt | 8:633350efee38 | 84 | int curr_state; |
peekpt | 8:633350efee38 | 85 | curr_state = _iin->read(); |
peekpt | 8:633350efee38 | 86 | |
peekpt | 8:633350efee38 | 87 | if (curr_state == 0) { |
peekpt | 8:633350efee38 | 88 | |
peekpt | 8:633350efee38 | 89 | _pressed_count++; |
peekpt | 8:633350efee38 | 90 | |
peekpt | 8:633350efee38 | 91 | if (_pressed_count > _longpress_num) { |
peekpt | 8:633350efee38 | 92 | if (_longpressed != true) { |
peekpt | 8:633350efee38 | 93 | if (_c_callback_longpressed != 0) { |
peekpt | 8:633350efee38 | 94 | _queue->call(*_c_callback_longpressed); |
ohtsuka | 5:cb4d45f41e17 | 95 | } |
peekpt | 8:633350efee38 | 96 | _longpressed = true; |
peekpt | 8:633350efee38 | 97 | } |
peekpt | 8:633350efee38 | 98 | } |
peekpt | 8:633350efee38 | 99 | } else { |
ohtsuka | 3:c7f97bebf2f7 | 100 | |
peekpt | 8:633350efee38 | 101 | if (_pressed_count >= _shortpress_num) { |
peekpt | 8:633350efee38 | 102 | _click_times++; |
ohtsuka | 5:cb4d45f41e17 | 103 | } |
peekpt | 8:633350efee38 | 104 | |
peekpt | 8:633350efee38 | 105 | _press_check->detach(); |
peekpt | 8:633350efee38 | 106 | _pressed_count = 0; |
peekpt | 8:633350efee38 | 107 | |
peekpt | 8:633350efee38 | 108 | _click_detect_timeout->detach(); |
peekpt | 8:633350efee38 | 109 | _click_detect_timeout->attach_us( |
peekpt | 8:633350efee38 | 110 | callback(this, &MultiClick::click_detect_timeout), _click_interval_us); |
peekpt | 8:633350efee38 | 111 | } |
ohtsuka | 1:67d677ad73e7 | 112 | } |
ohtsuka | 1:67d677ad73e7 | 113 | |
peekpt | 8:633350efee38 | 114 | void MultiClick::click_detect_timeout(void) { |
peekpt | 8:633350efee38 | 115 | |
peekpt | 8:633350efee38 | 116 | if (_longpressed) { |
peekpt | 8:633350efee38 | 117 | |
peekpt | 8:633350efee38 | 118 | } else { |
peekpt | 8:633350efee38 | 119 | switch (_click_times) { |
peekpt | 8:633350efee38 | 120 | case 0: |
peekpt | 8:633350efee38 | 121 | |
peekpt | 8:633350efee38 | 122 | break; |
ohtsuka | 4:8d6883cdc3c8 | 123 | |
peekpt | 8:633350efee38 | 124 | case 1: |
peekpt | 8:633350efee38 | 125 | if (_c_callback_clicked != 0) { |
peekpt | 8:633350efee38 | 126 | _queue->call(*_c_callback_clicked); |
peekpt | 8:633350efee38 | 127 | } |
peekpt | 8:633350efee38 | 128 | break; |
peekpt | 8:633350efee38 | 129 | |
peekpt | 8:633350efee38 | 130 | case 2: |
peekpt | 8:633350efee38 | 131 | if (_c_callback_doubleclicked != 0) { |
peekpt | 8:633350efee38 | 132 | _queue->call(*_c_callback_doubleclicked); |
peekpt | 8:633350efee38 | 133 | } |
peekpt | 8:633350efee38 | 134 | break; |
peekpt | 8:633350efee38 | 135 | default: |
peekpt | 8:633350efee38 | 136 | if (_c_callback_n_clicked != 0) { |
peekpt | 8:633350efee38 | 137 | _queue->call(*_c_callback_n_clicked, (_click_times)); |
peekpt | 8:633350efee38 | 138 | } |
ohtsuka | 4:8d6883cdc3c8 | 139 | } |
peekpt | 8:633350efee38 | 140 | } |
peekpt | 8:633350efee38 | 141 | |
peekpt | 8:633350efee38 | 142 | _click_times = 0; |
peekpt | 8:633350efee38 | 143 | _pressed_count = 0; |
ohtsuka | 0:9c5c674dcaea | 144 | } |
ohtsuka | 0:9c5c674dcaea | 145 | |
peekpt | 8:633350efee38 | 146 | void MultiClick::attach_clicked(void (*function)(void) = 0) { |
peekpt | 8:633350efee38 | 147 | _c_callback_clicked = function; |
peekpt | 8:633350efee38 | 148 | } |
ohtsuka | 0:9c5c674dcaea | 149 | |
peekpt | 8:633350efee38 | 150 | void MultiClick::attach_doubleclicked(void (*function)(void) = 0) { |
peekpt | 8:633350efee38 | 151 | _c_callback_doubleclicked = function; |
peekpt | 8:633350efee38 | 152 | } |
ohtsuka | 0:9c5c674dcaea | 153 | |
peekpt | 8:633350efee38 | 154 | void MultiClick::attach_n_clicked(void (*function)(int) = 0) { |
peekpt | 8:633350efee38 | 155 | _c_callback_n_clicked = function; |
peekpt | 8:633350efee38 | 156 | } |
ohtsuka | 3:c7f97bebf2f7 | 157 | |
peekpt | 8:633350efee38 | 158 | void MultiClick::attach_longpressed(void (*function)(void) = 0) { |
peekpt | 8:633350efee38 | 159 | _c_callback_longpressed = function; |
peekpt | 8:633350efee38 | 160 | } |