David Grayson / GeneralDebouncer

Dependents:   LineFollowing DeadReckoning

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GeneralDebouncer.h Source File

GeneralDebouncer.h

00001 #pragma once
00002 
00003 #include <mbed.h>
00004 
00005 class GeneralDebouncer
00006 {
00007     public:
00008     GeneralDebouncer(int32_t maxUpdateIntervalMicroseconds)
00009       : maxUpdateIntervalMicroseconds(maxUpdateIntervalMicroseconds)
00010     {
00011         timer.start();
00012         //this->maxUpdateIntervalMicroseconds = maxUpdateIntervalMicroseconds;
00013         reset();
00014     }
00015     
00016     void reset()
00017     {
00018         firstReportReceived = false;
00019         lastReportTimeMicroseconds = 0;
00020         spanBeginTimeMicroseconds = 0;
00021         currentState = 0;
00022     }
00023 
00024     uint32_t getState()
00025     {
00026         return currentState;   
00027     }
00028 
00029     uint32_t getTimeInCurrentStateMicroseconds()
00030     {
00031         //if ((time() - lastReportTimeMicroseconds) > maxUpdateIntervalMicroseconds)
00032         //{
00033         //    return 0;
00034         //}        
00035         return time() - spanBeginTimeMicroseconds;
00036     }
00037 
00038     void update(uint32_t state)
00039     {
00040         uint32_t time = this->time();
00041         // || (time - lastReportTimeMicroseconds) > maxUpdateIntervalMicroseconds
00042         if (!firstReportReceived  || state != currentState)
00043         {
00044             firstReportReceived = true;
00045             spanBeginTimeMicroseconds = time;
00046             currentState = state;
00047         }        
00048         lastReportTimeMicroseconds =  time;
00049     }
00050     
00051     private:
00052     uint32_t time()
00053     {
00054         return (uint32_t)timer.read_us();  // Cast int32_t to uint32_t.
00055     }
00056     
00057     private:
00058     public: // tmphax
00059     bool firstReportReceived;
00060     uint32_t const maxUpdateIntervalMicroseconds;
00061     uint32_t lastReportTimeMicroseconds;
00062     uint32_t spanBeginTimeMicroseconds;
00063     uint32_t currentState;
00064     Timer timer;
00065 };