Advertisement
mahesh2000

FWB.h modified

Mar 8th, 2016
3,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. // for types of button presses for the Arduino: click, double-click, long press (and release), very long press
  2. // might work with other controllers. modified from: http://pastebin.com/gQLTrHVF
  3. /*
  4.   i had to make a few changes (update it?) to make the code work. main changes:
  5.   1. REMOVED: #include "WProgram.h" (no longer required).
  6.   2. CHANGED: bpUP TO bp.
  7.   3. ADDED: event listeners for ALL four types of presses in the sample code.
  8.   4. CHANGED: time intervals for single, double, long clicks and for long press. these feel more intuitive to me.
  9.   5. CHANGED: event OnLongPress is raised ONLY after the button is released. this, again, feels more intuitive. code is tested and at http://pastebin.com/87cCn6h9
  10. */
  11.  
  12. //
  13. // jb_exe
  14. // Class from the code of JEFF'S ARDUINO BLOG
  15. // http://jmsarduino.blogspot.com/2009/10/4-way-button-click-double-click-hold.html
  16. //
  17. // the modified version at http://pastebin.com/gQLTrHVF
  18. // further modified by mahesh [at] tinymogul.com
  19.  
  20.  
  21. // How to use me :
  22. // "FWB_Project.pde"
  23. // #include "FWB.h"
  24. // #define BP 0 //the pin where your button is connected
  25. //
  26. // FWB bp;
  27. //
  28. // void OnClick(int pin) {
  29. //   //Your code here
  30. // }
  31. //
  32. // void OnDblClick(int pin) {
  33. //   //Your code here
  34. // }
  35. //
  36. // void OnLongPress(int pin) {
  37. //   //Your code here
  38. // }
  39. //
  40. // void OnVLongPress(int pin) {
  41. //   //Your code here
  42. // }
  43. //
  44. // void setup()
  45. // {
  46. //   // errors in code fixed here. empty event handlers added.
  47. //   bp.Configure(BP);
  48. //   bp.OnClick = OnClick;
  49. //   bp.OnDblClick = OnDblClick;
  50. //   bp.OnLongPress = OnLongPress;
  51. //   bp.OnVLongPress = OnVLongPress;
  52. // }
  53. //
  54. // void loop()
  55. // {
  56. //  // Test button state
  57. //  bp.CheckBP();
  58. // }
  59.  
  60. #define PULL_UP 1
  61. #define PULL_DOWN 0
  62.  
  63. class FWB
  64. {
  65. private:
  66.   int _pin;
  67.   boolean _pullMode;
  68.  
  69.   // Properties //
  70.   ////////////////
  71.  
  72.   // Debounce period to prevent flickering when pressing or releasing the button (in ms)
  73.   int Debounce;
  74.   // Max period between clicks for a double click event (in ms)
  75.   int DblClickDelay;
  76.   // Hold period for a long press event (in ms)
  77.   int LongPressDelay;
  78.   // Hold period for a very long press event (in ms)
  79.   int VLongPressDelay;
  80.  
  81.   // Variables //
  82.   ///////////////
  83.  
  84.   // Value read from button
  85.   boolean _state;
  86.   // Last value of button state
  87.   boolean _lastState;
  88.   // whether we're waiting for a double click (down)
  89.   boolean _dblClickWaiting;
  90.   // whether to register a double click on next release, or whether to wait and click
  91.   boolean _dblClickOnNextUp;
  92.   // whether it's OK to do a single click
  93.   boolean _singleClickOK;
  94.  
  95.   // time the button was pressed down
  96.   long _downTime;
  97.   // time the button was released
  98.   long _upTime;
  99.  
  100.   // whether to ignore the button release because the click+hold was triggered
  101.   boolean _ignoreUP;
  102.   // when held, whether to wait for the up event
  103.   boolean _waitForUP;
  104.   // whether or not the hold event happened already
  105.   boolean _longPressHappened;
  106.   // whether or not the long hold event happened already
  107.   boolean _vLongPressHappened;
  108.  
  109.   public:
  110.   void (*OnClick)(int pin);
  111.   void (*OnDblClick)(int pin);
  112.   void (*OnLongPress)(int pin);
  113.   void (*OnVLongPress)(int pin);
  114.  
  115.   FWB()
  116.   {
  117.     // Initialization of properties
  118.     Debounce = 20;
  119.     DblClickDelay = 250;
  120.     LongPressDelay = 750;
  121.     // LongPressDelay = 1000;    
  122.     VLongPressDelay = 3500;
  123.     // VLongPressDelay = 3000;
  124.  
  125.     // Initialization of variables
  126.     _state = true;
  127.     _lastState = true;
  128.     _dblClickWaiting = false;
  129.     _dblClickOnNextUp = false;
  130.     _singleClickOK = false; //Default = true
  131.     _downTime = -1;
  132.     _upTime = -1;
  133.     _ignoreUP = false;
  134.     _waitForUP = false;
  135.     _longPressHappened = false;
  136.     _vLongPressHappened = false;
  137.   }
  138.   void Configure(int pin, int pullMode = PULL_DOWN)
  139.   {
  140.     _pin = pin;
  141.     _pullMode = pullMode;
  142.     pinMode(_pin, INPUT);
  143.   }
  144.  
  145.   void CheckBP(void)
  146.   {
  147.     int resultEvent = 0;
  148.     long millisRes = millis();
  149.     _state = digitalRead(_pin) == HIGH;
  150.  
  151.     // Button pressed down
  152.     if (_state != _pullMode && _lastState == _pullMode && (millisRes - _upTime) > Debounce)
  153.     {
  154.       //Serial.println("button down");
  155.       _downTime = millisRes;
  156.       _ignoreUP = false;
  157.       _waitForUP = false;
  158.       _singleClickOK = true;
  159.       _longPressHappened = false;
  160.       _vLongPressHappened = false;
  161.       if ((millisRes - _upTime) < DblClickDelay && _dblClickOnNextUp == false && _dblClickWaiting == true)
  162.         _dblClickOnNextUp = true;
  163.       else
  164.         _dblClickOnNextUp = false;
  165.       _dblClickWaiting = false;
  166.     }
  167.     // Button released
  168.     else if (_state == _pullMode && _lastState != _pullMode && (millisRes - _downTime) > Debounce)
  169.     {
  170.       //Serial.println("button up");
  171.       if (_ignoreUP == false) //Replace "(!_ignoreUP)" by "(not _ignoreUP)"
  172.       {
  173.         _upTime = millisRes;
  174.         if (_dblClickOnNextUp == false) _dblClickWaiting = true;
  175.         else
  176.         {
  177.           resultEvent = 2;
  178.           _dblClickOnNextUp = false;
  179.           _dblClickWaiting = false;
  180.           _singleClickOK = false;
  181.         }
  182.       }
  183.     }
  184.  
  185.     // Test for normal click event: DblClickDelay expired
  186.     if (_state == _pullMode && (millisRes - _upTime) >= DblClickDelay && _dblClickWaiting == true && _dblClickOnNextUp == false && _singleClickOK == true && resultEvent != 2)
  187.     {
  188.       resultEvent = 1;
  189.       _dblClickWaiting = false;
  190.     }
  191.  
  192.     // added code: raise OnLongPress event when only when the button is released
  193.     if (_state == _pullMode && _longPressHappened && !_vLongPressHappened) {
  194.       resultEvent = 3;
  195.       _longPressHappened = false;
  196.     }
  197.  
  198.     // Test for hold
  199.     if (_state != _pullMode && (millisRes - _downTime) >= LongPressDelay)
  200.     {
  201.       // Trigger "normal" hold
  202.       if (_longPressHappened == false)
  203.       {
  204.         // resultEvent = 3;
  205.         _waitForUP = true;
  206.         _ignoreUP = true;
  207.         _dblClickOnNextUp = false;
  208.         _dblClickWaiting = false;
  209.         //_downTime = millis();
  210.         _longPressHappened = true;
  211.       }
  212.       // Trigger "long" hold
  213.       if ((millisRes - _downTime) >= VLongPressDelay)
  214.       {
  215.         if (_vLongPressHappened == false)
  216.         {
  217.           resultEvent = 4;
  218.           _vLongPressHappened = true;
  219.           //_longPressHappened = false;
  220.         }
  221.       }
  222.     }
  223.  
  224.     _lastState = _state;
  225.     //if (resultEvent!=0)
  226.     //  Serial.println((String)"resultEvent: " + (String) resultEvent);
  227.  
  228.     if (resultEvent == 1 && OnClick) OnClick(_pin);
  229.     if (resultEvent == 2 && OnDblClick) OnDblClick(_pin);
  230.     if (resultEvent == 3 && OnLongPress) OnLongPress(_pin);
  231.     if (resultEvent == 4 && OnVLongPress) OnVLongPress(_pin);
  232.     //  if (resultEvent != 0)
  233.     //    Usb.println(resultEvent);
  234.   }
  235.  
  236. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement