Modified version to work with EventQueue (outside of ISR)
MultiClick.h@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.h: |
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 "mbed.h" |
ohtsuka | 0:9c5c674dcaea | 40 | |
peekpt | 8:633350efee38 | 41 | class MultiClick { |
peekpt | 8:633350efee38 | 42 | public: |
peekpt | 8:633350efee38 | 43 | MultiClick(PinName pin, EventQueue *q); |
peekpt | 8:633350efee38 | 44 | MultiClick(PinName pin, PinMode m, EventQueue *q); |
peekpt | 8:633350efee38 | 45 | void attach_clicked(void (*function)(void)); |
peekpt | 8:633350efee38 | 46 | void attach_doubleclicked(void (*function)(void)); |
peekpt | 8:633350efee38 | 47 | void attach_n_clicked(void (*function)(int)); |
peekpt | 8:633350efee38 | 48 | void attach_longpressed(void (*function)(void)); |
ohtsuka | 1:67d677ad73e7 | 49 | |
ohtsuka | 0:9c5c674dcaea | 50 | private: |
peekpt | 8:633350efee38 | 51 | void isr_pressed(void); |
peekpt | 8:633350efee38 | 52 | void click_detect_timeout(void); |
peekpt | 8:633350efee38 | 53 | void press_check_func(void); |
ohtsuka | 1:67d677ad73e7 | 54 | |
peekpt | 8:633350efee38 | 55 | InterruptIn *_iin; |
peekpt | 8:633350efee38 | 56 | PinMode _mode; |
ohtsuka | 7:7d56935ba84d | 57 | |
peekpt | 8:633350efee38 | 58 | Ticker *_press_check; |
peekpt | 8:633350efee38 | 59 | Timeout *_click_detect_timeout; |
peekpt | 8:633350efee38 | 60 | |
peekpt | 8:633350efee38 | 61 | int _shortpress_num; |
peekpt | 8:633350efee38 | 62 | int _longpress_num; |
ohtsuka | 5:cb4d45f41e17 | 63 | |
peekpt | 8:633350efee38 | 64 | int _press_check_interval_us; |
peekpt | 8:633350efee38 | 65 | int _click_interval_us; |
peekpt | 8:633350efee38 | 66 | int _click_times; |
peekpt | 8:633350efee38 | 67 | |
peekpt | 8:633350efee38 | 68 | int _pressed_count; |
peekpt | 8:633350efee38 | 69 | bool _longpressed; |
peekpt | 8:633350efee38 | 70 | |
peekpt | 8:633350efee38 | 71 | EventQueue *_queue; |
peekpt | 8:633350efee38 | 72 | |
peekpt | 8:633350efee38 | 73 | void (*_c_callback_clicked)(void); |
peekpt | 8:633350efee38 | 74 | void (*_c_callback_doubleclicked)(void); |
peekpt | 8:633350efee38 | 75 | void (*_c_callback_n_clicked)(int); |
peekpt | 8:633350efee38 | 76 | void (*_c_callback_longpressed)(void); |
ohtsuka | 0:9c5c674dcaea | 77 | }; |