You can use multiple clicks event with one button. This library supports follow events, Single click, Double click, over 3 times click, Long press.
MultiClick.h@7:7d56935ba84d, 2016-06-16 (annotated)
- Committer:
- ohtsuka
- Date:
- Thu Jun 16 01:29:43 2016 +0000
- Revision:
- 7:7d56935ba84d
- Parent:
- 5:cb4d45f41e17
add file header(MIT License)
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 | |
ohtsuka | 7:7d56935ba84d | 12 | Copyright (c) 2016 Uematsu Electric Co.,Ltd. Toru OHTSUKA <t-ohtsuka@jupiter.ocn.ne.jp> |
ohtsuka | 7:7d56935ba84d | 13 | |
ohtsuka | 7:7d56935ba84d | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy |
ohtsuka | 7:7d56935ba84d | 15 | of this software and associated documentation files (the "Software"), to deal |
ohtsuka | 7:7d56935ba84d | 16 | in the Software without restriction, including without limitation the rights |
ohtsuka | 7:7d56935ba84d | 17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ohtsuka | 7:7d56935ba84d | 18 | copies of the Software, and to permit persons to whom the Software is |
ohtsuka | 7:7d56935ba84d | 19 | furnished to do so, subject to the following conditions: |
ohtsuka | 7:7d56935ba84d | 20 | |
ohtsuka | 7:7d56935ba84d | 21 | The above copyright notice and this permission notice shall be included in |
ohtsuka | 7:7d56935ba84d | 22 | all copies or substantial portions of the Software. |
ohtsuka | 7:7d56935ba84d | 23 | |
ohtsuka | 7:7d56935ba84d | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ohtsuka | 7:7d56935ba84d | 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ohtsuka | 7:7d56935ba84d | 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ohtsuka | 7:7d56935ba84d | 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ohtsuka | 7:7d56935ba84d | 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ohtsuka | 7:7d56935ba84d | 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ohtsuka | 7:7d56935ba84d | 30 | THE SOFTWARE. |
ohtsuka | 7:7d56935ba84d | 31 | */ |
ohtsuka | 7:7d56935ba84d | 32 | |
ohtsuka | 0:9c5c674dcaea | 33 | #include "mbed.h" |
ohtsuka | 0:9c5c674dcaea | 34 | |
ohtsuka | 5:cb4d45f41e17 | 35 | //#define DEBUG |
ohtsuka | 1:67d677ad73e7 | 36 | |
ohtsuka | 1:67d677ad73e7 | 37 | #ifdef DEBUG |
ohtsuka | 1:67d677ad73e7 | 38 | #define DBG_PRINT(...) printf(__VA_ARGS__) |
ohtsuka | 1:67d677ad73e7 | 39 | #else |
ohtsuka | 1:67d677ad73e7 | 40 | #define DBG_PRINT(...) |
ohtsuka | 1:67d677ad73e7 | 41 | #endif |
ohtsuka | 1:67d677ad73e7 | 42 | |
ohtsuka | 0:9c5c674dcaea | 43 | class MultiClick |
ohtsuka | 0:9c5c674dcaea | 44 | { |
ohtsuka | 0:9c5c674dcaea | 45 | public: |
ohtsuka | 0:9c5c674dcaea | 46 | MultiClick(PinName pin); |
ohtsuka | 0:9c5c674dcaea | 47 | MultiClick(PinName pin, PinMode m); |
ohtsuka | 0:9c5c674dcaea | 48 | void attach_clicked(void (*function)(void)); |
ohtsuka | 0:9c5c674dcaea | 49 | void attach_doubleclicked(void (*function)(void)); |
ohtsuka | 0:9c5c674dcaea | 50 | void attach_n_clicked(void (*function)(int)); |
ohtsuka | 3:c7f97bebf2f7 | 51 | void attach_longpressed(void (*function)(void)); |
ohtsuka | 0:9c5c674dcaea | 52 | |
ohtsuka | 0:9c5c674dcaea | 53 | private: |
ohtsuka | 1:67d677ad73e7 | 54 | void isr_pressed(void); |
ohtsuka | 5:cb4d45f41e17 | 55 | void click_detect_timeout( void ); |
ohtsuka | 5:cb4d45f41e17 | 56 | void press_check_func( void ); |
ohtsuka | 1:67d677ad73e7 | 57 | |
ohtsuka | 7:7d56935ba84d | 58 | InterruptIn *_iin; |
ohtsuka | 7:7d56935ba84d | 59 | PinMode _mode; |
ohtsuka | 7:7d56935ba84d | 60 | |
ohtsuka | 5:cb4d45f41e17 | 61 | Ticker *_press_check; |
ohtsuka | 5:cb4d45f41e17 | 62 | Timeout *_click_detect_timeout; |
ohtsuka | 0:9c5c674dcaea | 63 | |
ohtsuka | 5:cb4d45f41e17 | 64 | int _shortpress_num; |
ohtsuka | 5:cb4d45f41e17 | 65 | int _longpress_num; |
ohtsuka | 5:cb4d45f41e17 | 66 | |
ohtsuka | 5:cb4d45f41e17 | 67 | int _press_check_interval_us; |
ohtsuka | 1:67d677ad73e7 | 68 | int _click_interval_us; |
ohtsuka | 0:9c5c674dcaea | 69 | int _click_times; |
ohtsuka | 0:9c5c674dcaea | 70 | |
ohtsuka | 5:cb4d45f41e17 | 71 | int _pressed_count; |
ohtsuka | 3:c7f97bebf2f7 | 72 | bool _longpressed; |
ohtsuka | 4:8d6883cdc3c8 | 73 | |
ohtsuka | 0:9c5c674dcaea | 74 | void (*_c_callback_clicked)(void); |
ohtsuka | 0:9c5c674dcaea | 75 | void (*_c_callback_doubleclicked)(void); |
ohtsuka | 0:9c5c674dcaea | 76 | void (*_c_callback_n_clicked)(int); |
ohtsuka | 3:c7f97bebf2f7 | 77 | void (*_c_callback_longpressed)(void); |
ohtsuka | 0:9c5c674dcaea | 78 | }; |