Advertisement
jb_exe

FWB.h

Oct 2nd, 2011
6,925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. // jb_exe
  2. // Class from the code of JEFF'S ARDUINO BLOG
  3. // http://jmsarduino.blogspot.com/2009/10/4-way-button-click-double-click-hold.html
  4.  
  5.  
  6. // How to use me :
  7. // "FWB_Project.pde"
  8. // #include "FWB.h"
  9. // #define BP 0 //the pin where your button is connected
  10. //
  11. // FWB bp;
  12. //
  13. // void OnClick(int pin)
  14. // {
  15. //  //Your code here
  16. // }
  17. //
  18. // void setup()
  19. // {
  20. //  bpUP.Configure(BP);
  21. //  bpUP.OnClick = OnClick;
  22. // }
  23. //
  24. // void loop()
  25. // {
  26. //  // Test button state
  27. //  bp.CheckBP();
  28. // }
  29.  
  30.  
  31. #include "WProgram.h"
  32.  
  33. #define PULL_UP 1
  34. #define PULL_DOWN 0
  35.  
  36. class FWB
  37. {
  38. private:
  39.     int _pin;
  40.     boolean _pullMode;
  41.  
  42.     // Properties //
  43.     ////////////////
  44.  
  45.     // Debounce period to prevent flickering when pressing or releasing the button (in ms)
  46.     int Debounce;
  47.     // Max period between clicks for a double click event (in ms)
  48.     int DblClickDelay;
  49.     // Hold period for a long press event (in ms)
  50.     int LongPressDelay;
  51.     // Hold period for a very long press event (in ms)
  52.     int VLongPressDelay;
  53.  
  54.     // Variables //
  55.     ///////////////
  56.  
  57.     // Value read from button
  58.     boolean _state;
  59.     // Last value of button state
  60.     boolean _lastState;
  61.     // whether we're waiting for a double click (down)
  62.     boolean _dblClickWaiting;
  63.     // whether to register a double click on next release, or whether to wait and click
  64.     boolean _dblClickOnNextUp;
  65.     // whether it's OK to do a single click
  66.     boolean _singleClickOK;
  67.  
  68.     // time the button was pressed down
  69.     long _downTime;
  70.     // time the button was released
  71.     long _upTime;
  72.  
  73.     // whether to ignore the button release because the click+hold was triggered
  74.     boolean _ignoreUP;
  75.     // when held, whether to wait for the up event
  76.     boolean _waitForUP;
  77.     // whether or not the hold event happened already
  78.     boolean _longPressHappened;
  79.     // whether or not the long hold event happened already
  80.     boolean _vLongPressHappened;
  81.  
  82.     public:
  83.     void (*OnClick)(int pin);
  84.     void (*OnDblClick)(int pin);
  85.     void (*OnLongPress)(int pin);
  86.     void (*OnVLongPress)(int pin);
  87.  
  88.     FWB()
  89.     {
  90.         // Initialization of properties
  91.         Debounce = 20;
  92.         DblClickDelay = 250;
  93.         LongPressDelay = 1000;
  94.         VLongPressDelay = 3000;
  95.  
  96.         // Initialization of variables
  97.         _state = true;
  98.         _lastState = true;
  99.         _dblClickWaiting = false;
  100.         _dblClickOnNextUp = false;
  101.         _singleClickOK = false; //Default = true
  102.         _downTime = -1;
  103.         _upTime = -1;
  104.         _ignoreUP = false;
  105.         _waitForUP = false;
  106.         _longPressHappened = false;
  107.         _vLongPressHappened = false;
  108.     }
  109.     void Configure(int pin, int pullMode = PULL_DOWN)
  110.     {
  111.         _pin = pin;
  112.         _pullMode = pullMode;
  113.         pinMode(_pin, INPUT);
  114.     }
  115.  
  116.     void CheckBP(void)
  117.     {
  118.         int resultEvent = 0;
  119.         long millisRes = millis();
  120.         _state = digitalRead(_pin) == HIGH;
  121.  
  122.         // Button pressed down
  123.         if (_state != _pullMode && _lastState == _pullMode && (millisRes - _upTime) > Debounce)
  124.         {
  125.             _downTime = millisRes;
  126.             _ignoreUP = false;
  127.             _waitForUP = false;
  128.             _singleClickOK = true;
  129.             _longPressHappened = false;
  130.             _vLongPressHappened = false;
  131.             if ((millisRes - _upTime) < DblClickDelay && _dblClickOnNextUp == false && _dblClickWaiting == true)
  132.                 _dblClickOnNextUp = true;
  133.             else
  134.                 _dblClickOnNextUp = false;
  135.             _dblClickWaiting = false;
  136.         }
  137.         // Button released
  138.         else if (_state == _pullMode && _lastState != _pullMode && (millisRes - _downTime) > Debounce)
  139.         {
  140.             if (_ignoreUP == false) //Replace "(!_ignoreUP)" by "(not _ignoreUP)"
  141.             {
  142.                 _upTime = millisRes;
  143.                 if (_dblClickOnNextUp == false) _dblClickWaiting = true;
  144.                 else
  145.                 {
  146.                     resultEvent = 2;
  147.                     _dblClickOnNextUp = false;
  148.                     _dblClickWaiting = false;
  149.                     _singleClickOK = false;
  150.                 }
  151.             }
  152.         }
  153.  
  154.         // Test for normal click event: DblClickDelay expired
  155.         if (_state == _pullMode && (millisRes - _upTime) >= DblClickDelay && _dblClickWaiting == true && _dblClickOnNextUp == false && _singleClickOK == true && resultEvent != 2)
  156.         {
  157.             resultEvent = 1;
  158.             _dblClickWaiting = false;
  159.         }
  160.         // Test for hold
  161.         if (_state != _pullMode && (millisRes - _downTime) >= LongPressDelay)
  162.         {
  163.             // Trigger "normal" hold
  164.             if (_longPressHappened == false)
  165.             {
  166.                 resultEvent = 3;
  167.                 _waitForUP = true;
  168.                 _ignoreUP = true;
  169.                 _dblClickOnNextUp = false;
  170.                 _dblClickWaiting = false;
  171.                 //_downTime = millis();
  172.                 _longPressHappened = true;
  173.             }
  174.             // Trigger "long" hold
  175.             if ((millisRes - _downTime) >= VLongPressDelay)
  176.             {
  177.                 if (_vLongPressHappened == false)
  178.                 {
  179.                     resultEvent = 4;
  180.                     _vLongPressHappened = true;
  181.                 }
  182.             }
  183.         }
  184.  
  185.         _lastState = _state;
  186.  
  187.         if (resultEvent == 1 && OnClick) OnClick(_pin);
  188.         if (resultEvent == 2 && OnDblClick) OnDblClick(_pin);
  189.         if (resultEvent == 3 && OnLongPress) OnLongPress(_pin);
  190.         if (resultEvent == 4 && OnVLongPress) OnVLongPress(_pin);
  191.         //  if (resultEvent != 0)
  192.         //    Usb.println(resultEvent);
  193.     }
  194.  
  195.  
  196.  
  197.  
  198. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement