Modified version to work with EventQueue (outside of ISR)
Diff: MultiClick.cpp
- Revision:
- 3:c7f97bebf2f7
- Parent:
- 2:6af39916a4a2
- Child:
- 4:8d6883cdc3c8
--- a/MultiClick.cpp Mon Jun 13 09:11:53 2016 +0000 +++ b/MultiClick.cpp Mon Jun 13 10:15:15 2016 +0000 @@ -19,37 +19,55 @@ _timeout = new Timeout(); _debounce_timeout = new Timeout(); + _longpress_timeout = new Timeout(); _t = new Timer(); _t->start(); _click_interval_us = 300*1000; - _debounce_ignore_us =100*1000; + _debounce_ignore_us = 100*1000; + _longpress_us = 1000*1000; _debounce_ignore = false; + _longpressed = false; } void MultiClick::isr_pressed( void ) { + + // --- 長押し検出用タイマーを停止 + _longpress_timeout->detach(); + _longpressed = false; + // --- チャタリング(debounce)チェック ------------------------------------------------ if(_debounce_ignore == true) { DBG_PRINT("isr_pressed: bounce. "); fflush(stdout); - - return; } else { DBG_PRINT("isr_pressed called:\r\n"); fflush(stdout); - } + + // 長押し検出用タイマー開始 + _longpress_timeout->attach_us(this, &MultiClick::longpress_timeout, _longpress_us ); + } } void MultiClick::isr_released( void ) { + // 長押し検出用タイマーを停止 + _longpress_timeout->detach(); + // --- チャタリング(debounce)チェック ------------------------------------------------ if(_debounce_ignore == true) { DBG_PRINT("isr_released: bounce. "); fflush(stdout); return; + } else if(_longpressed == true) { // 長押しチェック + DBG_PRINT("isr_released: _longpressed. "); + fflush(stdout); + + _longpressed = false; + return; } else { DBG_PRINT("isr_released called:\r\n"); fflush(stdout); @@ -103,9 +121,22 @@ _click_times = 0; } +void MultiClick::longpress_timeout( void ) +{ + DBG_PRINT("===== longpress T.O.\r\n\r\n"); -void MultiClick::attach_clicked(void (*function)(void)) { _c_callback_clicked = function; } + if(_c_callback_longpressed != 0) { + (*_c_callback_longpressed)(); + } + + _longpressed = true; +} + + +void MultiClick::attach_clicked(void (*function)(void) = 0) { _c_callback_clicked = function; } void MultiClick::attach_doubleclicked(void (*function)(void) = 0) { _c_callback_doubleclicked = function; } void MultiClick::attach_n_clicked(void (*function)(int) = 0) { _c_callback_n_clicked = function; } + +void MultiClick::attach_longpressed(void (*function)(void) = 0) { _c_callback_longpressed = function; }