A hello world for inertial sensors
Dependencies: FXAS21000 FXOS8700Q mbed
Fork of FRDM-STBC-AGM01 by
button.cpp@4:5ab2bb2f062b, 2015-06-30 (annotated)
- Committer:
- Elecia
- Date:
- Tue Jun 30 16:23:32 2015 +0000
- Revision:
- 4:5ab2bb2f062b
- Parent:
- 3:123b546e4a5c
Added element14 link
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Elecia | 3:123b546e4a5c | 1 | #include "button.h" |
Elecia | 3:123b546e4a5c | 2 | #include "mbed.h" |
Elecia | 3:123b546e4a5c | 3 | |
Elecia | 3:123b546e4a5c | 4 | #define HIGH 1 |
Elecia | 3:123b546e4a5c | 5 | #define LOW 0 |
Elecia | 3:123b546e4a5c | 6 | |
Elecia | 3:123b546e4a5c | 7 | |
Elecia | 3:123b546e4a5c | 8 | void initializeButtonDebounce() |
Elecia | 3:123b546e4a5c | 9 | { |
Elecia | 3:123b546e4a5c | 10 | // possibly start ticker if you want to auto debounce |
Elecia | 3:123b546e4a5c | 11 | } |
Elecia | 3:123b546e4a5c | 12 | |
Elecia | 3:123b546e4a5c | 13 | // button press with hysterisis |
Elecia | 3:123b546e4a5c | 14 | bool debounceButtonPress(int reading) |
Elecia | 3:123b546e4a5c | 15 | { |
Elecia | 3:123b546e4a5c | 16 | |
Elecia | 3:123b546e4a5c | 17 | static int buttonState = HIGH; // the current reading from the input pin |
Elecia | 3:123b546e4a5c | 18 | static int lastButtonState = HIGH; // the previous reading from the input pin |
Elecia | 3:123b546e4a5c | 19 | static long lastDebounceTime = 0; // the last time the output pin was toggled |
Elecia | 3:123b546e4a5c | 20 | const long kDebounceDelay = 3; // the debounce time; increase if the output flickers |
Elecia | 3:123b546e4a5c | 21 | |
Elecia | 3:123b546e4a5c | 22 | lastDebounceTime++; |
Elecia | 3:123b546e4a5c | 23 | if (reading != lastButtonState) { |
Elecia | 3:123b546e4a5c | 24 | // reset the debouncing timer |
Elecia | 3:123b546e4a5c | 25 | lastDebounceTime = 0; |
Elecia | 3:123b546e4a5c | 26 | lastButtonState = reading; |
Elecia | 3:123b546e4a5c | 27 | } |
Elecia | 3:123b546e4a5c | 28 | if (lastDebounceTime >= kDebounceDelay) { |
Elecia | 3:123b546e4a5c | 29 | // whatever the reading is at, it's been there for longer |
Elecia | 3:123b546e4a5c | 30 | // than the debounce delay, so take it as the actual current state: |
Elecia | 3:123b546e4a5c | 31 | |
Elecia | 3:123b546e4a5c | 32 | // if the button state has changed: |
Elecia | 3:123b546e4a5c | 33 | if (reading != buttonState) { |
Elecia | 3:123b546e4a5c | 34 | buttonState = reading; |
Elecia | 3:123b546e4a5c | 35 | |
Elecia | 3:123b546e4a5c | 36 | // only indicate button pressed if low, input pulled up and button connects to low |
Elecia | 3:123b546e4a5c | 37 | if (buttonState == LOW) { |
Elecia | 3:123b546e4a5c | 38 | return true; |
Elecia | 3:123b546e4a5c | 39 | } |
Elecia | 3:123b546e4a5c | 40 | } |
Elecia | 3:123b546e4a5c | 41 | } |
Elecia | 3:123b546e4a5c | 42 | return false; |
Elecia | 3:123b546e4a5c | 43 | } |