Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
Button.h
00001 #pragma once 00002 00003 #include "mbed.h" 00004 00005 class Button 00006 { 00007 private: 00008 DigitalIn pin; 00009 00010 volatile bool last_state; 00011 volatile bool pressed; 00012 00013 volatile bool just_switched_to_pressed; 00014 volatile bool just_got_pressed; 00015 00016 public: 00017 Button(PinName pin_name): 00018 pin(pin_name) { 00019 00020 last_state = false; 00021 pressed = false; 00022 00023 just_switched_to_pressed = false; 00024 just_got_pressed = false; 00025 } 00026 00027 void poll_pin() { 00028 // We need to poll the pins periodically. 00029 // Normally one would use rise and fall interrupts, so this wouldn't be 00030 // needed. But the buttons we use generate so much chatter that 00031 // sometimes a rising or a falling edge doesn't get registered. 00032 // With all the confusion that accompanies it. 00033 00034 bool state = !pin; 00035 00036 // Debounce the button by looking over two periods. 00037 if (last_state && state && !pressed) { 00038 pressed = true; 00039 just_switched_to_pressed = true; 00040 } 00041 00042 if (!state) { 00043 pressed = false; 00044 } 00045 00046 last_state = state; 00047 } 00048 00049 void update() { 00050 if (just_got_pressed) { 00051 just_got_pressed = false; 00052 } 00053 if (just_switched_to_pressed) { 00054 just_got_pressed = true; 00055 just_switched_to_pressed = false; 00056 } 00057 } 00058 00059 bool is_pressed() { 00060 return pressed; 00061 }; 00062 // Only active just after a state change from not pressed to pressed. 00063 // Get's reset after `update()` is called. 00064 bool has_just_been_pressed() { 00065 return just_got_pressed; 00066 }; 00067 };
Generated on Thu Jul 14 2022 19:50:50 by
1.7.2