State machine
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
Button.h@9:27d00b64076e, 2018-10-31 (annotated)
- Committer:
- brass_phoenix
- Date:
- Wed Oct 31 08:17:35 2018 +0000
- Revision:
- 9:27d00b64076e
- Parent:
- 7:e7f808875bc4
+ Added dummy button transitions between states.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brass_phoenix | 1:cfa5abca6d43 | 1 | #pragma once |
brass_phoenix | 1:cfa5abca6d43 | 2 | |
brass_phoenix | 1:cfa5abca6d43 | 3 | #include "mbed.h" |
brass_phoenix | 1:cfa5abca6d43 | 4 | |
brass_phoenix | 3:4b19b6cf6cc7 | 5 | class Button |
brass_phoenix | 3:4b19b6cf6cc7 | 6 | { |
brass_phoenix | 1:cfa5abca6d43 | 7 | private: |
brass_phoenix | 7:e7f808875bc4 | 8 | DigitalIn pin; |
brass_phoenix | 3:4b19b6cf6cc7 | 9 | |
brass_phoenix | 7:e7f808875bc4 | 10 | volatile bool last_state; |
brass_phoenix | 1:cfa5abca6d43 | 11 | volatile bool pressed; |
brass_phoenix | 3:4b19b6cf6cc7 | 12 | |
brass_phoenix | 1:cfa5abca6d43 | 13 | volatile bool just_switched_to_pressed; |
brass_phoenix | 1:cfa5abca6d43 | 14 | volatile bool just_got_pressed; |
brass_phoenix | 3:4b19b6cf6cc7 | 15 | |
brass_phoenix | 1:cfa5abca6d43 | 16 | public: |
brass_phoenix | 1:cfa5abca6d43 | 17 | Button(PinName pin_name): |
brass_phoenix | 3:4b19b6cf6cc7 | 18 | pin(pin_name) { |
brass_phoenix | 3:4b19b6cf6cc7 | 19 | |
brass_phoenix | 7:e7f808875bc4 | 20 | last_state = false; |
brass_phoenix | 1:cfa5abca6d43 | 21 | pressed = false; |
brass_phoenix | 7:e7f808875bc4 | 22 | |
brass_phoenix | 7:e7f808875bc4 | 23 | just_switched_to_pressed = false; |
brass_phoenix | 7:e7f808875bc4 | 24 | just_got_pressed = false; |
brass_phoenix | 7:e7f808875bc4 | 25 | } |
brass_phoenix | 7:e7f808875bc4 | 26 | |
brass_phoenix | 7:e7f808875bc4 | 27 | void poll_pin() { |
brass_phoenix | 7:e7f808875bc4 | 28 | // We need to poll the pins periodically. |
brass_phoenix | 7:e7f808875bc4 | 29 | // Normally one would use rise and fall interrupts, so this wouldn't be |
brass_phoenix | 7:e7f808875bc4 | 30 | // needed. But the buttons we use generate so much chatter that |
brass_phoenix | 7:e7f808875bc4 | 31 | // sometimes a rising or a falling edge doesn't get registered. |
brass_phoenix | 7:e7f808875bc4 | 32 | // With all the confusion that accompanies it. |
brass_phoenix | 7:e7f808875bc4 | 33 | |
brass_phoenix | 7:e7f808875bc4 | 34 | bool state = !pin; |
brass_phoenix | 7:e7f808875bc4 | 35 | |
brass_phoenix | 7:e7f808875bc4 | 36 | // Debounce the button by looking over two periods. |
brass_phoenix | 9:27d00b64076e | 37 | if (last_state && state && !pressed) { |
brass_phoenix | 7:e7f808875bc4 | 38 | pressed = true; |
brass_phoenix | 7:e7f808875bc4 | 39 | just_switched_to_pressed = true; |
brass_phoenix | 7:e7f808875bc4 | 40 | } |
brass_phoenix | 7:e7f808875bc4 | 41 | |
brass_phoenix | 7:e7f808875bc4 | 42 | if (!state) { |
brass_phoenix | 7:e7f808875bc4 | 43 | pressed = false; |
brass_phoenix | 7:e7f808875bc4 | 44 | } |
brass_phoenix | 7:e7f808875bc4 | 45 | |
brass_phoenix | 7:e7f808875bc4 | 46 | last_state = state; |
brass_phoenix | 1:cfa5abca6d43 | 47 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 48 | |
brass_phoenix | 1:cfa5abca6d43 | 49 | void update() { |
brass_phoenix | 1:cfa5abca6d43 | 50 | if (just_got_pressed) { |
brass_phoenix | 1:cfa5abca6d43 | 51 | just_got_pressed = false; |
brass_phoenix | 1:cfa5abca6d43 | 52 | } |
brass_phoenix | 1:cfa5abca6d43 | 53 | if (just_switched_to_pressed) { |
brass_phoenix | 1:cfa5abca6d43 | 54 | just_got_pressed = true; |
brass_phoenix | 1:cfa5abca6d43 | 55 | just_switched_to_pressed = false; |
brass_phoenix | 1:cfa5abca6d43 | 56 | } |
brass_phoenix | 1:cfa5abca6d43 | 57 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 58 | |
brass_phoenix | 3:4b19b6cf6cc7 | 59 | bool is_pressed() { |
brass_phoenix | 3:4b19b6cf6cc7 | 60 | return pressed; |
brass_phoenix | 3:4b19b6cf6cc7 | 61 | }; |
brass_phoenix | 1:cfa5abca6d43 | 62 | // Only active just after a state change from not pressed to pressed. |
brass_phoenix | 1:cfa5abca6d43 | 63 | // Get's reset after `update()` is called. |
brass_phoenix | 3:4b19b6cf6cc7 | 64 | bool has_just_been_pressed() { |
brass_phoenix | 3:4b19b6cf6cc7 | 65 | return just_got_pressed; |
brass_phoenix | 3:4b19b6cf6cc7 | 66 | }; |
brass_phoenix | 1:cfa5abca6d43 | 67 | }; |