Modified version to work with EventQueue (outside of ISR)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MultiClick.cpp Source File

MultiClick.cpp

00001 /*
00002 MultiClick.cpp:
00003   For one button operation.
00004   This library suports these events,
00005     * Single click
00006     * Double click
00007     * N times click (over 3 times click)
00008     * Long press
00009 
00010 The MIT License (MIT)
00011 
00012 Copyright (c) 2016 Uematsu Electric Co.,Ltd. Toru OHTSUKA
00013 <t-ohtsuka@jupiter.ocn.ne.jp>
00014 
00015 Permission is hereby granted, free of charge, to any person obtaining a copy
00016 of this software and associated documentation files (the "Software"), to deal
00017 in the Software without restriction, including without limitation the rights
00018 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00019 copies of the Software, and to permit persons to whom the Software is
00020 furnished to do so, subject to the following conditions:
00021 
00022 The above copyright notice and this permission notice shall be included in
00023 all copies or substantial portions of the Software.
00024 
00025 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00026 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00027 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00028 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00029 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00030 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 THE SOFTWARE.
00032 */
00033 
00034 /*
00035      * Modified version to work with an EventQueue
00036      * Removed deprecated code
00037  */
00038 
00039 #include "MultiClick.h"
00040 
00041 MultiClick::MultiClick(PinName p, PinMode m, EventQueue *q) {
00042   _iin = new InterruptIn(p);
00043   _mode = m;
00044 
00045   _iin->mode(_mode);
00046 
00047   _queue = q;
00048 
00049   if (_mode == PullUp) {
00050 
00051     _iin->fall(callback(this, &MultiClick::isr_pressed));
00052   } else {
00053 
00054     _iin->rise(callback(this, &MultiClick::isr_pressed));
00055   }
00056 
00057   _click_detect_timeout = new Timeout();
00058   _press_check = new Ticker();
00059 
00060   _press_check_interval_us = 25 * 1000;
00061   _shortpress_num = 1;
00062   _longpress_num = 20;
00063   _click_interval_us = 300 * 1000;
00064 
00065   _pressed_count = 0;
00066   _longpressed = false;
00067 }
00068 
00069 void MultiClick::isr_pressed(void) {
00070 
00071   if (_pressed_count > 0) {
00072     press_check_func();
00073   } else {
00074     _pressed_count = 0;
00075     _press_check->detach();
00076     _press_check->attach_us(callback(this, &MultiClick::press_check_func),
00077                             _press_check_interval_us);
00078 
00079     _longpressed = false;
00080   }
00081 }
00082 
00083 void MultiClick::press_check_func(void) {
00084   int curr_state;
00085   curr_state = _iin->read();
00086 
00087   if (curr_state == 0) {
00088 
00089     _pressed_count++;
00090 
00091     if (_pressed_count > _longpress_num) {
00092       if (_longpressed != true) {
00093         if (_c_callback_longpressed != 0) {
00094           _queue->call(*_c_callback_longpressed);
00095         }
00096         _longpressed = true;
00097       }
00098     }
00099   } else {
00100 
00101     if (_pressed_count >= _shortpress_num) {
00102       _click_times++;
00103     }
00104 
00105     _press_check->detach();
00106     _pressed_count = 0;
00107 
00108     _click_detect_timeout->detach();
00109     _click_detect_timeout->attach_us(
00110         callback(this, &MultiClick::click_detect_timeout), _click_interval_us);
00111   }
00112 }
00113 
00114 void MultiClick::click_detect_timeout(void) {
00115 
00116   if (_longpressed) {
00117 
00118   } else {
00119     switch (_click_times) {
00120     case 0:
00121 
00122       break;
00123 
00124     case 1:
00125       if (_c_callback_clicked != 0) {
00126         _queue->call(*_c_callback_clicked);
00127       }
00128       break;
00129 
00130     case 2:
00131       if (_c_callback_doubleclicked != 0) {
00132         _queue->call(*_c_callback_doubleclicked);
00133       }
00134       break;
00135     default:
00136       if (_c_callback_n_clicked != 0) {
00137         _queue->call(*_c_callback_n_clicked, (_click_times));
00138       }
00139     }
00140   }
00141 
00142   _click_times = 0;
00143   _pressed_count = 0;
00144 }
00145 
00146 void MultiClick::attach_clicked(void (*function)(void) = 0) {
00147   _c_callback_clicked = function;
00148 }
00149 
00150 void MultiClick::attach_doubleclicked(void (*function)(void) = 0) {
00151   _c_callback_doubleclicked = function;
00152 }
00153 
00154 void MultiClick::attach_n_clicked(void (*function)(int) = 0) {
00155   _c_callback_n_clicked = function;
00156 }
00157 
00158 void MultiClick::attach_longpressed(void (*function)(void) = 0) {
00159   _c_callback_longpressed = function;
00160 }