MTDOT-BOX-EVB-Factory-Firmware
Dependencies: NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2
ButtonHandler/ButtonHandler.cpp@1:71125aa00e33, 2016-02-04 (annotated)
- Committer:
- Mike Fiore
- Date:
- Thu Feb 04 12:36:36 2016 -0600
- Revision:
- 1:71125aa00e33
add rest of source - version 2.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:71125aa00e33 | 1 | /* Copyright (c) <2016> <MultiTech Systems>, MIT License |
Mike Fiore |
1:71125aa00e33 | 2 | * |
Mike Fiore |
1:71125aa00e33 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Mike Fiore |
1:71125aa00e33 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Mike Fiore |
1:71125aa00e33 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Mike Fiore |
1:71125aa00e33 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Mike Fiore |
1:71125aa00e33 | 7 | * furnished to do so, subject to the following conditions: |
Mike Fiore |
1:71125aa00e33 | 8 | * |
Mike Fiore |
1:71125aa00e33 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Mike Fiore |
1:71125aa00e33 | 10 | * substantial portions of the Software. |
Mike Fiore |
1:71125aa00e33 | 11 | * |
Mike Fiore |
1:71125aa00e33 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Mike Fiore |
1:71125aa00e33 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Mike Fiore |
1:71125aa00e33 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Mike Fiore |
1:71125aa00e33 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Mike Fiore |
1:71125aa00e33 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Mike Fiore |
1:71125aa00e33 | 17 | */ |
Mike Fiore |
1:71125aa00e33 | 18 | |
Mike Fiore |
1:71125aa00e33 | 19 | #include "ButtonHandler.h" |
Mike Fiore |
1:71125aa00e33 | 20 | |
Mike Fiore |
1:71125aa00e33 | 21 | #define signal (int32_t)0xA0 |
Mike Fiore |
1:71125aa00e33 | 22 | |
Mike Fiore |
1:71125aa00e33 | 23 | typedef enum { |
Mike Fiore |
1:71125aa00e33 | 24 | b_none = 0, |
Mike Fiore |
1:71125aa00e33 | 25 | b_sw1_fall, |
Mike Fiore |
1:71125aa00e33 | 26 | b_sw1_rise, |
Mike Fiore |
1:71125aa00e33 | 27 | b_sw2_fall, |
Mike Fiore |
1:71125aa00e33 | 28 | b_sw2_rise |
Mike Fiore |
1:71125aa00e33 | 29 | } InternalButtonEvent; |
Mike Fiore |
1:71125aa00e33 | 30 | |
Mike Fiore |
1:71125aa00e33 | 31 | InternalButtonEvent event = b_none; |
Mike Fiore |
1:71125aa00e33 | 32 | bool check_sw1 = false; |
Mike Fiore |
1:71125aa00e33 | 33 | |
Mike Fiore |
1:71125aa00e33 | 34 | void b_worker(void const* argument) { |
Mike Fiore |
1:71125aa00e33 | 35 | ButtonHandler* b = (ButtonHandler*)argument; |
Mike Fiore |
1:71125aa00e33 | 36 | osEvent e; |
Mike Fiore |
1:71125aa00e33 | 37 | |
Mike Fiore |
1:71125aa00e33 | 38 | while (true) { |
Mike Fiore |
1:71125aa00e33 | 39 | e = Thread::signal_wait(signal, 250); |
Mike Fiore |
1:71125aa00e33 | 40 | if (e.status == osEventSignal) { |
Mike Fiore |
1:71125aa00e33 | 41 | switch (event) { |
Mike Fiore |
1:71125aa00e33 | 42 | case b_sw1_fall: |
Mike Fiore |
1:71125aa00e33 | 43 | if (! b->_sw1_running) { |
Mike Fiore |
1:71125aa00e33 | 44 | check_sw1 = true; |
Mike Fiore |
1:71125aa00e33 | 45 | b->_sw1_running = true; |
Mike Fiore |
1:71125aa00e33 | 46 | b->_sw1_timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 47 | b->_sw1_timer.start(); |
Mike Fiore |
1:71125aa00e33 | 48 | } |
Mike Fiore |
1:71125aa00e33 | 49 | break; |
Mike Fiore |
1:71125aa00e33 | 50 | |
Mike Fiore |
1:71125aa00e33 | 51 | case b_sw1_rise: |
Mike Fiore |
1:71125aa00e33 | 52 | if (b->_sw1_running) { |
Mike Fiore |
1:71125aa00e33 | 53 | check_sw1 = false; |
Mike Fiore |
1:71125aa00e33 | 54 | b->_sw1_running = false; |
Mike Fiore |
1:71125aa00e33 | 55 | b->_sw1_timer.stop(); |
Mike Fiore |
1:71125aa00e33 | 56 | b->_sw1_time = b->_sw1_timer.read_ms(); |
Mike Fiore |
1:71125aa00e33 | 57 | |
Mike Fiore |
1:71125aa00e33 | 58 | if (b->_sw1_time > b->_debounce_time) { |
Mike Fiore |
1:71125aa00e33 | 59 | b->_event = ButtonHandler::sw1_press; |
Mike Fiore |
1:71125aa00e33 | 60 | osSignalSet(b->_main, buttonSignal); |
Mike Fiore |
1:71125aa00e33 | 61 | } |
Mike Fiore |
1:71125aa00e33 | 62 | } |
Mike Fiore |
1:71125aa00e33 | 63 | break; |
Mike Fiore |
1:71125aa00e33 | 64 | |
Mike Fiore |
1:71125aa00e33 | 65 | case b_sw2_fall: |
Mike Fiore |
1:71125aa00e33 | 66 | if (! b->_sw2_running) { |
Mike Fiore |
1:71125aa00e33 | 67 | b->_sw2_running = true; |
Mike Fiore |
1:71125aa00e33 | 68 | b->_sw2_timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 69 | b->_sw2_timer.start(); |
Mike Fiore |
1:71125aa00e33 | 70 | } |
Mike Fiore |
1:71125aa00e33 | 71 | break; |
Mike Fiore |
1:71125aa00e33 | 72 | |
Mike Fiore |
1:71125aa00e33 | 73 | case b_sw2_rise: |
Mike Fiore |
1:71125aa00e33 | 74 | if (b->_sw2_running) { |
Mike Fiore |
1:71125aa00e33 | 75 | b->_sw2_running = false; |
Mike Fiore |
1:71125aa00e33 | 76 | b->_sw2_timer.stop(); |
Mike Fiore |
1:71125aa00e33 | 77 | b->_sw2_time = b->_sw2_timer.read_ms(); |
Mike Fiore |
1:71125aa00e33 | 78 | |
Mike Fiore |
1:71125aa00e33 | 79 | if (b->_sw2_time > b->_debounce_time) { |
Mike Fiore |
1:71125aa00e33 | 80 | b->_event = ButtonHandler::sw2_press; |
Mike Fiore |
1:71125aa00e33 | 81 | osSignalSet(b->_main, buttonSignal); |
Mike Fiore |
1:71125aa00e33 | 82 | } |
Mike Fiore |
1:71125aa00e33 | 83 | } |
Mike Fiore |
1:71125aa00e33 | 84 | break; |
Mike Fiore |
1:71125aa00e33 | 85 | |
Mike Fiore |
1:71125aa00e33 | 86 | default: |
Mike Fiore |
1:71125aa00e33 | 87 | break; |
Mike Fiore |
1:71125aa00e33 | 88 | } |
Mike Fiore |
1:71125aa00e33 | 89 | } |
Mike Fiore |
1:71125aa00e33 | 90 | |
Mike Fiore |
1:71125aa00e33 | 91 | if (check_sw1) { |
Mike Fiore |
1:71125aa00e33 | 92 | if (b->_sw1_timer.read_ms() > b->_hold_threshold) { |
Mike Fiore |
1:71125aa00e33 | 93 | check_sw1 = false; |
Mike Fiore |
1:71125aa00e33 | 94 | b->_sw1_running = false; |
Mike Fiore |
1:71125aa00e33 | 95 | b->_sw1_timer.stop(); |
Mike Fiore |
1:71125aa00e33 | 96 | b->_event = ButtonHandler::sw1_hold; |
Mike Fiore |
1:71125aa00e33 | 97 | osSignalSet(b->_main, buttonSignal); |
Mike Fiore |
1:71125aa00e33 | 98 | } |
Mike Fiore |
1:71125aa00e33 | 99 | } |
Mike Fiore |
1:71125aa00e33 | 100 | } |
Mike Fiore |
1:71125aa00e33 | 101 | } |
Mike Fiore |
1:71125aa00e33 | 102 | |
Mike Fiore |
1:71125aa00e33 | 103 | ButtonHandler::ButtonHandler(osThreadId main) |
Mike Fiore |
1:71125aa00e33 | 104 | : _main(main), |
Mike Fiore |
1:71125aa00e33 | 105 | _thread(b_worker, (void*)this), |
Mike Fiore |
1:71125aa00e33 | 106 | _sw1(PA_12), |
Mike Fiore |
1:71125aa00e33 | 107 | _sw2(PA_11), |
Mike Fiore |
1:71125aa00e33 | 108 | _sw1_time(0), |
Mike Fiore |
1:71125aa00e33 | 109 | _sw2_time(0), |
Mike Fiore |
1:71125aa00e33 | 110 | _sw1_running(false), |
Mike Fiore |
1:71125aa00e33 | 111 | _sw2_running(false), |
Mike Fiore |
1:71125aa00e33 | 112 | _event(none), |
Mike Fiore |
1:71125aa00e33 | 113 | _debounce_time(20), |
Mike Fiore |
1:71125aa00e33 | 114 | _hold_threshold(500) |
Mike Fiore |
1:71125aa00e33 | 115 | { |
Mike Fiore |
1:71125aa00e33 | 116 | // fall handler called on press, rise handler called on release |
Mike Fiore |
1:71125aa00e33 | 117 | _sw1.fall(this, &ButtonHandler::sw1_fall); |
Mike Fiore |
1:71125aa00e33 | 118 | _sw1.rise(this, &ButtonHandler::sw1_rise); |
Mike Fiore |
1:71125aa00e33 | 119 | // need to set mode to PullUp after attaching handlers - default is PullNone (see PinNames.h) |
Mike Fiore |
1:71125aa00e33 | 120 | _sw1.mode(PullUp); |
Mike Fiore |
1:71125aa00e33 | 121 | |
Mike Fiore |
1:71125aa00e33 | 122 | _sw2.fall(this, &ButtonHandler::sw2_fall); |
Mike Fiore |
1:71125aa00e33 | 123 | _sw2.rise(this, &ButtonHandler::sw2_rise); |
Mike Fiore |
1:71125aa00e33 | 124 | _sw2.mode(PullUp); |
Mike Fiore |
1:71125aa00e33 | 125 | } |
Mike Fiore |
1:71125aa00e33 | 126 | |
Mike Fiore |
1:71125aa00e33 | 127 | ButtonHandler::ButtonEvent ButtonHandler::getButtonEvent() { |
Mike Fiore |
1:71125aa00e33 | 128 | ButtonEvent event = _event; |
Mike Fiore |
1:71125aa00e33 | 129 | _event = none; |
Mike Fiore |
1:71125aa00e33 | 130 | return event; |
Mike Fiore |
1:71125aa00e33 | 131 | } |
Mike Fiore |
1:71125aa00e33 | 132 | |
Mike Fiore |
1:71125aa00e33 | 133 | void ButtonHandler::sw1_fall() { |
Mike Fiore |
1:71125aa00e33 | 134 | event = b_sw1_fall; |
Mike Fiore |
1:71125aa00e33 | 135 | _thread.signal_set(signal); |
Mike Fiore |
1:71125aa00e33 | 136 | _thread.signal_clr(signal); |
Mike Fiore |
1:71125aa00e33 | 137 | } |
Mike Fiore |
1:71125aa00e33 | 138 | |
Mike Fiore |
1:71125aa00e33 | 139 | void ButtonHandler::sw1_rise() { |
Mike Fiore |
1:71125aa00e33 | 140 | event = b_sw1_rise; |
Mike Fiore |
1:71125aa00e33 | 141 | _thread.signal_set(signal); |
Mike Fiore |
1:71125aa00e33 | 142 | _thread.signal_clr(signal); |
Mike Fiore |
1:71125aa00e33 | 143 | } |
Mike Fiore |
1:71125aa00e33 | 144 | |
Mike Fiore |
1:71125aa00e33 | 145 | void ButtonHandler::sw2_fall() { |
Mike Fiore |
1:71125aa00e33 | 146 | event = b_sw2_fall; |
Mike Fiore |
1:71125aa00e33 | 147 | _thread.signal_set(signal); |
Mike Fiore |
1:71125aa00e33 | 148 | _thread.signal_clr(signal); |
Mike Fiore |
1:71125aa00e33 | 149 | } |
Mike Fiore |
1:71125aa00e33 | 150 | |
Mike Fiore |
1:71125aa00e33 | 151 | void ButtonHandler::sw2_rise() { |
Mike Fiore |
1:71125aa00e33 | 152 | event = b_sw2_rise; |
Mike Fiore |
1:71125aa00e33 | 153 | _thread.signal_set(signal); |
Mike Fiore |
1:71125aa00e33 | 154 | _thread.signal_clr(signal); |
Mike Fiore |
1:71125aa00e33 | 155 | } |
Mike Fiore |
1:71125aa00e33 | 156 |