daily pastebin goal
89%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #if defined(ARDUINO) && ARDUINO >= 100
  2.     #include "Arduino.h"
  3. #else
  4.     #include "WProgram.h"
  5. #endif
  6. #include "Button.h"
  7. #include "Phase.h"
  8.  
  9.  
  10. Button::Button(bool teamNumber, uint8_t buttonPin, Phase* phase)
  11. {
  12.   pinMode(buttonPin, INPUT);
  13.   _teamNumber = teamNumber;
  14.   _buttonPin = buttonPin;
  15.   _phase = phase;
  16.   _wasPressed = 0;
  17.   _wasHeld = 0;
  18.   _lastPressTime = 0;
  19.   _lastReleaseTime = 0;
  20. }
  21.  
  22. /**
  23.  * Button.update()
  24.  *
  25.  * If the button is pressed, update the controlling team in Score
  26.  */
  27. void Button::update() {
  28.   // if the button was pressed last tick, and it is also pressed this tick, do nothing.
  29.   if (digitalRead(_buttonPin) && _wasPressed) {
  30.  
  31.     // if the button is pressed and has been for 1 second, mark as held
  32.     if (millis() - _lastPressTime > 1000 && !_wasHeld) {
  33.       processHold();
  34.     }
  35.    
  36.     return;
  37.   }
  38.  
  39.   // if the button was not pressed last tick, but is pressed this tick, process press.
  40.   else if (digitalRead(_buttonPin) && !_wasPressed) {
  41.     processPress();
  42.   }
  43.  
  44.   // if the button is not pressed this tick, but was pressed last tick, process release.
  45.   else if (!digitalRead(_buttonPin) && _wasPressed) {
  46.     processRelease();
  47.   }
  48.  
  49.  
  50. }
  51.  
  52.  
  53. /**
  54.  * Do the action that the button should perform based on the current phase.
  55.  */
  56. void Button::processPress() {
  57.  
  58.  
  59.  
  60.   // set the wasPressed boolean to TRUE which will be queried in later ticks to see if the button state was changed since last tick
  61.   _wasPressed = 1;
  62.  
  63.  
  64.   // set a start press time to later be used to determine if the button is being held (multi-button press&hold functionality)
  65.   _lastPressTime = millis();
  66.  
  67.  
  68.   /**
  69.    * Phase 0-- test phase. Button should advance to next phase when pressed
  70.    */
  71.   if (_phase->getCurrentPhase() == 0) {
  72.     _phase->advance();
  73.     return;
  74.   }
  75.  
  76.   /**
  77.    * Phase 1-- Hello phase. Button should do nothing when pressed
  78.    */
  79.   else if (_phase->getCurrentPhase() == 1) {
  80.     return;
  81.   }
  82.  
  83.  
  84.   /**
  85.    * Phase 2-- Programming > Game mode.
  86.    *   The phase where the type of game is chosen.
  87.    *   Red button cycles through game modes.
  88.    *   Green button selects game mode.
  89.    */
  90.   else if (_phase->getCurrentPhase() == 2) {
  91.  
  92.     // Red button cDycles through game modes
  93.     if (_teamNumber == 0) {
  94.       // @TODO
  95.     }
  96.     else if (_teamNumber == 1) {
  97.       // @TODO
  98.     }
  99.   }
  100.  
  101.  
  102.   /**
  103.    * Phase 3-- Programming > Domination > duration.
  104.    *   This is the phase when the user chooses the total cumulative time a team needs to control the point to win.
  105.    *   Green button increments the time by 1 minute (up to a maximum of 595 hours)
  106.    *   Red button decrements the time by 1 minute (down to a minimum of 1 second)
  107.    */
  108.   else if (_phase->getCurrentPhase() == 3) {
  109.     if (_teamNumber == 0) {
  110.       //_controller->incrementTimeToWin(60000);
  111.     }
  112.     else {
  113.       //_controller->decrementTimeToWin(60000);
  114.     }
  115.     return;
  116.   }
  117.  
  118.  
  119.   /**
  120.    * Phase 4-- Domination > Run
  121.    *   This is the phase where the Domination game is in progress.
  122.    *   Red button sets team 0 as controlling
  123.    *   Green button sets team 1 as controlling
  124.    *   Button should set _wasPressed to TRUE, set controlling team in _score, and timestamp now as the button's last press time.
  125.    */
  126.   else if (_phase->getCurrentPhase() == 4) {
  127.     //_controller->triggerButtonPress(_teamNumber);
  128.   }
  129.  
  130.  
  131.  
  132.   /**
  133.    * Phase 5-- Domination > Pause
  134.    *   Domination game mode is paused.
  135.    *   Red button should not respond to presses or releases.
  136.    *   Green button should not respond to presses or releases.
  137.    *   Red and green buttons simultaneously pressed & held for 5 seconds should resume game (switch to Phase 4)
  138.    */
  139.   else if (_phase->getCurrentPhase() == 5) {
  140.     // @todo
  141.   }
  142.    
  143.   /**
  144.    * Phase 6-- Domination > Win
  145.    *   A team has won the game.
  146.    *   Red button should not respond to presses or releases
  147.    *   Green button should not respond to presses or releases
  148.    *   Red and green buttons simultaneously pressed & held for 5 seconds should...
  149.    *     * reset game scores
  150.    *     * Go to phase 2
  151.    */
  152.    else if (_phase->getCurrentPhase() == 6) {
  153.      // @todo
  154.    }
  155.  
  156.  
  157.    
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. /**
  166.  * Do the release action that the button should perform based on the current phase.
  167.  */
  168. void Button::processRelease() {
  169.  
  170.    
  171.   _wasPressed = 0;
  172.   _wasHeld = 0;
  173.  
  174.   _lastReleaseTime = millis();
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /**
  181.  * processHold
  182.  *
  183.  * the action to do when the button is held
  184.  *
  185.  * This function is for single-button holds only.
  186.  * multi-button holds are handled in ButtonManager.
  187.  */
  188. void Button::processHold() {
  189.   _wasHeld = 1;
  190.  
  191.  
  192.   /**
  193.    * Phase 2-- Programming > Game mode.
  194.    *   The phase where the type of game is chosen.
  195.    *   Holding button 0
  196.    */
  197.   if (_phase->getCurrentPhase() == 2) {
  198.     //
  199.   }
  200. }
  201.  
  202.  
  203. /**
  204.  * getState
  205.  *
  206.  * 0 released
  207.  * 1 pressed
  208.  * 2 held
  209.  */
  210. int Button::getState() {
  211.  
  212.   // if the latest press was more recent than the latest release, the button is physically pressed
  213.   if (digitalRead(_buttonPin) == HIGH) {
  214.  
  215.     // if the button has been pressed for less than 1000 ms, consider it pressed
  216.     if (millis() - _lastPressTime < 1000) {
  217.       return 1;
  218.     }
  219.  
  220.     // if the button has been pressed for greater than or equal to 1000 ms, consider it held
  221.     else {
  222.       return 2;
  223.     }
  224.   }
  225.  
  226.   // if the latest release was more recent than the latest press, the button is released.
  227.   else {
  228.     return 0;
  229.   }
  230. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top