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.
swpol.hpp
00001 #include "mbed.h" 00002 00003 class SWPoll { 00004 private: 00005 enum State {LOW, LOW_DEBOUNCE, HIGH,HIGH_DEBOUNCE}; 00006 State state; 00007 DigitalIn& sw; 00008 DigitalOut& led; 00009 Timer t; 00010 00011 public: 00012 //Constructor 00013 SWPoll(DigitalIn& gpioIn,DigitalOut& gpioOut) : sw(gpioIn),led(gpioOut) 00014 { 00015 state = LOW; 00016 t.reset(); 00017 led = 0; 00018 } 00019 //Deconstructor 00020 ~SWPoll(){ 00021 //Shut Down 00022 t.stop(); 00023 t.reset(); 00024 led = 0; 00025 } 00026 // API - poll the switches 00027 // Use of a Timer to manage switch bounce 00028 void poll(){ 00029 switch (state) 00030 { 00031 // waiting for switch to rise 00032 case LOW: 00033 if (sw == 1){ 00034 state = LOW_DEBOUNCE; 00035 t.reset(); 00036 t.start(); 00037 } 00038 break; 00039 case LOW_DEBOUNCE: 00040 if (t.read_ms() >= 200){ 00041 state = HIGH; 00042 t.stop(); 00043 t.reset(); 00044 } 00045 break; 00046 case HIGH: 00047 if (sw == 0) { 00048 led = !led; 00049 state = HIGH_DEBOUNCE; 00050 t.reset(); 00051 t.start(); 00052 } 00053 break; 00054 case HIGH_DEBOUNCE: 00055 if (t.read_ms() >= 200) { 00056 state = LOW; 00057 t.stop(); 00058 t.reset(); 00059 } 00060 break; 00061 default: 00062 t.stop(); 00063 t.reset(); 00064 state = LOW; 00065 break; 00066 } //end switch 00067 //This is a Mealy Machine - so no output logic follows 00068 00069 } 00070 };
Generated on Sat Jul 16 2022 15:05:37 by
1.7.2